forked from encharm/dewebsockify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dewebsockify.js
executable file
·52 lines (42 loc) · 1.23 KB
/
dewebsockify.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
#!/usr/bin/env node
var net = require('net');
var WebSocket = require('ws');
var net = require('net');
if(process.argv.length < 4) {
console.log("Usage: dewebsockify ws://someserver/path <port>");
process.exit(1);
}
var port = process.argv[3];
var server = net.createServer(function(socket) {
var ws = new WebSocket(process.argv[2], {
rejectUnauthorized: false,
protocol: "binary"
});
socket.on('error', function(err) {
ws.close();
socket.destroy();
});
ws.on('error', function(err) {
console.log("Connection from", socket.address().address ,"to end-point...", err.toString());
socket.destroy();
});
ws.on('open', function() {
socket.address = socket.address().address;
console.log("Connection from", socket.address ,"accepted");
socket.on('data', function(data) {
ws.send(data);
});
});
ws.on('message', function(data, flags) {
socket.write(data);
});
socket.on('end', function() {
console.log("Connection from", socket.address ,"ended");
ws.removeAllListeners('close');
ws.close();
});
ws.on('close', function() {
console.log("Web socket server has closed connection, closing socket to", socket.address);
socket.destroy();
});
}).listen(port);