-
Notifications
You must be signed in to change notification settings - Fork 1
/
ports_scanner.js
38 lines (31 loc) · 1.17 KB
/
ports_scanner.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
const net = require("net");
module.exports = sys => (q, s, n) => {
if (q.method !== "POST") return n();
const ip = q.headers["x-forwarded-for"]?.split(",")[0] || q.socket.address().address;
const wl = sys.prepare("SELECT ip FROM ip_white WHERE ip = ?;");
const ib = sys.prepare("SELECT ip FROM ip_block WHERE ip = ?;");
const bl = sys.prepare("SELECT port FROM blocked_open_ports;").all().map(i => i.port);
if (wl.get(ip) || ib.get(ip) || !bl.length) return n();
const sock = new net.Socket();
let timeout = null;
sock.on('error', _ => null);
sock.on('close', _ => {
if (!bl.length || !sock.destroyed) {
sock.removeAllListeners('error');
sock.removeAllListeners('close');
sock.removeAllListeners('connect');
return;
};
sock.connect(bl.shift(), ip);
timeout = setTimeout(_ => sock.end(), 2000);
});
sock.on('connect', _ => {
clearTimeout(timeout);
console.log(`Detected open port at [${ip}]:${sock.remotePort}. Adding to blacklist.`);
sys.prepare("INSERT OR IGNORE INTO ip_block VALUES (?);").run(ip);
sock.destroy();
});
sock.connect(bl.shift(), ip);
timeout = setTimeout(_ => sock.end(), 2000);
n();
};