-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
33 lines (29 loc) · 1.03 KB
/
index.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
const net = require("net");
const WebSocket = require("ws");
function tcp_ws (target, forwardTo) {
if (!target && !forwardTo) return null;
var wss = new WebSocket.Server(forwardTo);
wss.on('connection', async function (ws, req) {
var socket = await new net.Socket().connect(target.port, target.hostname);
var duplex = WebSocket.createWebSocketStream(ws);
socket.pipe(duplex).pipe(socket);
req.on('close', socket.destroy);
duplex.on('error', () => socket.destroy());
socket.on('error', () => ws.close());
});
}
async function ws_tcp (target, port, hostname) {
if (!target && !forwardTo) return null;
var server = await new net.Server();
server.listen(port, hostname || '0.0.0.0');
server.on('connection', socket => {
var wss = new WebSocket(target);
var duplex = WebSocket.createWebSocketStream(wss);
duplex.pipe(socket).pipe(duplex);
socket.on('close', () => wss.close());
socket.on('error', () => wss.close());
duplex.on('error', () => socket.destroy());
});
}
module.exports.tcp = tcp_ws;
module.exports.ws = ws_tcp;