-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
89 lines (85 loc) · 2.75 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const http = require("http");
const port = process.env.PORT || 9191;
const net = require("net");
const url = require("url");
const requestHandler = (req, res) => {
// discard all request to proxy server except HTTP/1.1 CONNECT method
res.writeHead(405, { "Content-Type": "text/plain" });
res.end("Method not allowed");
};
const server = http.createServer(requestHandler);
const listener = server.listen(port, (err) => {
if (err) {
return console.error(err);
}
const info = listener.address();
console.log(
`Server is listening on address ${info.address} port ${info.port}`
);
});
server.on("connect", (req, clientSocket, head) => {
// listen only for HTTP/1.1 CONNECT method
console.log(
clientSocket.remoteAddress,
clientSocket.remotePort,
req.method,
req.url
);
// req.url.indexOf("youtube")
if (!req.headers["proxy-authorization"]) {
// here you can add check for any username/password, I just check that this header must exist!
clientSocket.write(
[
"HTTP/1.1 407 Proxy Authentication Required",
'Proxy-Authenticate: Basic realm="proxy"',
"Proxy-Connection: close",
].join("\r\n")
);
clientSocket.end("\r\n\r\n"); // empty body
return;
}
const { port, hostname } = url.parse(`//${req.url}`, false, true); // extract destination host and port from CONNECT request
if (hostname && port) {
const serverErrorHandler = (err) => {
console.error(err.message);
if (clientSocket) {
clientSocket.end(`HTTP/1.1 500 ${err.message}\r\n`);
}
};
const serverEndHandler = () => {
if (clientSocket) {
clientSocket.end(`HTTP/1.1 500 External Server End\r\n`);
}
};
const serverSocket = net.connect(port, hostname); // connect to destination host and port
const clientErrorHandler = (err) => {
console.error(err.message);
if (serverSocket) {
serverSocket.end();
}
};
const clientEndHandler = () => {
if (serverSocket) {
serverSocket.end();
}
};
clientSocket.on("error", clientErrorHandler);
clientSocket.on("end", clientEndHandler);
serverSocket.on("error", serverErrorHandler);
serverSocket.on("end", serverEndHandler);
serverSocket.on("connect", () => {
clientSocket.write(
["HTTP/1.1 200 Connection Established", "Proxy-agent: Node-VPN"].join(
"\r\n"
)
);
clientSocket.write("\r\n\r\n"); // empty body
// "blindly" (for performance) pipe client socket and destination socket between each other
serverSocket.pipe(clientSocket, { end: false });
clientSocket.pipe(serverSocket, { end: false });
});
} else {
clientSocket.end("HTTP/1.1 400 Bad Request\r\n");
clientSocket.destroy();
}
});