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 ###############################################################################
29 log
= logging
.getLogger("location.export")
33 _location
.NETWORK_FLAG_ANONYMOUS_PROXY
: "A1",
34 _location
.NETWORK_FLAG_SATELLITE_PROVIDER
: "A2",
35 _location
.NETWORK_FLAG_ANYCAST
: "A3",
38 class OutputWriter(object):
42 def __init__(self
, f
, prefix
=None):
43 self
.f
, self
.prefix
= f
, prefix
45 # Immediately write the header
49 def open(cls
, filename
, **kwargs
):
51 Convenience function to open a file
53 f
= open(filename
, cls
.mode
)
55 return cls(f
, **kwargs
)
58 return "<%s f=%s>" % (self
.__class
__.__name
__, self
.f
)
60 def _write_header(self
):
62 The header of the file
66 def _write_footer(self
):
68 The footer of the file
72 def write(self
, network
):
73 self
.f
.write("%s\n" % network
)
77 Called when all data has been written
85 class IpsetOutputWriter(OutputWriter
):
91 def _write_header(self
):
92 self
.f
.write("create %s hash:net family inet hashsize 1024 maxelem 65536\n" % self
.prefix
)
94 def write(self
, network
):
95 self
.f
.write("add %s %s\n" % (self
.prefix
, network
))
98 class NftablesOutputWriter(OutputWriter
):
104 def _write_header(self
):
105 self
.f
.write("define %s = {\n" % self
.prefix
)
107 def _write_footer(self
):
110 def write(self
, network
):
111 self
.f
.write(" %s,\n" % network
)
114 class XTGeoIPOutputWriter(OutputWriter
):
116 Formats the output in that way, that it can be loaded by
117 the xt_geoip kernel module from xtables-addons.
122 def write(self
, network
):
123 n
= ipaddress
.ip_network("%s" % network
)
125 for address
in (n
.network_address
, n
.broadcast_address
):
126 bytes
= socket
.inet_pton(
127 socket
.AF_INET6
if address
.version
== 6 else socket
.AF_INET
,
135 "ipset" : IpsetOutputWriter
,
136 "list" : OutputWriter
,
137 "nftables" : NftablesOutputWriter
,
138 "xt_geoip" : XTGeoIPOutputWriter
,
141 class Exporter(object):
142 def __init__(self
, db
, writer
):
143 self
.db
, self
.writer
= db
, writer
145 def export(self
, directory
, families
, countries
, asns
):
146 for family
in families
:
147 log
.debug("Exporting family %s" % family
)
151 # Create writers for countries
152 for country_code
in countries
:
153 filename
= self
._make
_filename
(
154 directory
, prefix
=country_code
, suffix
=self
.writer
.suffix
, family
=family
,
157 writers
[country_code
] = self
.writer
.open(filename
, prefix
="CC_%s" % country_code
)
159 # Create writers for ASNs
161 filename
= self
._make
_filename
(
162 directory
, "AS%s" % asn
, suffix
=self
.writer
.suffix
, family
=family
,
165 writers
[asn
] = self
.writer
.open(filename
, prefix
="AS%s" % asn
)
167 # Get all networks that match the family
168 networks
= self
.db
.search_networks(family
=family
)
170 # Walk through all networks
171 for network
in networks
:
172 # Write matching countries
174 writers
[network
.country_code
].write(network
)
178 # Write matching ASNs
180 writers
[network
.asn
].write(network
)
186 if network
.has_flag(flag
):
187 # Fetch the "fake" country code
188 country
= flags
[flag
]
191 writers
[country
].write(network
)
195 # Write everything to the filesystem
196 for writer
in writers
.values():
199 def _make_filename(self
, directory
, prefix
, suffix
, family
):
200 filename
= "%s.%s%s" % (
201 prefix
, suffix
, "6" if family
== socket
.AF_INET6
else "4"
204 return os
.path
.join(directory
, filename
)