-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
67 lines (58 loc) · 2.43 KB
/
index.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
const fs = require('fs');
const http = require('http');
const https = require('https');
const net = require('net');
const request = require('request');
const tls = require('tls');
const url = require('url');
(async function () {
const web_res = (req, res) => {
res.writeHead(200);
let r = { method: req.method, headers: req.headers };
res.end(`<html><body><pre>${JSON.stringify(r, null, 2)}</pre></body></html>`);
};
const web_on_connection = s => console.log('client connected to web server');
// -- http web server --
const web_server = http.createServer(web_res);
web_server.on('connection', web_on_connection);
web_server.listen(80);
// -- https web server --
const webs_opt = { key: fs.readFileSync('key.pem'), cert: fs.readFileSync('cert.pem') };
const webs_server = https.createServer(webs_opt, web_res);
webs_server.on('connection', web_on_connection);
webs_server.listen(443);
// -- tcp proxy --
const tcp_proxy = net.createServer(s => {
s.on('data', buf => {
console.log('proxy data:', buf.slice(0, 7), buf.toString('utf8', 0, 7));
if ('CONNECT' == buf.toString('utf8', 0, 7)) {
s.write('HTTP/1.1 200 OK\r\n\r\n');
} else {
if (!web_socket.destroyed)
web_socket.write(buf);
}
});
web_socket.pipe(s);
});
tcp_proxy.listen(3000);
// -- client request --
const request_url = 'https://localhost'; // 'https://httpbin.org/get?q=1';
const request_url_parts = url.parse(request_url);
request_url_parts.port = request_url_parts.protocol == 'https:' ? 443 : 80;
// -- socket to web --
const web_socket = net.createConnection({ host: request_url_parts.hostname, port: request_url_parts.port });
await new Promise(resolve => {
web_socket.on('connect', s => { console.log('web_socket connected'); resolve(); });
});
web_socket.on('data', buf => console.log('web_socket data:', buf.slice(0, 7), buf.toString('utf8', 0, 7)));
request.get({ url: request_url, proxy: 'http://127.0.0.1:3000', strictSSL: false }, (err, res) => {
if (err) {
console.log('REQ ERROR', err);
} else {
console.log('REQ OK', res.body);
}
web_server.close();
webs_server.close();
tcp_proxy.close();
});
})();