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, flatten
=True):
43 self
.f
, self
.prefix
, self
.flatten
= f
, prefix
, flatten
45 # The previously written network
46 self
._last
_network
= None
48 # Immediately write the header
52 def open(cls
, filename
, **kwargs
):
54 Convenience function to open a file
56 f
= open(filename
, cls
.mode
)
58 return cls(f
, **kwargs
)
61 return "<%s f=%s>" % (self
.__class
__.__name
__, self
.f
)
63 def _flatten(self
, network
):
65 Checks if the given network needs to be written to file,
66 or if it is a subnet of the previously written network.
68 if self
._last
_network
and network
.is_subnet_of(self
._last
_network
):
71 # Remember this network for the next call
72 self
._last
_network
= network
75 def _write_header(self
):
77 The header of the file
81 def _write_footer(self
):
83 The footer of the file
87 def _write_network(self
, network
):
88 self
.f
.write("%s\n" % network
)
90 def write(self
, network
):
91 if self
.flatten
and self
._flatten
(network
):
92 log
.debug("Skipping writing network %s (last one was %s)" % (network
, self
._last
_network
))
95 return self
._write
_network
(network
)
99 Called when all data has been written
107 class IpsetOutputWriter(OutputWriter
):
113 def _write_header(self
):
114 self
.f
.write("create %s hash:net family inet hashsize 1024 maxelem 65536\n" % self
.prefix
)
116 def _write_network(self
, network
):
117 self
.f
.write("add %s %s\n" % (self
.prefix
, network
))
120 class NftablesOutputWriter(OutputWriter
):
126 def _write_header(self
):
127 self
.f
.write("define %s = {\n" % self
.prefix
)
129 def _write_footer(self
):
132 def _write_network(self
, network
):
133 self
.f
.write(" %s,\n" % network
)
136 class XTGeoIPOutputWriter(OutputWriter
):
138 Formats the output in that way, that it can be loaded by
139 the xt_geoip kernel module from xtables-addons.
144 def _write_network(self
, network
):
145 for address
in (network
._first
_address
, network
._last
_address
):
146 self
.f
.write(address
)
150 "ipset" : IpsetOutputWriter
,
151 "list" : OutputWriter
,
152 "nftables" : NftablesOutputWriter
,
153 "xt_geoip" : XTGeoIPOutputWriter
,
156 class Exporter(object):
157 def __init__(self
, db
, writer
):
158 self
.db
, self
.writer
= db
, writer
160 def export(self
, directory
, families
, countries
, asns
):
161 for family
in families
:
162 log
.debug("Exporting family %s" % family
)
166 # Create writers for countries
167 for country_code
in countries
:
168 filename
= self
._make
_filename
(
169 directory
, prefix
=country_code
, suffix
=self
.writer
.suffix
, family
=family
,
172 writers
[country_code
] = self
.writer
.open(filename
, prefix
="CC_%s" % country_code
)
174 # Create writers for ASNs
176 filename
= self
._make
_filename
(
177 directory
, "AS%s" % asn
, suffix
=self
.writer
.suffix
, family
=family
,
180 writers
[asn
] = self
.writer
.open(filename
, prefix
="AS%s" % asn
)
182 # Filter countries from special country codes
184 country_code
for country_code
in countries
if not country_code
in FLAGS
.values()
187 # Get all networks that match the family
188 networks
= self
.db
.search_networks(family
=family
,
189 country_codes
=country_codes
, asns
=asns
, flatten
=True)
191 # Walk through all networks
192 for network
in networks
:
193 # Write matching countries
195 writers
[network
.country_code
].write(network
)
199 # Write matching ASNs
201 writers
[network
.asn
].write(network
)
207 if network
.has_flag(flag
):
208 # Fetch the "fake" country code
209 country
= FLAGS
[flag
]
212 writers
[country
].write(network
)
216 # Write everything to the filesystem
217 for writer
in writers
.values():
220 def _make_filename(self
, directory
, prefix
, suffix
, family
):
221 filename
= "%s.%s%s" % (
222 prefix
, suffix
, "6" if family
== socket
.AF_INET6
else "4"
225 return os
.path
.join(directory
, filename
)