forked from vyos/vyos-1x
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeoip.py
More file actions
323 lines (253 loc) · 11.8 KB
/
Copy pathgeoip.py
File metadata and controls
323 lines (253 loc) · 11.8 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import csv
import gzip
import os
import sqlite3
import zipfile
from io import TextIOWrapper
from pathlib import Path
from time import strftime
from vyos.remote import download
from vyos.template import is_ipv4, render
from vyos.utils.dict import dict_search_recursive
from vyos.utils.process import run
nftables_geoip_conf = '/run/nftables-geoip.conf'
dbip_database_raw = '/usr/share/vyos-geoip/dbip-country-lite.csv.gz'
dbip_asn_database_raw = '/usr/share/vyos-geoip/dbip-asn-lite.csv.gz'
mm_database_raw = '/usr/share/vyos-geoip/maxmind-country.zip'
mm_asn_database_raw = '/usr/share/vyos-geoip/maxmind-asn.zip'
geoip_database_path = '/var/cache/vyos/geoip-lookup.db'
geoip_lock_file = '/var/lock/vyos-geoip.lock'
# Raw data
def geoip_download_dbip():
url = 'https://download.db-ip.com/free/dbip-country-lite-{}.csv.gz'.format(strftime("%Y-%m"))
asn_url = 'https://download.db-ip.com/free/dbip-asn-lite-{}.csv.gz'.format(strftime("%Y-%m"))
try:
dirname = os.path.dirname(dbip_database_raw)
if not os.path.exists(dirname):
os.mkdir(dirname)
download(dbip_database_raw, url)
download(dbip_asn_database_raw, asn_url)
return True
except:
return False
def geoip_download_maxmind(account_id : str, license_key: str, lite : bool) -> bool:
db_str = 'GeoLite2' if lite else 'GeoIP2'
url = f'https://{account_id}:{license_key}@download.maxmind.com/geoip/databases/{db_str}-Country-CSV/download?suffix=zip'
asn_url = f'https://{account_id}:{license_key}@download.maxmind.com/geoip/databases/{db_str}-ASN-CSV/download?suffix=zip'
try:
dirname = os.path.dirname(mm_database_raw)
if not os.path.exists(dirname):
os.mkdir(dirname)
download(mm_database_raw, url)
download(mm_asn_database_raw, asn_url)
return True
except:
return False
# VyOS database
def db_is_initialised():
if not os.path.exists(geoip_database_path):
return False
with sqlite3.connect(geoip_database_path) as conn:
cur = conn.cursor()
cur.execute("PRAGMA table_info(geoip_ranges);")
rows = cur.fetchall()
return len(rows) > 0
def db_initialise():
dirname = os.path.dirname(geoip_database_path)
if not os.path.exists(dirname):
os.mkdir(dirname)
with sqlite3.connect(geoip_database_path) as conn:
cur = conn.cursor()
cur.execute("""
CREATE TABLE IF NOT EXISTS geoip_ranges (
country_code TEXT,
asn INT,
range TEXT NOT NULL,
version INT NOT NULL
)
""")
cur.execute('CREATE INDEX IF NOT EXISTS idx_cc_version ON geoip_ranges(country_code, version)')
cur.execute('CREATE INDEX IF NOT EXISTS idx_asn_version ON geoip_ranges(asn, version)')
conn.commit()
def db_import_dbip_ranges(replace=True, delete_file=False):
if not os.path.exists(dbip_database_raw):
return False
if not os.path.exists(geoip_database_path):
return False
try:
with sqlite3.connect(geoip_database_path) as conn:
cur = conn.cursor()
if replace:
cur.execute('DELETE FROM geoip_ranges')
with gzip.open(dbip_database_raw, mode='rt') as csv_fh:
reader = csv.reader(csv_fh)
for start, end, code in reader:
version = 4 if is_ipv4(start) else 6
cur.execute('INSERT INTO geoip_ranges (country_code, range, version) VALUES (?, ?, ?)', (code.lower(), f'{start}-{end}', version))
with gzip.open(dbip_asn_database_raw, mode='rt') as csv_fh:
reader = csv.reader(csv_fh)
for start, end, asn, _ in reader:
version = 4 if is_ipv4(start) else 6
cur.execute('INSERT INTO geoip_ranges (asn, range, version) VALUES (?, ?, ?)', (asn, f'{start}-{end}', version))
conn.commit()
if delete_file:
os.unlink(dbip_database_raw)
os.unlink(dbip_asn_database_raw)
return True
except:
return False
def db_import_maxmind_ranges(replace=True, delete_file=False):
if not os.path.exists(mm_database_raw):
return False
if not zipfile.is_zipfile(mm_database_raw):
return False
if not os.path.exists(geoip_database_path):
return False
try:
with sqlite3.connect(geoip_database_path) as conn:
cur = conn.cursor()
if replace:
cur.execute('DELETE FROM geoip_ranges')
with zipfile.ZipFile(mm_database_raw, mode='r') as zip_fh:
directory = os.path.dirname(zip_fh.namelist()[0])
prefix = 'GeoLite2' if any(f.startswith('GeoLite2') for f in zip_fh.namelist()) else 'GeoIP2'
ipv4_file = f'{directory}/{prefix}-Country-Blocks-IPv4.csv'
ipv6_file = f'{directory}/{prefix}-Country-Blocks-IPv6.csv'
locations_file = f'{directory}/{prefix}-Country-Locations-en.csv'
locations_map = {}
with zip_fh.open(locations_file) as raw_csv_fh:
with TextIOWrapper(raw_csv_fh, encoding='utf-8') as csv_fh:
reader = csv.DictReader(csv_fh)
for row in reader:
id = row['geoname_id']
locations_map[id] = row['country_iso_code']
with zip_fh.open(ipv4_file) as raw_csv_fh:
with TextIOWrapper(raw_csv_fh, encoding='utf-8') as csv_fh:
reader = csv.DictReader(csv_fh)
for row in reader:
id = row['geoname_id']
if not id or id not in locations_map:
continue
code = locations_map[id]
cur.execute('INSERT INTO geoip_ranges (country_code, range, version) VALUES (?, ?, 4)', (code.lower(), row['network']))
with zip_fh.open(ipv6_file) as raw_csv_fh:
with TextIOWrapper(raw_csv_fh, encoding='utf-8') as csv_fh:
reader = csv.DictReader(csv_fh)
for row in reader:
id = row['geoname_id']
if not id or id not in locations_map:
continue
code = locations_map[id]
cur.execute('INSERT INTO geoip_ranges (country_code, range, version) VALUES (?, ?, 6)', (code.lower(), row['network']))
with zipfile.ZipFile(mm_asn_database_raw, mode='r') as zip_fh:
directory = os.path.dirname(zip_fh.namelist()[0])
prefix = 'GeoLite2' if any(f.startswith('GeoLite2') for f in zip_fh.namelist()) else 'GeoIP2'
ipv4_file = f'{directory}/{prefix}-ASN-Blocks-IPv4.csv'
ipv6_file = f'{directory}/{prefix}-ASN-Blocks-IPv6.csv'
with zip_fh.open(ipv4_file) as raw_csv_fh:
with TextIOWrapper(raw_csv_fh, encoding='utf-8') as csv_fh:
reader = csv.DictReader(csv_fh)
for row in reader:
cur.execute('INSERT INTO geoip_ranges (asn, range, version) VALUES (?, ?, 4)', (row['autonomous_system_number'], row['network']))
with zip_fh.open(ipv6_file) as raw_csv_fh:
with TextIOWrapper(raw_csv_fh, encoding='utf-8') as csv_fh:
reader = csv.DictReader(csv_fh)
for row in reader:
cur.execute('INSERT INTO geoip_ranges (asn, range, version) VALUES (?, ?, 6)', (row['autonomous_system_number'], row['network']))
conn.commit()
if delete_file:
os.unlink(mm_database_raw)
os.unlink(mm_asn_database_raw)
return True
except:
return False
def db_return_cc_ranges(codes, version):
out = []
with sqlite3.connect(geoip_database_path) as conn:
cur = conn.cursor()
ph = ','.join(['?'] * len(codes))
for row in cur.execute(f'SELECT range FROM geoip_ranges WHERE version = ? AND country_code IN ({ph})', [version, *codes]):
out.append(row[0])
return out
def db_return_asn_ranges(asn, version):
out = []
with sqlite3.connect(geoip_database_path) as conn:
cur = conn.cursor()
ph = ','.join(['?'] * len(asn))
for row in cur.execute(f'SELECT range FROM geoip_ranges WHERE version = ? AND asn IN ({ph})', [version, *asn]):
out.append(row[0])
return out
# Update
def geoip_refresh():
with GeoIPLock(geoip_lock_file) as lock:
if not lock:
return True
if not os.path.exists(nftables_geoip_conf):
return False
result = run(f'nft --file {nftables_geoip_conf}')
if result != 0:
return False
return True
def geoip_update(firewall=None, policy=None):
with GeoIPLock(geoip_lock_file) as lock:
if not lock:
print("Script is already running")
return False
if not firewall and not policy:
print("Firewall and policy are not configured")
return True
if not os.path.exists(geoip_database_path):
print("Running one-time database initialisation")
db_initialise()
db_import_dbip_ranges()
firewall_sets = {'v4': {}, 'v6': {}}
policy_sets = {'v4': {}, 'v6': {}}
if firewall:
for codes, path in dict_search_recursive(firewall, 'country_code'):
if path[0] == 'policy':
continue
version = 6 if path[0] == 'ipv6' else 4
vprefix = '6' if version == 6 else ''
set_name = f'GEOIP_CC{vprefix}_{path[1]}_{path[2]}_{path[4]}'
firewall_sets[f'v{version}'][set_name] = db_return_cc_ranges(codes, version)
for asns, path in dict_search_recursive(firewall, 'asn'):
version = 6 if path[0] == 'ipv6' else 4
vprefix = '6' if version == 6 else ''
set_name = f'GEOIP_ASN{vprefix}_{path[1]}_{path[2]}_{path[4]}'
firewall_sets[f'v{version}'][set_name] = db_return_asn_ranges(asns, version)
if policy:
for codes, path in dict_search_recursive(policy, 'country_code'):
if path[0] == 'firewall':
continue
version = 6 if path[0] == 'route6' else 4
vprefix = '6' if version == 6 else ''
set_name = f'GEOIP_CC{vprefix}_{path[0]}_{path[1]}_{path[3]}'
policy_sets[f'v{version}'][set_name] = db_return_cc_ranges(codes, version)
for asns, path in dict_search_recursive(policy, 'asn'):
version = 6 if path[0] == 'route6' else 4
vprefix = '6' if version == 6 else ''
set_name = f'GEOIP_ASN{vprefix}_{path[0]}_{path[1]}_{path[3]}'
policy_sets[f'v{version}'][set_name] = db_return_asn_ranges(asns, version)
render(
nftables_geoip_conf,
'firewall/nftables-geoip-update.j2',
{'firewall_sets': firewall_sets, 'policy_sets': policy_sets},
group='vyattacfg',
permission=0o664,
)
result = run(f'nft --file {nftables_geoip_conf}')
if result != 0:
print('Error: GeoIP failed to update firewall and/or policy')
return False
return True
# Utility
class GeoIPLock(object):
def __init__(self, file):
self.file = file
def __enter__(self):
if os.path.exists(self.file):
return False
Path(self.file).touch()
return True
def __exit__(self, exc_type, exc_value, tb):
os.unlink(self.file)