Repository navigation
Expand file tree
/
Copy pathproxy.js
More file actions
100 lines (89 loc) · 3.33 KB
/
Copy pathproxy.js
File metadata and controls
100 lines (89 loc) · 3.33 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
var net = require('net'),
socks = require('./socks.js'),
info = console.log.bind(console);
// Create server
// The server accepts SOCKS connections. This particular server acts as a proxy.
var HOST='0.0.0.0',
PORT='6801',
server = socks.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:
var proxy = net.createConnection({port:port, host:address,localAddress:process.argv[2]||undefined}, proxy_ready);
var localAddress,localPort;
proxy.on('connect', function(){
info('%s:%d <== %s:%d ==> %s:%d',socket.remoteAddress,socket.remotePort,
proxy.localAddress,proxy.localPort,proxy.remoteAddress,proxy.remotePort);
localAddress=proxy.localAddress;
localPort=proxy.localPort;
}.bind(this));
proxy.on('data', function(d) {
try {
//console.log('receiving ' + d.length + ' bytes from proxy');
if (!socket.write(d)) {
proxy.pause();
socket.on('drain', function(){
proxy.resume();
});
setTimeout(function(){
proxy.resume();
}, 100);
}
} 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');
if (!proxy.write(d)) {
socket.pause();
proxy.on('drain', function(){
socket.resume();
});
setTimeout(function(){
socket.resume();
}, 100);
}
} catch(err) {
}
});
proxy.on('error', function(err){
//console.log('Ignore proxy error');
});
proxy.on('close', function(had_error) {
try {
if(localAddress && localPort)
console.log('The proxy %s:%d closed', localAddress, localPort);
else
console.error('Connect to %s:%d failed', address, port);
socket.end();
} catch (err) {
}
}.bind(this));
socket.on('error', function(err){
//console.log('Ignore socket error');
});
socket.on('close', function(had_error) {
try {
if (this.proxy !== undefined) {
proxy.removeAllListeners('data');
proxy.end();
}
//console.error('The socket %s:%d closed',socket.remoteAddress,socket.remotePort);
} catch (err) {
}
}.bind(this));
}, process.argv[3]&&process.argv[4]&&{username:process.argv[3],password:process.argv[4]});
server.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:%s', HOST, PORT);
server.close();
server.listen(PORT, HOST);
}, 10000);
}
});
server.listen(PORT, HOST);
// vim: set filetype=javascript syntax=javascript :