src: refine api interfaces
This commit is contained in:
parent
21468707ec
commit
889b2bd6e6
2
bin/bootstrap.js
vendored
2
bin/bootstrap.js
vendored
@ -17,7 +17,7 @@ function obtainConfig(file) {
|
|||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = function bootstrap(configPath, {core: {Hub, Config}}) {
|
module.exports = function bootstrap(configPath, {Hub, Config}) {
|
||||||
try {
|
try {
|
||||||
Config.init(obtainConfig(configPath));
|
Config.init(obtainConfig(configPath));
|
||||||
if (cluster.isMaster && __WORKERS__ > 0) {
|
if (cluster.isMaster && __WORKERS__ > 0) {
|
||||||
|
@ -10,11 +10,6 @@ import {getPresetClassByName} from '../presets';
|
|||||||
import {isValidHostname, isValidPort, logger} from '../utils';
|
import {isValidHostname, isValidPort, logger} from '../utils';
|
||||||
import {DNS_DEFAULT_EXPIRE} from './dns-cache';
|
import {DNS_DEFAULT_EXPIRE} from './dns-cache';
|
||||||
|
|
||||||
export const AVAILABLE_PROTOCOLS = [
|
|
||||||
'tcp', 'socks', 'socks5', 'socks4', 'socks4a',
|
|
||||||
'http', 'https', 'ws', 'tls'
|
|
||||||
];
|
|
||||||
|
|
||||||
function loadFileSync(file) {
|
function loadFileSync(file) {
|
||||||
return fs.readFileSync(path.resolve(process.cwd(), file));
|
return fs.readFileSync(path.resolve(process.cwd(), file));
|
||||||
}
|
}
|
||||||
@ -336,9 +331,13 @@ export class Config {
|
|||||||
if (typeof protocol !== 'string') {
|
if (typeof protocol !== 'string') {
|
||||||
throw Error('service protocol is invalid');
|
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);
|
const _protocol = protocol.slice(0, -1);
|
||||||
if (!AVAILABLE_PROTOCOLS.includes(_protocol)) {
|
if (!AVAILABLE_LOCAL_PROTOCOLS.includes(_protocol)) {
|
||||||
throw Error(`service protocol must be: ${AVAILABLE_PROTOCOLS.join(', ')}`);
|
throw Error(`service protocol must be: ${AVAILABLE_LOCAL_PROTOCOLS.join(', ')}`);
|
||||||
}
|
}
|
||||||
if (_protocol === 'tls') {
|
if (_protocol === 'tls') {
|
||||||
if (typeof json.tls_cert !== 'string' || json.tls_cert === '') {
|
if (typeof json.tls_cert !== 'string' || json.tls_cert === '') {
|
||||||
|
@ -6,7 +6,7 @@ import ws from 'ws';
|
|||||||
import {Balancer} from './balancer';
|
import {Balancer} from './balancer';
|
||||||
import {Config} from './config';
|
import {Config} from './config';
|
||||||
import * as MiddlewareManager from './middleware';
|
import * as MiddlewareManager from './middleware';
|
||||||
import {createRelay} from '../transports';
|
import {createRelay} from './relay';
|
||||||
import {logger} from '../utils';
|
import {logger} from '../utils';
|
||||||
import {tcp, http, socks} from '../proxies';
|
import {tcp, http, socks} from '../proxies';
|
||||||
|
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
import EventEmitter from 'events';
|
import EventEmitter from 'events';
|
||||||
|
import uniqueId from 'lodash.uniqueid';
|
||||||
import {Pipe} from './pipe';
|
import {Pipe} from './pipe';
|
||||||
import {PIPE_ENCODE, PIPE_DECODE} from './middleware';
|
import {PIPE_ENCODE, PIPE_DECODE} from './middleware';
|
||||||
import {CONNECT_TO_REMOTE, CONNECTION_CREATED} from '../presets';
|
import {CONNECT_TO_REMOTE, CONNECTION_CREATED} from '../presets';
|
||||||
|
import {TcpInbound, TcpOutbound, TlsInbound, TlsOutbound, WsInbound, WsOutbound} from '../transports';
|
||||||
|
|
||||||
function preparePresets() {
|
function preparePresets() {
|
||||||
let presets = __PRESETS__;
|
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;
|
||||||
|
}
|
||||||
|
12
src/index.js
12
src/index.js
@ -1,7 +1,5 @@
|
|||||||
import * as core from './core';
|
export * from './core';
|
||||||
import * as presets from './presets';
|
export * from './presets';
|
||||||
import * as proxies from './proxies';
|
export * from './proxies';
|
||||||
import * as transports from './transports';
|
export * from './transports';
|
||||||
import * as utils from './utils';
|
export * from './utils';
|
||||||
|
|
||||||
export {core, presets, proxies, transports, utils};
|
|
||||||
|
@ -181,7 +181,7 @@ export class IPresetStatic extends IPreset {
|
|||||||
* @param clazz
|
* @param clazz
|
||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
export function checkClass(clazz) {
|
export function checkPresetClass(clazz) {
|
||||||
if (typeof clazz !== 'function') {
|
if (typeof clazz !== 'function') {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import {checkClass} from './defs';
|
import {checkPresetClass} from './defs';
|
||||||
|
|
||||||
// functional
|
// functional
|
||||||
import StatsPreset from './stats';
|
import StatsPreset from './stats';
|
||||||
@ -70,7 +70,7 @@ function getPresetClassByName(name) {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw Error(`cannot find preset: "${name}" from built-in modules or external: ${err.message}`);
|
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`);
|
throw Error(`definition of preset "${name}" is invalid`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user