]>
Commit | Line | Data |
---|---|---|
5118a4b8 MT |
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 | |
4439e317 MT |
21 | import ipaddress |
22 | import os | |
23 | import socket | |
5118a4b8 | 24 | import sys |
a68a46f5 | 25 | import time |
5118a4b8 MT |
26 | |
27 | # Load our location module | |
28 | import location | |
7dccb767 | 29 | from location.i18n import _ |
5118a4b8 | 30 | |
4439e317 MT |
31 | # Output formatters |
32 | ||
33 | class OutputFormatter(object): | |
71e0ad0b MT |
34 | def __init__(self, ns): |
35 | self.ns = ns | |
36 | ||
4439e317 MT |
37 | def __enter__(self): |
38 | # Open the output | |
39 | self.open() | |
40 | ||
41 | return self | |
42 | ||
43 | def __exit__(self, type, value, tb): | |
44 | if tb is None: | |
45 | self.close() | |
46 | ||
71e0ad0b MT |
47 | @property |
48 | def name(self): | |
49 | if "country_code" in self.ns: | |
50 | return "networks_country_%s" % self.ns.country_code[0] | |
51 | ||
52 | elif "asn" in self.ns: | |
53 | return "networks_AS%s" % self.ns.asn[0] | |
54 | ||
4439e317 MT |
55 | def open(self): |
56 | pass | |
57 | ||
58 | def close(self): | |
59 | pass | |
60 | ||
61 | def network(self, network): | |
62 | print(network) | |
63 | ||
64 | ||
6da14cc1 MT |
65 | class IpsetOutputFormatter(OutputFormatter): |
66 | """ | |
67 | For nftables | |
68 | """ | |
69 | def open(self): | |
70 | print("create %s hash:net family inet hashsize 1024 maxelem 65536" % self.name) | |
71 | ||
72 | def network(self, network): | |
73 | print("add %s %s" % (self.name, network)) | |
74 | ||
75 | ||
71e0ad0b MT |
76 | class NftablesOutputFormatter(OutputFormatter): |
77 | """ | |
78 | For nftables | |
79 | """ | |
80 | def open(self): | |
81 | print("define %s = {" % self.name) | |
82 | ||
83 | def close(self): | |
84 | print("}") | |
85 | ||
86 | def network(self, network): | |
87 | print(" %s," % network) | |
88 | ||
89 | ||
4439e317 MT |
90 | class XTGeoIPOutputFormatter(OutputFormatter): |
91 | """ | |
92 | Formats the output in that way, that it can be loaded by | |
93 | the xt_geoip kernel module from xtables-addons. | |
94 | """ | |
95 | def network(self, network): | |
96 | n = ipaddress.ip_network("%s" % network) | |
97 | ||
98 | for address in (n.network_address, n.broadcast_address): | |
99 | bytes = socket.inet_pton( | |
100 | socket.AF_INET6 if address.version == 6 else socket.AF_INET, | |
101 | "%s" % address, | |
102 | ) | |
103 | ||
104 | os.write(1, bytes) | |
105 | ||
106 | ||
5118a4b8 | 107 | class CLI(object): |
4439e317 | 108 | output_formats = { |
6da14cc1 | 109 | "ipset" : IpsetOutputFormatter, |
4439e317 | 110 | "list" : OutputFormatter, |
71e0ad0b | 111 | "nftables" : NftablesOutputFormatter, |
4439e317 MT |
112 | "xt_geoip" : XTGeoIPOutputFormatter, |
113 | } | |
114 | ||
5118a4b8 MT |
115 | def parse_cli(self): |
116 | parser = argparse.ArgumentParser( | |
117 | description=_("Location Database Command Line Interface"), | |
118 | ) | |
119 | subparsers = parser.add_subparsers() | |
120 | ||
121 | # Global configuration flags | |
122 | parser.add_argument("--debug", action="store_true", | |
123 | help=_("Enable debug output")) | |
124 | ||
ddb184be MT |
125 | # version |
126 | parser.add_argument("--version", action="version", | |
d2714e4a | 127 | version="%(prog)s @VERSION@") |
ddb184be | 128 | |
2538ed9a MT |
129 | # database |
130 | parser.add_argument("--database", "-d", | |
131 | default="@databasedir@/database.db", help=_("Path to database"), | |
132 | ) | |
133 | ||
726f9984 MT |
134 | # public key |
135 | parser.add_argument("--public-key", "-k", | |
136 | default="@databasedir@/signing-key.pem", help=_("Public Signing Key"), | |
137 | ) | |
138 | ||
5118a4b8 MT |
139 | # lookup an IP address |
140 | lookup = subparsers.add_parser("lookup", | |
141 | help=_("Lookup one or multiple IP addresses"), | |
142 | ) | |
143 | lookup.add_argument("address", nargs="+") | |
144 | lookup.set_defaults(func=self.handle_lookup) | |
145 | ||
a68a46f5 MT |
146 | # Dump the whole database |
147 | dump = subparsers.add_parser("dump", | |
148 | help=_("Dump the entire database"), | |
149 | ) | |
150 | dump.add_argument("output", nargs="?", type=argparse.FileType("w")) | |
151 | dump.set_defaults(func=self.handle_dump) | |
152 | ||
fadc1af0 MT |
153 | # Get AS |
154 | get_as = subparsers.add_parser("get-as", | |
155 | help=_("Get information about one or multiple Autonomous Systems"), | |
156 | ) | |
157 | get_as.add_argument("asn", nargs="+") | |
158 | get_as.set_defaults(func=self.handle_get_as) | |
159 | ||
da3e360e MT |
160 | # Search for AS |
161 | search_as = subparsers.add_parser("search-as", | |
162 | help=_("Search for Autonomous Systems that match the string"), | |
163 | ) | |
164 | search_as.add_argument("query", nargs=1) | |
165 | search_as.set_defaults(func=self.handle_search_as) | |
166 | ||
43154ed7 MT |
167 | # List all networks in an AS |
168 | list_networks_by_as = subparsers.add_parser("list-networks-by-as", | |
169 | help=_("Lists all networks in an AS"), | |
170 | ) | |
171 | list_networks_by_as.add_argument("asn", nargs=1, type=int) | |
44e5ef71 | 172 | list_networks_by_as.add_argument("--family", choices=("ipv6", "ipv4")) |
4439e317 MT |
173 | list_networks_by_as.add_argument("--output-format", |
174 | choices=self.output_formats.keys(), default="list") | |
43154ed7 MT |
175 | list_networks_by_as.set_defaults(func=self.handle_list_networks_by_as) |
176 | ||
ccc7ab4e | 177 | # List all networks in a country |
b5cdfad7 | 178 | list_networks_by_cc = subparsers.add_parser("list-networks-by-cc", |
ccc7ab4e MT |
179 | help=_("Lists all networks in a country"), |
180 | ) | |
b5cdfad7 | 181 | list_networks_by_cc.add_argument("country_code", nargs=1) |
44e5ef71 | 182 | list_networks_by_cc.add_argument("--family", choices=("ipv6", "ipv4")) |
4439e317 MT |
183 | list_networks_by_cc.add_argument("--output-format", |
184 | choices=self.output_formats.keys(), default="list") | |
b5cdfad7 | 185 | list_networks_by_cc.set_defaults(func=self.handle_list_networks_by_cc) |
ccc7ab4e | 186 | |
bbdb2e0a MT |
187 | # List all networks with flags |
188 | list_networks_by_flags = subparsers.add_parser("list-networks-by-flags", | |
189 | help=_("Lists all networks with flags"), | |
190 | ) | |
191 | list_networks_by_flags.add_argument("--anonymous-proxy", | |
192 | action="store_true", help=_("Anonymous Proxies"), | |
193 | ) | |
194 | list_networks_by_flags.add_argument("--satellite-provider", | |
195 | action="store_true", help=_("Satellite Providers"), | |
196 | ) | |
197 | list_networks_by_flags.add_argument("--anycast", | |
198 | action="store_true", help=_("Anycasts"), | |
199 | ) | |
44e5ef71 | 200 | list_networks_by_flags.add_argument("--family", choices=("ipv6", "ipv4")) |
bbdb2e0a MT |
201 | list_networks_by_flags.add_argument("--output-format", |
202 | choices=self.output_formats.keys(), default="list") | |
203 | list_networks_by_flags.set_defaults(func=self.handle_list_networks_by_flags) | |
204 | ||
78f37815 MT |
205 | args = parser.parse_args() |
206 | ||
f9de5e61 MT |
207 | # Enable debug logging |
208 | if args.debug: | |
209 | location.logger.set_level(logging.DEBUG) | |
210 | ||
78f37815 MT |
211 | # Print usage if no action was given |
212 | if not "func" in args: | |
213 | parser.print_usage() | |
214 | sys.exit(2) | |
215 | ||
216 | return args | |
5118a4b8 MT |
217 | |
218 | def run(self): | |
219 | # Parse command line arguments | |
220 | args = self.parse_cli() | |
221 | ||
2538ed9a MT |
222 | # Open database |
223 | try: | |
224 | db = location.Database(args.database) | |
225 | except FileNotFoundError as e: | |
226 | sys.stderr.write("location-query: Could not open database %s: %s\n" \ | |
227 | % (args.database, e)) | |
228 | sys.exit(1) | |
229 | ||
6961aaf3 MT |
230 | # Translate family (if present) |
231 | if "family" in args: | |
232 | if args.family == "ipv6": | |
233 | args.family = socket.AF_INET6 | |
234 | elif args.family == "ipv4": | |
235 | args.family = socket.AF_INET | |
236 | else: | |
237 | args.family = 0 | |
44e5ef71 | 238 | |
5118a4b8 | 239 | # Call function |
2538ed9a | 240 | ret = args.func(db, args) |
5118a4b8 MT |
241 | |
242 | # Return with exit code | |
243 | if ret: | |
244 | sys.exit(ret) | |
245 | ||
246 | # Otherwise just exit | |
247 | sys.exit(0) | |
248 | ||
2538ed9a | 249 | def handle_lookup(self, db, ns): |
5118a4b8 MT |
250 | ret = 0 |
251 | ||
fbf925c8 MT |
252 | format = " %-24s: %s" |
253 | ||
5118a4b8 MT |
254 | for address in ns.address: |
255 | try: | |
fbf925c8 | 256 | network = db.lookup(address) |
5118a4b8 | 257 | except ValueError: |
9f2f5d13 | 258 | print(_("Invalid IP address: %s") % address, file=sys.stderr) |
5118a4b8 MT |
259 | |
260 | args = { | |
261 | "address" : address, | |
fbf925c8 | 262 | "network" : network, |
5118a4b8 MT |
263 | } |
264 | ||
265 | # Nothing found? | |
fbf925c8 | 266 | if not network: |
9f2f5d13 | 267 | print(_("Nothing found for %(address)s") % args, file=sys.stderr) |
5118a4b8 MT |
268 | ret = 1 |
269 | continue | |
270 | ||
fbf925c8 MT |
271 | print("%s:" % address) |
272 | print(format % (_("Network"), network)) | |
5118a4b8 | 273 | |
fbf925c8 MT |
274 | # Print country |
275 | if network.country_code: | |
276 | print(format % (_("Country"), network.country_code)) | |
5118a4b8 | 277 | |
fbf925c8 MT |
278 | # Print AS information |
279 | if network.asn: | |
280 | autonomous_system = db.get_as(network.asn) | |
5118a4b8 | 281 | |
fbf925c8 MT |
282 | print(format % ( |
283 | _("Autonomous System"), | |
284 | autonomous_system or "AS%s" % network.asn), | |
285 | ) | |
5118a4b8 MT |
286 | |
287 | return ret | |
288 | ||
a68a46f5 MT |
289 | def handle_dump(self, db, ns): |
290 | # Use output file or write to stdout | |
291 | f = ns.output or sys.stdout | |
292 | ||
293 | # Write metadata | |
294 | f.write("#\n# Location Database Export\n#\n") | |
295 | ||
296 | f.write("# Generated: %s\n" % time.strftime( | |
297 | "%a, %d %b %Y %H:%M:%S GMT", time.gmtime(db.created_at), | |
298 | )) | |
299 | ||
300 | if db.vendor: | |
301 | f.write("# Vendor: %s\n" % db.vendor) | |
302 | ||
303 | if db.license: | |
304 | f.write("# License: %s\n" % db.license) | |
305 | ||
306 | f.write("#\n") | |
307 | ||
308 | if db.description: | |
309 | for line in db.description.splitlines(): | |
310 | f.write("# %s\n" % line) | |
311 | ||
312 | f.write("#\n") | |
313 | ||
314 | # Iterate over all ASes | |
315 | for a in db.ases: | |
316 | f.write("\n") | |
317 | f.write("aut-num: AS%s\n" % a.number) | |
318 | f.write("name: %s\n" % a.name) | |
319 | ||
320 | # Iterate over all networks | |
321 | for n in db.networks: | |
322 | f.write("\n") | |
323 | f.write("net: %s\n" % n) | |
324 | ||
325 | if n.country_code: | |
326 | f.write("country: %s\n" % n.country_code) | |
327 | ||
328 | if n.asn: | |
329 | f.write("autnum: %s\n" % n.asn) | |
330 | ||
2538ed9a | 331 | def handle_get_as(self, db, ns): |
fadc1af0 MT |
332 | """ |
333 | Gets information about Autonomous Systems | |
334 | """ | |
335 | ret = 0 | |
336 | ||
337 | for asn in ns.asn: | |
338 | try: | |
339 | asn = int(asn) | |
340 | except ValueError: | |
9f2f5d13 | 341 | print(_("Invalid ASN: %s") % asn, file=sys.stderr) |
fadc1af0 MT |
342 | ret = 1 |
343 | continue | |
344 | ||
345 | # Fetch AS from database | |
2538ed9a | 346 | a = db.get_as(asn) |
fadc1af0 MT |
347 | |
348 | # Nothing found | |
349 | if not a: | |
9f2f5d13 | 350 | print(_("Could not find AS%s") % asn, file=sys.stderr) |
fadc1af0 MT |
351 | ret = 1 |
352 | continue | |
353 | ||
354 | print(_("AS%(asn)s belongs to %(name)s") % { "asn" : a.number, "name" : a.name }) | |
355 | ||
356 | return ret | |
5118a4b8 | 357 | |
2538ed9a | 358 | def handle_search_as(self, db, ns): |
da3e360e MT |
359 | for query in ns.query: |
360 | # Print all matches ASes | |
2538ed9a | 361 | for a in db.search_as(query): |
da3e360e MT |
362 | print(a) |
363 | ||
4439e317 MT |
364 | def __get_output_formatter(self, ns): |
365 | try: | |
366 | cls = self.output_formats[ns.output_format] | |
367 | except KeyError: | |
368 | cls = OutputFormatter | |
369 | ||
71e0ad0b | 370 | return cls(ns) |
4439e317 | 371 | |
43154ed7 | 372 | def handle_list_networks_by_as(self, db, ns): |
4439e317 MT |
373 | with self.__get_output_formatter(ns) as f: |
374 | for asn in ns.asn: | |
375 | # Print all matching networks | |
44e5ef71 | 376 | for n in db.search_networks(asn=asn, family=ns.family): |
4439e317 | 377 | f.network(n) |
43154ed7 | 378 | |
ccc7ab4e | 379 | def handle_list_networks_by_cc(self, db, ns): |
4439e317 MT |
380 | with self.__get_output_formatter(ns) as f: |
381 | for country_code in ns.country_code: | |
382 | # Print all matching networks | |
44e5ef71 | 383 | for n in db.search_networks(country_code=country_code, family=ns.family): |
4439e317 MT |
384 | f.network(n) |
385 | ||
bbdb2e0a MT |
386 | def handle_list_networks_by_flags(self, db, ns): |
387 | flags = 0 | |
388 | ||
389 | if ns.anonymous_proxy: | |
390 | flags |= location.NETWORK_FLAG_ANONYMOUS_PROXY | |
391 | ||
392 | if ns.satellite_provider: | |
393 | flags |= location.NETWORK_FLAG_SATELLITE_PROVIDER | |
394 | ||
395 | if ns.anycast: | |
396 | flags |= location.NETWORK_FLAG_ANYCAST | |
397 | ||
398 | with self.__get_output_formatter(ns) as f: | |
44e5ef71 | 399 | for n in db.search_networks(flags=flags, family=ns.family): |
bbdb2e0a MT |
400 | f.network(n) |
401 | ||
ccc7ab4e | 402 | |
5118a4b8 MT |
403 | def main(): |
404 | # Run the command line interface | |
405 | c = CLI() | |
406 | c.run() | |
407 | ||
408 | main() |