core: minor refactor

This commit is contained in:
Micooz 2018-05-06 18:32:31 +08:00
parent 1929f8c1a7
commit be9dd7adcc
2 changed files with 10 additions and 9 deletions

@ -50,7 +50,7 @@ export class Config {
presets = null; presets = null;
udp_presets = null; udp_presets = null;
mux = null; mux = false;
mux_concurrency = null; mux_concurrency = null;
log_path = null; log_path = null;
@ -153,8 +153,8 @@ export class Config {
this.redirect = server.redirect; this.redirect = server.redirect;
} }
// mux // mux, mux_concurrency
this.mux = !!server.mux; this.mux = server.mux === true;
if (this.is_client) { if (this.is_client) {
this.mux_concurrency = server.mux_concurrency || 10; this.mux_concurrency = server.mux_concurrency || 10;
} }

@ -186,18 +186,19 @@ export class Hub {
} }
async _createServerOnServer() { async _createServerOnServer() {
const { local_protocol, local_host, local_port, tls_key, tls_cert } = this._config;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const address = { const address = {
host: this._config.local_host, host: local_host,
port: this._config.local_port, port: local_port,
}; };
const onListening = (server) => { const onListening = (server) => {
const service = `${this._config.local_protocol}://${this._config.local_host}:${this._config.local_port}`; const service = `${local_protocol}://${local_host}:${local_port}`;
logger.info(`[hub] blinksocks server is running at ${service}`); logger.info(`[hub] blinksocks server is running at ${service}`);
resolve(server); resolve(server);
}; };
let server = null; let server = null;
switch (this._config.local_protocol) { switch (local_protocol) {
case 'tcp': { case 'tcp': {
server = net.createServer(); server = net.createServer();
server.on('connection', this._onConnection); server.on('connection', this._onConnection);
@ -219,13 +220,13 @@ export class Hub {
break; break;
} }
case 'tls': { case 'tls': {
server = tls.createServer({ key: [this._config.tls_key], cert: [this._config.tls_cert] }); server = tls.createServer({ key: tls_key, cert: tls_cert });
server.on('secureConnection', this._onConnection); server.on('secureConnection', this._onConnection);
server.listen(address, () => onListening(server)); server.listen(address, () => onListening(server));
break; break;
} }
default: default:
return reject(Error(`unsupported protocol: "${this._config.local_protocol}"`)); return reject(Error(`unsupported protocol: "${local_protocol}"`));
} }
server.on('error', reject); server.on('error', reject);
}); });