]>
git.ipfire.org Git - people/ms/libloc.git/blob - src/python/location-importer.in
2 ###############################################################################
4 # libloc - A library to determine the location of someone on the Internet #
6 # Copyright (C) 2020 IPFire Development Team <info@ipfire.org> #
8 # This library is free software; you can redistribute it and/or #
9 # modify it under the terms of the GNU Lesser General Public #
10 # License as published by the Free Software Foundation; either #
11 # version 2.1 of the License, or (at your option) any later version. #
13 # This library is distributed in the hope that it will be useful, #
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
16 # Lesser General Public License for more details. #
18 ###############################################################################
27 # Load our location module
29 import location
.database
30 import location
.importer
31 from location
.i18n
import _
34 log
= logging
.getLogger("location.importer")
45 parser
= argparse
.ArgumentParser(
46 description
=_("Location Importer Command Line Interface"),
48 subparsers
= parser
.add_subparsers()
50 # Global configuration flags
51 parser
.add_argument("--debug", action
="store_true",
52 help=_("Enable debug output"))
55 parser
.add_argument("--version", action
="version",
56 version
="%(prog)s @VERSION@")
59 parser
.add_argument("--database-host", required
=True,
60 help=_("Database Hostname"), metavar
=_("HOST"))
61 parser
.add_argument("--database-name", required
=True,
62 help=_("Database Name"), metavar
=_("NAME"))
63 parser
.add_argument("--database-username", required
=True,
64 help=_("Database Username"), metavar
=_("USERNAME"))
65 parser
.add_argument("--database-password", required
=True,
66 help=_("Database Password"), metavar
=_("PASSWORD"))
69 update_whois
= subparsers
.add_parser("update-whois", help=_("Update WHOIS Information"))
70 update_whois
.set_defaults(func
=self
.handle_update_whois
)
72 args
= parser
.parse_args()
74 # Enable debug logging
76 log
.setLevel(logging
.DEBUG
)
78 # Print usage if no action was given
79 if not "func" in args
:
86 # Parse command line arguments
87 args
= self
.parse_cli()
90 self
.db
= self
._setup
_database
(args
)
95 # Return with exit code
102 def _setup_database(self
, ns
):
104 Initialise the database
106 # Connect to database
107 db
= location
.database
.Connection(
108 host
=ns
.database_host
, database
=ns
.database_name
,
109 user
=ns
.database_username
, password
=ns
.database_password
,
112 with db
.transaction():
115 CREATE TABLE IF NOT EXISTS autnums(number integer, name text);
116 CREATE UNIQUE INDEX IF NOT EXISTS autnums_number ON autnums(number);
119 CREATE TABLE IF NOT EXISTS networks(network inet, autnum integer, country text);
120 CREATE UNIQUE INDEX IF NOT EXISTS networks_network ON networks(network);
125 def handle_update_whois(self
, ns
):
126 downloader
= location
.importer
.Downloader()
128 # Download all sources
129 with self
.db
.transaction():
130 # Create some temporary tables to store parsed data
132 CREATE TEMPORARY TABLE _autnums(number integer, organization text)
134 CREATE UNIQUE INDEX _autnums_number ON _autnums(number);
136 CREATE TEMPORARY TABLE _organizations(handle text, name text)
138 CREATE UNIQUE INDEX _organizations_handle ON _organizations(handle);
141 for source
in location
.importer
.WHOIS_SOURCES
:
142 with downloader
.request(source
, return_blocks
=True) as f
:
144 self
._parse
_block
(block
)
147 INSERT INTO autnums(number, name)
148 SELECT _autnums.number, _organizations.name FROM _autnums
149 LEFT JOIN _organizations ON _autnums.organization = _organizations.handle
150 ON CONFLICT (number) DO UPDATE SET name = excluded.name;
153 # Download all extended sources
154 for source
in location
.importer
.EXTENDED_SOURCES
:
155 with self
.db
.transaction():
157 with downloader
.request(source
) as f
:
159 self
._parse
_line
(line
)
161 def _parse_block(self
, block
):
162 # Get first line to find out what type of block this is
166 if line
.startswith("aut-num:"):
167 return self
._parse
_autnum
_block
(block
)
170 elif line
.startswith("organisation:"):
171 return self
._parse
_org
_block
(block
)
173 def _parse_autnum_block(self
, block
):
177 key
, val
= split_line(line
)
180 m
= re
.match(r
"^(AS|as)(\d+)", val
)
182 autnum
["asn"] = m
.group(2)
191 # Insert into database
192 self
.db
.execute("INSERT INTO _autnums(number, organization) \
193 VALUES(%s, %s) ON CONFLICT (number) DO UPDATE SET \
194 organization = excluded.organization",
195 autnum
.get("asn"), autnum
.get("org"),
198 def _parse_org_block(self
, block
):
202 key
, val
= split_line(line
)
204 if key
in ("organisation", "org-name"):
211 self
.db
.execute("INSERT INTO _organizations(handle, name) \
212 VALUES(%s, %s) ON CONFLICT (handle) DO \
213 UPDATE SET name = excluded.name",
214 org
.get("organisation"), org
.get("org-name"),
217 def _parse_line(self
, line
):
219 if line
.startswith("2"):
223 if line
.startswith("#"):
227 registry
, country_code
, type, line
= line
.split("|", 3)
229 log
.warning("Could not parse line: %s" % line
)
232 # Skip any lines that are for stats only
233 if country_code
== "*":
236 if type in ("ipv6", "ipv4"):
237 return self
._parse
_ip
_line
(country_code
, type, line
)
239 def _parse_ip_line(self
, country
, type, line
):
241 address
, prefix
, date
, status
, organization
= line
.split("|")
245 # Try parsing the line without organization
247 address
, prefix
, date
, status
= line
.split("|")
249 log
.warning("Unhandled line format: %s" % line
)
252 # Skip anything that isn't properly assigned
253 if not status
in ("assigned", "allocated"):
256 # Cast prefix into an integer
260 log
.warning("Invalid prefix: %s" % prefix
)
262 # Fix prefix length for IPv4
264 prefix
= 32 - int(math
.log(prefix
, 2))
266 # Try to parse the address
268 network
= ipaddress
.ip_network("%s/%s" % (address
, prefix
), strict
=False)
270 log
.warning("Invalid IP address: %s" % address
)
273 self
.db
.execute("INSERT INTO networks(network, country) \
274 VALUES(%s, %s) ON CONFLICT (network) DO \
275 UPDATE SET country = excluded.country",
276 "%s" % network
, country
,
280 def split_line(line
):
281 key
, colon
, val
= line
.partition(":")
283 # Strip any excess space
290 # Run the command line interface