-
Notifications
You must be signed in to change notification settings - Fork 9
/
tcp-proxy.js
109 lines (100 loc) · 3.8 KB
/
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
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
/*
* Based on:
* https://github.com/bgaff/tcp.js
* https://github.com/bgaff/vnc.js
* http://engineering.linkedin.com/javascript/vncjs-how-build-javascript-vnc-client-24-hour-hackday
*/
var Proxy = function (client, appProcessor) {
//create the socket to connect to remote server on behalf of the client
var net = require("net");
var socket = new net.Socket();
socket.setEncoding("ascii");
socket.clientid = client.id;
var socketIsConnected = false;
var clientIsConnected = true; //at this stage this can be only true
socket.on("end", function () {
console.log("socket END clientid: " + socket.clientid + " on client socket.io id: " + client.id);
socketIsConnected = false;
if (clientIsConnected)
client.send(JSON.stringify({
action: "closed"
}));
});
socket.on("connect", function () {
console.log("socket CONNECT clientid: " + socket.clientid + " on client socket.io id: " + client.id);
socketIsConnected = true;
console.log("socket connected");
if (clientIsConnected)
client.send(JSON.stringify({
action: "connected"
}));
});
socket.on("data", function (data) {
console.log("socket DATA clientid: " + socket.clientid + " on client socket.io id: " + client.id);
console.log("RECV:" + data + ", length: " + data.length );
if (clientIsConnected)
client.send(JSON.stringify({
action: "data",
data: data
}));
});
//client related
client.on("reconnect", function () {
console.log("client on RECONNECT.io socket.io: " + client.id + " for socket clientid:" + socket.clientid);
});
client.on("disconnect", function () {
console.log("client on DISCONNECT.io socket.io: " + client.id + " for socket clientid:" + socket.clientid);
clientIsConnected = false;
if (socketIsConnected) {
/*
* app related data that the proxy should not be concerned with:
* remove nick from webUsers list
* send irc.quit
*/
if (typeof appProcessor === "function") {
appProcessor("disconnect", null, null, socket);
}
setTimeout(function () {
socketIsConnected = false;
socket.destroy();
console.log("client disconnected");
}, 1000);
}
});
client.on("message", function (message) {
console.log("client on MESSAGE.io socket.io: " + client.id + " for socket clientid:" + socket.clientid);
msg = JSON.parse(message);
switch (msg.action) {
case "connect":
console.log("CONNECT: " + msg.host + ":" + msg.port);
socket.connect(msg.port, msg.host);
break;
case "disconnect":
console.log("DISCONNECT");
if (socketIsConnected)
socket.end();
break;
case "data":
console.log("SEND");
if (socketIsConnected) {
console.log(msg.data);
socket.write(msg.data);
}
break;
default:
/*
* app related data that the proxy should not be concerned with:
* - requestStatistics()
* - addWebUser(nick)
* - updateWebUser(former, current)
* - listWebUsers()
*/
console.log(msg.action);
if (typeof appProcessor === "function") {
appProcessor("message", client, msg, null);
}
}
});
}
//export proxy
module.exports = Proxy;