-
Notifications
You must be signed in to change notification settings - Fork 8
/
SOCK2HTTP.js
196 lines (168 loc) · 6.39 KB
/
SOCK2HTTP.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
/* eslint-disable class-methods-use-this */
var http = require("http");
var url = require("url");
const Socks = require('socks').SocksClient;
var fs = require("fs");
var util = require("util");
module.exports = function (log) {
class ProxyServer extends http.Server {
constructor(options) {
super();
//http.Server.call(this, () => { });
this.proxyList = [];
if (options.socks) {
// stand alone proxy loging
this.loadProxy(options.socks);
} else if (options.socksList) {
// proxy list loading
this.loadProxyFile(options.socksList);
if (options.proxyListReloadTimeout) {
setInterval(
() => {
this.loadProxyFile(options.socksList);
},
options.proxyListReloadTimeout * 1000
);
}
}
this.addListener(
'request',
this.requestListener.bind(this, () => this.randomElement(this.proxyList))
);
this.addListener(
'connect',
this.connectListener.bind(this, () => this.randomElement(this.proxyList))
);
}
randomElement(array) {
return array[Math.floor(Math.random() * array.length)];
}
getProxyObject(host, port, login, password) {
return {
ipaddress: host,
// eslint-disable-next-line radix
port: parseInt(port, 10),
type: 5,
authentication: {
username: login || '',
password: password || ''
}
};
}
parseProxyLine(line) {
const proxyInfo = line.split(':');
if (proxyInfo.length !== 4 && proxyInfo.length !== 2) {
throw new Error(`Incorrect proxy line: ${line}`);
}
return this.getProxyObject.apply(this, proxyInfo);
}
requestListener(getProxyInfo, request, response) {
log("[SOCKS2HTTP]", `request: ${request.url}`);
const proxy = getProxyInfo();
const ph = url.parse(request.url);
const socksAgent = new Socks.Agent({
proxy,
destination: {
host: ph.hostname,
port: ph.port
}
});
const options = {
port: ph.port,
hostname: ph.hostname,
method: request.method,
path: ph.path,
headers: request.headers,
agent: socksAgent
};
const proxyRequest = http.request(options);
request.on('error', (err) => {
log("[SOCKS2HTTP]", `${err.message}`);
proxyRequest.destroy(err);
});
proxyRequest.on('error', (error) => {
log("[SOCKS2HTTP]", `${error.message} on proxy ${proxy.ipaddress}:${proxy.port}`);
response.writeHead(500);
response.end('Connection error\n');
});
proxyRequest.on('response', (proxyResponse) => {
proxyResponse.pipe(response);
response.writeHead(proxyResponse.statusCode, proxyResponse.headers);
});
request.pipe(proxyRequest);
}
connectListener(getProxyInfo, request, socketRequest, head) {
const proxy = getProxyInfo();
const ph = url.parse(`http://${request.url}`);
const host = ph.hostname;
const port = parseInt(ph.port);
const options = {
proxy,
destination: {
host,
port
},
command: 'connect'
};
let socket;
socketRequest.on('error', (err) => {
log("[SOCKS2HTTP]", `${err.message}`);
if (socket) {
socket.destroy(err);
}
});
Socks.createConnection(options, (error, _socket) => {
try {
socket = _socket.socket;
socket.on('error', (err) => {
log("[SOCKS2HTTP]", `${err.message}`);
socketRequest.destroy(err);
});
} catch (ex) { }
if (error) {
// error in SocksSocket creation
log("[SOCKS2HTTP]", `${error.message} connection creating on ${proxy.ipaddress}:${proxy.port}`);
socketRequest.write(`HTTP/${request.httpVersion} 500 Connection error\r\n\r\n`);
socketRequest.end();
return;
}
// tunneling to the host
socket.pipe(socketRequest);
socketRequest.pipe(socket);
socket.write(head);
socketRequest.write(`HTTP/${request.httpVersion} 200 Connection established\r\n\r\n`);
socket.resume();
});
}
loadProxy(proxyLine) {
try {
this.proxyList.push(this.parseProxyLine(proxyLine));
} catch (ex) {
log("[SOCKS2HTTP]", ex.message);
}
}
loadProxyFile(fileName) {
log("[SOCKS2HTTP]", `Loading proxy list from file: ${fileName}`);
fs.readFile(fileName, (err, data) => {
if (err) {
log("[SOCKS2HTTP]", `Impossible to read the proxy file : ${fileName} error : ${err.message}`);
return;
}
const lines = data.toString().split('\n');
const proxyList = [];
for (let i = 0; i < lines.length; i += 1) {
if (!(lines[i] !== '' && lines[i].charAt(0) !== '#')) {
try {
proxyList.push(this.parseProxyLine(lines[i]));
} catch (ex) {
log("[SOCKS2HTTP]", ex.message);
}
}
}
this.proxyList = proxyList;
});
}
}
util.inherits(ProxyServer, http.Server);
return ProxyServer;
}