]> git.ipfire.org Git - ipfire-2.x.git/commitdiff
geoip-functions.pl: Add functions to export locations and to flush them.
authorStefan Schantl <stefan.schantl@ipfire.org>
Mon, 9 Dec 2019 10:05:31 +0000 (11:05 +0100)
committerStefan Schantl <stefan.schantl@ipfire.org>
Mon, 9 Dec 2019 10:05:31 +0000 (11:05 +0100)
The export_locations() function requires an array of country codes which
should be exported by the location-exporter script.

The flush_exported_locations() function is used to flush (delete) all
exported location files.

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

index 7fbd5eedee3ef9944ff5d6d443b5a656d5955b1b..e5322972773f04676c4f6417c01058afa5c82062 100644 (file)
@@ -186,5 +186,53 @@ sub get_geoip_locations() {
        return @sorted_locations;
 }
 
+# Function to flush all exported GeoIP locations.
+sub flush_exported_locations () {
+       # Check if the xt_geoip_db_directory exists.
+       if (-e $xt_geoip_db_directory) {
+               # Perform a direcory listing.
+               opendir (DIR, $xt_geoip_db_directory) or die "Could not open $xt_geoip_db_directory. $!\n";
+
+               # Loop through the files.
+               while (my $file = readdir(DIR)) {
+                       # Check if the element is a file.
+                       if (-f "$xt_geoip_db_directory/$file") {
+                               # Delete it.
+                               unlink("$xt_geoip_db_directory/$file");
+                       }
+               }
+       }
+}
+
+# Function which calls location-exporter to export a given array
+# of locations.
+sub export_locations (\@) {
+       my @locations = @{ shift() };
+
+       # String to store the given locations and pass it to the exporter tool.
+       my $locations_string;
+
+       # Only export IPv4 addresses.
+       my $family = "--family=ipv4";
+
+       # Specify xt_geoip as output format.
+       my $format = "--format=xt_geoip";
+
+       # Location export command.
+       my @command = ("/usr/bin/location-exporter", "--directory=$xt_geoip_db_directory", "$format", "$family");
+
+       # Check if the export directory exists, otherwise create it.
+       unless (-d $xt_geoip_db_directory) { mkdir $xt_geoip_db_directory };
+
+       # Loop through the array of locations which needs to be exported.
+       foreach my $location (@locations) {
+               # Add location to the command array.
+               push(@command, $location);
+       }
+
+       # Execute location-exporter to export the requested country codes.
+       system(@command) == 0
+               or die "@command failed: $?";
+}
 
 1;