geoip: simplify handling table column names

Signed-off-by: Philip Prindeville <philipp@redfish-solutions.com>
This commit is contained in:
Philip Prindeville
2018-04-30 02:06:05 +02:00
committed by Jan Engelhardt
parent 9057fb48f3
commit 56fba3ecff

View File

@@ -58,6 +58,8 @@ sub loadCountries
{ {
my $file = "$dir/GeoLite2-Country-Locations-en.csv"; my $file = "$dir/GeoLite2-Country-Locations-en.csv";
sub id; sub cc; sub long; sub ct; sub cn;
%countryId = (); %countryId = ();
%countryName = (); %countryName = ();
@@ -79,19 +81,24 @@ sub loadCountries
# verify that the columns we need are present # verify that the columns we need are present
map { die "Table has no $pairs{$_} column\n" unless (exists $header{$_}); } keys %pairs; map { die "Table has no $pairs{$_} column\n" unless (exists $header{$_}); } keys %pairs;
my $id = $header{geoname_id}; my %remapping = (
my $cc = $header{country_iso_code}; id => 'geoname_id',
my $long = $header{country_name}; cc => 'country_iso_code',
my $ct = $header{continent_code}; long => 'country_name',
my $cn = $header{continent_name}; ct => 'continent_code',
cn => 'continent_name',
);
# now create a function which returns the value of that column #
map { eval "sub $_ () { \$header{\$remapping{$_}}; }" ; } keys %remapping;
while (my $row = $csv->getline($fh)) { while (my $row = $csv->getline($fh)) {
if ($row->[$cc] eq '' && $row->[$long] eq '') { if ($row->[cc] eq '' && $row->[long] eq '') {
$countryId{$row->[$id]} = $row->[$ct]; $countryId{$row->[id]} = $row->[ct];
$countryName{$row->[$ct]} = $row->[$cn]; $countryName{$row->[ct]} = $row->[cn];
} else { } else {
$countryId{$row->[$id]} = $row->[$cc]; $countryId{$row->[id]} = $row->[cc];
$countryName{$row->[$cc]} = $row->[$long]; $countryName{$row->[cc]} = $row->[long];
} }
} }
@@ -100,6 +107,9 @@ sub loadCountries
$countryName{O1} = 'Other Country'; $countryName{O1} = 'Other Country';
close($fh); close($fh);
# clean up the namespace
undef &id; undef &cc; undef &long; undef &ct; undef &cn;
} }
sub lookupCountry sub lookupCountry
@@ -121,9 +131,11 @@ sub lookupCountry
sub collect sub collect
{ {
my ($file, $fh, $net, $id, $rid, $proxy, $sat, $row); my ($file, $fh, $row);
my (%country, %header); my (%country, %header);
sub net; sub id; sub rid; sub proxy; sub sat;
my %pairs = ( my %pairs = (
network => 'Network', network => 'Network',
registered_country_geoname_id => 'Registered Country ID', registered_country_geoname_id => 'Registered Country ID',
@@ -152,17 +164,22 @@ sub collect
# verify that the columns we need are present # verify that the columns we need are present
map { die "Table has no %pairs{$_} column\n" unless (exists $header{$_}); } keys %pairs; map { die "Table has no %pairs{$_} column\n" unless (exists $header{$_}); } keys %pairs;
$net = $header{network}; my %remapping = (
$id = $header{geoname_id}; net => 'network',
$rid = $header{registered_country_geoname_id}; id => 'geoname_id',
$proxy = $header{is_anonymous_proxy}; rid => 'registered_country_geoname_id',
$sat = $header{is_satellite_provider}; proxy => 'is_anonymous_proxy',
sat => 'is_satellite_provider',
);
# now create a function which returns the value of that column #
map { eval "sub $_ () { \$header{\$remapping{$_}}; }" ; } keys %remapping;
while ($row = $csv->getline($fh)) { while ($row = $csv->getline($fh)) {
my ($cc, $cidr); my ($cc, $cidr);
$cc = lookupCountry($row->[$id], $row->[$rid], $row->[$proxy], $row->[$sat]); $cc = lookupCountry($row->[id], $row->[rid], $row->[proxy], $row->[sat]);
$cidr = $row->[$net]; $cidr = $row->[net];
$country{$cc}->{pool_v4}->add($cidr); $country{$cc}->{pool_v4}->add($cidr);
if ($. % 4096 == 0) { if ($. % 4096 == 0) {
@@ -174,6 +191,9 @@ sub collect
close($fh); close($fh);
# clean up the namespace
undef &net; undef &id; undef &rid; undef &proxy; undef &sat;
$file = "$dir/GeoLite2-Country-Blocks-IPv6.csv"; $file = "$dir/GeoLite2-Country-Blocks-IPv6.csv";
open($fh, '<', $file) || die "Can't open IPv6 database\n"; open($fh, '<', $file) || die "Can't open IPv6 database\n";
@@ -186,17 +206,15 @@ sub collect
# verify that the columns we need are present # verify that the columns we need are present
map { die "Table has no %pairs{$_} column\n" unless (exists $header{$_}); } keys %pairs; map { die "Table has no %pairs{$_} column\n" unless (exists $header{$_}); } keys %pairs;
$net = $header{network}; # unlikely the IPv6 table has different columns, but just to be sure
$id = $header{geoname_id}; # create a function which returns the value of that column #
$rid = $header{registered_country_geoname_id}; map { eval "sub $_ () { \$header{\$remapping{$_}}; }" ; } keys %remapping;
$proxy = $header{is_anonymous_proxy};
$sat = $header{is_satellite_provider};
while ($row = $csv->getline($fh)) { while ($row = $csv->getline($fh)) {
my ($cc, $cidr); my ($cc, $cidr);
$cc = lookupCountry($row->[$id], $row->[$rid], $row->[$proxy], $row->[$sat]); $cc = lookupCountry($row->[id], $row->[rid], $row->[proxy], $row->[sat]);
$cidr = $row->[$net]; $cidr = $row->[net];
$country{$cc}->{pool_v6}->add($cidr); $country{$cc}->{pool_v6}->add($cidr);
if ($. % 4096 == 0) { if ($. % 4096 == 0) {
@@ -208,6 +226,9 @@ sub collect
close($fh); close($fh);
# clean up the namespace
undef &net; undef &id; undef &rid; undef &proxy; undef &sat;
return \%country; return \%country;
} }