forked from gvangool/node-socks
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsocksproxy5.js
More file actions
132 lines (106 loc) · 3.61 KB
/
socksproxy5.js
File metadata and controls
132 lines (106 loc) · 3.61 KB
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
var net = require('net'),
socks5 = require('./socks5.js');
var exec = require('child_process').exec;
var d = require('domain').create();
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
d.run(function() {
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
if(1==numCPUs) cluster.fork();//make sure it is more than 2
cluster.on('exit', function(worker, code, signal) {
if (worker.suicide !== true) {
exec('taskkill /pid '+worker.process.pid +' /T /F');
}
var exitCode = worker.process.exitCode;
console.log('worker ' + worker.process.pid + ' died ('+exitCode+'). restarting...');
//
cluster.fork();
});
} else {
// Workers can share any TCP connection
// In this case its a proxy server
// Create server
// The server accepts SOCKS connections. This particular server acts as a proxy.
var PORT5='8888',
server5 = socks5.createServer(function(socket, port, address, proxy_ready) {
// Implement your own proxy here! Do encryption, tunnelling, whatever! Go flippin' mental!
// I plan to tunnel everything including SSH over an HTTP tunnel. For now, though, here is the plain proxy:
console.log('Got through the first part of the SOCKS protocol.')
var proxy = net.createConnection(port, address, proxy_ready);
// d.add(proxy);
proxy.on('data', function(d) {
try {
// console.log('receiving ' + d.length + ' bytes from proxy');
socket.write(d);
} catch(err) {
}
});
socket.on('data', function(d) {
// If the application tries to send data before the proxy is ready, then that is it's own problem.
try {
// console.log('sending ' + d.length + ' bytes to proxy');
proxy.write(d);
} catch(err) {
}
});
proxy.on('end', function(had_error) {
socket.end();
console.error('The proxy closed');
}.bind(this));
socket.on('end', function(had_error) {
if (this.proxy !== undefined) {
proxy.removeAllListeners('data');
proxy.end();
}
console.error('The application closed');
}.bind(this));
socket.on('error', function(had_error) {
if (this.proxy !== undefined) {
proxy.removeAllListeners('data');
proxy.destroy();
}
console.error('The application error');
}.bind(this));
proxy.on('error', function(had_error) {
socket.destroy();
console.error('The proxy error');
}.bind(this));
socket.setTimeout(60000, function(error){
if (this.proxy !== undefined) {
proxy.removeAllListeners('data');
proxy.end();
}
this.end();
console.error('socket timeout 60000ms');
}.bind(this));
proxy.setTimeout(60000, function(error){
proxy.removeAllListeners('data');
proxy.end();
this.end();
console.error('proxy socket timeout 60000ms');
}.bind(this));
});
server5.on('error', function (e) {
console.error('SERVER ERROR: %j', e);
if (e.code == 'EADDRINUSE') {
console.log('Address in use, retrying in 10 seconds...');
setTimeout(function () {
console.log('Reconnecting to %s',PORT);
server.close();
server.listen(PORT5);
}, 10000);
}
});
server5.listen(PORT5);
}
});
d.on('error', function(er) {
// an error occurred somewhere.
// if we throw it now, it will crash the program
// with the normal line number and stack message.
console.log('ERROR!: %s ',er);
});