2014-10-17 02:21:06 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2020-06-07 21:26:11 +00:00
|
|
|
const Errors = require('./enig_error.js').Errors;
|
|
|
|
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
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// deps
|
2020-06-07 21:26:11 +00:00
|
|
|
const paths = require('path');
|
|
|
|
const async = require('async');
|
|
|
|
const assert = require('assert');
|
2015-04-17 04:29:53 +00:00
|
|
|
|
2020-06-08 04:20:34 +00:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
2018-11-08 03:24:05 +00:00
|
|
|
exports.init = init;
|
|
|
|
exports.getDefaultPath = getDefaultPath;
|
2015-04-19 08:13:13 +00:00
|
|
|
|
2020-06-10 03:18:51 +00:00
|
|
|
class Config extends ConfigLoader {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2020-06-08 04:20:34 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 03:18:51 +00:00
|
|
|
static create(basePath, options, 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',
|
|
|
|
];
|
|
|
|
const replaceKeys = [
|
|
|
|
'args', 'sendArgs', 'recvArgs', 'recvArgsNonBatch',
|
|
|
|
];
|
|
|
|
|
|
|
|
options.defaultConfig = DefaultConfig();
|
|
|
|
|
|
|
|
options.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));
|
|
|
|
}
|
|
|
|
}
|
2020-06-10 03:18:51 +00:00
|
|
|
};
|
2020-06-08 04:20:34 +00:00
|
|
|
|
2020-06-10 03:18:51 +00:00
|
|
|
return ConfigLoader.create(basePath, options, cb);
|
2020-06-08 04:20:34 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-21 01:57:06 +00:00
|
|
|
let currentConfiguration = {};
|
2015-04-19 08:13:13 +00:00
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
function hasMessageConferenceAndArea(config) {
|
2018-06-22 05:15:04 +00:00
|
|
|
assert(_.isObject(config.messageConferences)); // we create one ourself!
|
2016-09-20 03:28:21 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
const nonInternalConfs = Object.keys(config.messageConferences).filter(confTag => {
|
|
|
|
return 'system_internal' !== confTag;
|
|
|
|
});
|
2016-09-20 03:28:21 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(0 === nonInternalConfs.length) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-20 03:28:21 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
// :TODO: there is likely a better/cleaner way of doing this
|
2016-09-20 03:28:21 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
let result = false;
|
|
|
|
_.forEach(nonInternalConfs, confTag => {
|
|
|
|
if(_.has(config.messageConferences[confTag], 'areas') &&
|
2018-06-23 03:26:46 +00:00
|
|
|
Object.keys(config.messageConferences[confTag].areas) > 0)
|
2018-06-22 05:15:04 +00:00
|
|
|
{
|
|
|
|
result = true;
|
|
|
|
return false; // stop iteration
|
|
|
|
}
|
|
|
|
});
|
2016-09-20 03:28:21 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return result;
|
2016-02-03 04:35:59 +00:00
|
|
|
}
|
2016-09-20 03:28:21 +00:00
|
|
|
|
2018-11-17 20:14:51 +00:00
|
|
|
const ArrayReplaceKeyPaths = [
|
|
|
|
'loginServers.ssh.algorithms.kex',
|
|
|
|
'loginServers.ssh.algorithms.cipher',
|
|
|
|
'loginServers.ssh.algorithms.hmac',
|
|
|
|
'loginServers.ssh.algorithms.compress',
|
|
|
|
];
|
|
|
|
|
|
|
|
const ArrayReplaceKeys = [
|
|
|
|
'args',
|
|
|
|
'sendArgs', 'recvArgs', 'recvArgsNonBatch',
|
|
|
|
];
|
|
|
|
|
2018-06-14 03:02:00 +00:00
|
|
|
function mergeValidateAndFinalize(config, cb) {
|
2020-06-07 21:26:11 +00:00
|
|
|
const defaultConfig = DefaultConfig();
|
2018-11-17 20:14:51 +00:00
|
|
|
|
|
|
|
const arrayReplaceKeyPathsMutable = _.clone(ArrayReplaceKeyPaths);
|
|
|
|
const shouldReplaceArray = (arr, key) => {
|
|
|
|
if(ArrayReplaceKeys.includes(key)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for(let i = 0; i < arrayReplaceKeyPathsMutable.length; ++i) {
|
|
|
|
const o = _.get(defaultConfig, arrayReplaceKeyPathsMutable[i]);
|
|
|
|
if(_.isEqual(o, arr)) {
|
|
|
|
arrayReplaceKeyPathsMutable.splice(i, 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function mergeWithDefaultConfig(callback) {
|
|
|
|
const mergedConfig = _.mergeWith(
|
2018-11-17 20:14:51 +00:00
|
|
|
defaultConfig,
|
|
|
|
config,
|
|
|
|
(defConfig, userConfig, key) => {
|
|
|
|
if(Array.isArray(defConfig) && Array.isArray(userConfig)) {
|
|
|
|
//
|
|
|
|
// Arrays are special: Some we merge, while others
|
|
|
|
// we simply replace.
|
|
|
|
//
|
|
|
|
if(shouldReplaceArray(defConfig, key)) {
|
|
|
|
return userConfig;
|
|
|
|
} else {
|
|
|
|
return _.uniq(defConfig.concat(userConfig));
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return callback(null, mergedConfig);
|
|
|
|
},
|
|
|
|
function validate(mergedConfig, callback) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Various sections must now exist in config
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Logic is broken here:
|
2018-06-22 05:15:04 +00:00
|
|
|
if(hasMessageConferenceAndArea(mergedConfig)) {
|
|
|
|
return callback(Errors.MissingConfig('Please create at least one message conference and area!'));
|
|
|
|
}
|
|
|
|
return callback(null, mergedConfig);
|
|
|
|
},
|
|
|
|
function setIt(mergedConfig, callback) {
|
2020-06-08 03:21:31 +00:00
|
|
|
currentConfiguration = mergedConfig;
|
2018-06-22 05:15:04 +00:00
|
|
|
exports.get = () => currentConfiguration;
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
if(cb) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2016-02-03 04:35:59 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 18:31:24 +00:00
|
|
|
function init(configPath, options, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(!cb && _.isFunction(options)) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const changed = ( { fileName, fileRoot } ) => {
|
|
|
|
const reCachedPath = paths.join(fileRoot, fileName);
|
|
|
|
ConfigCache.getConfig(reCachedPath, (err, config) => {
|
|
|
|
if(!err) {
|
2018-06-24 03:02:16 +00:00
|
|
|
mergeValidateAndFinalize(config, err => {
|
|
|
|
if(!err) {
|
|
|
|
const Events = require('./events.js');
|
|
|
|
Events.emit(Events.getSystemEvents().ConfigChanged);
|
|
|
|
}
|
|
|
|
});
|
2018-07-03 01:32:27 +00:00
|
|
|
} else {
|
|
|
|
console.stdout(`Configuration ${reCachedPath} is invalid: ${err.message}`); // eslint-disable-line no-console
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const ConfigCache = require('./config_cache.js');
|
|
|
|
const getConfigOptions = {
|
2018-06-23 03:26:46 +00:00
|
|
|
filePath : configPath,
|
2020-06-10 03:18:51 +00:00
|
|
|
hotReload : options.hotReload,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
2020-06-10 03:18:51 +00:00
|
|
|
if(options.hotReload) {
|
2018-06-22 05:15:04 +00:00
|
|
|
getConfigOptions.callback = changed;
|
|
|
|
}
|
|
|
|
ConfigCache.getConfigWithOptions(getConfigOptions, (err, config) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mergeValidateAndFinalize(config, cb);
|
|
|
|
});
|
2015-04-19 08:13:13 +00:00
|
|
|
}
|
2014-10-17 02:21:06 +00:00
|
|
|
|
2015-04-19 08:13:13 +00:00
|
|
|
function getDefaultPath() {
|
2018-06-23 03:26:46 +00:00
|
|
|
// e.g. /enigma-bbs-install-path/config/
|
2018-06-22 05:15:04 +00:00
|
|
|
return './config/';
|
2015-04-19 08:13:13 +00:00
|
|
|
}
|
2020-06-10 03:18:51 +00:00
|
|
|
|
|
|
|
exports.Config = Config;
|