forked from shadowsocks/shadowsocks-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·188 lines (169 loc) · 5.08 KB
/
server.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
// Generated by CoffeeScript 1.3.3
(function() {
var KEY, PORT, config, configContent, decryptTable, encrypt, encryptTable, fs, 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);
PORT = config.server_port;
KEY = config.password;
timeout = Math.floor(config.timeout * 1000);
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, 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;
connection.on("data", function(data) {
var addrtype, buf;
encrypt.encrypt(decryptTable, data);
if (stage === 5) {
if (!remote.write(data)) {
connection.pause();
}
return;
}
if (stage === 0) {
try {
addrtype = data[0];
if (addrtype === 3) {
addrLen = data[1];
} else if (addrtype !== 1) {
console.warn("unsupported addrtype: " + addrtype);
connection.end();
return;
}
if (addrtype === 1) {
remoteAddr = inetNtoa(data.slice(1, 5));
remotePort = data.readUInt16BE(5);
headerLength = 7;
} else {
remoteAddr = data.slice(2, 2 + addrLen).toString("binary");
remotePort = data.readUInt16BE(2 + addrLen);
headerLength = 2 + addrLen + 2;
}
console.log(remoteAddr);
remote = net.connect(remotePort, remoteAddr, function() {
var i, piece;
console.log("connecting " + remoteAddr);
i = 0;
while (i < cachedPieces.length) {
piece = cachedPieces[i];
remote.write(piece);
i++;
}
cachedPieces = null;
return stage = 5;
});
remote.on("data", function(data) {
encrypt.encrypt(encryptTable, data);
if (!connection.write(data)) {
return remote.pause();
}
});
remote.on("end", function() {
console.log("remote disconnected");
console.log("concurrent connections: " + server.connections);
return connection.end();
});
remote.on("error", function() {
if (stage === 4) {
console.warn("remote connection refused");
connection.end();
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) {
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");
}
});
console.log(server.maxConnections);
}).call(this);