-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiptables.js
31 lines (27 loc) · 828 Bytes
/
iptables.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
const {execSync} = require("child_process");
function netstat() {
const output = execSync("netstat -natd").toString();
const matches = output.match(/tcp\s+\d+\s+\d+\s+[\d\.:]+\s+(?<address>[\d\.]+):5671\s+ESTABLISHED/i);
if (matches && matches.groups) {
return matches.groups.address;
}
}
function iptablesDrop() {
const foreignAddress = netstat();
if (!foreignAddress) {
console.log(`Run "iptables -A INPUT -s x.x.x.x -j DROP" (replace x.x.x.x with Foreign Address)`);
} else {
const command = `iptables -A INPUT -s ${foreignAddress} -j DROP`;
console.log(`Running "${command}"`);
execSync(command);
}
}
function iptablesReset() {
const command = "iptables -D INPUT 1";
console.log(`Running "${command}"`);
execSync(command);
}
module.exports = {
iptablesDrop,
iptablesReset
};