]> git.ipfire.org Git - people/stevee/ipfire-2.x.git/commitdiff
ipblocklist-functions.pl: Introduce load_blocklists() function
authorStefan Schantl <stefan.schantl@ipfire.org>
Fri, 14 Apr 2023 15:56:34 +0000 (17:56 +0200)
committerStefan Schantl <stefan.schantl@ipfire.org>
Fri, 14 Apr 2023 15:56:34 +0000 (17:56 +0200)
This function is responsible for loading the enabled blocklists.

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
config/cfgroot/ipblocklist-functions.pl

index 1206a1b3d1a0688b7b5faf9a5b51101ac2e75926..ad0f482d1d0aca86f9eebfd2d4f7e1016fab3dfe 100644 (file)
@@ -201,6 +201,81 @@ sub download_blocklist ($) {
        return;
 }
 
+#
+## The load_blocklists function.
+#
+## This function is responsible for loading all enabled blocklists.
+##
+## It will gain all enabled blocklists and perform a de-duplication of
+## the blocked addresses/networks on the single blocklists while reading the
+## cached files before calling the load_blocklist() function.
+#
+sub load_blocklists () {
+       # Hash to store the single elements of all blocklists,
+       # to de-duplicate them.
+       my %addresses = ();
+
+       # Story any errors.
+       my @errors;
+
+       # Get all enabled blocklists.
+       my @enabled_blocklists = &get_enabled_blocklists();
+
+       # Loop through the array of enabled blocklists.
+       foreach my $list (@enabled_blocklists) {
+               # Temporary array to store the uniqe blocklist
+               # elements.
+               my @data = ();
+
+               # Get the file name of the cached blocklist.
+               my $file = &get_cached_blocklist_file($list);
+
+               # Open the file for reading.
+               open(FILE, $file) if (-e $file);
+
+               # Read-in the file content.
+               my @file_content = <FILE>;
+
+               # Close file handle.
+               close(FILE);
+
+               # Skip list if the file does not contain any content.
+               next unless(@file_content);
+
+               # Loop through the file content.
+               foreach my $line (@file_content) {
+                       # Remove newline.
+                       chomp($line);
+
+                       # Skip element if the address / network allready has
+                       # been seen. In this case an entry in the addresses
+                       # hash exists.
+                       next if($addresses{$line});
+
+                       # Add the element to the addresses hash.
+                       $addresses{$line} = $list;
+
+                       # Add the line to the temporary blocklist array.
+                       push(@data, $line);
+               }
+
+               # Skip the blocklist if there are  no elements to add.
+               next unless(@data);
+
+               # Call function to load the blocklist.
+               my $error = &load_blocklist($list, @data);
+
+               # Check if there is an error to apply.
+               if ($error) {
+                       # Append the error to the array of errors.
+                       push(@errors, "Could not load $list - $error");
+               }
+       }
+
+       # Return any errors
+       return @errors;
+}
+
 #
 ## sub parse_ip_or_net_list( line )
 ##