forked from gvangool/node-socks
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsocksproxy4.js
More file actions
60 lines (48 loc) · 1.57 KB
/
socksproxy4.js
File metadata and controls
60 lines (48 loc) · 1.57 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
var net = require('net'),
socks4 = require('./socks4.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 PORT4='9999',
server4 = socks4.createServer();
server4.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(PORT4);
}, 10000);
}
});
server4.listen(PORT4);
}
});
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);
});