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
|
#!/usr/bin/env python3
#
# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Note: script is used both in opmode and by build hook `40-init-geoip-database.chroot`
import argparse
import sys
from vyos.configquery import ConfigTreeQuery
from vyos.geoip import geoip_download_dbip
from vyos.geoip import geoip_download_maxmind
from vyos.geoip import db_initialise
from vyos.geoip import db_is_initialised
from vyos.geoip import db_import_dbip_ranges
from vyos.geoip import db_import_maxmind_ranges
from vyos.geoip import geoip_update
from vyos.utils.process import run
def get_config(conf):
return (
conf.get_config_dict(['firewall', 'global-options', 'geoip'], key_mangling=('-', '_'), get_first_key=True,
no_tag_node_value_mangle=True, with_defaults=True),
conf.get_config_dict(['firewall'], key_mangling=('-', '_'), get_first_key=True,
no_tag_node_value_mangle=True) if conf.exists(['firewall']) else None,
conf.get_config_dict(['policy'], key_mangling=('-', '_'), get_first_key=True,
no_tag_node_value_mangle=True) if conf.exists(['policy']) else None,
)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--init", help="Initialise", action="store_true")
parser.add_argument('--download', help="Download", action="store_true")
args = parser.parse_args()
if args.init:
db_initialise()
db_import_dbip_ranges(delete_file=True)
sys.exit(0)
conf = ConfigTreeQuery()
if not conf.exists(['system', 'name-server']):
print('There are no system name-servers configured')
sys.exit(1)
options, firewall, policy = get_config(conf)
src_addr = options.get('source_address', '')
vrf = options.get('vrf', None)
if args.download or not vrf:
if options['provider'] == 'db-ip':
print('Downloading latest DB-IP database...')
if not geoip_download_dbip(source_address=src_addr):
print('Failed to download, aborting.')
sys.exit(1)
elif options['provider'] == 'maxmind':
account_id = options['maxmind_account_id']
license_key = options['maxmind_license_key']
lite = 'maxmind_lite' in options
print('Downloading latest MaxMind database...')
if not geoip_download_maxmind(account_id, license_key, lite, source_address=src_addr):
print('Failed to download, aborting.')
sys.exit(1)
if args.download:
sys.exit(0)
elif vrf:
run(['python3', __file__, '--download'], stdout=None, stderr=None, vrf=vrf)
if not db_is_initialised():
db_initialise()
print('Extracting database...')
if options['provider'] == 'db-ip':
if not db_import_dbip_ranges(delete_file=True):
print('Failed to extract, aborting.')
sys.exit(1)
elif options['provider'] == 'maxmind':
if not db_import_maxmind_ranges(delete_file=True):
print('Failed to extract, aborting.')
sys.exit(1)
if not geoip_update(firewall=firewall, policy=policy):
sys.exit(1)
|