-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwithout_http.js
58 lines (51 loc) · 1.69 KB
/
without_http.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
const net = require('net');
(async function () {
const web = net.createServer();
web.on('connection', s => {
console.log('web connected');
s.on('data', d => {
console.log('web data: ' + d);
s.write('NEW DATA FROM WEB');
});
s.on('error', err => console.log('web err: ' + err));
s.on('end', () => console.log('web end'));
});
web.listen(3002);
const web_socket = net.createConnection({ host: '127.0.0.1', port: 3002 });
await new Promise(resolve => {
web_socket.on('connect', () => {
console.log('web_socket connected');
resolve();
});
});
const proxy = net.createServer();
proxy.on('connection', s => {
console.log('proxy connected');
s.on('data', d => {
console.log('proxy data: ' + d);
if ('CONNECT' == d)
s.write('OK');
else
web_socket.write(d);
});
s.on('error', err => console.log('proxy err: ' + err));
s.on('end', () => console.log('proxy end'));
web_socket.pipe(s);
});
proxy.listen(3001);
const client = net.createConnection({ host: '127.0.0.1', port: 3001 });
client.on('data', d => {
console.log('client data: ' + d);
if (d == 'OK')
client.write('some data');
else {
client.end();
web_socket.end();
proxy.close();
web.close();
}
});
client.on('error', err => console.log('client err: ' + err));
client.on('end', () => console.log('client end'));
client.write('CONNECT');
})();