-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.js
More file actions
153 lines (130 loc) · 4.59 KB
/
proxy.js
File metadata and controls
153 lines (130 loc) · 4.59 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// TCP socket proxy with programmable fault injection.
// Sits between client and braid-text server, allowing tests
// to inject disconnects, delays, blackholes, RSTs, and corruption.
var net = require("net")
var { EventEmitter } = require("events")
class SocketProxy extends EventEmitter {
constructor({ listen_port, target_host, target_port }) {
super()
this.listen_port = listen_port
this.target_host = target_host || "127.0.0.1"
this.target_port = target_port
this.mode = "passthrough" // passthrough | blackhole | delay | rst | close | corrupt
this.delay_ms = 0
this.connections = []
this.server = null
}
start() {
return new Promise((resolve, reject) => {
this.server = net.createServer(client_sock => {
var target_sock = net.createConnection(this.target_port, this.target_host)
var conn = { client: client_sock, target: target_sock, alive: true }
this.connections.push(conn)
var cleanup = () => {
conn.alive = false
this.connections = this.connections.filter(c => c !== conn)
client_sock.destroy()
target_sock.destroy()
}
client_sock.on("error", cleanup)
target_sock.on("error", cleanup)
client_sock.on("close", cleanup)
target_sock.on("close", cleanup)
// client -> server
client_sock.on("data", data => {
if (!conn.alive) return
this._forward(data, target_sock, conn, "c2s")
})
// server -> client
target_sock.on("data", data => {
if (!conn.alive) return
this._forward(data, client_sock, conn, "s2c")
})
this.emit("connection", conn)
})
this.server.on("error", reject)
this.server.listen(this.listen_port, "127.0.0.1", () => {
this.listen_port = this.server.address().port
resolve(this.listen_port)
})
})
}
_forward(data, dest, conn, direction) {
switch (this.mode) {
case "passthrough":
dest.write(data)
break
case "blackhole":
// silently drop all data
break
case "delay":
setTimeout(() => {
if (conn.alive) dest.write(data)
}, this.delay_ms)
break
case "rst":
// send a TCP RST by destroying with an error
conn.client.destroy()
conn.target.destroy()
break
case "close":
// graceful close
conn.client.end()
conn.target.end()
break
case "corrupt":
// flip some bits
var corrupted = Buffer.from(data)
if (corrupted.length > 0) {
var idx = Math.floor(Math.random() * corrupted.length)
corrupted[idx] ^= 0xff
}
dest.write(corrupted)
break
case "close-server-side":
// only kill the server->client direction
if (direction === "s2c") {
conn.client.destroy()
conn.target.destroy()
} else {
dest.write(data)
}
break
default:
dest.write(data)
}
}
// Switch proxy mode. Returns previous mode.
set_mode(mode, opts) {
var prev = this.mode
this.mode = mode
if (opts && opts.delay_ms) this.delay_ms = opts.delay_ms
return prev
}
// Kill all current connections
disconnect_all() {
for (var conn of this.connections.slice()) {
conn.client.destroy()
conn.target.destroy()
conn.alive = false
}
this.connections = []
}
// Kill connections then restore passthrough
reset() {
this.disconnect_all()
this.mode = "passthrough"
this.delay_ms = 0
}
connection_count() {
return this.connections.filter(c => c.alive).length
}
async stop() {
this.disconnect_all()
if (this.server) {
await new Promise(resolve => this.server.close(resolve))
this.server = null
}
}
}
module.exports = { SocketProxy }