Skip to content

Instantly share code, notes, and snippets.

@jat001
Created March 1, 2015 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jat001/8d7b019c576910a45941 to your computer and use it in GitHub Desktop.
Save jat001/8d7b019c576910a45941 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
#-----------------------------------------------------------------------------
# HostInfo AWStats plugin
# This plugin allow you to add information on hosts, like a whois fields.
#-----------------------------------------------------------------------------
# Perl Required Modules: XWhois
#-----------------------------------------------------------------------------
# $Revision: 1.12 $ - $Author: eldy $ - $Date: 2004/03/27 18:09:00 $
use strict;
no strict 'refs';
#-----------------------------------------------------------------------------
# PLUGIN VARIABLES
#-----------------------------------------------------------------------------
# <-----
# ENTER HERE THE MINIMUM AWSTATS VERSION REQUIRED BY YOUR PLUGIN
# AND THE NAME OF ALL FUNCTIONS THE PLUGIN MANAGE.
my $PluginNeedAWStatsVersion = '6.0';
my $PluginHooksFunctions = 'ShowInfoHost';
# ----->
# <-----
# IF YOUR PLUGIN NEED GLOBAL VARIABLES, THEY MUST BE DECLARED HERE.
use vars qw/
$ipfile
/;
# ----->
#-----------------------------------------------------------------------------
# PLUGIN FUNCTION: Init_pluginname
#-----------------------------------------------------------------------------
sub Init_qqhostinfo {
my $InitParams = shift;
my $checkversion =& Check_Plugin_Version($PluginNeedAWStatsVersion);
# <-----
# ENTER HERE CODE TO DO INIT PLUGIN ACTIONS
debug('InitParams = ' . $InitParams, 1);
$ipfile = $InitParams;
# ----->
return ($checkversion ? $checkversion : $PluginHooksFunctions);
}
#-----------------------------------------------------------------------------
# PLUGIN FUNCTION: ShowInfoHost_pluginname
# UNIQUE: NO (Several plugins using this function can be loaded)
# Function called to add additionnal columns to the Hosts report.
# This function is called when building rows of the report (One call for each
# row). So it allows you to add a column in report, for example with code :
# print "<TD>This is a new cell for $param</TD>";
# Parameters: Host name or ip
#-----------------------------------------------------------------------------
sub ShowInfoHost_qqhostinfo {
my $param = "$_[0]";
# <-----
if ($param eq '__title__') {
print '<th width="80">Location</th>';
}
elsif ($param) {
print '<td>';
print ipwhere($param);
print '</td>';
}
else {
print '<td>&nbsp;</td>';
}
return 1;
# ----->
}
sub ipwhere {
my ($ip, @ip, $ipNum, $ipbegin, $ipend, $ipAllNum, $BeginNum, $EndNum, $Middle, $ipData1, $ipData2, $ip1num, $ip2num, $DataSeek, $ipFlag, $ipSeek, $AddrSeek, $AddrSeek2, $ipAddr1, $ipAddr2, $ipaddr, $unknow);
$ip = shift;
@ip = split(/\./, $ip);
$ipNum = $ip[0] * 16777216 + $ip[1] * 65536 + $ip[2] * 256 + $ip[3];
open(FILE, $ipfile);
binmode(FILE);
sysread(FILE, $ipbegin, 4);
sysread(FILE, $ipend, 4);
$ipbegin = unpack('L', $ipbegin);
$ipend = unpack('L', $ipend);
$ipAllNum = ($ipend - $ipbegin) / 7 + 1;
$BeginNum = 0;
$EndNum = $ipAllNum;
Bgn:
$Middle = int(($EndNum + $BeginNum) / 2);
seek(FILE, $ipbegin + 7 * $Middle, 0);
read(FILE, $ipData1, 4);
$ip1num = unpack('L', $ipData1);
if ($ip1num > $ipNum) {
$EndNum = $Middle;
goto Bgn;
}
read(FILE, $DataSeek, 3);
$DataSeek = unpack('L', $DataSeek . "\0");
seek(FILE, $DataSeek, 0);
read(FILE, $ipData2, 4);
$ip2num = unpack('L', $ipData2);
if ($ip2num < $ipNum) {
goto End if ($Middle == $BeginNum);
$BeginNum = $Middle;
goto Bgn;
}
$/ = "\0";
read(FILE, $ipFlag, 1);
if ($ipFlag eq "\1") {
read(FILE, $ipSeek, 3);
$ipSeek = unpack('L', $ipSeek . "\0");
seek(FILE, $ipSeek, 0);
read(FILE, $ipFlag, 1);
}
if ($ipFlag eq "\2") {
read(FILE, $AddrSeek, 3);
read(FILE, $ipFlag, 1);
if($ipFlag eq "\2") {
read(FILE, $AddrSeek2, 3);
$AddrSeek2 = unpack('L', $AddrSeek2 . "\0");
seek(FILE,$AddrSeek2,0);
} else {
seek(FILE, -1, 1);
}
$ipAddr2 = <FILE>;
$AddrSeek = unpack('L', $AddrSeek . "\0");
seek(FILE, $AddrSeek, 0);
$ipAddr1 = <FILE>;
} else {
seek(FILE, -1, 1);
$ipAddr1 = <FILE>;
read(FILE, $ipFlag, 1);
if($ipFlag eq "\2") {
read(FILE, $AddrSeek2, 3);
$AddrSeek2 = unpack('L', $AddrSeek2 . "\0");
seek(FILE, $AddrSeek2, 0);
} else {
seek(FILE, -1, 1);
}
$ipAddr2 = <FILE>;
}
End:
chomp($ipAddr1, $ipAddr2);
$/ = "\n";
close(FILE);
$ipAddr2 = '' if ($ipAddr2 =~ /http/i);
$ipaddr = $ipAddr1 . ' ' . $ipAddr2;
$ipaddr =~ s/CZ88\.NET|^\s+|\s+$//isg;
$unknow = encode('gb2312', decode('utf8', '未知'));
$ipaddr = encode('gb2312', decode('utf8', '未知地区')) if ($ipaddr =~ /\Q$unknow\E|http/i || $ipaddr eq '');
return encode($PageCode, decode('gb2312', $ipaddr));
}
1; # Do not remove this line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment