const EventEmitter = require('events');
const https = require('https');
class dyndnss extends EventEmitter {
constructor() {
super();
global.plgManager.logger.info('-------- PLUGIN DYNDNSS -----------');
//this.plgManager = global.plgManager;
this.registerForEvents();
this.serviceProcId = null;
this.ownExternalIp = '';
this.loadConfig((success) => {
if (success) {
global.plgManager.logger.info('PLUGIN | DYNDNSS | successfully loaded Configuration from core database');
this.initializePlugin((successInit) => {
if (successInit) {
global.plgManager.logger.info('PLUGIN | DYNDNSS | successfully initialized plugin. Ready to update subdomain now.');
// initialize Update once at startup
this.updateDomain((successUpdate) => {
if (successUpdate) {
global.plgManager.logger.info('PLUGIN | DYNDNSS | successfully updated subdomain at startup of domain');
} else {
global.plgManager.logger.error('PLUGIN | DYNDNSS | Could not update subdomain at startup of plugin');
}
});
} else {
global.plgManager.logger.error('PLUGIN | DYNDNSS | something went wrong initializing plugin');
}
});
} else {
global.plgManager.logger.error('PLUGIN | DYNDNSS | something went wrong loading configuration. Can not update subdomain!!');
}
});
this.transporter = null;
}
/**
* Registration of Events at the plugin manager (hook in to events provided by the plugin manager)
*/
registerForEvents() {
global.plgManager.logger.info('PLUGIN | DYNDNSS | Registering events')
global.plgManager.on('event_refresh_extip', (ipInfo) => {
global.plgManager.logger.info('PLUGIN | DYNDNSS | EVENT event_refresh_extip recieved');
if (this.ownExternalIp !== ipInfo) {
this.ownExternalIp = ipInfo;
this.initializePlugin((success) => {
if (success) {
this.updateDomain((success) => {
if (success) {
global.plgManager.logger.info('PLUGIN | DYNDNSS | successfully updated subdomain after change of external ip-address');
} else {
global.plgManager.logger.info('PLUGIN | DYNDNSS | successfully updated subdomain after change of external ip-address');
}
});
}
});
} else {
global.plgManager.logger.info('PLUGIN | DYNDNSS | ip hasn\'t changed. No Update at dyndnss.net needed. sleeping');
}
});
}
/**
* Plugin-specific Getters and Setters
*
*/
set subdomain(subdomain) {
this._subdomain = subdomain;
}
get subdomain() {
return this._subdomain;
}
set password(password) {
this._password = password;
}
get password() {
return this._password;
}
set username(username) {
this._username = username;
}
get username() {
return this._username;
}
set ownExternalIp(ownExternalIp) {
this._ownExternalIp = ownExternalIp;
}
get ownExternalIp() {
return this._ownExternalIp;
}
/**
* Plugin-specific implementation
*/
updateDomain(callback) {
global.plgManager.logger.debug('PLUGIN | DYNDNSS | Try updating subdomain ' + this.subdomain);
var getPath = "/?user=" + this.username + "&pass=" + this.password + "&domain=" + this.subdomain + "&updater=other";
console.log('GetPath: ' + getPath);
const options = {
hostname: 'dyndnss.net',
port: 443,
path: getPath,
method: 'GET'
}
const req = https.request(options, res => {
console.log(`DYNDNSS | update | statusCode: ${res.statusCode}`)
res.on('data', (d) => {
console.log(d);
})
if (res.statusCode === 200) {
callback(true);
} else {
callback(false);
}
})
req.on('error', error => {
console.error('DYNDNSS | update | error: ' + error.message);
callback(false);
})
req.end()
}
/**
* Loads configuration for KatSys Service
* TBD: Needs to be pulled from plugin config store
*/
loadConfig(callback) {
global.plgManager.loadConfigFromStore('dyndnss', (configRows) => {
if (configRows !== null) {
this.config = configRows[0];
this.config = JSON.parse(this.config.configstore);
global.plgManager.logger.info('PLUGIN | DYNDNSS | Configuration reloaded from core database');
callback(true);
} else {
global.plgManager.logger.info('PLUGIN | DYNDNSS | Found no configuration reloaded in core database');
callback(false);
}
})
}
/**
* returns config parameter for given fieldname
* @param {string} fieldname
*/
getConfigValue(fieldname) {
var retVal = null;
this.config.fields.forEach((field) => {
if (field.fieldname === fieldname) {
retVal = field.value;
}
});
return retVal;
}
/**
* initializes all settings. Returns false, if a needed setting is not valid or not set
* @param {function} callback success (true/false)
*/
initializePlugin(callback) {
this.subdomain = this.getConfigValue('subdomain');
this.username = this.getConfigValue('username');
this.password = this.getConfigValue('password');
if ((this.subdomain !== null) && (this.password !== null)) {
return callback(true);
} else {
return callback(false);
}
}
}
module.exports = dyndnss;