-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxy.js
73 lines (62 loc) · 1.66 KB
/
proxy.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
var net = require("net");
var http = require("http");
const getHeaders = (rawHeaders) => {
let headers = {};
for (let i = 0; i < rawHeaders.length; i += 2) {
// https://imququ.com/post/the-proxy-connection-header-in-http-request.html
if (rawHeaders[i] === "Proxy-Connection") {
headers.connection = rawHeaders[i + 1];
} else {
headers[rawHeaders[i]] = rawHeaders[i + 1];
}
}
return headers;
};
// ECONNRESET https://zhuanlan.zhihu.com/p/86953757
process.on("uncaughtException", function (err) {
console.log(err.stack);
});
const app = http.createServer();
app.on("request", (request, response) => {
const url = new URL(request.url);
const options = {
hostname: request.headers.host,
port: 80,
path: url.pathname,
method: request.method,
headers: getHeaders(request.rawHeaders),
};
// 请求真实的服务器
const client = http
.request(options, (res) => {
response.writeHead(res.statusCode, res.headers);
res.pipe(response);
})
.on("error", () => {
console.log("http error");
response.end();
})
.on("close", () => {
console.log("http close");
response.end();
});
request.pipe(client);
});
app.on("connect", (request, socket) => {
const [hostname, port] = request.url.split(":");
const client = net
.connect(port, hostname, () => {
socket.write("HTTP/1.1 200 Connection Established\r\n\r\n");
client.pipe(socket);
})
.on("error", () => {
console.log("https error");
socket.end();
})
.on("close", () => {
console.log("https close");
socket.end();
});
socket.pipe(client);
});
app.listen(1080);