-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
create_db.py
executable file
·300 lines (265 loc) · 10.7 KB
/
create_db.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import gzip
import time
from multiprocessing import cpu_count, Queue, Process, current_process
import logging
import re
import os.path
from db.model import Block
from db.helper import setup_connection
from netaddr import iprange_to_cidrs
VERSION = '2.0'
FILELIST = ['afrinic.db.gz', 'apnic.db.inet6num.gz', 'apnic.db.inetnum.gz', 'arin.db.gz',
'lacnic.db.gz', 'ripe.db.inetnum.gz', 'ripe.db.inet6num.gz']
NUM_WORKERS = cpu_count()
LOG_FORMAT = '%(asctime)-15s - %(name)-9s - %(levelname)-8s - %(processName)-11s - %(filename)s - %(message)s'
COMMIT_COUNT = 10000
NUM_BLOCKS = 0
CURRENT_FILENAME = "empty"
class ContextFilter(logging.Filter):
def filter(self, record):
record.filename = CURRENT_FILENAME
return True
logger = logging.getLogger('create_db')
logger.setLevel(logging.INFO)
f = ContextFilter()
logger.addFilter(f)
formatter = logging.Formatter(LOG_FORMAT)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
def get_source(filename: str):
if filename.startswith('afrinic'):
return b'afrinic'
elif filename.startswith('apnic'):
return b'apnic'
elif filename.startswith('arin'):
return b'arin'
elif 'lacnic' in filename:
return b'lacnic'
elif filename.startswith('ripe'):
return b'ripe'
else:
logger.error(f"Can not determine source for {filename}")
return None
def parse_property(block: str, name: str) -> str:
match = re.findall(b'^%s:\s?(.+)$' % (name), block, re.MULTILINE)
if match:
# remove empty lines and remove multiple names
x = b' '.join(list(filter(None, (x.strip().replace(
b"%s: " % name, b'').replace(b"%s: " % name, b'') for x in match))))
# remove multiple whitespaces by using a split hack
# decode to latin-1 so it can be inserted in the database
return ' '.join(x.decode('latin-1').split())
else:
return None
def parse_property_inetnum(block: str):
# IPv4
match = re.findall(
rb'^inetnum:[\s]*((?:\d{1,3}\.){3}\d{1,3})[\s]*-[\s]*((?:\d{1,3}\.){3}\d{1,3})', block, re.MULTILINE)
if match:
# netaddr can only handle strings, not bytes
ip_start = match[0][0].decode('utf-8')
ip_end = match[0][1].decode('utf-8')
cidrs = iprange_to_cidrs(ip_start, ip_end)
return cidrs
# direct CIDR in lacnic db
match = re.findall(rb'^inetnum:[\s]*((?:\d{1,3}\.){3}\d{1,3}/\d+)', block, re.MULTILINE)
if match:
return match[0]
# lacnic with wrong ip
# inetnum: 177.46.7/24
match = re.findall(rb'^inetnum:[\s]*((?:\d{1,3}\.){2}\d{1,3}/\d+)', block, re.MULTILINE)
if match:
tmp = match[0].split(b"/")
return f"{tmp[0].decode('utf-8')}.0/{tmp[1].decode('utf-8')}".encode("utf-8")
# inetnum: 148.204/16
match = re.findall(rb'^inetnum:[\s]*((?:\d{1,3}\.){1}\d{1,3}/\d+)', block, re.MULTILINE)
if match:
tmp = match[0].split(b"/")
return f"{tmp[0].decode('utf-8')}.0.0/{tmp[1].decode('utf-8')}".encode("utf-8")
# IPv6
match = re.findall(
rb'^inet6num:[\s]*([0-9a-fA-F:\/]{1,43})', block, re.MULTILINE)
if match:
return match[0]
# ARIN route IPv4
match = re.findall(
rb'^route:[\s]*((?:\d{1,3}\.){3}\d{1,3}/\d{1,2})', block, re.MULTILINE)
if match:
return match[0]
# ARIN route6 IPv6
match = re.findall(
rb'^route6:[\s]*([0-9a-fA-F:\/]{1,43})', block, re.MULTILINE)
if match:
return match[0]
return None
def read_blocks(filename: str) -> list:
if filename.endswith('.gz'):
opemethod = gzip.open
else:
opemethod = open
cust_source = get_source(filename.split('/')[-1])
single_block = b''
blocks = []
with opemethod(filename, mode='rb') as f:
for line in f:
# skip comments
if line.startswith(b'%') or line.startswith(b'#') or line.startswith(b'remarks:'):
continue
# block end
if line.strip() == b'':
if single_block.startswith(b'inetnum:') or single_block.startswith(b'inet6num:') or single_block.startswith(b'route:') or single_block.startswith(b'route6:'):
# add source
single_block += b"cust_source: %s" % (cust_source)
blocks.append(single_block)
if len(blocks) % 1000 == 0:
logger.debug(
f"parsed another 1000 blocks ({len(blocks)} so far)")
single_block = b''
# comment out to only parse x blocks
# if len(blocks) == 100:
# break
else:
single_block = b''
else:
single_block += line
logger.info(f"Got {len(blocks)} blocks")
global NUM_BLOCKS
NUM_BLOCKS = len(blocks)
return blocks
def parse_blocks(jobs: Queue, connection_string: str):
session = setup_connection(connection_string)
counter = 0
BLOCKS_DONE = 0
start_time = time.time()
while True:
block = jobs.get()
if block is None:
break
inetnum = parse_property_inetnum(block)
if not inetnum:
# invalid entry, do not parse
logger.warning(f"Could not parse inetnum on block {block}. skipping")
continue
netname = parse_property(block, b'netname')
# No netname field in ARIN block, try origin
if not netname:
netname = parse_property(block, b'origin')
description = parse_property(block, b'descr')
country = parse_property(block, b'country')
# if we have a city object, append it to the country
city = parse_property(block, b'city')
if city:
country = f"{country} - {city}"
maintained_by = parse_property(block, b'mnt-by')
created = parse_property(block, b'created')
last_modified = parse_property(block, b'last-modified')
if not last_modified:
changed = parse_property(block, b'changed')
# ***@ripe.net 19960624
# a.c@domain.com 20060331
# maybe repeated multiple times, we only take the first
if re.match(r'^.+?@.+? \d+', changed):
date = changed.split(" ")[1].strip()
if len(date) == 8:
year = int(date[0:4])
month = int(date[4:6])
day = int(date[6:8])
# some sanity checks for dates
if month >= 1 and month <=12 and day >= 1 and day <= 31:
last_modified = f"{year}-{month}-{day}"
else:
logger.debug(f"ignoring invalid changed date {date}")
else:
logger.debug(f"ignoring invalid changed date {date}")
elif "@" in changed:
# email in changed field without date
logger.debug(f"ignoring invalid changed date {changed}")
else:
last_modified = changed
status = parse_property(block, b'status')
source = parse_property(block, b'cust_source')
if isinstance(inetnum, list):
for cidr in inetnum:
b = Block(inetnum=str(cidr), netname=netname, description=description, country=country,
maintained_by=maintained_by, created=created, last_modified=last_modified, source=source, status=status)
session.add(b)
else:
b = Block(inetnum=inetnum.decode('utf-8'), netname=netname, description=description, country=country,
maintained_by=maintained_by, created=created, last_modified=last_modified, source=source, status=status)
session.add(b)
counter += 1
BLOCKS_DONE += 1
if counter % COMMIT_COUNT == 0:
session.commit()
session.close()
session = setup_connection(connection_string)
# not really accurate at the moment
percent = (BLOCKS_DONE * NUM_WORKERS * 100) / NUM_BLOCKS
if percent > 100:
percent = 100
logger.debug('committed {} blocks ({} seconds) {:.1f}% done.'.format(
counter, round(time.time() - start_time, 2), percent))
counter = 0
start_time = time.time()
session.commit()
logger.debug('committed last blocks')
session.close()
logger.debug(f"{current_process().name} finished")
def main(connection_string):
overall_start_time = time.time()
# reset database
setup_connection(connection_string, create_db=True)
for entry in FILELIST:
global CURRENT_FILENAME
CURRENT_FILENAME = entry
f_name = f"./databases/{entry}"
if os.path.exists(f_name):
logger.info(f"parsing database file: {f_name}")
start_time = time.time()
blocks = read_blocks(f_name)
logger.info(f"database parsing finished: {round(time.time() - start_time, 2)} seconds")
logger.info('parsing blocks')
start_time = time.time()
jobs = Queue()
workers = []
# start workers
logger.debug(f"starting {NUM_WORKERS} processes")
for _ in range(NUM_WORKERS):
p = Process(target=parse_blocks, args=(
jobs, connection_string,), daemon=True)
p.start()
workers.append(p)
# add tasks
for b in blocks:
jobs.put(b)
for _ in range(NUM_WORKERS):
jobs.put(None)
jobs.close()
jobs.join_thread()
# wait to finish
for p in workers:
p.join()
logger.info(
f"block parsing finished: {round(time.time() - start_time, 2)} seconds")
else:
logger.info(
f"File {f_name} not found. Please download using download_dumps.sh")
CURRENT_FILENAME = "empty"
logger.info(
f"script finished: {round(time.time() - overall_start_time, 2)} seconds")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Create DB')
parser.add_argument('-c', dest='connection_string', type=str,
required=True, help="Connection string to the postgres database")
parser.add_argument("-d", "--debug", action="store_true",
help="set loglevel to DEBUG")
parser.add_argument('--version', action='version',
version=f"%(prog)s {VERSION}")
args = parser.parse_args()
if args.debug:
logger.setLevel(logging.DEBUG)
main(args.connection_string)