Repository navigation
Expand file tree
/
Copy pathwsproxy-srv.js
More file actions
152 lines (125 loc) · 3.33 KB
/
Copy pathwsproxy-srv.js
File metadata and controls
152 lines (125 loc) · 3.33 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
var net = require('net');
var http = require('http');
var common = require('./wsproxy-common.js');
var timelog = common.timelog;
var g_socket_id = 0;
var pipe_listener = net.createServer().listen(8986);
var pipe_sockets = [];
function pipe_alloc(assoc_id) {
for (var i = 0; i < pipe_sockets.length; i++) {
if (pipe_sockets[i].assoc_id == 0) {
pipe_sockets[i].assoc_id = assoc_id;
return pipe_sockets[i];
}
}
return null;
}
function pipe_list() {
for (var i = 0; i < pipe_sockets.length; i++) {
var assoc_id = pipe_sockets[i].assoc_id;
if (assoc_id == 0)
timelog('pipe[' + i + '] (free)');
else
timelog('pipe[' + i + '] => ' +
'public socket' + assoc_id);
}
}
function pipe_free(assoc_id) {
for (var i = 0; i < pipe_sockets.length; i++) {
if (pipe_sockets[i].assoc_id == assoc_id) {
timelog('pipe[' + i + '] public socket' +
assoc_id + ' ended.');
/* send FIN to the other end. */
pipe_sockets[i].end();
/* remove it from array. */
pipe_sockets.splice(i, 1);
return;
}
}
timelog('nothing ended.');
}
pipe_listener.on('connection', function(socket) {
timelog('new pipe accepted.');
common.setupSocket(socket);
socket.on('error', function(err){
timelog('pipe ERR: ' + err.message);
/* the same with on_end */
pipe_free(this.assoc_id);
this.unpipe(); /* to notify its associated
pub socket to close itself. */
this.destroy();
this.unref();
/* print all current pipes */
pipe_list();
});
socket.on('end', function() {
pipe_free(this.assoc_id);
this.unpipe(); /* to notify its associated
pub socket to close itself. */
this.destroy();
this.unref();
/* print all current pipes */
pipe_list();
});
socket.assoc_id = 0;
pipe_sockets.push(socket);
/* print all current pipes */
pipe_list();
});
var pub_listener = http.createServer().listen(8999);
timelog('listening...');
pub_listener.on('upgrade', function(req, socket, head) {
/* print client info */
client_info = {
'IP': socket.remoteAddress,
'port': socket.remotePort,
'UsrAgent': req.headers['user-agent'],
};
timelog(client_info);
/* setup socket */
common.setupSocket(socket);
/* allocate ID */
socket.assoc_id = (++g_socket_id);
timelog('new public socket' + socket.assoc_id);
/* allocate a free pipe socket */
var free_socket = pipe_alloc(socket.assoc_id);
if (free_socket == null) {
timelog('no free pipe');
return;
} else {
timelog('a free pipe allocated.');
}
/* print all current pipes */
pipe_list();
/* forward this upgrade message first */
if (head && head.length) socket.unshift(head);
var upgradeMsg =
req.method + ' ' + req.url + '\r\n' +
common.headerLines(req) + '\r\n';
free_socket.write(upgradeMsg);
/* associate sockets for incomming traffic */
socket.pipe(free_socket);
free_socket.pipe(socket);
/* disassociate pipe on deletion */
socket.on('end', function() {
pipe_free(this.assoc_id);
this.destroy();
this.unref();
/* print all current pipes */
pipe_list();
});
socket.on('error', function(err){
timelog('pub socket ERR: ' + err.message);
/* the same with on_end */
pipe_free(this.assoc_id);
this.destroy();
this.unref();
/* print all current pipes */
pipe_list();
});
socket.on('unpipe', function() {
timelog('pub socket unpiped.');
this.destroy();
this.unref();
});
});