-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathblocklists_simple.py
72 lines (57 loc) · 1.63 KB
/
blocklists_simple.py
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
#!/usr/bin/env python
# encoding utf-8
"""
exabgp: aggregate_requests.py
based partially on aggregate.py from https://adamkuj.net/blog/2014/04/08/a-utility-to-perform-ipv4-ipv6-prefix-aggregation/
"""
from IPy import IP, IPSet
import requests
import socket
from sys import stdout
from time import sleep
a = IPSet()
b = IPSet()
# how long should we sleep in minutes?
mins = 30
expires = ''
nexthop = ' next-hop 0.0.0.1 origin incomplete as-path [64666 64666 64666]\n'
#nexthop = ' next-hop self community [64512:666]\n'
blocklists = ['https://www.spamhaus.org/drop/drop.txt',
'https://www.spamhaus.org/drop/edrop.txt',
'https://rules.emergingthreats.net/fwrules/emerging-Block-IPs.txt']
def makeprefix(ip):
net = IP(ip, make_net=True)
net.NoPrefixForSingleIp = None
return net
def fetch():
a = IPSet([])
for blocklist in blocklists:
r = requests.get(blocklist)
for line in r.iter_lines():
if linefilter(line):
a.add(makeprefix(linefilter(line)))
for prefix in b:
if b.len() > 0 and b.__contains__(prefix) and not a.__contains__(prefix):
a.discard(prefix)
stdout.write('withdraw route ' + str(prefix) + nexthop)
stdout.flush()
for prefix in a:
if a.__contains__(prefix) and not b.__contains__(prefix):
stdout.write('announce route ' + str(prefix) + nexthop)
stdout.flush()
b.add(a)
def linefilter(line):
if line.startswith(';'):
if line.startswith('; Expires:'):
expires = line.lstrip('; Expires: ')
else:
pass
pass
elif line.startswith('#'):
pass
else:
ip = line.split(' ')[0].split(';')[0].split('#')[0].strip().decode()
return ip
while True:
fetch()
sleep(mins * 60)