[1/2] rules.pl: Autodetect ipset db file to restore.

Message ID 20220302194306.4110-1-stefan.schantl@ipfire.org
State Accepted
Commit 58418009693fc43c895c210d11e3ad1b18c3222c
Headers
Series [1/2] rules.pl: Autodetect ipset db file to restore. |

Commit Message

Stefan Schantl March 2, 2022, 7:43 p.m. UTC
  This commit allows the ipset_restore() function to auto-detect
which set file needs to be restored.

Currently it is limitated to country codes only, because we currently
does not support anything else.

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
---
 config/firewall/rules.pl | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
  

Patch

diff --git a/config/firewall/rules.pl b/config/firewall/rules.pl
index b12764d18..b8c602538 100644
--- a/config/firewall/rules.pl
+++ b/config/firewall/rules.pl
@@ -945,8 +945,9 @@  sub ipset_get_sets () {
 sub ipset_restore ($) {
 	my ($set) = @_;
 
-	my $file_prefix = "ipset4";
-	my $db_file = "$Location::Functions::ipset_db_directory/$set.$file_prefix";
+	# Empty variable to store the db file, which should be
+	# restored by ipset.
+	my $db_file;
 
 	# Check if the set already has been loaded.
 	if($ipset_loaded_sets{$set}) {
@@ -954,6 +955,15 @@  sub ipset_restore ($) {
 		return;
 	}
 
+	# Check if the given set name is a country code.
+	if($set ~~ @locations) {
+		# Libloc adds "ipset4" as prefix to all exported IPv4 data.
+		my $file_prefix = "ipset4";
+
+		# Generate full path and filename for the ipset db file to restore.
+		$db_file = "$Location::Functions::ipset_db_directory/$set.$file_prefix";
+	}
+
 	# Check if the generated file exists.
 	if (-f $db_file) {
 		# Run ipset and restore the given set.