forked from shadowsocks/shadowsocks-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.js
executable file
·225 lines (204 loc) · 6.46 KB
/
local.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
// Generated by CoffeeScript 1.3.3
(function() {
var KEY, PORT, REMOTE_PORT, SERVER, config, configContent, decryptTable, encrypt, encryptTable, fs, getServer, inetAton, inetNtoa, net, server, tables, timeout;
inetNtoa = function(buf) {
return buf[0] + "." + buf[1] + "." + buf[2] + "." + buf[3];
};
inetAton = function(ipStr) {
var buf, i, parts;
parts = ipStr.split(".");
if (parts.length !== 4) {
return null;
} else {
buf = new Buffer(4);
i = 0;
while (i < 4) {
buf[i] = +parts[i];
i++;
}
return buf;
}
};
fs = require("fs");
configContent = fs.readFileSync("config.json");
config = JSON.parse(configContent);
SERVER = config.server;
REMOTE_PORT = config.server_port;
PORT = config.local_port;
KEY = config.password;
timeout = Math.floor(config.timeout * 1000);
getServer = function() {
if (SERVER instanceof Array) {
return SERVER[Math.floor(Math.random() * SERVER.length)];
} else {
return SERVER;
}
};
net = require("net");
encrypt = require("./encrypt");
console.log("calculating ciphers");
tables = encrypt.getTable(KEY);
encryptTable = tables[0];
decryptTable = tables[1];
server = net.createServer(function(connection) {
var addrLen, addrToSend, cachedPieces, headerLength, remote, remoteAddr, remotePort, stage;
console.log("server connected");
console.log("concurrent connections: " + server.connections);
stage = 0;
headerLength = 0;
remote = null;
cachedPieces = [];
addrLen = 0;
remoteAddr = null;
remotePort = null;
addrToSend = "";
connection.on("data", function(data) {
var aServer, addrtype, buf, cmd, reply, tempBuf;
if (stage === 5) {
encrypt.encrypt(encryptTable, data);
if (!remote.write(data)) {
connection.pause();
}
return;
}
if (stage === 0) {
tempBuf = new Buffer(2);
tempBuf.write("\u0005\u0000", 0);
connection.write(tempBuf);
stage = 1;
return;
}
if (stage === 1) {
try {
cmd = data[1];
addrtype = data[3];
if (cmd !== 1) {
console.warn("unsupported cmd: " + cmd);
reply = new Buffer("\u0005\u0007\u0000\u0001", "binary");
connection.end(reply);
return;
}
if (addrtype === 3) {
addrLen = data[4];
} else if (addrtype !== 1) {
console.warn("unsupported addrtype: " + addrtype);
connection.end();
return;
}
addrToSend = data.slice(3, 4).toString("binary");
if (addrtype === 1) {
remoteAddr = inetNtoa(data.slice(4, 8));
addrToSend += data.slice(4, 10).toString("binary");
remotePort = data.readUInt16BE(8);
headerLength = 10;
} else {
remoteAddr = data.slice(5, 5 + addrLen).toString("binary");
addrToSend += data.slice(4, 5 + addrLen + 2).toString("binary");
remotePort = data.readUInt16BE(5 + addrLen);
headerLength = 5 + addrLen + 2;
}
buf = new Buffer(10);
buf.write("\u0005\u0000\u0000\u0001", 0, 4, "binary");
buf.write("\u0000\u0000\u0000\u0000", 4, 4, "binary");
buf.writeInt16BE(remotePort, 8);
connection.write(buf);
aServer = getServer();
remote = net.connect(REMOTE_PORT, aServer, function() {
var addrToSendBuf, i, piece;
console.log("connecting " + remoteAddr + " via " + aServer);
addrToSendBuf = new Buffer(addrToSend, "binary");
encrypt.encrypt(encryptTable, addrToSendBuf);
remote.write(addrToSendBuf);
i = 0;
while (i < cachedPieces.length) {
piece = cachedPieces[i];
encrypt.encrypt(encryptTable, piece);
remote.write(piece);
i++;
}
cachedPieces = null;
return stage = 5;
});
remote.on("data", function(data) {
encrypt.encrypt(decryptTable, data);
if (!connection.write(data)) {
return remote.pause();
}
});
remote.on("end", function() {
console.log("remote disconnected");
connection.end();
return console.log("concurrent connections: " + server.connections);
});
remote.on("error", function() {
if (stage === 4) {
console.warn("remote connection refused");
connection.destroy();
return;
}
console.warn("remote error");
connection.end();
return console.log("concurrent connections: " + server.connections);
});
remote.on("drain", function() {
return connection.resume();
});
remote.setTimeout(timeout, function() {
connection.end();
return remote.destroy();
});
if (data.length > headerLength) {
buf = new Buffer(data.length - headerLength);
data.copy(buf, 0, headerLength);
cachedPieces.push(buf);
buf = null;
}
return stage = 4;
} catch (e) {
console.warn(e);
connection.destroy();
if (remote) {
return remote.destroy();
}
}
} else {
if (stage === 4) {
return cachedPieces.push(data);
}
}
});
connection.on("end", function() {
console.log("server disconnected");
if (remote) {
remote.destroy();
}
return console.log("concurrent connections: " + server.connections);
});
connection.on("error", function() {
console.warn("server error");
if (remote) {
remote.destroy();
}
return console.log("concurrent connections: " + server.connections);
});
connection.on("drain", function() {
if (remote && stage === 5) {
return remote.resume();
}
});
return connection.setTimeout(timeout, function() {
if (remote) {
remote.destroy();
}
return connection.destroy();
});
});
server.listen(PORT, function() {
return console.log("server listening at port " + PORT);
});
server.on("error", function(e) {
if (e.code === "EADDRINUSE") {
return console.warn("Address in use, aborting");
}
});
}).call(this);