2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2020-06-07 21:26:11 +00:00
|
|
|
const DefaultConfig = require('./config_default');
|
2020-06-10 03:18:51 +00:00
|
|
|
const ConfigLoader = require('./config_loader');
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2020-06-08 04:20:34 +00:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
2020-06-12 03:16:15 +00:00
|
|
|
// Global system configuration instance; see Config.create()
|
|
|
|
let systemConfigInstance;
|
2015-04-19 08:13:13 +00:00
|
|
|
|
2020-06-12 03:16:15 +00:00
|
|
|
exports.Config = class Config extends ConfigLoader {
|
2020-06-10 03:18:51 +00:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2020-06-08 04:20:34 +00:00
|
|
|
}
|
|
|
|
|
2020-06-12 03:16:15 +00:00
|
|
|
static create(baseConfigPath, cb) {
|
2020-06-11 01:15:49 +00:00
|
|
|
const replacePaths = [
|
|
|
|
'loginServers.ssh.algorithms.kex',
|
|
|
|
'loginServers.ssh.algorithms.cipher',
|
|
|
|
'loginServers.ssh.algorithms.hmac',
|
|
|
|
'loginServers.ssh.algorithms.compress',
|
|
|
|
];
|
2020-06-12 03:16:15 +00:00
|
|
|
|
2020-06-11 01:15:49 +00:00
|
|
|
const replaceKeys = [
|
|
|
|
'args', 'sendArgs', 'recvArgs', 'recvArgsNonBatch',
|
|
|
|
];
|
|
|
|
|
2020-06-12 03:16:15 +00:00
|
|
|
const options = {
|
|
|
|
defaultConfig : DefaultConfig,
|
|
|
|
defaultsCustomizer : (defaultVal, configVal, key, path) => {
|
|
|
|
if (Array.isArray(defaultVal) && Array.isArray(configVal)) {
|
|
|
|
if (replacePaths.includes(path) || replaceKeys.includes(key)) {
|
|
|
|
// full replacement using user config value
|
|
|
|
return configVal;
|
|
|
|
} else {
|
|
|
|
// merge user config & default config; keep only unique
|
|
|
|
_.uniq(defaultVal.concat(configVal));
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-06-16 03:21:26 +00:00
|
|
|
onReload : err => {
|
|
|
|
if (!err) {
|
|
|
|
const Events = require('./events.js');
|
|
|
|
Events.emit(Events.getSystemEvents().ConfigChanged);
|
|
|
|
}
|
|
|
|
},
|
2020-06-12 03:16:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
systemConfigInstance = new Config(options);
|
|
|
|
systemConfigInstance.init(baseConfigPath, err => {
|
|
|
|
if (err) {
|
|
|
|
console.stdout(`Configuration ${baseConfigPath} error: ${err.message}`); // eslint-disable-line no-console
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
2016-02-03 04:35:59 +00:00
|
|
|
|
2020-06-12 03:16:15 +00:00
|
|
|
// late bind an exported get method to the global Config
|
|
|
|
// instance we just created
|
|
|
|
exports.get = systemConfigInstance.get.bind(systemConfigInstance);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-06-12 03:16:15 +00:00
|
|
|
return cb(err);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-12 03:16:15 +00:00
|
|
|
static getDefaultPath() {
|
|
|
|
// e.g. /enigma-bbs-install-path/config/
|
|
|
|
return './config/';
|
|
|
|
}
|
|
|
|
};
|