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 ###############################################################################
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",
36 _location
.NETWORK_FLAG_DROP
: "XD",
39 class OutputWriter(object):
43 def __init__(self
, f
, prefix
=None):
44 self
.f
, self
.prefix
= f
, prefix
46 # Immediately write the header
50 def open(cls
, filename
, **kwargs
):
52 Convenience function to open a file
54 f
= open(filename
, cls
.mode
)
56 return cls(f
, **kwargs
)
59 return "<%s f=%s>" % (self
.__class
__.__name
__, self
.f
)
61 def _write_header(self
):
63 The header of the file
67 def _write_footer(self
):
69 The footer of the file
73 def write(self
, network
):
74 self
.f
.write("%s\n" % network
)
78 Called when all data has been written
86 class IpsetOutputWriter(OutputWriter
):
92 def _write_header(self
):
93 self
.f
.write("create %s hash:net family inet hashsize 1024 maxelem 65536\n" % self
.prefix
)
95 def write(self
, network
):
96 self
.f
.write("add %s %s\n" % (self
.prefix
, network
))
99 class NftablesOutputWriter(OutputWriter
):
105 def _write_header(self
):
106 self
.f
.write("define %s = {\n" % self
.prefix
)
108 def _write_footer(self
):
111 def write(self
, network
):
112 self
.f
.write(" %s,\n" % network
)
115 class XTGeoIPOutputWriter(OutputWriter
):
117 Formats the output in that way, that it can be loaded by
118 the xt_geoip kernel module from xtables-addons.
123 def write(self
, network
):
124 self
.f
.write(network
._first
_address
)
125 self
.f
.write(network
._last
_address
)
129 "ipset" : IpsetOutputWriter
,
130 "list" : OutputWriter
,
131 "nftables" : NftablesOutputWriter
,
132 "xt_geoip" : XTGeoIPOutputWriter
,
135 class Exporter(object):
136 def __init__(self
, db
, writer
):
137 self
.db
, self
.writer
= db
, writer
139 def export(self
, directory
, families
, countries
, asns
):
140 for family
in families
:
141 log
.debug("Exporting family %s" % family
)
145 # Create writers for countries
146 for country_code
in countries
:
147 filename
= self
._make
_filename
(
148 directory
, prefix
=country_code
, suffix
=self
.writer
.suffix
, family
=family
,
151 writers
[country_code
] = self
.writer
.open(filename
, prefix
="CC_%s" % country_code
)
153 # Create writers for ASNs
155 filename
= self
._make
_filename
(
156 directory
, "AS%s" % asn
, suffix
=self
.writer
.suffix
, family
=family
,
159 writers
[asn
] = self
.writer
.open(filename
, prefix
="AS%s" % asn
)
161 # Filter countries from special country codes
163 country_code
for country_code
in countries
if not country_code
in FLAGS
.values()
166 # Get all networks that match the family
167 networks
= self
.db
.search_networks(family
=family
,
168 country_codes
=country_codes
, asns
=asns
, flatten
=True)
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
)