presets: use this.resolveTargetAddress() instead of broadcast(...)

This commit is contained in:
Micooz 2018-04-15 20:34:59 +08:00
parent 6d392d845c
commit 100f1fe48d
4 changed files with 37 additions and 62 deletions

@ -1,7 +1,6 @@
import crypto from 'crypto';
import { EVP_BytesToKey, numberToBuffer, hmac, hash } from '../utils';
import { IPresetAddressing } from './defs';
import { CONNECT_TO_REMOTE } from './actions';
// available HMACs and length
const HMAC_METHODS = {
@ -162,7 +161,7 @@ export default class BaseAuthPreset extends IPresetAddressing {
}
}
serverIn({ buffer, next, broadcast, fail }) {
serverIn({ buffer, next, fail }) {
if (!this._isHeaderRecv) {
if (this._isConnecting) {
@ -179,18 +178,11 @@ export default class BaseAuthPreset extends IPresetAddressing {
// notify to connect to the real server
this._isConnecting = true;
broadcast({
type: CONNECT_TO_REMOTE,
payload: {
host: host,
port: port,
onConnected: () => {
next(Buffer.concat([data, this._pending]));
this._isHeaderRecv = true;
this._isConnecting = false;
this._pending = null;
}
}
this.resolveTargetAddress({ host, port }, () => {
next(Buffer.concat([data, this._pending]));
this._isHeaderRecv = true;
this._isConnecting = false;
this._pending = null;
});
} else {
return buffer;
@ -203,20 +195,13 @@ export default class BaseAuthPreset extends IPresetAddressing {
return Buffer.concat([this.encodeHeader(), buffer]);
}
serverInUdp({ buffer, next, broadcast, fail }) {
serverInUdp({ buffer, next, fail }) {
const decoded = this.decodeHeader({ buffer, fail });
if (!decoded) {
return;
}
const { host, port, data } = decoded;
broadcast({
type: CONNECT_TO_REMOTE,
payload: {
host: host,
port: port,
onConnected: () => next(data)
}
});
this.resolveTargetAddress({ host, port }, () => next(data));
}
}

@ -163,4 +163,15 @@ export class IPresetAddressing extends IPreset {
}
/**
* DO NOT overwrite it!
* call it when target address was resolved on server side,
* @param host
* @param port
* @param callback
*/
resolveTargetAddress({ host, port }, callback) {
}
}

@ -2,7 +2,6 @@ import net from 'net';
import ip from 'ip';
import { isValidHostname, numberToBuffer } from '../utils';
import { IPresetAddressing } from './defs';
import { CONNECT_TO_REMOTE } from './actions';
const ATYP_V4 = 0x01;
const ATYP_V6 = 0x04;
@ -156,7 +155,7 @@ export default class SsBasePreset extends IPresetAddressing {
}
}
serverIn({ buffer, next, broadcast, fail }) {
serverIn({ buffer, next, fail }) {
if (!this._isHeaderRecv) {
// shadowsocks(python) aead cipher put [atyp][dst.addr][dst.port] into the first chunk
@ -175,21 +174,13 @@ export default class SsBasePreset extends IPresetAddressing {
// notify to connect to the real server
this._isConnecting = true;
broadcast({
type: CONNECT_TO_REMOTE,
payload: {
host: host,
port: port,
// once connected
onConnected: () => {
if (this._pending !== null) {
next(Buffer.concat([data, this._pending]));
}
this._isHeaderRecv = true;
this._isConnecting = false;
this._pending = null;
}
this.resolveTargetAddress({ host, port }, () => {
if (this._pending !== null) {
next(Buffer.concat([data, this._pending]));
}
this._isHeaderRecv = true;
this._isConnecting = false;
this._pending = null;
});
} else {
return buffer;
@ -202,7 +193,7 @@ export default class SsBasePreset extends IPresetAddressing {
return Buffer.concat([this.encodeHeader(), buffer]);
}
serverInUdp({ buffer, next, broadcast, fail }) {
serverInUdp({ buffer, next, fail }) {
const decoded = this.decodeHeader({ buffer, fail });
if (!decoded) {
return;
@ -211,14 +202,7 @@ export default class SsBasePreset extends IPresetAddressing {
this._atyp = getHostType(host);
this._host = this._atyp === ATYP_DOMAIN ? Buffer.from(host) : ip.toBuffer(host);
this._port = numberToBuffer(port);
broadcast({
type: CONNECT_TO_REMOTE,
payload: {
host: host,
port: port,
onConnected: () => next(data)
}
});
this.resolveTargetAddress({ host, port }, () => next(data));
}
}

@ -2,7 +2,6 @@ import net from 'net';
import crypto from 'crypto';
import ip from 'ip';
import { IPresetAddressing } from './defs';
import { CONNECT_TO_REMOTE } from './actions';
import {
hmac,
hash,
@ -287,7 +286,7 @@ export default class V2rayVmessPreset extends IPresetAddressing {
this._adBuf.put(buffer, { next, fail });
}
serverIn({ buffer, next, broadcast, fail }) {
serverIn({ buffer, next, fail }) {
if (!this._isHeaderRecv) {
if (this._isBroadCasting) {
@ -405,18 +404,14 @@ export default class V2rayVmessPreset extends IPresetAddressing {
this._security = securityType;
this._isBroadCasting = true;
broadcast({
type: CONNECT_TO_REMOTE,
payload: {
host: addrType === ATYP_DOMAIN ? addr.toString() : ip.toString(addr),
port: port,
onConnected: () => {
this._adBuf.put(Buffer.concat([data, this._staging]), { next, fail });
this._isHeaderRecv = true;
this._isBroadCasting = false;
this._staging = null;
}
}
this.resolveTargetAddress({
host: addrType === ATYP_DOMAIN ? addr.toString() : ip.toString(addr),
port: port,
}, () => {
this._adBuf.put(Buffer.concat([data, this._staging]), { next, fail });
this._isHeaderRecv = true;
this._isBroadCasting = false;
this._staging = null;
});
} else {
this._adBuf.put(buffer, { next, fail });