-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild_geoip.py
executable file
·78 lines (71 loc) · 2.5 KB
/
build_geoip.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
#! python3
import sys
import struct
import socket
from collections import defaultdict
def apnic2bin(filename):
# http://ftp.apnic.net/stats/apnic/delegated-apnic-latest
# apnic|TH|ipv4|1.0.128.0|32768|20110408|allocated
records = defaultdict(list)
for line in open(filename):
if line[0] == '#':
continue
fields = line.strip().split('|')
if fields[2] != 'ipv4':
continue
if fields[1] == '*':
continue
cc = fields[1]
b = struct.unpack('!I', socket.inet_aton(fields[3]))[0]
e = b | (int(fields[4])-1)
records[cc].append([b, e])
for r in records:
records[r].sort()
records[r] = records[r][:65535]
with open('geoipdb.bin', 'wb') as fp:
for r in sorted(records.keys()):
fp.write(bytes(r[::-1], 'utf-8'))
fp.write(len(records[r]).to_bytes(2, 'little'))
for b, e in records[r]:
fp.write(b.to_bytes(4, 'little'))
fp.write(e.to_bytes(4, 'little'))
offset = 0
with open('geoipdb.idx', 'wb') as fp:
for r in sorted(records.keys()):
fp.write(bytes(r[::-1], 'utf-8'))
fp.write(offset.to_bytes(4, 'little'))
offset += 2 + 2 + 8 * len(records[r])
def csv2bin(filename):
# https://legacy-geoip-csv.ufficyo.com/
records = defaultdict(list)
for line in open(filename):
fields = line.strip().split(',')
cc = fields[4][1:-1]
# ipv6 not supported
if ":" in fields[0]:
continue
records[cc].append([int(fields[2][1:-1]), int(fields[3][1:-1])])
for r in records:
records[r].sort()
records[r] = records[r][:65535]
with open('geoipdb.bin', 'wb') as fp:
for r in sorted(records.keys()):
fp.write(bytes(r[::-1], 'utf-8'))
fp.write(len(records[r]).to_bytes(2, 'little'))
for b, e in records[r]:
fp.write(b.to_bytes(4, 'little'))
fp.write(e.to_bytes(4, 'little'))
offset = 0
with open('geoipdb.idx', 'wb') as fp:
for r in sorted(records.keys()):
fp.write(bytes(r[::-1], 'utf-8'))
fp.write(offset.to_bytes(4, 'little'))
offset += 2 + 2 + 8 * len(records[r])
if __name__ == '__main__':
if len(sys.argv) != 3:
print(f'usage: python {sys.argv[0]} maxmind|apnic <file>')
exit(1)
if sys.argv[1] == 'maxmind':
csv2bin(sys.argv[2])
else:
apnic2bin(sys.argv[2])