Commit 71e0ad0b authored by Michael Tremer's avatar Michael Tremer

location-query: Allow exporting data for nftables

Fixes: #12201
Signed-off-by: default avatarMichael Tremer <michael.tremer@ipfire.org>
parent 4439e317
...@@ -50,6 +50,7 @@ or countries. ...@@ -50,6 +50,7 @@ or countries.
directly loaded into other software. The following options are available: directly loaded into other software. The following options are available:
+ +
* 'list' (default): Just lists all networks, one per line * 'list' (default): Just lists all networks, one per line
* 'nftables': For nftables
* 'xt_geoip': Returns a list of networks to be loaded into the xt_geoip * 'xt_geoip': Returns a list of networks to be loaded into the xt_geoip
kernel module kernel module
......
...@@ -38,6 +38,9 @@ def _(singular, plural=None, n=None): ...@@ -38,6 +38,9 @@ def _(singular, plural=None, n=None):
# Output formatters # Output formatters
class OutputFormatter(object): class OutputFormatter(object):
def __init__(self, ns):
self.ns = ns
def __enter__(self): def __enter__(self):
# Open the output # Open the output
self.open() self.open()
...@@ -48,6 +51,14 @@ class OutputFormatter(object): ...@@ -48,6 +51,14 @@ class OutputFormatter(object):
if tb is None: if tb is None:
self.close() self.close()
@property
def name(self):
if "country_code" in self.ns:
return "networks_country_%s" % self.ns.country_code[0]
elif "asn" in self.ns:
return "networks_AS%s" % self.ns.asn[0]
def open(self): def open(self):
pass pass
...@@ -58,6 +69,20 @@ class OutputFormatter(object): ...@@ -58,6 +69,20 @@ class OutputFormatter(object):
print(network) print(network)
class NftablesOutputFormatter(OutputFormatter):
"""
For nftables
"""
def open(self):
print("define %s = {" % self.name)
def close(self):
print("}")
def network(self, network):
print(" %s," % network)
class XTGeoIPOutputFormatter(OutputFormatter): class XTGeoIPOutputFormatter(OutputFormatter):
""" """
Formats the output in that way, that it can be loaded by Formats the output in that way, that it can be loaded by
...@@ -78,6 +103,7 @@ class XTGeoIPOutputFormatter(OutputFormatter): ...@@ -78,6 +103,7 @@ class XTGeoIPOutputFormatter(OutputFormatter):
class CLI(object): class CLI(object):
output_formats = { output_formats = {
"list" : OutputFormatter, "list" : OutputFormatter,
"nftables" : NftablesOutputFormatter,
"xt_geoip" : XTGeoIPOutputFormatter, "xt_geoip" : XTGeoIPOutputFormatter,
} }
...@@ -246,7 +272,7 @@ class CLI(object): ...@@ -246,7 +272,7 @@ class CLI(object):
except KeyError: except KeyError:
cls = OutputFormatter cls = OutputFormatter
return cls() return cls(ns)
def handle_list_networks_by_as(self, db, ns): def handle_list_networks_by_as(self, db, ns):
with self.__get_output_formatter(ns) as f: with self.__get_output_formatter(ns) as f:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment