forked from Rosuav/shed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_traffic.pike
31 lines (30 loc) · 1.18 KB
/
find_traffic.pike
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
void allow(int addr,int len)
{
string block=sprintf("%d.%d.%d.%d/%d",addr>>24,(addr>>16)&255,(addr>>8)&255,addr&255,len);
write("Allowing: %s\n",block);
Process.create_process(({"iptables","-A","INPUT","--src",block,"-j","ACCEPT"}))->wait();
}
int main()
{
int addr;
for (int len=1;len<=32;++len)
{
allow(addr+(1<<(32-len)),len);
allow(addr,len);
int idx0,idx1;
while (1)
{
sleep(2);
[[idx1,int traf1],[idx0,int traf0]]=array_sscanf((Process.run("iptables --line-numbers -nvxL INPUT")->stdout/"\n")[<2..<1][*],"%d %*d %d");
write("%d for 1, %d for 0\n",traf1,traf0);
if (traf1<10000 && traf0<10000) {write("Insufficient traffic to be confident; waiting for more.\n"); continue;}
if (traf1>traf0) addr+=(1<<(32-len));
break;
}
Process.create_process(({"iptables","-D","INPUT",(string)idx0}))->wait();
Process.create_process(({"iptables","-D","INPUT",(string)idx1}))->wait();
}
string ip=sprintf("%d.%d.%d.%d",addr>>24,(addr>>16)&255,(addr>>8)&255,addr&255);
Process.create_process(({"whois",ip}))->wait();
Process.create_process(({"ip","r","get",ip}))->wait();
}