From f0aee19c061059435ca62d8cd14c3235acc70b6e Mon Sep 17 00:00:00 2001 From: Stefan Schantl Date: Fri, 14 Apr 2023 17:56:34 +0200 Subject: [PATCH] ipblocklist-functions.pl: Introduce load_blocklists() function This function is responsible for loading the enabled blocklists. Signed-off-by: Stefan Schantl --- config/cfgroot/ipblocklist-functions.pl | 75 +++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/config/cfgroot/ipblocklist-functions.pl b/config/cfgroot/ipblocklist-functions.pl index 1206a1b3d..ad0f482d1 100644 --- a/config/cfgroot/ipblocklist-functions.pl +++ b/config/cfgroot/ipblocklist-functions.pl @@ -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 = ; + + # 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 ) ## -- 2.39.5