2 ###############################################################################
4 # libloc - A library to determine the location of someone on the Internet #
6 # Copyright (C) 2020-2021 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 ###############################################################################
30 log
= logging
.getLogger("location.export")
34 _location
.NETWORK_FLAG_ANONYMOUS_PROXY
: "A1",
35 _location
.NETWORK_FLAG_SATELLITE_PROVIDER
: "A2",
36 _location
.NETWORK_FLAG_ANYCAST
: "A3",
37 _location
.NETWORK_FLAG_DROP
: "XD",
40 class OutputWriter(object):
44 # Enable network flattening (i.e. networks cannot overlap)
47 def __init__(self
, f
, family
=None, prefix
=None):
52 # Call any custom initialization
55 # Immediately write the header
60 To be overwritten by anything that inherits from this
65 def open(cls
, filename
, *args
, **kwargs
):
67 Convenience function to open a file
69 f
= open(filename
, cls
.mode
)
71 return cls(f
, *args
, **kwargs
)
74 return "<%s f=%s>" % (self
.__class
__.__name
__, self
.f
)
76 def _write_header(self
):
78 The header of the file
82 def _write_footer(self
):
84 The footer of the file
88 def write(self
, network
):
89 self
.f
.write("%s\n" % network
)
93 Called when all data has been written
101 class IpsetOutputWriter(OutputWriter
):
107 # The value is being used if we don't know any better
108 DEFAULT_HASHSIZE
= 64
110 # We aim for this many networks in a bucket on average. This allows us to choose
111 # how much memory we want to sacrifice to gain better performance. The lower the
112 # factor, the faster a lookup will be, but it will use more memory.
113 # We will aim for only using three quarters of all buckets to avoid any searches
114 # through the linked lists.
115 HASHSIZE_FACTOR
= 0.75
124 Calculates an optimized hashsize
126 # Return the default value if we don't know the size of the set
127 if not self
.networks
:
128 return self
.DEFAULT_HASHSIZE
130 # Find the nearest power of two that is larger than the number of networks
131 # divided by the hashsize factor.
132 exponent
= math
.log(self
.networks
/ self
.HASHSIZE_FACTOR
, 2)
134 # Return the size of the hash
135 return 2 ** math
.ceil(exponent
)
137 def _write_header(self
):
138 # This must have a fixed size, because we will write the header again in the end
139 self
.f
.write("create %s hash:net family inet%s" % (
141 "6" if self
.family
== socket
.AF_INET6
else ""
143 self
.f
.write(" hashsize %8d maxelem 1048576 -exist\n" % self
.hashsize
)
144 self
.f
.write("flush %s\n" % self
.prefix
)
146 def write(self
, network
):
147 self
.f
.write("add %s %s\n" % (self
.prefix
, network
))
149 # Increment network counter
152 def _write_footer(self
):
153 # Jump back to the beginning of the file
156 # Rewrite the header with better configuration
160 class NftablesOutputWriter(OutputWriter
):
166 def _write_header(self
):
167 self
.f
.write("define %s = {\n" % self
.prefix
)
169 def _write_footer(self
):
172 def write(self
, network
):
173 self
.f
.write(" %s,\n" % network
)
176 class XTGeoIPOutputWriter(OutputWriter
):
178 Formats the output in that way, that it can be loaded by
179 the xt_geoip kernel module from xtables-addons.
185 def write(self
, network
):
186 self
.f
.write(network
._first
_address
)
187 self
.f
.write(network
._last
_address
)
191 "ipset" : IpsetOutputWriter
,
192 "list" : OutputWriter
,
193 "nftables" : NftablesOutputWriter
,
194 "xt_geoip" : XTGeoIPOutputWriter
,
197 class Exporter(object):
198 def __init__(self
, db
, writer
):
199 self
.db
, self
.writer
= db
, writer
201 def export(self
, directory
, families
, countries
, asns
):
202 for family
in families
:
203 log
.debug("Exporting family %s" % family
)
207 # Create writers for countries
208 for country_code
in countries
:
209 filename
= self
._make
_filename
(
210 directory
, prefix
=country_code
, suffix
=self
.writer
.suffix
, family
=family
,
213 writers
[country_code
] = self
.writer
.open(filename
, family
, prefix
="%s" % country_code
)
215 # Create writers for ASNs
217 filename
= self
._make
_filename
(
218 directory
, "AS%s" % asn
, suffix
=self
.writer
.suffix
, family
=family
,
221 writers
[asn
] = self
.writer
.open(filename
, family
, prefix
="AS%s" % asn
)
223 # Filter countries from special country codes
225 country_code
for country_code
in countries
if not country_code
in FLAGS
.values()
228 # Get all networks that match the family
229 networks
= self
.db
.search_networks(family
=family
,
230 country_codes
=country_codes
, asns
=asns
, flatten
=self
.writer
.flatten
)
232 # Walk through all networks
233 for network
in networks
:
234 # Write matching countries
236 writers
[network
.country_code
].write(network
)
240 # Write matching ASNs
242 writers
[network
.asn
].write(network
)
248 if network
.has_flag(flag
):
249 # Fetch the "fake" country code
250 country
= FLAGS
[flag
]
253 writers
[country
].write(network
)
257 # Write everything to the filesystem
258 for writer
in writers
.values():
261 def _make_filename(self
, directory
, prefix
, suffix
, family
):
262 filename
= "%s.%s%s" % (
263 prefix
, suffix
, "6" if family
== socket
.AF_INET6
else "4"
266 return os
.path
.join(directory
, filename
)