App-geoip
view release on metacpan or search on metacpan
push @json, \%json;
}
elsif (!$conf{short}) {
say " EU member : ", $_->{reg_ctry_eu} ? "Yes" : "No";
say " Satellite : ", $_->{satellite} ? "Yes" : "No";
say " Anon Proxy: ", $_->{anon_proxy} ? "Yes" : "No";
}
}
$dbh->rollback;
$dbh->disconnect;
if ($conf{json}) {
say $opt_J
? JSON::PP->new->pretty->allow_nonref->encode (\@json)
: JSON::PP->new->ascii ->allow_nonref->encode (\@json);
}
sub dec2dms {
my $dec = shift or return "";
my $deg = int $dec;
my $dm = abs ($dec - $deg) * 60;
my $min = int $dm;
my $sec = ($dm - $min) * 60;
sprintf "%d\x{00b0}%02d'%05.2f\"", $deg, $min, $sec;
} # dec2dms
sub distance {
my ($lat_c, $lon_c, $lat_s, $lon_s) = @_;
$gis and
return $gis->distance ($lat_c, $lon_c, $lat_s, $lon_s)->meters / 1000.;
my $rad = 6371; # km
# Convert angles from degrees to radians
my $dlat = deg2rad ($lat_s - $lat_c);
my $dlon = deg2rad ($lon_s - $lon_c);
my $x = sin ($dlat / 2) * sin ($dlat / 2) +
cos (deg2rad ($lat_c)) * cos (deg2rad ($lat_s)) *
sin ($dlon / 2) * sin ($dlon / 2);
return $rad * 2 * atan2 (sqrt ($x), sqrt (1 - $x)); # km
} # distance
sub getconf {
my $home = $ENV{HOME} || $ENV{USERPROFILE} || $ENV{HOMEPATH};
foreach my $rcf (grep { -s }
"$home/geoip.rc", "$home/.geoiprc", "$home/.config/geoip") {
my $mode = (stat $rcf)[2];
$mode & 022 and next;
open my $fh, "<", $rcf or next;
while (<$fh>) {
m/^\s*[;#]/ and next;
my ($k, $v) = (m/^\s*([-\w]+)\s*[:=]\s*(.*\S)/) or next;
$conf{ lc $k
=~ s{-}{_}gr
=~ s{^use_}{}ir
=~ s{^(json_)?(?:unicode|utf-?8?)$}{utf8}ir
=~ s{^dist$}{distance}ir
} = $v
=~ s{(?:U\+?|\\[Uu])([0-9A-Fa-f]{2,7})}{chr hex $1}ger
=~ s{^(?:no|false)$}{0}ir
=~ s{^(?:yes|true)$}{1}ir;
}
}
} # getconf
1;
__END__
=encoding utf-8
=head1 NAME
geoip - a tool to show geographical data based on hostname or IP address(es)
=head1 SYNOPSIS
geoip --help
geoip --fetch [--no-update]
geoip [options] [host|IP ...]
=head1 DESCRIPTION
This tool uses a database to use the (pre-fetched) GeoIP2 data from MaxMind
to show related geographical information for IP addresses. This information
can optionally be extended with information from online WHOIS services and
or derived data, like distance to the location of the server this tool runs
on or a configured local location.
The output is plain text or JSON. JSON may be short or formatted.
=head2 Configuration
The tool allows the use of configuration files. It tests for existence of
the files listed here. All existing files is read (in this order) if it is
only writable by the author (mode C<0640> should do).
$home/geoip.rc
$home/.geoiprc
$home/.config/geoip
where C<$home> is either of C<$HOME>, C<$USERPROFILE>, or C<$HOMEPATH>.
The format of the file is
# Comment
; Comment
option : value
option = value
where the C<:> and C<=> are equal and whitespace around them is optional
and ignored. The values C<False> and C<No> (case insensitive) are the same
as C<0> and the values C<True> and C<Yes> are equal to C<1>. For readability
you can prefix C<use_> to most options (it is ignored). The use of C<-> in
( run in 2.373 seconds using v1.01-cache-2.11-cpan-fe3c2283af0 )