-
Notifications
You must be signed in to change notification settings - Fork 1
/
GeoIP.pm
161 lines (120 loc) · 5.15 KB
/
GeoIP.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package Plack::Middleware::GeoIP;
use strict;
use warnings;
use 5.008;
our $VERSION = 0.03;
use parent qw/Plack::Middleware/;
use Geo::IP;
use Plack::Util::Accessor qw( GeoIPDBFile GeoIPEnableUTF8 );
sub geoip_flag {
my $self = shift;
my $flag = shift;
return GEOIP_MEMORY_CACHE if $flag eq 'MemoryCache';
return GEOIP_CHECK_CACHE if $flag eq 'CheckCache';
return GEOIP_INDEX_CACHE if $flag eq 'IndexCache';
return GEOIP_MMAP_CACHE if $flag eq 'MMapCache';
return GEOIP_STANDARD;
}
sub prepare_app {
my $self = shift;
if (my $dbfiles = $self->GeoIPDBFile) {
my @dbfiles = ref $dbfiles ? @{ $dbfiles } : ($dbfiles);
foreach my $dbfile (@dbfiles) {
my ($filename, $flag) = ref $dbfile ? @{ $dbfile } : ($dbfile, 'Standard');
# combine flags
my @flags = ref $flag ? @{ $flag } : ($flag);
my $flags = GEOIP_STANDARD;
foreach my $f (@flags) {
$flags |= $self->geoip_flag($f);
}
my $gi = Geo::IP->open($filename, $flags);
$gi->set_charset(GEOIP_CHARSET_UTF8) if $self->GeoIPEnableUTF8;
push @{ $self->{gips} }, $gi;
}
} else {
my $gi = Geo::IP->new(GEOIP_STANDARD);
push @{ $self->{gips} }, $gi;
}
}
sub call {
my $self = shift;
my $env = shift;
my $ipaddr = $env->{REMOTE_ADDR};
foreach my $gi (@{ $self->{gips} }) {
my $type = $gi->database_edition;
if (GEOIP_COUNTRY_EDITION == $type) {
if (my $code = $gi->country_code_by_addr($ipaddr)) {
$env->{GEOIP_COUNTRY_CODE} = $code;
$env->{GEOIP_COUNTRY_CODE3} = $gi->country_code3_by_addr($ipaddr);
$env->{GEOIP_COUNTRY_NAME} = $gi->country_name_by_addr($ipaddr);
$env->{GEOIP_CONTINENT_CODE} = $gi->continent_code_by_country_code($code);
}
} elsif (GEOIP_CITY_EDITION_REV0 == $type or GEOIP_CITY_EDITION_REV1 == $type) {
if (my $record = $gi->record_by_addr($ipaddr)) {
$env->{GEOIP_COUNTRY_CODE} = $record->country_code;
$env->{GEOIP_COUNTRY_CODE3} = $record->country_code3;
$env->{GEOIP_COUNTRY_NAME} = $record->country_name;
$env->{GEOIP_LATITUDE} = $record->latitude;
$env->{GEOIP_LONGITUDE} = $record->longitude;
$env->{GEOIP_CONTINENT_CODE} = $record->continent_code;
$env->{GEOIP_TIME_ZONE} = $record->time_zone if $record->time_zone;
$env->{GEOIP_REGION} = $record->region if $record->region;
$env->{GEOIP_REGION_NAME} = $record->region_name if $record->region and $record->region_name;
$env->{GEOIP_CITY} = $record->city if $record->city;
$env->{GEOIP_POSTAL_CODE} = $record->postal_code if $record->postal_code;
$env->{GEOIP_METRO_CODE} = $record->metro_code if $record->metro_code;
$env->{GEOIP_AREA_CODE} = $record->area_code if $record->area_code;
}
}
}
return $self->app->($env);
}
1;
=head1 NAME
Plack::Middleware::GeoIP - Find country and city of origin of a web request
=head1 SYNOPSIS
# with Plack::Middleware::RealIP
enable 'Plack::Middleware::RealIP',
header => 'X-Forwarded-For',
trusted_proxy => [ qw(192.168.1.0/24 192.168.2.1) ];
enable 'Plack::Middleware::GeoIP',
GeoIPDBFile => [ '/path/to/GeoIP.dat', '/path/to/GeoIPCity.dat' ],
GeoIPEnableUTF8 => 1;
=head1 DESCRIPTION
Plack::Middleware::GeoIP is a loose port of the Apache module
mod_geoip. It uses Geo::IP to lookup the country and city that a web
request originated from.
All requests are looked up and GEOIP_* variables are added to PSGI
environment hash. For improved performance, you may want to only enable
this middleware for specific URL's.
The following PSGI environment variables are set by this middleware:
GeoIP Country Edition:
GEOIP_COUNTRY_CODE, GEOIP_COUNTRY_CODE3, GEOIP_COUNTRY_NAME,
GEOIP_CONTINENT_CODE
GeoIP City Edition:
GEOIP_COUNTRY_CODE, GEOIP_COUNTRY_CODE3, GEOIP_COUNTRY_NAME,
GEOIP_CONTINENT_CODE, GEOIP_LATITUDE, GEOIP_LONGITUDE, GEOIP_TIME_ZONE,
GEOIP_REGION, GEOIP_REGION_NAME, GEOIP_CITY, GEOIP_POSTAL_CODE,
GEOIP_METRO_CODE, GEOIP_AREA_CODE
=head1 CONFIGURATION
=over 4
=item GeoIPDBFile
GeoIPDBFile => '/path/to/GeoIP.dat'
GeoIPDBFile => [ '/path/to/GeoIP.dat', '/path/to/GeoIPCity.dat' ]
GeoIPDBFile => [ '/path/to/GeoIP.dat', [ '/path/to/GeoIPCity.dat', 'MemoryCache' ] ]
GeoIPDBFile => [ '/path/to/GeoIP.dat', [ '/path/to/GeoIPCity.dat', [ qw(MemoryCache CheckCache) ] ] ]
Path to GeoIP data file. GeoIP flags may also be specified. Accepted
flags are Standard, MemoryCache, CheckCache, IndexCache, and MMapCache.
=item GeoIPEnableUTF8
GeoIPEnableUTF8 => 1
Turn on utf8 characters for city names.
=back
=head1 AUTHOR
Sherwin Daganato E<lt>sherwin@daganato.comE<gt>
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 SEE ALSO
L<mod_geoip|http://www.maxmind.com/app/mod_geoip>
L<Geo::IP>
=cut