]>
Commit | Line | Data |
---|---|---|
1dcd8715 SS |
1 | #!/usr/bin/perl -w |
2 | ############################################################################ | |
3 | # # | |
4 | # This file is part of the IPFire Firewall. # | |
5 | # # | |
6 | # IPFire is free software; you can redistribute it and/or modify # | |
7 | # it under the terms of the GNU General Public License as published by # | |
8 | # the Free Software Foundation; either version 2 of the License, or # | |
9 | # (at your option) any later version. # | |
10 | # # | |
11 | # IPFire is distributed in the hope that it will be useful, # | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
14 | # GNU General Public License for more details. # | |
15 | # # | |
16 | # You should have received a copy of the GNU General Public License # | |
17 | # along with IPFire; if not, write to the Free Software # | |
18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # | |
19 | # # | |
20 | # Copyright (C) 2015 IPFire Team <info@ipfire.org>. # | |
21 | # # | |
22 | ############################################################################ | |
23 | ||
24 | package GeoIP; | |
25 | ||
0ca3baed | 26 | use Geo::IP::PurePerl; |
b013822b | 27 | use Locale::Codes::Country; |
1dcd8715 | 28 | |
d499e86b MT |
29 | my $geoip_database_dir = "/var/lib/GeoIP"; |
30 | my $location_database = "GeoLite2-Country-Locations-en.csv"; | |
31 | ||
0ca3baed | 32 | my $database; |
00793c27 MT |
33 | |
34 | sub lookup($) { | |
35 | my $address = shift; | |
36 | ||
0ca3baed SS |
37 | # Load the database into memory if not already done |
38 | if (!$database) { | |
39 | $database = Geo::IP::PurePerl->new(GEOIP_MEMORY_CACHE); | |
00793c27 MT |
40 | } |
41 | ||
0ca3baed SS |
42 | # Return the name of the country |
43 | return $database->country_code_by_name($address); | |
00793c27 MT |
44 | } |
45 | ||
1dcd8715 SS |
46 | # Function to get the flag icon for a specified country code. |
47 | sub get_flag_icon($) { | |
48 | my ($input) = @_; | |
49 | ||
50 | # Webserver's root dir. (Required for generating full path) | |
51 | my $webroot = "/srv/web/ipfire/html"; | |
52 | ||
53 | # Directory which contains the flag icons. | |
54 | my $flagdir = "/images/flags"; | |
55 | ||
56 | # File extension of the country flags. | |
57 | my $ext = "png"; | |
58 | ||
59 | # Remove whitespaces. | |
60 | chomp($input); | |
61 | ||
dfbee171 SS |
62 | # Convert given country code to upper case. |
63 | my $ccode = uc($input); | |
1dcd8715 SS |
64 | |
65 | # Generate filename, based on the contry code in lower case | |
66 | # and the defined file extension. | |
67 | my $file = join('.', $ccode,$ext); | |
68 | ||
69 | # Generate path inside webroot to the previously generated file. | |
70 | my $flag_icon = join('/', $flagdir,$file); | |
71 | ||
72 | # Generate absolute path to the icon file. | |
73 | my $absolute_path = join('', $webroot,$flag_icon); | |
74 | ||
75 | # Check if the a icon file exists. | |
76 | if (-e "$absolute_path") { | |
77 | # Return content of flag_icon. | |
78 | return $flag_icon; | |
dfbee171 SS |
79 | } else { |
80 | # If no icon for the specified country exists, try to use | |
81 | # the icon for "unknown". | |
82 | my $ccode = "unknown"; | |
83 | ||
b1ad5b8b MT |
84 | # Redoing all the stuff from above for the "unknown" icon. |
85 | my $file = join('.', $ccode, $ext); | |
86 | my $flag_icon = join('/', $flagdir, $file); | |
87 | my $absolute_path = join('', $webroot, $flag_icon); | |
dfbee171 SS |
88 | |
89 | # Check if the icon is present. | |
90 | if (-e "$absolute_path") { | |
91 | # Return "unknown" icon. | |
92 | return $flag_icon; | |
93 | } | |
1dcd8715 SS |
94 | } |
95 | } | |
96 | ||
97 | # Function to get the county name by a given country code. | |
98 | sub get_full_country_name($) { | |
99 | my ($input) = @_; | |
100 | my $name; | |
101 | ||
102 | # Remove whitespaces. | |
103 | chomp($input); | |
104 | ||
105 | # Convert input into lower case format. | |
106 | my $code = lc($input); | |
107 | ||
108 | # Handle country codes which are not in the list. | |
109 | if ($code eq "a1") { $name = "Anonymous Proxy" } | |
110 | elsif ($code eq "a2") { $name = "Satellite Provider" } | |
111 | elsif ($code eq "o1") { $name = "Other Country" } | |
112 | elsif ($code eq "ap") { $name = "Asia/Pacific Region" } | |
113 | elsif ($code eq "eu") { $name = "Europe" } | |
114 | elsif ($code eq "yu") { $name = "Yugoslavia" } | |
115 | else { | |
116 | # Use perl built-in module to get the country code. | |
b013822b | 117 | $name = &Locale::Codes::Country::code2country($code); |
1dcd8715 SS |
118 | } |
119 | ||
120 | return $name; | |
121 | } | |
122 | ||
30c59cbb SS |
123 | # Function to get all available GeoIP locations. |
124 | sub get_geoip_locations() { | |
abe21498 | 125 | my @locations = (); |
30c59cbb SS |
126 | |
127 | # Open the location database. | |
abe21498 | 128 | open(LOCATION, "$geoip_database_dir/$location_database") or return @locations; |
30c59cbb SS |
129 | |
130 | # Loop through the file. | |
131 | while(my $line = <LOCATION>) { | |
132 | # Remove newlines. | |
133 | chomp($line); | |
134 | ||
135 | # Split the line content. | |
136 | my ($geoname_id, $locale_code, $continent_code, $continent_name, $country_iso_code, $country_name, $is_in_european_union) = split(/\,/, $line); | |
137 | ||
138 | # Check if the country_iso_code is upper case. | |
139 | if($country_iso_code =~ /[A-Z]/) { | |
140 | # Add the current ISO code. | |
141 | push(@locations, $country_iso_code); | |
142 | } | |
143 | } | |
144 | ||
145 | # Close filehandle. | |
146 | close(LOCATION); | |
147 | ||
148 | # Sort locations array in alphabetical order. | |
149 | my @sorted_locations = sort(@locations); | |
150 | ||
151 | # Return the array.. | |
152 | return @sorted_locations; | |
153 | } | |
154 | ||
155 | ||
1dcd8715 | 156 | 1; |