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 for address
in (network
._first
_address
, network
._last
_address
):
124 self
.f
.write(address
)
128 "ipset" : IpsetOutputWriter
,
129 "list" : OutputWriter
,
130 "nftables" : NftablesOutputWriter
,
131 "xt_geoip" : XTGeoIPOutputWriter
,
134 class Exporter(object):
135 def __init__(self
, db
, writer
):
136 self
.db
, self
.writer
= db
, writer
138 def export(self
, directory
, families
, countries
, asns
):
139 for family
in families
:
140 log
.debug("Exporting family %s" % family
)
144 # Create writers for countries
145 for country_code
in countries
:
146 filename
= self
._make
_filename
(
147 directory
, prefix
=country_code
, suffix
=self
.writer
.suffix
, family
=family
,
150 writers
[country_code
] = self
.writer
.open(filename
, prefix
="CC_%s" % country_code
)
152 # Create writers for ASNs
154 filename
= self
._make
_filename
(
155 directory
, "AS%s" % asn
, suffix
=self
.writer
.suffix
, family
=family
,
158 writers
[asn
] = self
.writer
.open(filename
, prefix
="AS%s" % asn
)
160 # Filter countries from special country codes
162 country_code
for country_code
in countries
if not country_code
in FLAGS
.values()
165 # Get all networks that match the family
166 networks
= self
.db
.search_networks(family
=family
,
167 country_codes
=country_codes
, asns
=asns
, flatten
=True)
169 # Walk through all networks
170 for network
in networks
:
171 # Write matching countries
173 writers
[network
.country_code
].write(network
)
177 # Write matching ASNs
179 writers
[network
.asn
].write(network
)
185 if network
.has_flag(flag
):
186 # Fetch the "fake" country code
187 country
= FLAGS
[flag
]
190 writers
[country
].write(network
)
194 # Write everything to the filesystem
195 for writer
in writers
.values():
198 def _make_filename(self
, directory
, prefix
, suffix
, family
):
199 filename
= "%s.%s%s" % (
200 prefix
, suffix
, "6" if family
== socket
.AF_INET6
else "4"
203 return os
.path
.join(directory
, filename
)