forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_tcp_proxy.js
68 lines (52 loc) · 1.33 KB
/
node_tcp_proxy.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
const net = require("net");
process.on("uncaughtException", function (error) {
console.error(error);
});
if (process.argv.length != 4) {
console.log("usage: %s <localport> <remoteport>", process.argv[1]);
process.exit();
}
const localport = process.argv[2];
const remoteport = process.argv[3];
const remotehost = "127.0.0.1";
const server = net.createServer(function (localsocket) {
const remotesocket = new net.Socket();
remotesocket.connect(remoteport, remotehost);
localsocket.on("data", function (data) {
const flushed = remotesocket.write(data);
if (!flushed) {
localsocket.pause();
}
});
remotesocket.on("data", function (data) {
const flushed = localsocket.write(data);
if (!flushed) {
remotesocket.pause();
}
});
localsocket.on("drain", function () {
remotesocket.resume();
});
remotesocket.on("drain", function () {
localsocket.resume();
});
localsocket.on("close", function () {
remotesocket.end();
});
remotesocket.on("close", function () {
localsocket.end();
});
localsocket.on("error", function () {
localsocket.end();
});
remotesocket.on("error", function () {
remotesocket.end();
});
});
server.listen(localport);
console.log(
"redirecting connections from 127.0.0.1:%d to %s:%d",
localport,
remotehost,
remoteport,
);