-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
37 lines (31 loc) · 1009 Bytes
/
index.ts
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
import * as WebSocket from 'ws';
import {Socket, Server as TCPServer} from 'net';
import codec, {NULL_CHAR} from './codec';
export default function startProxy(remoteIP: string, {wsPort, tcpPort, remotePort = 1710, log = false}: {wsPort?: number; tcpPort?: number; remotePort?: number; log?: boolean}) {
const socket = new Socket();
const core = codec(socket, log);
socket.connect({host: remoteIP, port: remotePort});
if (wsPort) {
new WebSocket.Server({port: wsPort})
.on('connection', (ws: WebSocket) => {
ws.on('message', message => {
core.writeStream.write(message);
});
core.readStream.on('data', data => {
ws.send(data);
});
});
}
if (tcpPort) {
new TCPServer(socket => {
const client = codec(socket, false);
client.readStream.on('data', message => {
core.writeStream.write(message);
core.writeStream.write(NULL_CHAR);
});
core.readStream.on('data', message => {
client.writeStream.write(message);
});
}).listen(tcpPort);
}
}