diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2021-03-09 02:44:59 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2021-03-09 02:44:59 +0300 |
commit | f7ca888651e737f18a0ea760326922da9d80e471 (patch) | |
tree | f61463affaf09ede1d6c720e618453775dbe065e /src/lib | |
parent | fc004fe70a89c43068b2a455c02f7a813adcc509 (diff) |
improvements, see full commit message
- added new option 'always_allow_localhost'
- improve jobctl, allow connecting to password-protected instances
without using configuration file
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/config.js | 105 | ||||
-rw-r--r-- | src/lib/server.js | 14 |
2 files changed, 55 insertions, 64 deletions
diff --git a/src/lib/config.js b/src/lib/config.js index 73a6226..289a79f 100644 --- a/src/lib/config.js +++ b/src/lib/config.js @@ -2,7 +2,7 @@ const fs = require('fs') const ini = require('ini') const {isNumeric} = require('./util') -let config = null +let config = {} function readFile(file) { if (!fs.existsSync(file)) @@ -22,32 +22,34 @@ function processScheme(source, scheme) { let value = source[key] ?? opts.default ?? null - switch (opts.type) { - case 'int': - if (!isNumeric(value)) - throw new Error(`'${key}' must be an integer`) - value = parseInt(value, 10) - break - - case 'float': - if (!isNumeric(value)) - throw new Error(`'${key}' must be a float`) - value = parseFloat(value) - break - - case 'object': - if (typeof value !== 'object') - throw new Error(`'${key}' must be an object`) - break - - case 'boolean': - if (value !== null) { - value = value.trim() - value = ['true', '1'].includes(value) - } else { - value = false - } - break + if (value !== null) { + switch (opts.type) { + case 'int': + if (!isNumeric(value)) + throw new Error(`'${key}' must be an integer`) + value = parseInt(value, 10) + break + + case 'float': + if (!isNumeric(value)) + throw new Error(`'${key}' must be a float`) + value = parseFloat(value) + break + + case 'object': + if (typeof value !== 'object') + throw new Error(`'${key}' must be an object`) + break + + case 'boolean': + if (typeof value === 'string') { + value = value.trim() + value = ['true', '1'].includes(value) + } else { + value = !!value + } + break + } } result[key] = value @@ -64,6 +66,7 @@ function parseWorkerConfig(file) { host: {required: true}, port: {required: true, type: 'int'}, password: {}, + always_allow_localhost: {type: 'boolean', default: false}, master_host: {}, master_port: {type: 'int', default: 0}, @@ -111,6 +114,7 @@ function parseMasterConfig(file) { host: {required: true}, port: {required: true, type: 'int'}, password: {}, + always_allow_localhost: {type: 'boolean', default: false}, ping_interval: {default: 30, type: 'int'}, poke_throttle_interval: {default: 0.5, type: 'float'}, @@ -124,14 +128,8 @@ function parseMasterConfig(file) { /** * @param {string} file - * @param {{ - * master: boolean, - * log_level: string|undefined, - * host: string, - * port: int, - * }} inputOptions */ -function parseJobctlConfig(file, inputOptions) { +function parseJobctlConfig(file) { config = {} const raw = readFile(file) @@ -141,17 +139,17 @@ function parseJobctlConfig(file, inputOptions) { log_level: {default: 'warn'}, })) - if (inputOptions.master) - config.master = inputOptions.master + // if (inputOptions.master) + // config.master = inputOptions.master Object.assign(config, processScheme(raw, { host: {default: '127.0.0.1'}, - port: {default: config.master ? 7081 : 7080, type: 'int'} + port: {/*default: config.master ? 7081 : 7080,*/ type: 'int'} })) - for (let key of ['log_level', 'host', 'port']) { - if (inputOptions[key]) - config[key] = inputOptions[key] - } + // for (let key of ['log_level', 'host', 'port']) { + // if (inputOptions[key]) + // config[key] = inputOptions[key] + // } // console.log('parseJobctlConfig [2]', config) } @@ -164,26 +162,27 @@ function get(key = null) { if (key === null) return config - if (typeof config !== 'object') - throw new Error(`config is not loaded`) - - if (!(key in config)) - throw new Error(`config: ${key} not found`) - - return config[key] + return config[key] || null } /** - * @param {object} opts + * @param {object|string} key + * @param value */ -// function set(opts) { -// Object.assign(config, opts) -// } +function set(key, value) { + if (!config) + config = {} + if (typeof key === 'object') { + Object.assign(config, key) + } else { + config[key] = value + } +} module.exports = { parseWorkerConfig, parseMasterConfig, parseJobctlConfig, get, - // set, + set, }
\ No newline at end of file diff --git a/src/lib/server.js b/src/lib/server.js index 051b8be..8ed2be9 100644 --- a/src/lib/server.js +++ b/src/lib/server.js @@ -261,13 +261,7 @@ class Connection extends EventEmitter { * @type {boolean} * @private */ - this._isAuthorized = config.get('password') === '' - - /** - * @type {boolean} - * @private - */ - this._textConversationAllowed = false + this._isAuthorized = !config.get('password') /** * @type {boolean} @@ -328,10 +322,8 @@ class Connection extends EventEmitter { this.remoteAddress = socket.remoteAddress this.remotePort = socket.remotePort - if (this.remoteAddress === '127.0.0.1') { + if (this.remoteAddress === '127.0.0.1' && config.get('always_allow_localhost') === true) this._isAuthorized = true - this._textConversationAllowed = true - } this._setLogger() this._setSocketEvents() @@ -548,7 +540,7 @@ class Connection extends EventEmitter { // send password once (when talking to jobd-master) if (!this._isAuthorized) { - message.setPassword(config.get('password')) + message.setPassword(config.get('password') || '') this._isAuthorized = true } |