]> git.ipfire.org Git - ipfire-2.x.git/blame - html/cgi-bin/ipinfo.cgi
Hardcode theme to ipfire
[ipfire-2.x.git] / html / cgi-bin / ipinfo.cgi
CommitLineData
ac1cfefa 1#!/usr/bin/perl
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
4dc8de6f 5# Copyright (C) 2007-2020 IPFire Team <info@ipfire.org> #
70df8302
MT
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
ac1cfefa 21
f367d5b3 22use CGI;
ac1cfefa
MT
23use IO::Socket;
24use strict;
25
26# enable only the following on debugging purpose
27#use warnings;
28#use CGI::Carp 'fatalsToBrowser';
29
986e08d9 30require '/var/ipfire/general-functions.pl';
ac1cfefa
MT
31require "${General::swroot}/lang.pl";
32require "${General::swroot}/header.pl";
dca3f207 33require "${General::swroot}/location-functions.pl";
ac1cfefa 34
4dc8de6f
PM
35# Load colours for current theme...
36my %color = ();
37my %mainsettings = ();
38&General::readhash("${General::swroot}/main/settings", \%mainsettings);
8186b372 39&General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
4dc8de6f 40
ac1cfefa
MT
41my %cgiparams=();
42
43&Header::showhttpheaders();
44
ac1cfefa
MT
45my @lines=();
46my $extraquery='';
f367d5b3 47
4e033257
SS
48# Hash which contains the whois servers from
49# the responisible RIR of the continent.
50my %whois_servers_by_continent = (
51 "AF" => "whois.afrinic.net",
52 "AS" => "whois.apnic.net",
53 "EU" => "whois.ripe.net",
54 "NA" => "whois.arin.net",
55 "SA" => "whois.lacnic.net"
56);
57
58# Default whois server if no continent could be determined.
59my $whois_server = "whois.arin.net";
60
f367d5b3
MT
61my $addr = CGI::param("ip") || "";
62
63if (&General::validip($addr)) {
4dc8de6f
PM
64 # Write HTML page header...
65 &Header::openpage($Lang::tr{'ip info for'} . ' ' . $addr, 1, '');
66 &Header::openbigbox('100%', 'left');
67
ac1cfefa
MT
68 my $iaddr = inet_aton($addr);
69 my $hostname = gethostbyaddr($iaddr, AF_INET);
4dc8de6f 70 if (!$hostname) { $hostname = $Lang::tr{'ptr lookup failed'}; }
ac1cfefa 71
4dc8de6f 72 # Enumerate location information for IP address...
07e42be9 73 my $ccode = &Location::Functions::lookup_country_code($addr);
4dc8de6f 74 my $cname = &Location::Functions::get_full_country_name($ccode);
33975f57 75 my @network_flags = &Location::Functions::address_has_flags($addr);
4e033257
SS
76
77 # Try to get the continent of the country code.
07e42be9 78 my $continent = &Location::Functions::get_continent_code($ccode);
4e033257 79
4dc8de6f
PM
80 # Enumerate Autonomous System details for IP address...
81 my $asn = &Location::Functions::lookup_asn($addr);
82 my $as_name;
83 if ($asn) {
84 $as_name = &Location::Functions::get_as_name($asn);
85
86 # In case we have found an AS name, make output more readable...
87 if ($as_name) {
88 $as_name = "- " . $as_name;
89 }
90 $asn = "AS" . $asn;
91 } else {
92 $asn = $Lang::tr{'asn lookup failed'};
93 }
94
4e033257
SS
95 # Check if a whois server for the continent is known.
96 if($whois_servers_by_continent{$continent}) {
97 # Use it.
98 $whois_server = $whois_servers_by_continent{$continent};
99 }
100
dca3f207 101 my $flag_icon = &Location::Functions::get_flag_icon($ccode);
9b5a17bf 102
4dc8de6f
PM
103 &Header::openbox('100%', 'left', $Lang::tr{'ip basic info'});
104
105 print <<END;
106 <center>
107 <table class="tbl" width='100%'>
108 <tr>
109 <td bgcolor='$color{'color22'}'><strong>$Lang::tr{'country'}</strong></td>
1a3c5355 110 <td bgcolor='$color{'color22'}'>$cname <a href='country.cgi#$ccode'><img src="$flag_icon" border="0" align="absmiddle" alt="$cname" title="$cname" /></td>
4dc8de6f
PM
111 </tr>
112 <tr>
113 <td bgcolor='$color{'color20'}'><strong>$Lang::tr{'ptr'}</strong></td>
114 <td bgcolor='$color{'color20'}'>$hostname</td>
115 </tr>
116 <tr>
117 <td bgcolor='$color{'color22'}'><strong>$Lang::tr{'autonomous system'}</strong></td>
118 <td bgcolor='$color{'color22'}'>$asn $as_name</td>
119 </tr>
120END
121
122 # Check if the address has a flag.
123 if (@network_flags) {
124 # Get amount of flags for this network.
125 my $flags_amount = @network_flags;
126 my $processed_flags;
127
128 # Loop through the array of network_flags.
129 foreach my $network_flag (@network_flags) {
130 # Increment value of processed flags.
131 $processed_flags++;
132
133 # Get the network flag name.
134 my $network_flag_name = &Location::Functions::get_full_country_name($network_flag);
135
136 # Colorize columns.
137 my $col;
138 if ($processed_flags % 2) {
139 $col = "bgcolor='$color{'color20'}'"; }
140 else {
141 $col = "bgcolor='$color{'color22'}'";
142 }
143
144 # Write table row...
145 print <<END;
146 <tr>
147 <td $col><strong>$network_flag_name</strong></td>
148 <td $col>$Lang::tr{'yes'}</td>
149 </tr>
150END
151 }
152 }
153
154 print " </table>\n";
155 print " </center>\n";
156
157 &Header::closebox();
158
4e033257 159 my $sock = new IO::Socket::INET ( PeerAddr => $whois_server, PeerPort => 43, Proto => 'tcp');
ac1cfefa
MT
160 if ($sock)
161 {
4e033257 162 print $sock "$addr\n";
ac1cfefa 163 while (<$sock>) {
4e033257 164 $extraquery = $1 if (/ReferralServer: whois:\/\/(\S+)\s+/);
ac1cfefa
MT
165 push(@lines,$_);
166 }
167 close($sock);
168 if ($extraquery) {
169 undef (@lines);
4e033257
SS
170 $whois_server = $extraquery;
171 my $sock = new IO::Socket::INET ( PeerAddr => $whois_server, PeerPort => 43, Proto => 'tcp');
ac1cfefa
MT
172 if ($sock)
173 {
174 print $sock "$addr\n";
175 while (<$sock>) {
176 push(@lines,$_);
177 }
178 }
179 else
180 {
4e033257 181 @lines = ( "$Lang::tr{'unable to contact'} $whois_server" );
ac1cfefa
MT
182 }
183 }
184 }
185 else
186 {
4e033257 187 @lines = ( "$Lang::tr{'unable to contact'} $whois_server" );
ac1cfefa
MT
188 }
189
4dc8de6f 190 &Header::openbox('100%', 'left', $Lang::tr{'whois results from'} . " " . $whois_server);
e5b1e648 191
ac1cfefa
MT
192 print "<pre>\n";
193 foreach my $line (@lines) {
194 print &Header::cleanhtml($line,"y");
195 }
196 print "</pre>\n";
197 &Header::closebox();
f367d5b3 198} else {
4dc8de6f
PM
199 # Open HTML page header in case of invalid IP addresses
200 &Header::openpage($Lang::tr{'ip info'}, 1, '');
201 &Header::openbigbox('100%', 'left');
202
f367d5b3
MT
203 &Header::openbox('100%', 'left', $Lang::tr{'invalid ip'});
204 print <<EOF;
205 <p style="text-align: center;">
206 $Lang::tr{'invalid ip'}
207 </p>
208EOF
209 &Header::closebox();
ac1cfefa
MT
210}
211
212print <<END
213<div align='center'>
214<table width='80%'>
215<tr>
415cbcec 216 <td align='center'><a href='$ENV{'HTTP_REFERER'}'><img src='/images/back.png' alt='$Lang::tr{'back'}' title='$Lang::tr{'back'}' /></a></td>
ac1cfefa
MT
217</tr>
218</table>
219</div>
220END
221;
222
223&Header::closebigbox();
224
225&Header::closepage();