-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (65 loc) · 2.5 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
import dns from "dns";
import http from "http";
import net from "net";
import socks5 from "simple-socks";
const ALLOWED_DNS = process.env.ALLOWED_DNS;
const HOST = process.env.HOST;
const PROXY_PORT = parseInt(process.env.PROXY_PORT);
const PAC_SERVER_PORT = parseInt(process.env.PAC_SERVER_PORT);
const socksServer = socks5.createServer({
connectionFilter: function(destination, origin, callback) {
if (net.isIP(destination.address)) {
dns.reverse(destination.address, function(err, hostnames) {
if (!hostnames || !hostnames.length) {
console.log("Denying connection from %s:%d to %s:%d (no DNS available)",
origin.address, origin.port, destination.address, destination.port);
return callback(new Error("Connection denied"));
}
if (hostnames[0] === ALLOWED_DNS) {
return callback();
} else {
console.log("Denying connection from %s:%d to %s:%d (DNS: %s)",
origin.address, origin.port, destination.address, destination.port, hostnames[0]);
return callback(new Error("Connection denied"));
}
});
} else {
if (destination.address === ALLOWED_DNS) {
return callback();
} else {
console.log("Denying connection from %s:%d to %s:%d",
origin.address, origin.port, destination.address, destination.port);
return callback(new Error("Connection denied"));
}
}
}
});
socksServer.listen(PROXY_PORT);
socksServer.on("handshake", function(socket) {
console.log("New connection from %s:%d", socket.remoteAddress, socket.remotePort);
});
// When a reqest arrives for a remote destination
socksServer.on("proxyConnect", function(info, destination) {
console.log("Connected to %s:%d", info.address, info.port);
});
socksServer.on("proxyError", function(error) {
console.error("Error connecting to remote server");
console.error(error);
});
socksServer.on("proxyEnd", function(response, args) {
console.log("Socket closed with code %d", response);
console.log(args);
});
const pacConfig = `function FindProxyForURL(url, host) {
if (dnsDomainIs(host, "${ALLOWED_DNS}") || dnsDomainIs(host, ".${ALLOWED_DNS}")) {
return "SOCKS ${HOST}:${PROXY_PORT}; DIRECT";
} else {
return "DIRECT";
}
}`;
const pacServer = http.createServer(function(request, response) {
console.log("New connection to PAC server");
response.writeHead(200, { "Content-Type": "application/x-ns-proxy-autoconfig" });
response.end(pacConfig);
});
pacServer.listen(PAC_SERVER_PORT);