/
createServerFilter.mjs
49 lines (40 loc) · 1.26 KB
/
createServerFilter.mjs
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
// npm i ipaddr.js
import { createProxyServer } from "../dist/index.js";
import ipaddr from "ipaddr.js";
import { reverse } from "node:dns/promises";
const server = createProxyServer({
filter: (destinationPort, destinationAddress, socket) => {
console.log("Attempting to connect to...");
console.log({ address: destinationAddress, port: destinationPort });
console.log();
console.log("Inbound origin of request is...");
console.log({ address: socket.remoteAddress, port: socket.remotePort });
const checkHostname = (hostname) => {
if (!/github/.test(hostname)) {
console.log(
"Not allowing connection to %s:%s",
destinationAddress,
destinationPort,
);
return false;
}
return true;
};
// prevent looking up ip address
if (!ipaddr.isValid(destinationAddress))
return checkHostname(destinationAddress);
return reverse(destinationAddress).then((hostnames) => {
if (!hostnames.some((host) => /github/.test(host))) {
console.log(
"Not allowing connection to %s:%s",
destinationAddress,
destinationPort,
);
return false;
}
return true;
});
},
});
// start listening!
server.listen(1080);