-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocklist.py
94 lines (85 loc) · 2.42 KB
/
blocklist.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
import argparse
import gzip
import ipaddress
import os
import sys
import urllib.request
if sys.version_info[0] != 3 or sys.version_info[1] < 3:
print("This script requires Python version 3.3 or above")
sys.exit(1)
parser = argparse.ArgumentParser(
description='''download block lists from iblocklist.com
and convert to ipfilter.dat.
''',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
'-D',
'--debug',
type=int,
default=0,
choices=range(0, 3),
help='debug flag'
)
parser.add_argument(
'-i',
'--infile',
default='lists.input',
help='input file with urls of lists'
)
parser.add_argument(
'-o',
'--outfile',
default='ipfilter.dat',
help='output file'
)
args = parser.parse_args()
def dprint(level, *a, **kwargs):
if args.debug >= level:
print(*a, file=sys.stderr, **kwargs)
def removeZeros(ip):
# splits the ip by "."
# converts the words to integeres to remove leading removeZeros
# convert back the integer to string and join them back to a string
new_ip = ".".join([str(int(i)) for i in ip.split(".")])
return new_ip
print(os.path.dirname(os.path.realpath(__file__)))
if os.path.isabs(args.infile):
input = args.infile
else:
input = os.path.join(os.getcwd(), args.infile)
if not os.access(input, os.R_OK):
input = os.path.join(os.path.dirname(os.path.realpath(__file__)),
args.infile)
if not os.access(input, os.R_OK):
dprint(0,'cannot read input file:', args.infile)
nets = []
for line in open(input, 'r'):
url = line.split('#')[0].strip()
if not url:
continue
try:
response = urllib.request.urlopen(url)
except Exception as e:
dprint(0,'%s: ' % url, e)
else:
dprint(1,url)
for line in gzip.open(response):
line = line.decode(errors='ignore').strip()
a = line.rsplit(':',1)
if len(a) != 2:
dprint(2,'skipped line:', line)
continue
comment, range = a
a = range.split('-')
if len(a) != 2:
dprint(2,'bad ip range:', line)
continue
a = map(removeZeros, a)
start,end = map(ipaddress.IPv4Address, a)
nets.extend(list(ipaddress.summarize_address_range(start, end)))
outf = open(args.outfile,'w')
for n in sorted(list(set(nets))):
print(n, file=outf)
outf.close()