]> git.ipfire.org Git - location/libloc.git/blob - src/python/location.in
10618e2f2b74c44e78815e9665a4166fbf5bb52b
[location/libloc.git] / src / python / location.in
1 #!/usr/bin/python3
2 ###############################################################################
3 # #
4 # libloc - A library to determine the location of someone on the Internet #
5 # #
6 # Copyright (C) 2017 IPFire Development Team <info@ipfire.org> #
7 # #
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. #
12 # #
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. #
17 # #
18 ###############################################################################
19
20 import argparse
21 import datetime
22 import ipaddress
23 import logging
24 import os
25 import shutil
26 import socket
27 import sys
28 import time
29
30 # Load our location module
31 import location
32 import location.downloader
33 from location.i18n import _
34
35 # Setup logging
36 log = logging.getLogger("location")
37
38 # Output formatters
39
40 class OutputFormatter(object):
41 def __init__(self, ns):
42 self.ns = ns
43
44 def __enter__(self):
45 # Open the output
46 self.open()
47
48 return self
49
50 def __exit__(self, type, value, tb):
51 if tb is None:
52 self.close()
53
54 @property
55 def name(self):
56 if "country_code" in self.ns:
57 return "networks_country_%s" % self.ns.country_code[0]
58
59 elif "asn" in self.ns:
60 return "networks_AS%s" % self.ns.asn[0]
61
62 def open(self):
63 pass
64
65 def close(self):
66 pass
67
68 def network(self, network):
69 print(network)
70
71
72 class IpsetOutputFormatter(OutputFormatter):
73 """
74 For nftables
75 """
76 def open(self):
77 print("create %s hash:net family inet hashsize 1024 maxelem 65536" % self.name)
78
79 def network(self, network):
80 print("add %s %s" % (self.name, network))
81
82
83 class NftablesOutputFormatter(OutputFormatter):
84 """
85 For nftables
86 """
87 def open(self):
88 print("define %s = {" % self.name)
89
90 def close(self):
91 print("}")
92
93 def network(self, network):
94 print(" %s," % network)
95
96
97 class XTGeoIPOutputFormatter(OutputFormatter):
98 """
99 Formats the output in that way, that it can be loaded by
100 the xt_geoip kernel module from xtables-addons.
101 """
102 def network(self, network):
103 n = ipaddress.ip_network("%s" % network)
104
105 for address in (n.network_address, n.broadcast_address):
106 bytes = socket.inet_pton(
107 socket.AF_INET6 if address.version == 6 else socket.AF_INET,
108 "%s" % address,
109 )
110
111 os.write(1, bytes)
112
113
114 class CLI(object):
115 output_formats = {
116 "ipset" : IpsetOutputFormatter,
117 "list" : OutputFormatter,
118 "nftables" : NftablesOutputFormatter,
119 "xt_geoip" : XTGeoIPOutputFormatter,
120 }
121
122 def parse_cli(self):
123 parser = argparse.ArgumentParser(
124 description=_("Location Database Command Line Interface"),
125 )
126 subparsers = parser.add_subparsers()
127
128 # Global configuration flags
129 parser.add_argument("--debug", action="store_true",
130 help=_("Enable debug output"))
131 parser.add_argument("--quiet", action="store_true",
132 help=_("Enable quiet mode"))
133
134 # version
135 parser.add_argument("--version", action="version",
136 version="%(prog)s @VERSION@")
137
138 # database
139 parser.add_argument("--database", "-d",
140 default="@databasedir@/database.db", help=_("Path to database"),
141 )
142
143 # public key
144 parser.add_argument("--public-key", "-k",
145 default="@databasedir@/signing-key.pem", help=_("Public Signing Key"),
146 )
147
148 # Show the database version
149 version = subparsers.add_parser("version",
150 help=_("Show database version"))
151 version.set_defaults(func=self.handle_version)
152
153 # lookup an IP address
154 lookup = subparsers.add_parser("lookup",
155 help=_("Lookup one or multiple IP addresses"),
156 )
157 lookup.add_argument("address", nargs="+")
158 lookup.set_defaults(func=self.handle_lookup)
159
160 # Dump the whole database
161 dump = subparsers.add_parser("dump",
162 help=_("Dump the entire database"),
163 )
164 dump.add_argument("output", nargs="?", type=argparse.FileType("w"))
165 dump.set_defaults(func=self.handle_dump)
166
167 # Update
168 update = subparsers.add_parser("update", help=_("Update database"))
169 update.set_defaults(func=self.handle_update)
170
171 # Verify
172 verify = subparsers.add_parser("verify",
173 help=_("Verify the downloaded database"))
174 verify.set_defaults(func=self.handle_verify)
175
176 # Get AS
177 get_as = subparsers.add_parser("get-as",
178 help=_("Get information about one or multiple Autonomous Systems"),
179 )
180 get_as.add_argument("asn", nargs="+")
181 get_as.set_defaults(func=self.handle_get_as)
182
183 # Search for AS
184 search_as = subparsers.add_parser("search-as",
185 help=_("Search for Autonomous Systems that match the string"),
186 )
187 search_as.add_argument("query", nargs=1)
188 search_as.set_defaults(func=self.handle_search_as)
189
190 # List all networks in an AS
191 list_networks_by_as = subparsers.add_parser("list-networks-by-as",
192 help=_("Lists all networks in an AS"),
193 )
194 list_networks_by_as.add_argument("asn", nargs=1, type=int)
195 list_networks_by_as.add_argument("--family", choices=("ipv6", "ipv4"))
196 list_networks_by_as.add_argument("--output-format",
197 choices=self.output_formats.keys(), default="list")
198 list_networks_by_as.set_defaults(func=self.handle_list_networks_by_as)
199
200 # List all networks in a country
201 list_networks_by_cc = subparsers.add_parser("list-networks-by-cc",
202 help=_("Lists all networks in a country"),
203 )
204 list_networks_by_cc.add_argument("country_code", nargs=1)
205 list_networks_by_cc.add_argument("--family", choices=("ipv6", "ipv4"))
206 list_networks_by_cc.add_argument("--output-format",
207 choices=self.output_formats.keys(), default="list")
208 list_networks_by_cc.set_defaults(func=self.handle_list_networks_by_cc)
209
210 # List all networks with flags
211 list_networks_by_flags = subparsers.add_parser("list-networks-by-flags",
212 help=_("Lists all networks with flags"),
213 )
214 list_networks_by_flags.add_argument("--anonymous-proxy",
215 action="store_true", help=_("Anonymous Proxies"),
216 )
217 list_networks_by_flags.add_argument("--satellite-provider",
218 action="store_true", help=_("Satellite Providers"),
219 )
220 list_networks_by_flags.add_argument("--anycast",
221 action="store_true", help=_("Anycasts"),
222 )
223 list_networks_by_flags.add_argument("--family", choices=("ipv6", "ipv4"))
224 list_networks_by_flags.add_argument("--output-format",
225 choices=self.output_formats.keys(), default="list")
226 list_networks_by_flags.set_defaults(func=self.handle_list_networks_by_flags)
227
228 args = parser.parse_args()
229
230 # Configure logging
231 if args.debug:
232 location.logger.set_level(logging.DEBUG)
233 elif args.quiet:
234 location.logger.set_level(logging.WARNING)
235
236 # Print usage if no action was given
237 if not "func" in args:
238 parser.print_usage()
239 sys.exit(2)
240
241 return args
242
243 def run(self):
244 # Parse command line arguments
245 args = self.parse_cli()
246
247 # Open database
248 try:
249 db = location.Database(args.database)
250 except FileNotFoundError as e:
251 sys.stderr.write("location: Could not open database %s: %s\n" \
252 % (args.database, e))
253 sys.exit(1)
254
255 # Translate family (if present)
256 if "family" in args:
257 if args.family == "ipv6":
258 args.family = socket.AF_INET6
259 elif args.family == "ipv4":
260 args.family = socket.AF_INET
261 else:
262 args.family = 0
263
264 # Call function
265 try:
266 ret = args.func(db, args)
267
268 # Catch invalid inputs
269 except ValueError as e:
270 sys.stderr.write("%s\n" % e)
271 ret = 2
272
273 # Return with exit code
274 if ret:
275 sys.exit(ret)
276
277 # Otherwise just exit
278 sys.exit(0)
279
280 def handle_version(self, db, ns):
281 """
282 Print the version of the database
283 """
284 t = time.strftime(
285 "%a, %d %b %Y %H:%M:%S GMT", time.gmtime(db.created_at),
286 )
287
288 print(t)
289
290 def handle_lookup(self, db, ns):
291 ret = 0
292
293 format = " %-24s: %s"
294
295 for address in ns.address:
296 try:
297 network = db.lookup(address)
298 except ValueError:
299 print(_("Invalid IP address: %s") % address, file=sys.stderr)
300
301 args = {
302 "address" : address,
303 "network" : network,
304 }
305
306 # Nothing found?
307 if not network:
308 print(_("Nothing found for %(address)s") % args, file=sys.stderr)
309 ret = 1
310 continue
311
312 print("%s:" % address)
313 print(format % (_("Network"), network))
314
315 # Print country
316 if network.country_code:
317 country = db.get_country(network.country_code)
318
319 print(format % (
320 _("Country"),
321 country.name if country else network.country_code),
322 )
323
324 # Print AS information
325 if network.asn:
326 autonomous_system = db.get_as(network.asn)
327
328 print(format % (
329 _("Autonomous System"),
330 autonomous_system or "AS%s" % network.asn),
331 )
332
333 # Anonymous Proxy
334 if network.has_flag(location.NETWORK_FLAG_ANONYMOUS_PROXY):
335 print(format % (
336 _("Anonymous Proxy"), _("yes"),
337 ))
338
339 # Satellite Provider
340 if network.has_flag(location.NETWORK_FLAG_SATELLITE_PROVIDER):
341 print(format % (
342 _("Satellite Provider"), _("yes"),
343 ))
344
345 # Anycast
346 if network.has_flag(location.NETWORK_FLAG_ANYCAST):
347 print(format % (
348 _("Anycast"), _("yes"),
349 ))
350
351 return ret
352
353 def handle_dump(self, db, ns):
354 # Use output file or write to stdout
355 f = ns.output or sys.stdout
356
357 # Format everything like this
358 format = "%-24s %s\n"
359
360 # Write metadata
361 f.write("#\n# Location Database Export\n#\n")
362
363 f.write("# Generated: %s\n" % time.strftime(
364 "%a, %d %b %Y %H:%M:%S GMT", time.gmtime(db.created_at),
365 ))
366
367 if db.vendor:
368 f.write("# Vendor: %s\n" % db.vendor)
369
370 if db.license:
371 f.write("# License: %s\n" % db.license)
372
373 f.write("#\n")
374
375 if db.description:
376 for line in db.description.splitlines():
377 f.write("# %s\n" % line)
378
379 f.write("#\n")
380
381 # Iterate over all ASes
382 for a in db.ases:
383 f.write("\n")
384 f.write(format % ("aut-num:", "AS%s" % a.number))
385 f.write(format % ("name:", a.name))
386
387 flags = {
388 location.NETWORK_FLAG_ANONYMOUS_PROXY : "is-anonymous-proxy:",
389 location.NETWORK_FLAG_SATELLITE_PROVIDER : "is-satellite-provider:",
390 location.NETWORK_FLAG_ANYCAST : "is-anycast:",
391 }
392
393 # Iterate over all networks
394 for n in db.networks:
395 f.write("\n")
396 f.write(format % ("net:", n))
397
398 if n.country_code:
399 f.write(format % ("country:", n.country_code))
400
401 if n.asn:
402 f.write(format % ("aut-num:", n.asn))
403
404 # Print all flags
405 for flag in flags:
406 if n.has_flag(flag):
407 f.write(format % (flags[flag], "yes"))
408
409 def handle_get_as(self, db, ns):
410 """
411 Gets information about Autonomous Systems
412 """
413 ret = 0
414
415 for asn in ns.asn:
416 try:
417 asn = int(asn)
418 except ValueError:
419 print(_("Invalid ASN: %s") % asn, file=sys.stderr)
420 ret = 1
421 continue
422
423 # Fetch AS from database
424 a = db.get_as(asn)
425
426 # Nothing found
427 if not a:
428 print(_("Could not find AS%s") % asn, file=sys.stderr)
429 ret = 1
430 continue
431
432 print(_("AS%(asn)s belongs to %(name)s") % { "asn" : a.number, "name" : a.name })
433
434 return ret
435
436 def handle_search_as(self, db, ns):
437 for query in ns.query:
438 # Print all matches ASes
439 for a in db.search_as(query):
440 print(a)
441
442 def handle_update(self, db, ns):
443 # Fetch the timestamp we need from DNS
444 t = location.discover_latest_version()
445
446 # Parse timestamp into datetime format
447 timestamp = datetime.datetime.fromtimestamp(t) if t else None
448
449 # Check the version of the local database
450 if db and timestamp and db.created_at >= timestamp.timestamp():
451 log.info("Already on the latest version")
452 return
453
454 # Download the database into the correct directory
455 tmpdir = os.path.dirname(ns.database)
456
457 # Create a downloader
458 d = location.downloader.Downloader()
459
460 # Try downloading a new database
461 try:
462 t = d.download(public_key=ns.public_key, timestamp=timestamp, tmpdir=tmpdir)
463
464 # If no file could be downloaded, log a message
465 except FileNotFoundError as e:
466 log.error("Could not download a new database")
467 return 1
468
469 # If we have not received a new file, there is nothing to do
470 if not t:
471 return 3
472
473 # Move temporary file to destination
474 shutil.move(t.name, ns.database)
475
476 return 0
477
478 def handle_verify(self, ns):
479 try:
480 db = location.Database(ns.database)
481 except FileNotFoundError as e:
482 log.error("%s: %s" % (ns.database, e))
483 return 127
484
485 # Verify the database
486 with open(ns.public_key, "r") as f:
487 if not db.verify(f):
488 log.error("Could not verify database")
489 return 1
490
491 # Success
492 log.debug("Database successfully verified")
493 return 0
494
495 def __get_output_formatter(self, ns):
496 try:
497 cls = self.output_formats[ns.output_format]
498 except KeyError:
499 cls = OutputFormatter
500
501 return cls(ns)
502
503 def handle_list_networks_by_as(self, db, ns):
504 with self.__get_output_formatter(ns) as f:
505 for asn in ns.asn:
506 # Print all matching networks
507 for n in db.search_networks(asn=asn, family=ns.family):
508 f.network(n)
509
510 def handle_list_networks_by_cc(self, db, ns):
511 with self.__get_output_formatter(ns) as f:
512 for country_code in ns.country_code:
513 # Print all matching networks
514 for n in db.search_networks(country_code=country_code, family=ns.family):
515 f.network(n)
516
517 def handle_list_networks_by_flags(self, db, ns):
518 flags = 0
519
520 if ns.anonymous_proxy:
521 flags |= location.NETWORK_FLAG_ANONYMOUS_PROXY
522
523 if ns.satellite_provider:
524 flags |= location.NETWORK_FLAG_SATELLITE_PROVIDER
525
526 if ns.anycast:
527 flags |= location.NETWORK_FLAG_ANYCAST
528
529 if not flags:
530 raise ValueError(_("You must at least pass one flag"))
531
532 with self.__get_output_formatter(ns) as f:
533 for n in db.search_networks(flags=flags, family=ns.family):
534 f.network(n)
535
536
537 def main():
538 # Run the command line interface
539 c = CLI()
540 c.run()
541
542 main()