forked from VincentChanX/shadowsocks-over-websocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcprelay.js
475 lines (417 loc) · 13.3 KB
/
tcprelay.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
const net = require('net');
const path = require('path');
const log4js = require('log4js');
const WebSocket = require('ws');
const Encryptor = require('shadowsocks/lib/shadowsocks/encrypt').Encryptor;
const MAX_CONNECTIONS = 50000;
const TCP_RELAY_TYPE_LOCAL = 1;
const TCP_RELAY_TYPE_SERVER = 2;
const ADDRESS_TYPE_IPV4 = 0x01;
const ADDRESS_TYPE_DOMAIN_NAME = 0x03;
const ADDRESS_TYPE_IPV6 = 0x04;
const ADDRESS_TYPE = {
1: 'IPV4',
3: 'DOMAIN_NAME',
4: 'IPV6'
};
const VERSION = 0x05;
const METHOD_NO_AUTHENTICATION_REQUIRED = 0x00;
const METHOD_GSSAPI = 0x01;
const METHOD_USERNAME_PASSWORD = 0x02;
const METHOD_NO_ACCEPTABLE_METHODS = 0xff;
const CMD_CONNECT = 0x01;
const CMD_BIND = 0x02;
const CMD_UDP_ASSOCIATE = 0x03;
const CMD = {
1: 'CONNECT',
2: 'BIND',
3: 'UDP_ASSOCIATE'
};
const REPLIE_SUCCEEDED = 0x00;
const REPLIE_GENERAL_SOCKS_SERVER_FAILURE = 0x01;
const REPLIE_CONNECTION_NOT_ALLOWED_BY_RULESET = 0x02;
const REPLIE_NETWORK_UNREACHABLE = 0x03;
const REPLIE_HOST_UNREACHABLE = 0x04;
const REPLIE_CONNECTION_REFUSED = 0x05;
const REPLIE_TTL_EXPIRED = 0x06;
const REPLIE_COMMAND_NOT_SUPPORTED = 0x07;
const REPLIE_ADDRESS_TYPE_NOT_SUPPORTED = 0x08;
const STAGE_INIT = 0;
const STAGE_ADDR = 1;
const STAGE_UDP_ASSOC = 2;
const STAGE_DNS = 3;
const STAGE_CONNECTING = 4;
const STAGE_STREAM = 5;
const STAGE_DESTROYED = -1;
const STAGE = {
[-1]: 'STAGE_DESTROYED',
0: 'STAGE_INIT',
1: 'STAGE_ADDR',
2: 'STAGE_UDP_ASSOC',
3: 'STAGE_DNS',
4: 'STAGE_CONNECTING',
5: 'STAGE_STREAM'
};
const SERVER_STATUS_INIT = 0;
const SERVER_STATUS_RUNNING = 1;
const SERVER_STATUS_STOPPED = 2;
var globalConnectionId = 1;
var connections = {};
function parseAddressHeader(data, offset) {
var addressType = data.readUInt8(offset);
var headerLen, dstAddr, dstPort, dstAddrLen;
//domain name
if (addressType == ADDRESS_TYPE_DOMAIN_NAME) {
dstAddrLen = data.readUInt8(offset + 1);
dstAddr = data.slice(offset + 2, offset + 2 + dstAddrLen).toString();
dstPort = data.readUInt16BE(offset + 2 + dstAddrLen);
headerLen = 4 + dstAddrLen;
}
//ipv4
else if (addressType == ADDRESS_TYPE_IPV4) {
dstAddr = data.slice(offset + 1, offset + 5).join('.').toString();
dstPort = data.readUInt16BE(offset + 5);
headerLen = 7;
} else {
return false;
}
return {
addressType: addressType,
headerLen: headerLen,
dstAddr: dstAddr,
dstPort: dstPort
};
}
function TCPRelay(config, isLocal) {
this.isLocal = isLocal;
this.server = null;
this.status = SERVER_STATUS_INIT;
this.config = require('./config.json');
if (config) {
this.config = Object.assign(this.config, config);
}
this.logger = null;
this.logLevel = 'error';
this.logFile = null;
this.serverName = null;
}
TCPRelay.prototype.getStatus = function() {
return this.status;
};
TCPRelay.prototype.setServerName = function(serverName) {
this.serverName = serverName;
return this;
};
TCPRelay.prototype.getServerName = function() {
if (!this.serverName) {
this.serverName = this.isLocal ? 'local' : 'server';
}
return this.serverName;
};
TCPRelay.prototype.setLogLevel = function(logLevel) {
this.logLevel = logLevel;
return this;
};
TCPRelay.prototype.getLogLevel = function() {
return this.logLevel;
};
TCPRelay.prototype.setLogFile = function(logFile) {
if (logFile && !path.isAbsolute(logFile)) {
logFile = process.cwd() + '/' + logFile;
}
this.logFile = logFile;
return this;
};
TCPRelay.prototype.getLogFile = function() {
return this.logFile;
};
TCPRelay.prototype.initLogger = function() {
if (this.logFile) {
log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file(this.logFile), this.getServerName());
}
this.logger = log4js.getLogger(this.getServerName());
this.logger.setLevel(this.logLevel);
};
TCPRelay.prototype.initServer = function() {
var self = this;
return new Promise(function(resolve, reject) {
var config = self.config;
var port = self.isLocal ? config.localPort : config.serverPort;
var address = self.isLocal ? config.localAddress : config.serverAddress;
var server;
if (self.isLocal) {
server = self.server = net.createServer({
allowHalfOpen: true,
});
server.maxConnections = MAX_CONNECTIONS;
server.on('connection', function(connection) {
return self.handleConnectionByLocal(connection);
});
server.on('close', function() {
self.logger.info('server is closed');
self.status = SERVER_STATUS_STOPPED;
});
server.listen(port, address);
} else {
server = self.server = new WebSocket.Server({
host: address,
port: port,
perMessageDeflate: false,
backlog: MAX_CONNECTIONS
});
server.on('connection', function(connection) {
return self.handleConnectionByServer(connection);
});
}
server.on('error', function(error) {
self.logger.fatal('an error of', self.getServerName(), 'occured', error);
self.status = SERVER_STATUS_STOPPED;
reject(error);
});
server.on('listening', function() {
self.logger.info(self.getServerName(), 'is listening on', address + ':' + port);
self.status = SERVER_STATUS_RUNNING;
resolve();
});
});
};
//server
TCPRelay.prototype.handleConnectionByServer = function(connection) {
var self = this;
var config = self.config;
var method = config.method;
var password = config.password;
var serverAddress = config.serverAddress;
var serverPort = config.serverPort;
var logger = self.logger;
var encryptor = new Encryptor(password, method);
var stage = STAGE_INIT;
var connectionId = (globalConnectionId++) % MAX_CONNECTIONS;
var targetConnection, addressHeader;
var dataCache = [];
logger.info(`[${connectionId}]: accept connection from local`);
connections[connectionId] = connection;
connection.on('message', function(data) {
data = encryptor.decrypt(data);
logger.debug(`[${connectionId}]: read data[length = ${data.length}] from local connection at stage[${STAGE[stage]}]`);
switch (stage) {
case STAGE_INIT:
if (data.length < 7) {
stage = STAGE_DESTROYED;
return connection.close();
}
addressHeader = parseAddressHeader(data, 0);
if (!addressHeader) {
stage = STAGE_DESTROYED;
return connection.close();
}
logger.info(`[${connectionId}]: connecting to ${addressHeader.dstAddr}:${addressHeader.dstPort}`);
stage = STAGE_CONNECTING;
targetConnection = net.createConnection({
port: addressHeader.dstPort,
host: addressHeader.dstAddr,
allowHalfOpen: true
}, function() {
logger.info(`[${connectionId}]: connecting to target`);
dataCache = Buffer.concat(dataCache);
targetConnection.write(dataCache, function() {
logger.debug(`[${connectionId}]: write data[length = ${dataCache.length}] to target connection`);
dataCache = null;
});
stage = STAGE_STREAM;
});
targetConnection.on('data', function(data) {
logger.debug(`[${connectionId}]: read data[length = ${data.length}] from target connection`);
if (connection.readyState == WebSocket.OPEN) {
connection.send(encryptor.encrypt(data), {
binary: true
}, function() {
logger.debug(`[${connectionId}]: write data[length = ${data.length}] to local connection`);
});
}
});
targetConnection.setKeepAlive(true, 5000);
targetConnection.on('end', function() {
connection.close();
});
targetConnection.on('error', function(error) {
logger.error(`[${connectionId}]: an error of target connection occured`, error);
stage = STAGE_DESTROYED;
targetConnection.destroy();
connection.close();
});
if (data.length > addressHeader.headerLen) {
dataCache.push(data.slice(addressHeader.headerLen));
}
break;
case STAGE_CONNECTING:
dataCache.push(data);
break;
case STAGE_STREAM:
targetConnection.write(data, function() {
logger.debug(`[${connectionId}]: write data[length = ${data.length}] to target connection`);
});
break;
}
});
connection.on('ping', function() {
return connection.pong('', false, true);
});
connection.on('close', function(hadError) {
logger.info(`[${connectionId}]: close event[had error = ${hadError}] of local connection has been triggered`);
connections[connectionId] = null;
targetConnection && targetConnection.destroy();
});
connection.on('error', function(error) {
logger.error(`[${connectionId}]: an error of connection local occured`, error);
connection.terminate();
connections[connectionId] = null;
targetConnection && targetConnection.end();
});
};
//local
TCPRelay.prototype.handleConnectionByLocal = function(connection) {
var self = this;
var config = self.config;
var method = config.method;
var password = config.password;
var serverAddress = config.serverAddress;
var serverPort = config.serverPort;
var logger = self.logger;
var encryptor = new Encryptor(password, method);
var stage = STAGE_INIT;
var connectionId = (globalConnectionId++) % MAX_CONNECTIONS;
var serverConnection, cmd, addressHeader, ping;
var canWriteToLocalConnection = true;
var dataCache = [];
logger.info(`[${connectionId}]: accept connection from client`);
connections[connectionId] = connection;
connection.setKeepAlive(true, 10000);
connection.on('data', function(data) {
logger.debug(`[${connectionId}]: read data[length = ${data.length}] from client connection at stage[${STAGE[stage]}]`);
switch (stage) {
case STAGE_INIT:
if (data.length < 3 || data.readUInt8(0) != 5) {
stage = STAGE_DESTROYED;
return connection.end();
}
connection.write("\x05\x00");
stage = STAGE_ADDR;
break;
case STAGE_ADDR:
if (data.length < 10 || data.readUInt8(0) != 5) {
stage = STAGE_DESTROYED;
return connection.end();
}
cmd = data.readUInt8(1);
addressHeader = parseAddressHeader(data, 3);
if (!addressHeader) {
stage = STAGE_DESTROYED;
return connection.end();
}
//only supports connect cmd
if (cmd != CMD_CONNECT) {
logger.error('[${connectionId}]: only supports connect cmd');
stage = STAGE_DESTROYED;
return connection.end("\x05\x07\x00\x01\x00\x00\x00\x00\x00\x00");
}
logger.info(`[${connectionId}]: connecting to ${addressHeader.dstAddr}:${addressHeader.dstPort}`);
connection.write("\x05\x00\x00\x01\x00\x00\x00\x00\x00\x00");
stage = STAGE_CONNECTING;
serverConnection = new WebSocket('ws://' + serverAddress + ':' + serverPort, {
perMessageDeflate: false
});
serverConnection.on('open', function() {
logger.info(`[${connectionId}]: connecting to server`);
serverConnection.send(encryptor.encrypt(data.slice(3)), function() {
stage = STAGE_STREAM;
dataCache = Buffer.concat(dataCache);
serverConnection.send(encryptor.encrypt(dataCache), {
binary: true
}, function() {
logger.debug(`[${connectionId}]: write data[length = ${dataCache.length}] to client connection`);
dataCache = null;
});
});
ping = setInterval(function() {
serverConnection.ping('', false, true);
}, 30000);
});
serverConnection.on('message', function(data) {
logger.debug(`[${connectionId}]: read data[length = ${data.length}] from server connection`);
canWriteToLocalConnection && connection.write(encryptor.decrypt(data), function() {
logger.debug(`[${connectionId}]: write data[length = ${data.length}] to client connection`);
});
});
serverConnection.on('error', function(error) {
ping && clearInterval(ping);
logger.error(`[${connectionId}]: an error of server connection occured`, error);
stage = STAGE_DESTROYED;
connection.end();
});
serverConnection.on('close', function() {
logger.info(`[${connectionId}]: server connection is closed`);
ping && clearInterval(ping);
stage = STAGE_DESTROYED;
connection.end();
});
if (data.length > addressHeader.headerLen + 3) {
dataCache.push(data.slice(addressHeader.headerLen + 3));
}
break;
case STAGE_CONNECTING:
dataCache.push(data);
break;
case STAGE_STREAM:
canWriteToLocalConnection && serverConnection.send(encryptor.encrypt(data), {
binary: true
}, function() {
logger.debug(`[${connectionId}]: write data[length = ${data.length}] to server connection`);
});
break;
}
});
connection.on('end', function() {
stage = STAGE_DESTROYED;
logger.info(`[${connectionId}]: end event of client connection has been triggered`);
});
connection.on('close', function(hadError) {
logger.info(`[${connectionId}]: close event[had error = ${hadError}] of client connection has been triggered`);
stage = STAGE_DESTROYED;
canWriteToLocalConnection = false;
connections[connectionId] = null;
serverConnection && serverConnection.terminate();
});
connection.on('error', function(error) {
logger.error(`[${connectionId}]: an error of client connection occured`, error);
stage = STAGE_DESTROYED;
connection.destroy();
canWriteToLocalConnection = false;
connections[connectionId] = null;
serverConnection && serverConnection.close();
});
};
TCPRelay.prototype.bootstrap = function() {
this.initLogger();
return this.initServer();
};
TCPRelay.prototype.stop = function() {
var self = this;
var connId = null;
return new Promise(function(resolve, reject) {
if (self.server) {
self.server.close(function() {
resolve();
});
for (connId in connections) {
if (connections[connId]) {
self.isLocal ? connections[connId].destroy() : connections[connId].terminate();
}
}
} else {
resolve();
}
});
};
module.exports.TCPRelay = TCPRelay;