src: refine api interfaces

This commit is contained in:
Micooz 2017-10-21 11:57:58 +08:00
parent 21468707ec
commit 889b2bd6e6
7 changed files with 32 additions and 19 deletions

2
bin/bootstrap.js vendored

@ -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) {

@ -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 === '') {

@ -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';

@ -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;
}

@ -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';

@ -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;
}

@ -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`);
}
}