-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmux_server.js
180 lines (149 loc) · 3.71 KB
/
mux_server.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
var config = {
host: null,
port: 2001
}
var net = require('net');
var log = require('./log');
var mappings = {
proxy: proxy,
rproxy: rproxy,
rproxycfg: rproxy_config
}
var proxy_config = {}
function proxy(socket, chunk)
{
var config = proxy_config[socket];
if (config) {
return;
}
config = {};
array = chunk.toString().split(':');
if (array.length == 2) {
config.target = array[0];
config.port = array[1];
log.info('received proxy config ' + config.target + ':' + config.port)
var client_socket = net.connect(config.port, config.target, function() {
proxy_config[socket] = config;
socket.write('cont');
client_socket.pipe(socket);
socket.pipe(client_socket);
log.info('proxy to ' + config.target + ':' + config.port)
});
} else {
log.warn('proxy config error: ' + chunk.toString());
socket.end();
}
socket.on('end', function() {
delete proxy_config[socket];
});
}
var rproxy_servers = {};
var rproxy_queue = {};
var rproxy_socket = {};
var rproxy_client_socket = {};
function rproxy(socket, chunk)
{
port = chunk.toString();
log.info('received rproxy port ' + port);
rproxy_client_socket[port] = socket;
socket.write('cont');
}
function process_rproxy_queue_async(port) {
process.nextTick(function() {
process_rproxy_queue(port);
});
}
function process_rproxy_queue(port) {
if (!rproxy_client_socket[port]) {
setTimeout(function() {
process_rproxy_queue_async(port);
}, 10);
return;
}
var buffer = rproxy_queue[port].shift();
if (typeof buffer == 'undefined') { // empty queue
setTimeout(function() {
process_rproxy_queue_async(port);
}, 10);
return;
}
if (buffer === null) {
rproxy_client_socket[port].end();
delete rproxy_client_socket[port];
} else {
rproxy_client_socket[port].write(buffer);
}
process_rproxy_queue_async(port);
}
function start_rproxy_servers(ports)
{
for (var i in ports) {
var port = ports[i];
rproxy_queue[port] = [];
var server = net.createServer(function(socket) {
if (rproxy_socket[port]) {
socket.end();
return;
}
rproxy_socket[port] = socket;
socket.on('data', function(chunk) {
rproxy_queue[port].push(chunk);
});
socket.on('end', function() {
rproxy_queue[port].push(null);
delete rproxy_socket[port];
});
});
server.listen(port, config.host, function() {
rproxy_servers[port] = server;
host = config.host;
if (host == null) {
host = '*';
}
log.info('rproxy server running at ' + host + ':' + port);
});
process_rproxy_queue_async(port);
}
}
function stop_rproxy_servers()
{
for (var port in rproxy_servers) {
var server = rproxy_servers[port];
server.close();
}
rproxy_servers = {};
rproxy_queue = {};
rproxy_socket = {};
}
function rproxy_config(socket, chunk)
{
var new_rproxy_config_string = chunk.toString();
var cfg = JSON.parse(new_rproxy_config_string);
log.info('received rproxy config ' + new_rproxy_config_string);
socket.end('cont');
stop_rproxy_servers();
start_rproxy_servers(cfg.ports);
}
var server = net.createServer(function(socket) {
var func = null;
socket.on('data', function(chunk) {
if (func === null) {
func = mappings[chunk];
if (func === null) {
log.warn('terminating request of ' + chunk.toString());
socket.end();
} else {
socket.write('cont');
}
} else {
func(socket, chunk);
}
});
});
server.listen(config.port, config.host, function() {
host = config.host;
if (host == null) {
host = '*';
}
log.info('server running at ' + host + ':' + config.port);
});