2016-12-15 21:30:55 +00:00
|
|
|
const dns = require('dns');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const config = require(__dirname+'/config');
|
|
|
|
const logger = require(__dirname+'/logger');
|
|
|
|
const webirc_data_path = path.resolve(__dirname+'/../webirc.data.json');
|
2016-09-25 13:09:55 +00:00
|
|
|
|
|
|
|
let webirc_data = {};
|
2016-11-05 18:52:58 +00:00
|
|
|
let timeouts = {};
|
2016-09-25 13:09:55 +00:00
|
|
|
|
|
|
|
function writeToFile() {
|
|
|
|
fs.writeFile(webirc_data_path, JSON.stringify(webirc_data, null, '\t'), function (err) {if (err) throw err;});
|
|
|
|
}
|
|
|
|
|
2016-11-14 15:20:27 +00:00
|
|
|
function timeoutRefresh(address, seconds) {
|
|
|
|
if(timeouts[address])
|
|
|
|
clearTimeout(timeouts[address]);
|
|
|
|
|
2016-12-15 21:30:55 +00:00
|
|
|
timeouts[address] = setTimeout(()=>{resolveAddress(address);}, seconds*1000);
|
2016-11-14 15:20:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function resolveAddress(address, force) {
|
2016-12-15 21:30:55 +00:00
|
|
|
logger.debugLog('** WEBIRC ** Attempting to update IP for '+address);
|
2016-11-05 18:52:58 +00:00
|
|
|
let obj = webirc_data[address];
|
|
|
|
|
|
|
|
if(!obj) return;
|
|
|
|
|
2016-11-14 15:20:27 +00:00
|
|
|
if((Date.now() - obj.last_update)/1000 < config.webirc.resolveInterval && !force) {
|
2016-11-05 18:52:58 +00:00
|
|
|
let nextTime = (config.webirc.resolveInterval - (Date.now() - obj.last_update)/1000);
|
2016-11-14 15:20:27 +00:00
|
|
|
|
2016-12-15 21:30:55 +00:00
|
|
|
logger.debugLog('** WEBIRC ** {0} IP is {1}, refresh in {2} seconds'.format(address, obj.cached_ip, Math.floor(nextTime)));
|
2016-11-05 18:52:58 +00:00
|
|
|
|
2016-11-14 15:20:27 +00:00
|
|
|
return timeoutRefresh(address, nextTime);
|
2016-11-05 18:52:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
dns.resolve(address, (err, data) => {
|
2016-12-21 19:00:43 +00:00
|
|
|
if(address === '127.0.0.1' || address === '0.0.0.0' || address === 'localhost') {
|
|
|
|
logger.debugLog('** WEBIRC ** Ignoring localhost entry..');
|
|
|
|
return resolve('127.0.0.1');
|
|
|
|
}
|
|
|
|
|
2016-11-05 18:52:58 +00:00
|
|
|
if(err!=null) return reject(err);
|
|
|
|
let ip = data.length > 0 ? data[0] : null;
|
2016-12-21 19:00:43 +00:00
|
|
|
|
2016-11-05 18:52:58 +00:00
|
|
|
if(ip) {
|
|
|
|
resolve(ip);
|
|
|
|
} else {
|
2016-12-15 21:30:55 +00:00
|
|
|
reject(new Error('no ips'));
|
2016-11-05 18:52:58 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}).then((data) => {
|
2016-12-15 21:30:55 +00:00
|
|
|
logger.debugLog('** WEBIRC ** Updated DNS for {0}; IP is now {1}'.format(address, data));
|
2016-11-14 15:20:27 +00:00
|
|
|
|
2016-11-05 18:52:58 +00:00
|
|
|
webirc_data[address].last_update = Date.now();
|
|
|
|
webirc_data[address].cached_ip = data;
|
|
|
|
|
2016-11-14 15:20:27 +00:00
|
|
|
writeToFile();
|
|
|
|
timeoutRefresh(address, config.webirc.resolveInterval);
|
2016-11-05 18:52:58 +00:00
|
|
|
}, (err) => {
|
2016-12-15 21:30:55 +00:00
|
|
|
logger.debugLog('** WEBIRC ** Failed to updated DNS for {0}; IP is still {1}'.format(address, webirc_data[address].cached_ip));
|
2016-11-05 18:52:58 +00:00
|
|
|
|
2016-11-14 15:20:27 +00:00
|
|
|
timeoutRefresh(address, (config.webirc.resolveInterval+60));
|
2016-11-05 18:52:58 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-14 15:20:27 +00:00
|
|
|
function reload(force) {
|
|
|
|
if(!config.webirc.enabled) return;
|
|
|
|
|
2016-09-25 13:09:55 +00:00
|
|
|
try {
|
|
|
|
fs.accessSync(webirc_data_path, fs.F_OK);
|
|
|
|
|
|
|
|
webirc_data = require(webirc_data_path);
|
|
|
|
|
|
|
|
if (require.cache && require.cache[webirc_data_path]) {
|
|
|
|
delete require.cache[webirc_data_path];
|
|
|
|
}
|
|
|
|
} catch(e) {
|
|
|
|
writeToFile();
|
|
|
|
}
|
2016-11-05 18:52:58 +00:00
|
|
|
|
|
|
|
for(let adr in webirc_data) {
|
2016-11-14 15:20:27 +00:00
|
|
|
resolveAddress(adr, force);
|
2016-11-05 18:52:58 +00:00
|
|
|
}
|
2016-09-25 13:09:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function get_password(server_ip) {
|
2016-11-05 18:52:58 +00:00
|
|
|
let ip = null;
|
|
|
|
for(let a in webirc_data) {
|
|
|
|
if(webirc_data[a].cached_ip == server_ip)
|
|
|
|
ip = webirc_data[a];
|
|
|
|
}
|
2016-09-25 13:09:55 +00:00
|
|
|
|
2016-11-05 18:52:58 +00:00
|
|
|
return ip;
|
2016-09-25 13:09:55 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 12:08:36 +00:00
|
|
|
class WebIRCAuthenticator {
|
|
|
|
constructor(userInfo) {
|
|
|
|
this.userInfo = userInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
authenticate(connection) {
|
|
|
|
let serverpass = get_password(connection.config.address);
|
|
|
|
if(serverpass)
|
2016-12-15 21:30:55 +00:00
|
|
|
connection.socket.write('WEBIRC {0} {1} {2} {3}\r\n'.format(serverpass.password, connection.config.username,
|
|
|
|
this.userInfo.hostname, this.userInfo.ipaddr));
|
2016-10-02 12:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-25 13:09:55 +00:00
|
|
|
module.exports = {
|
|
|
|
reload: reload,
|
2016-10-02 12:08:36 +00:00
|
|
|
authenticator: WebIRCAuthenticator,
|
2016-09-25 13:09:55 +00:00
|
|
|
get_password: get_password,
|
|
|
|
writeToFile: writeToFile
|
2016-11-14 15:20:27 +00:00
|
|
|
};
|
2016-09-25 13:09:55 +00:00
|
|
|
|
|
|
|
process.on('SIGUSR1', () => {
|
2016-12-15 21:30:55 +00:00
|
|
|
logger.log('\n!!! Received SIGUSR1; Reloading webirc data.. !!!\n');
|
2016-11-14 15:20:27 +00:00
|
|
|
reload(true);
|
2016-09-25 13:09:55 +00:00
|
|
|
});
|
|
|
|
|
2016-11-14 15:20:27 +00:00
|
|
|
reload(false);
|