-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
144 lines (129 loc) · 4.22 KB
/
client.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
'use strict';
import assert from 'assert';
import { isIPv4 } from 'net';
import { format } from 'util'; // node.js built-in
import { DIRECT_PROXY } from './settings.js';
import { ConnectProxy } from './ConnectProxy.js';
import { UDPProxy } from './UDPProxy.js';
import { extract_ip_chain, sanitize } from './util.js';
import { vpn_make, vpn_connect} from './vpn.js';
const textDecoder = new TextDecoder();
let lastlog = null;
let lastlogcount = 0;
export class Client {
constructor(id, socket, request) {
this.id = id;
this.socket = socket;
this.ip_chain = extract_ip_chain(request);
this.target = null;
this.socket.on('message', this.handle_message.bind(this));
this.socket.on('error', this.handle_error.bind(this));
this.socket.on('close', this.handle_close.bind(this));
this.log("New client from ", this.ip_chain);
}
log() {
let line = [`[CLIENT ${this.id}]`, ...arguments].map(o => format("%s", o)).join(" ");
if (lastlog != line) {
if (lastlogcount > 0) {
console.log(lastlog + ` [repeated ${lastlogcount} times]`);
}
console.log(line);
lastlog = line;
lastlogcount = 0;
} else {
lastlogcount++;
}
}
send(data) {
let binary =
Buffer.isBuffer(data) ||
(data instanceof ArrayBuffer) ||
ArrayBuffer.isView(data);
if (this.socket) {
this.socket.send(data, {binary});
}
}
close() {
let socket = this.socket;
this.socket = null;
if (socket) {
socket.close();
}
let target = this.target;
this.target = null;
if (target) {
target.close();
}
}
handle_error() {
this.log("Error");
this.close();
}
handle_close() {
this.socket = null;
this.close();
}
handle_message(buffer, isBinary) {
// node.js specific fix: Convert Buffer to ArrayBuffer
buffer = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
if (!isBinary) {
buffer = textDecoder.decode(buffer);
}
if (this.target) {
this.target.forward(buffer);
} else {
this.handle_command(buffer);
}
}
handle_command(data) {
data = sanitize(data);
let tokens = data.split(' ');
let command = tokens[0];
let response = null;
if (command == 'MAKEVPN') {
const game = tokens[1];
const [serverCode, clientCode] = vpn_make(game);
response = `NEWVPN ${serverCode} ${clientCode}`;
} else if (command == 'VPN') {
const code = tokens[1];
const bindport = parseInt(tokens[5], 10);
this.target = vpn_connect(this, code, bindport);
if (this.target == null) {
this.log(`VPN connect failed`);
this.close();
return;
}
response = 'BIND OK';
} else if (command == 'PROXY') {
assert(tokens[2] == 'TCP' || tokens[2] == 'UDP');
const isUDP = (tokens[2] == 'UDP');
const ip = sanitize(tokens[3]);
const port = parseInt(sanitize(tokens[4]));
assert(isIPv4(ip));
assert(port >= 1 && port < 65536);
this.target = route(this, isUDP, ip, port);
if (!this.target) {
this.log(`Proxy to udp=${isUDP}, ip=${ip}, port=${port} rejected`);
response = 'PROXY FAIL';
} else {
response = 'PROXY OK';
}
} else {
this.log('Unhandled command: ', data);
this.close();
return;
}
this.send(response);
}
}
const PROXY_MAP = new Map(DIRECT_PROXY.map(([vip,ip,port]) => [vip, [ip, port]]));
function route(client, isUDP, ip, port) {
if (!isUDP && ip == '10.0.0.1' && port == 8080) {
return new ConnectProxy(client);
}
if (isUDP && PROXY_MAP.has(ip)) {
let [real_ip, real_port] = PROXY_MAP.get(ip);
return new UDPProxy(client, real_ip, real_port);
}
return null;
}