diff --git a/bin/bootstrap.js b/bin/bootstrap.js index 6a8a402..23cb070 100644 --- a/bin/bootstrap.js +++ b/bin/bootstrap.js @@ -17,7 +17,7 @@ function obtainConfig(file) { return json; } -module.exports = function bootstrap(configPath, {core: {Hub, Config}}) { +module.exports = function bootstrap(configPath, {Hub, Config}) { try { Config.init(obtainConfig(configPath)); if (cluster.isMaster && __WORKERS__ > 0) { diff --git a/src/core/config.js b/src/core/config.js index 8f24d92..3b4d932 100644 --- a/src/core/config.js +++ b/src/core/config.js @@ -10,11 +10,6 @@ import {getPresetClassByName} from '../presets'; import {isValidHostname, isValidPort, logger} from '../utils'; import {DNS_DEFAULT_EXPIRE} from './dns-cache'; -export const AVAILABLE_PROTOCOLS = [ - 'tcp', 'socks', 'socks5', 'socks4', 'socks4a', - 'http', 'https', 'ws', 'tls' -]; - function loadFileSync(file) { return fs.readFileSync(path.resolve(process.cwd(), file)); } @@ -336,9 +331,13 @@ export class Config { if (typeof protocol !== 'string') { throw Error('service protocol is invalid'); } + const AVAILABLE_LOCAL_PROTOCOLS = [ + 'tcp', 'socks', 'socks5', 'socks4', 'socks4a', + 'http', 'https', 'ws', 'tls' + ]; const _protocol = protocol.slice(0, -1); - if (!AVAILABLE_PROTOCOLS.includes(_protocol)) { - throw Error(`service protocol must be: ${AVAILABLE_PROTOCOLS.join(', ')}`); + if (!AVAILABLE_LOCAL_PROTOCOLS.includes(_protocol)) { + throw Error(`service protocol must be: ${AVAILABLE_LOCAL_PROTOCOLS.join(', ')}`); } if (_protocol === 'tls') { if (typeof json.tls_cert !== 'string' || json.tls_cert === '') { diff --git a/src/core/hub.js b/src/core/hub.js index e00b9bc..7fd399d 100644 --- a/src/core/hub.js +++ b/src/core/hub.js @@ -6,7 +6,7 @@ import ws from 'ws'; import {Balancer} from './balancer'; import {Config} from './config'; import * as MiddlewareManager from './middleware'; -import {createRelay} from '../transports'; +import {createRelay} from './relay'; import {logger} from '../utils'; import {tcp, http, socks} from '../proxies'; diff --git a/src/core/relay.js b/src/core/relay.js index 0ee34fb..cf0196d 100644 --- a/src/core/relay.js +++ b/src/core/relay.js @@ -1,7 +1,9 @@ import EventEmitter from 'events'; +import uniqueId from 'lodash.uniqueid'; import {Pipe} from './pipe'; import {PIPE_ENCODE, PIPE_DECODE} from './middleware'; import {CONNECT_TO_REMOTE, CONNECTION_CREATED} from '../presets'; +import {TcpInbound, TcpOutbound, TlsInbound, TlsOutbound, WsInbound, WsOutbound} from '../transports'; function preparePresets() { let presets = __PRESETS__; @@ -118,3 +120,17 @@ export class Relay extends EventEmitter { } } + +const mapping = { + 'tcp': [TcpInbound, TcpOutbound], + 'tls': [TlsInbound, TlsOutbound], + 'ws': [WsInbound, WsOutbound] +}; + +export function createRelay(transport, context, proxyRequest = null) { + const [Inbound, Outbound] = __IS_CLIENT__ ? [TcpInbound, mapping[transport][1]] : [mapping[transport][0], TcpOutbound]; + const props = {context, Inbound, Outbound, proxyRequest}; + const relay = new Relay(props); + relay.id = uniqueId(`${transport}_`); + return relay; +} diff --git a/src/index.js b/src/index.js index 6bb79e4..b3993a8 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,5 @@ -import * as core from './core'; -import * as presets from './presets'; -import * as proxies from './proxies'; -import * as transports from './transports'; -import * as utils from './utils'; - -export {core, presets, proxies, transports, utils}; +export * from './core'; +export * from './presets'; +export * from './proxies'; +export * from './transports'; +export * from './utils'; diff --git a/src/presets/defs.js b/src/presets/defs.js index 608675a..597fb16 100644 --- a/src/presets/defs.js +++ b/src/presets/defs.js @@ -181,7 +181,7 @@ export class IPresetStatic extends IPreset { * @param clazz * @returns {boolean} */ -export function checkClass(clazz) { +export function checkPresetClass(clazz) { if (typeof clazz !== 'function') { return false; } diff --git a/src/presets/index.js b/src/presets/index.js index cb8423e..6e65f52 100644 --- a/src/presets/index.js +++ b/src/presets/index.js @@ -1,4 +1,4 @@ -import {checkClass} from './defs'; +import {checkPresetClass} from './defs'; // functional import StatsPreset from './stats'; @@ -70,7 +70,7 @@ function getPresetClassByName(name) { } catch (err) { throw Error(`cannot find preset: "${name}" from built-in modules or external: ${err.message}`); } - if (!checkClass(clazz)) { + if (!checkPresetClass(clazz)) { throw Error(`definition of preset "${name}" is invalid`); } }