File Coverage

blib/lib/Mail/Exim/ACL/Geolocation.pm
Criterion Covered Total %
statement 29 38 76.3
branch 3 8 37.5
condition n/a
subroutine 7 8 87.5
pod 2 2 100.0
total 41 56 73.2


line stmt bran cond sub pod time code
1             package Mail::Exim::ACL::Geolocation;
2              
3             # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
4              
5 1     1   239267 use 5.016;
  1         11  
6 1     1   6 use warnings;
  1         3  
  1         97  
7 1     1   9 use utf8;
  1         3  
  1         6  
8              
9             our $VERSION = 1.005;
10              
11 1     1   97 use Exporter qw(import);
  1         1  
  1         71  
12 1     1   879 use IP::Geolocation::MMDB;
  1         90114  
  1         356  
13              
14             our @EXPORT_OK = qw(country_code asn_lookup);
15              
16             my @directories = qw(
17             /var/lib/GeoIP
18             /usr/local/share/GeoIP
19             /usr/share/GeoIP
20             /opt/share/GeoIP
21             /var/db/GeoIP
22             );
23              
24             my @country_databases = qw(
25             GeoIP2-Country.mmdb
26             GeoIP2-City.mmdb
27             dbip-country.mmdb
28             dbip-city.mmdb
29             GeoLite2-Country.mmdb
30             GeoLite2-City.mmdb
31             dbip-country-lite.mmdb
32             dbip-city-lite.mmdb
33             );
34              
35             my @asn_databases = qw(
36             GeoIP2-ASN.mmdb
37             dbip-asn.mmdb
38             GeoLite2-ASN.mmdb
39             dbip-asn-lite.mmdb
40             );
41              
42             my $country_reader = eval {
43             my $filename = $ENV{COUNTRY_DB}
44             || _first_database(\@directories, \@country_databases);
45             IP::Geolocation::MMDB->new(file => $filename);
46             };
47              
48             my $asn_reader = eval {
49             my $filename = $ENV{ASN_DB}
50             || _first_database(\@directories, \@asn_databases);
51             IP::Geolocation::MMDB->new(file => $filename);
52             };
53              
54             sub country_code {
55 2     2 1 194774 my $ip_address = shift;
56              
57 2         4 return eval { $country_reader->getcc($ip_address) };
  2         12  
58             }
59              
60             sub asn_lookup {
61 1     1 1 850 my $ip_address = shift;
62              
63 1         2 my $asn_tag;
64 1         2 my $asn = eval { $asn_reader->record_for_address($ip_address) };
  1         13  
65 1 50       4 if (defined $asn) {
66 1         3 my $number = $asn->{autonomous_system_number};
67 1 50       8 if (defined $number) {
68 1         1 $asn_tag = $number;
69 1         2 my $organization = $asn->{autonomous_system_organization};
70 1 50       2 if (defined $organization) {
71 1         4 $asn_tag .= q{ } . $organization;
72             }
73             }
74             }
75 1         7 return $asn_tag;
76             }
77              
78             sub _first_database {
79 0     0     my ($directories, $databases) = @_;
80              
81 0           for my $dir (@{$directories}) {
  0            
82 0           for my $db (@{$databases}) {
  0            
83 0           my $filename = $dir . q{/} . $db;
84 0 0         if (-r $filename) {
85 0           return $filename;
86             }
87             }
88             }
89 0           return;
90             }
91              
92             1;
93             __END__