-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
97 lines (87 loc) · 2.69 KB
/
server.js
File metadata and controls
97 lines (87 loc) · 2.69 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
const http = require('http');
const https = require('https');
var net = require('net');
function main() {
const server = http.createServer(function (req, res) {
//console.log(req.headers);
//console.log(req.url);
console.log("Serving HTTP request");
if (req.url === "http://connectivitycheck.gstatic.com/generate_204" || !req.url || !(req.url.startsWith("https://") || req.url.startsWith("http://"))) {
res.writeHead(302, { Location: "https://www.google.com" });
res.write("That's not where you want to go!");
res.end();
}
else {
const out_req = (req.url.startsWith("https") ? https : http).request(req.url, { headers: req.headers, method: req.method });
out_req.on("response", out_res => {
res.writeHead(out_res.statusCode, out_res.headers);
out_res.pipe(res, true);
});
out_req.on("error", err => {
res.writeHead(500);
res.write(err.name);
res.write(err.message);
res.end();
})
req.pipe(out_req, true);
}
});
const regex_hostport = /^([^:]+)(:([0-9]+))?$/;
const getHostPortFromString = function (hostString, defaultPort) {
var host = hostString;
var port = defaultPort;
var result = regex_hostport.exec(hostString);
if (result != null) {
host = result[1];
if (result[2] != null) {
port = result[3];
}
}
return ([host, port]);
};
server.addListener('connect', function (req, socket, bodyhead) {
console.log("Connect event.");
var hostPort = getHostPortFromString(req.url, 443);
var hostDomain = hostPort[0];
var port = parseInt(hostPort[1]);
console.log("Proxying HTTPS request");
var proxySocket = new net.Socket();
proxySocket.connect(port, hostDomain, function () {
proxySocket.write(bodyhead);
socket.write("HTTP/" + req.httpVersion + " 200 Connection established\r\n\r\n");
}
);
proxySocket.on('data', function (chunk) {
socket.write(chunk);
});
proxySocket.on('end', function () {
socket.end();
});
proxySocket.on('error', function () {
socket.write("HTTP/" + req.httpVersion + " 500 Connection error\r\n\r\n");
socket.end();
});
socket.on('data', function (chunk) {
proxySocket.write(chunk);
});
socket.on('end', function () {
proxySocket.end();
});
socket.on('error', function () {
proxySocket.end();
});
});
server.on("error", err => {
console.log(err);
});
server.listen(80);
}
function main2() {
try {
main();
} catch (e) {
console.log(e);
main2();
}
}
main2();