-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
160 lines (110 loc) · 3.3 KB
/
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
#!/usr/bin/env node
let net = require('net');
let conf = require('./config.js');
let proxy;
let tasks = {};
let taskCnt = 1;
let server = net.createServer(function (client) {
if (!proxy) {
client.end('No proxy connected');
return;
}
if (taskCnt > 10240) {
taskCnt = 1;
}
let taskId = taskCnt++;
console.log('New user-request #', taskId);
tasks[taskId] = client;
client.on('data', function (data) {
if (!proxy) {
client.end('No proxy connected');
return;
}
let buf = Buffer.alloc(6);
buf.writeUInt16BE(taskId, 0);
buf.writeUInt32BE(data.length, 2);
buf = Buffer.concat([buf, data]);
proxy.write(buf);
});
client.on('end', function () {
console.log('user-request end #', taskId);
if (proxy && tasks[taskId]) {
let buf = Buffer.alloc(6);
buf.writeUInt16BE(taskId, 0);
buf.writeUInt32BE(0, 2);
proxy.write(buf);
delete tasks[taskId];
}
});
client.on('error', function (error) {
console.log('user-request #' + taskId + ' Error: ' + error.toString());
delete tasks[taskId];
});
});
server.on('error', function (error) {
console.log('user-request error: ' + error.toString());
});
server.listen(conf.public.port, conf.public.host, function () {
console.log('user-request listening on %s:%s', conf.public.host, conf.public.port);
});
let proxyListener = net.createServer(function (client) {
proxy = client;
let buf = Buffer.alloc(0);
proxy.on('data', function (data) {
buf = Buffer.concat([buf, data]);
while (buf.length >= 6) {
let taskId = buf.readInt16BE(0);
let size = buf.readInt32BE(2);
// Proxy informs - shared server close connection
if (!size) {
if (tasks[taskId]) {
console.log('Closing request #' + taskId);
tasks[taskId].end();
delete tasks[taskId];
}
buf = buf.slice(6);
}
else {
// Check completed frames in buffer
if (6 + size <= buf.length) {
// console.log('Data from proxy for request #' + taskId + ', len=' + size);
if (tasks[taskId]) {
tasks[taskId].write(buf.slice(6, size + 6));
}
buf = buf.slice(6 + size);
}
else {
// console.log('There are some partial data for request #' + taskId + ', len=' + (buf.length - 6) + ', needs=' + size);
break;
}
}
}
});
proxy.on('end', function () {
Object.keys(tasks).forEach(function (task) {
tasks[task].end('Proxy disconnected');
});
tasks = {};
proxy = undefined;
});
proxy.on('error', function (error) {
console.log('proxy-listener client error: ' + error.toString());
Object.keys(tasks).forEach(function (task) {
tasks[task].end('Proxy disconnected');
});
tasks = {};
proxy = undefined;
});
console.log('Proxy connected');
});
proxyListener.on('error', function (error) {
console.log('proxy-listener server error: ' + error.toString());
Object.keys(tasks).forEach(function (task) {
tasks[task].end('Proxy disconnected');
});
tasks = {};
proxy = undefined;
});
proxyListener.listen(conf.upstream.port, conf.upstream.host, function () {
console.log('proxy-listener server listening on %s:%s', conf.upstream.host, conf.upstream.port);
});