From dc39dd7e5e16b9edd1542a3dada0b1780ac521fd Mon Sep 17 00:00:00 2001 From: manolamancha <> Date: Fri, 30 Apr 2010 12:36:09 +0000 Subject: [PATCH] Added code to parse resolved hosts to IPs so that override files will work if DNS resolution was used --- wwwroot/cgi-bin/plugins/geoip.pm | 8 +- wwwroot/cgi-bin/plugins/geoip_asn_maxmind.pm | 188 +++++++++--------- wwwroot/cgi-bin/plugins/geoip_city_maxmind.pm | 22 +- wwwroot/cgi-bin/plugins/geoip_isp_maxmind.pm | 125 ++++++++---- wwwroot/cgi-bin/plugins/geoip_org_maxmind.pm | 124 ++++++++---- .../cgi-bin/plugins/geoip_region_maxmind.pm | 153 ++++++++++---- 6 files changed, 410 insertions(+), 210 deletions(-) diff --git a/wwwroot/cgi-bin/plugins/geoip.pm b/wwwroot/cgi-bin/plugins/geoip.pm index bd75e6f3..8c315647 100644 --- a/wwwroot/cgi-bin/plugins/geoip.pm +++ b/wwwroot/cgi-bin/plugins/geoip.pm @@ -179,7 +179,9 @@ sub ShowInfoHost_geoip { } print ""; if ($key && $ip==4) { - my $res = $TmpDomainLookup{$param}||''; + my $res; + if ($gi){ $res = $TmpDomainLookup{$gi->get_ip_address($param)}||''; } + else { $res = $TmpDomainLookup{$param}||''; } if (!$res){$res=lc($gi->country_code_by_addr($param)) if $gi;} if ($Debug) { debug(" Plugin $PluginName: GetCountryByIp for $param: [$res]",5); } if ($res) { print $DomainsHashIDLib{$res}?$DomainsHashIDLib{$res}:"$Message[0]"; } @@ -189,7 +191,9 @@ sub ShowInfoHost_geoip { print "$Message[0]"; } if (! $key) { - my $res = $TmpDomainLookup{$param}||''; + my $res; + if ($gi){ $res = $TmpDomainLookup{$gi->get_ip_address($param)}||''; } + else { $res = $TmpDomainLookup{$param}||''; } if (!$res){$res=lc($gi->country_code_by_addr($param)) if $gi;} if ($Debug) { debug(" Plugin $PluginName: GetCountryByHostname for $param: [$res]",5); } if ($res) { print $DomainsHashIDLib{$res}?$DomainsHashIDLib{$res}:"$Message[0]"; } diff --git a/wwwroot/cgi-bin/plugins/geoip_asn_maxmind.pm b/wwwroot/cgi-bin/plugins/geoip_asn_maxmind.pm index 27ac1604..77dbef0b 100644 --- a/wwwroot/cgi-bin/plugins/geoip_asn_maxmind.pm +++ b/wwwroot/cgi-bin/plugins/geoip_asn_maxmind.pm @@ -49,11 +49,11 @@ my $OverrideFile=""; # <----- # IF YOUR PLUGIN NEED GLOBAL VARIABLES, THEY MUST BE DECLARED HERE. use vars qw/ -$geoip_org_maxmind -%_org_p -%_org_h -%_org_k -%_org_l +$geoip_asn_maxmind +%_asn_p +%_asn_h +%_asn_k +%_asn_l $MAXNBOFSECTIONGIR $MAXLENGTH /; @@ -96,12 +96,12 @@ sub Init_geoip_asn_maxmind { if ($link){$LookupLink=$link;} debug(" Plugin $PluginName: GeoIP initialized type=$type mode=$mode, link=$link",1); if ($type eq 'geoippureperl') { - $geoip_org_maxmind = Geo::IP::PurePerl->open($datafile, $mode); + $geoip_asn_maxmind = Geo::IP::PurePerl->open($datafile, $mode); } else { - $geoip_org_maxmind = Geo::IP->open($datafile, $mode); + $geoip_asn_maxmind = Geo::IP->open($datafile, $mode); } # Fails on some GeoIP version -# debug(" Plugin geoip_org_maxmind: GeoIP initialized database_info=".$geoip_org_maxmind->database_info()); +# debug(" Plugin geoip_org_maxmind: GeoIP initialized database_info=".$geoip_asn_maxmind->database_info()); # -----> return ($checkversion?$checkversion:"$PluginHooksFunctions"); @@ -120,9 +120,9 @@ sub AddHTMLMenuLink_geoip_asn_maxmind { # <----- if ($Debug) { debug(" Plugin $PluginName: AddHTMLMenuLink"); } if ($categ eq 'who') { - $menu->{$PluginName}=0.5; # Pos - $menulink->{$PluginName}=2; # Type of link - $menutext->{$PluginName}="Organizations"; # Text + $menu->{"plugin_$PluginName"}=0.7; # Pos + $menulink->{"plugin_$PluginName"}=2; # Type of link + $menutext->{"plugin_$PluginName"}="ASNs"; # Text } # -----> return 0; @@ -147,7 +147,7 @@ sub AddHTMLGraph_geoip_asn_maxmind { if ($Debug) { debug(" Plugin $PluginName: AddHTMLGraph $categ $menu $menulink $menutext"); } my $title='AS Numbers'; &tab_head("$title",19,0,'org'); - print "AS Numbers: ".((scalar keys %_org_h)-($_org_h{'unknown'}?1:0)).""; + print "AS Numbers: ".((scalar keys %_asn_h)-($_asn_h{'unknown'}?1:0)).""; print "ISP\n"; if ($ShowISP =~ /P/i) { print "$Message[56]"; } if ($ShowISP =~ /P/i) { print "$Message[15]"; } @@ -158,35 +158,35 @@ sub AddHTMLGraph_geoip_asn_maxmind { print "\n"; $total_p=$total_h=$total_k=0; my $count=0; - &BuildKeyList($MaxRowsInHTMLOutput,$MinHit{'Org'},\%_org_h,\%_org_h); + &BuildKeyList($MaxRowsInHTMLOutput,$MinHit{'Org'},\%_asn_h,\%_asn_h); foreach my $key (@keylist) { if ($key eq 'unknown') { next; } my $p_p; my $p_h; - if ($TotalPages) { $p_p=int($_org_p{$key}/$TotalPages*1000)/10; } - if ($TotalHits) { $p_h=int($_org_h{$key}/$TotalHits*1000)/10; } + if ($TotalPages) { $p_p=int($_asn_p{$key}/$TotalPages*1000)/10; } + if ($TotalHits) { $p_h=int($_asn_h{$key}/$TotalHits*1000)/10; } print ""; - my $org=$key; $org =~ s/_/ /g; - my $idx = index($org, ' '); + my $asn=$key; $asn =~ s/_/ /g; + my $idx = index($asn, ' '); # get lookup link my $link = ''; if ($LookupLink){ - if ($idx < 0 && $org =~ m/^A/){ $link .= $LookupLink.$org; } - elsif (substr($org, 0, $idx) =~ m/^A/){$link .= $LookupLink.substr($org, 0, $idx); } + if ($idx < 0 && $asn =~ m/^A/){ $link .= $LookupLink.$asn; } + elsif (substr($asn, 0, $idx) =~ m/^A/){$link .= $LookupLink.substr($asn, 0, $idx); } if ($link){ $link = "";} } - print "".$link.ucfirst(($idx > -1 ? substr($org, 0, $idx) : $org)); + print "".$link.ucfirst(($idx > -1 ? substr($asn, 0, $idx) : $asn)); print ($link ? "" : "").""; - print "".($idx > -1 ? substr($org, $idx+1) : " ")."\n"; - if ($ShowISP =~ /P/i) { print "".($_org_p{$key}?Format_Number($_org_p{$key}):" ").""; } - if ($ShowISP =~ /P/i) { print "".($_org_p{$key}?"$p_p %":' ').""; } - if ($ShowISP =~ /H/i) { print "".($_org_h{$key}?Format_Number($_org_h{$key}):" ").""; } - if ($ShowISP =~ /H/i) { print "".($_org_h{$key}?"$p_h %":' ').""; } - if ($ShowISP =~ /B/i) { print "".Format_Bytes($_org_k{$key}).""; } - if ($ShowISP =~ /L/i) { print "".($_org_p{$key}?Format_Date($_org_l{$key},1):'-').""; } + print "".($idx > -1 ? substr($asn, $idx+1) : " ")."\n"; + if ($ShowISP =~ /P/i) { print "".($_asn_p{$key}?Format_Number($_asn_p{$key}):" ").""; } + if ($ShowISP =~ /P/i) { print "".($_asn_p{$key}?"$p_p %":' ').""; } + if ($ShowISP =~ /H/i) { print "".($_asn_h{$key}?Format_Number($_asn_h{$key}):" ").""; } + if ($ShowISP =~ /H/i) { print "".($_asn_h{$key}?"$p_h %":' ').""; } + if ($ShowISP =~ /B/i) { print "".Format_Bytes($_asn_k{$key}).""; } + if ($ShowISP =~ /L/i) { print "".($_asn_p{$key}?Format_Date($_asn_l{$key},1):'-').""; } print "\n"; - $total_p += $_org_p{$key}||0; - $total_h += $_org_h{$key}; - $total_k += $_org_k{$key}||0; + $total_p += $_asn_p{$key}||0; + $total_h += $_asn_h{$key}; + $total_k += $_asn_k{$key}||0; $count++; } if ($Debug) { debug("Total real / shown : $TotalPages / $total_p - $TotalHits / $total_h - $TotalBytes / $total_h",2); } @@ -210,9 +210,9 @@ sub AddHTMLGraph_geoip_asn_maxmind { print ""; print "$Message[2]/$Message[0]"; print " \n"; - if ($ShowISP =~ /P/i) { print "".($rest_p?$rest_p:" ").""; } + if ($ShowISP =~ /P/i) { print "".($rest_p?Format_Number($rest_p):" ").""; } if ($ShowISP =~ /P/i) { print "".($rest_p?"$p_p %":' ').""; } - if ($ShowISP =~ /H/i) { print "".($rest_h?$rest_h:" ").""; } + if ($ShowISP =~ /H/i) { print "".($rest_h?Format_Number($rest_h):" ").""; } if ($ShowISP =~ /H/i) { print "".($rest_h?"$p_h %":' ').""; } if ($ShowISP =~ /B/i) { print "".Format_Bytes($rest_k).""; } if ($ShowISP =~ /L/i) { print " "; } @@ -271,49 +271,51 @@ sub ShowInfoHost_geoip_asn_maxmind { $key=$param; } print ""; - my $org; + my $asn = 0; if ($key && $ip==4) { - $org = $TmpLookup{$param}||''; - if (!$org && $type eq 'geoippureperl') + if ($geoip_asn_maxmind){$asn = $TmpLookup{$geoip_asn_maxmind->get_ip_address($param)};} + else {$asn = $TmpLookup{$param};} + if (!$asn && $type eq 'geoippureperl') { # Function org_by_addr does not exists in PurePerl but org_by_name do same - $org=$geoip_org_maxmind->org_by_name($param) if $geoip_org_maxmind; + $asn=$geoip_asn_maxmind->org_by_name($param) if $geoip_asn_maxmind; } - elsif (!$org) + elsif (!$asn) { - $org=$geoip_org_maxmind->org_by_addr($param) if $geoip_org_maxmind; + $asn=$geoip_asn_maxmind->org_by_addr($param) if $geoip_asn_maxmind; } - if ($Debug) { debug(" Plugin $PluginName: GetASNByIp for $param: [$org]",5); } + if ($Debug) { debug(" Plugin $PluginName: GetASNByIp for $param: [$asn]",5); } } if ($key && $ip==6) { - debug(" Plugin $PlugiName: IPv6 not supported by MaxMind Free DBs: $key",3); + debug(" Plugin $PluginName: IPv6 not supported by MaxMind Free DBs: $key",3); } if (! $key) { - $org = $TmpLookup{$param}||''; - if (!$org && $type eq 'geoippureperl') + if ($geoip_asn_maxmind){$asn = $TmpLookup{$geoip_asn_maxmind->get_ip_address($param)};} + else {$asn = $TmpLookup{$param};} + if (!$asn && $type eq 'geoippureperl') { - $org=$geoip_org_maxmind->org_by_name($param) if $geoip_org_maxmind; + $asn=$geoip_asn_maxmind->org_by_name($param) if $geoip_asn_maxmind; } - elsif (!$org) + elsif (!$asn) { - $org=$geoip_org_maxmind->org_by_name($param) if $geoip_org_maxmind; + $asn=$geoip_asn_maxmind->org_by_name($param) if $geoip_asn_maxmind; } - if ($Debug) { debug(" Plugin $PluginName: GetOrgByHostname for $param: [$org]",5); } + if ($Debug) { debug(" Plugin $PluginName: GetOrgByHostname for $param: [$asn]",5); } } - if ($org) { + if (length($asn)>0) { my $link = ''; - my $idx = index(trim($org), ' '); + my $idx = index(trim($asn), ' '); if ($LookupLink){ - if ($idx < 0 && $org =~ m/^A/){ $link .= $LookupLink.$org; } - elsif (substr($org, 0, $idx) =~ m/^A/){$link .= $LookupLink.substr($org, 0, $idx); } + if ($idx < 0 && $asn =~ m/^A/){ $link .= $LookupLink.$asn; } + elsif (substr($asn, 0, $idx) =~ m/^A/){$link .= $LookupLink.substr($asn, 0, $idx); } } if ($link){ $link = "";} - if ($idx > -1 ) {$org = substr(trim($org), $idx+1);} - if (length($org) <= $MAXLENGTH) { - print "$link$org".($link ? "" : ""); + if ($idx > -1 ) {$asn = substr(trim($asn), $idx+1);} + if (length($asn) <= $MAXLENGTH) { + print "$link$asn".($link ? "" : ""); } else { - print $link.substr($org,0,$MAXLENGTH).'...'.($link ? "" : ""); + print $link.substr($asn,0,$MAXLENGTH).'...'.($link ? "" : ""); } } else { print "$Message[0]"; } @@ -335,7 +337,7 @@ sub SectionInitHashArray_geoip_asn_maxmind { # my $param="$_[0]"; # <----- if ($Debug) { debug(" Plugin $PluginName: Init_HashArray"); } - %_org_p = %_org_h = %_org_k = %_org_l =(); + %_asn_p = %_asn_h = %_asn_k = %_asn_l =(); # -----> return 0; } @@ -348,26 +350,27 @@ sub SectionInitHashArray_geoip_asn_maxmind { sub SectionProcessIp_geoip_asn_maxmind { my $param="$_[0]"; # Param must be an IP # <----- - my $org; + my $asn = 0; if (!$LoadedOverride){&LoadOverrideFile_geoip_asn_maxmind();} - my $org = $TmpLookup{$param}||''; - if (!$org && $type eq 'geoippureperl') + if ($geoip_asn_maxmind){$asn = $TmpLookup{$geoip_asn_maxmind->get_ip_address($param)};} + else {$asn = $TmpLookup{$param};} + if (!$asn && $type eq 'geoippureperl') { # Function org_by_addr does not exists in PurePerl but org_by_name do same - $org=$geoip_org_maxmind->org_by_name($param) if $geoip_org_maxmind; + $asn=$geoip_asn_maxmind->org_by_name($param) if $geoip_asn_maxmind; } - elsif (!$org) + elsif (!$asn) { - $org=$geoip_org_maxmind->org_by_addr($param) if $geoip_org_maxmind; + $asn=$geoip_asn_maxmind->org_by_addr($param) if $geoip_asn_maxmind; } - if ($Debug) { debug(" Plugin $PluginName: GetASNByIp for $param: [$org]",5); } - if ($org) { - $org =~ s/\s/_/g; - $_org_h{$org}++; + if ($Debug) { debug(" Plugin $PluginName: GetASNByIp for $param: [$asn]",5); } + if ($asn) { + $asn =~ s/\s/_/g; + $_asn_h{$asn}++; } else { - $_org_h{'unknown'}++; + $_asn_h{'unknown'}++; } -# if ($timerecord > $_org_l{$city}) { $_org_l{$city}=$timerecord; } +# if ($timerecord > $_asn_l{$city}) { $_asn_l{$city}=$timerecord; } # -----> return; } @@ -380,24 +383,25 @@ sub SectionProcessIp_geoip_asn_maxmind { sub SectionProcessHostname_geoip_asn_maxmind { my $param="$_[0]"; # Param must be an IP # <----- - my $org; - my $org = $TmpLookup{$param}||''; - if (!$org && $type eq 'geoippureperl') + my $asn=0; + if ($geoip_asn_maxmind){$asn = $TmpLookup{$geoip_asn_maxmind->get_ip_address($param)};} + else {$asn = $TmpLookup{$param};} + if (!$asn && $type eq 'geoippureperl') { - $org=$geoip_org_maxmind->org_by_name($param) if $geoip_org_maxmind; + $asn=$geoip_asn_maxmind->org_by_name($param) if $geoip_asn_maxmind; } - elsif (!$org) + elsif (!$asn) { - $org=$geoip_org_maxmind->org_by_name($param) if $geoip_org_maxmind; + $asn=$geoip_asn_maxmind->org_by_name($param) if $geoip_asn_maxmind; } - if ($Debug) { debug(" Plugin $PluginName: GetOrgByHostname for $param: [$org]",5); } - if ($org) { - $org =~ s/\s/_/g; - $_org_h{$org}++; + if ($Debug) { debug(" Plugin $PluginName: GetOrgByHostname for $param: [$asn]",5); } + if ($asn) { + $asn =~ s/\s/_/g; + $_asn_h{$asn}++; } else { - $_org_h{'unknown'}++; + $_asn_h{'unknown'}++; } -# if ($timerecord > $_org_l{$city}) { $_org_l{$city}=$timerecord; } +# if ($timerecord > $_asn_l{$city}) { $_asn_l{$city}=$timerecord; } # -----> return; } @@ -421,7 +425,7 @@ sub SectionReadHistory_geoip_asn_maxmind { $count++; if ($issectiontoload) { $countloaded++; - if ($field[2]) { $_org_h{$field[0]}+=$field[2]; } + if ($field[2]) { $_asn_h{$field[0]}+=$field[2]; } } } $_=; @@ -442,29 +446,29 @@ sub SectionReadHistory_geoip_asn_maxmind { #----------------------------------------------------------------------------- sub SectionWriteHistory_geoip_asn_maxmind { my ($xml,$xmlbb,$xmlbs,$xmlbe,$xmlrb,$xmlrs,$xmlre,$xmleb,$xmlee)=(shift,shift,shift,shift,shift,shift,shift,shift,shift); - if ($Debug) { debug(" Plugin $PluginName: SectionWriteHistory_$PluginName start - ".(scalar keys %_org_h)); } + if ($Debug) { debug(" Plugin $PluginName: SectionWriteHistory_$PluginName start - ".(scalar keys %_asn_h)); } # <----- print HISTORYTMP "\n"; if ($xml) { print HISTORYTMP "
$MAXNBOFSECTIONGIR\n"; } print HISTORYTMP "# Plugin key - Pages - Hits - Bandwidth - Last access\n"; #print HISTORYTMP "# The $MaxNbOfExtra[$extranum] first number of hits are first\n"; $ValueInFile{'plugin_$PluginName'}=tell HISTORYTMP; - print HISTORYTMP "${xmlbb}BEGIN_PLUGIN_$PluginName${xmlbs}".(scalar keys %_org_h)."${xmlbe}\n"; - &BuildKeyList($MAXNBOFSECTIONGIR,1,\%_org_h,\%_org_h); + print HISTORYTMP "${xmlbb}BEGIN_PLUGIN_$PluginName${xmlbs}".(scalar keys %_asn_h)."${xmlbe}\n"; + &BuildKeyList($MAXNBOFSECTIONGIR,1,\%_asn_h,\%_asn_h); my %keysinkeylist=(); foreach (@keylist) { $keysinkeylist{$_}=1; - #my $page=$_org_p{$_}||0; - #my $bytes=$_org_k{$_}||0; - #my $lastaccess=$_org_l{$_}||''; - print HISTORYTMP "${xmlrb}$_${xmlrs}0${xmlrs}", $_org_h{$_}, "${xmlrs}0${xmlrs}0${xmlre}\n"; next; + #my $page=$_asn_p{$_}||0; + #my $bytes=$_asn_k{$_}||0; + #my $lastaccess=$_asn_l{$_}||''; + print HISTORYTMP "${xmlrb}$_${xmlrs}0${xmlrs}", $_asn_h{$_}, "${xmlrs}0${xmlrs}0${xmlre}\n"; next; } - foreach (keys %_org_h) { + foreach (keys %_asn_h) { if ($keysinkeylist{$_}) { next; } - #my $page=$_org_p{$_}||0; - #my $bytes=$_org_k{$_}||0; - #my $lastaccess=$_org_l{$_}||''; - print HISTORYTMP "${xmlrb}$_${xmlrs}0${xmlrs}", $_org_h{$_}, "${xmlrs}0${xmlrs}0${xmlre}\n"; next; + #my $page=$_asn_p{$_}||0; + #my $bytes=$_asn_k{$_}||0; + #my $lastaccess=$_asn_l{$_}||''; + print HISTORYTMP "${xmlrb}$_${xmlrs}0${xmlrs}", $_asn_h{$_}, "${xmlrs}0${xmlrs}0${xmlre}\n"; next; } print HISTORYTMP "${xmleb}END_PLUGIN_$PluginName${xmlee}\n"; # -----> diff --git a/wwwroot/cgi-bin/plugins/geoip_city_maxmind.pm b/wwwroot/cgi-bin/plugins/geoip_city_maxmind.pm index 95e1546c..5c52a13d 100755 --- a/wwwroot/cgi-bin/plugins/geoip_city_maxmind.pm +++ b/wwwroot/cgi-bin/plugins/geoip_city_maxmind.pm @@ -4578,7 +4578,9 @@ sub ShowInfoHost_geoip_city_maxmind { if ($key && $ip==4) { my $country; my $city; - my @res = @{$TmpDomainLookup{$param}}; + my @res; + if ($geoip_city_maxmind){@res = @{$TmpDomainLookup{$geoip_city_maxmind->get_ip_address($param)}};} + else {@res = @{$TmpDomainLookup{$param}};} if (@res){ $country = $res[0]; $city = $res[4]; @@ -4617,7 +4619,9 @@ sub ShowInfoHost_geoip_city_maxmind { if (! $key) { my $country; my $city; - my @res = @{$TmpDomainLookup{$param}}; + my @res; + if ($geoip_city_maxmind){@res = @{$TmpDomainLookup{$geoip_city_maxmind->get_ip_address($param)}};} + else {@res = @{$TmpDomainLookup{$param}};} if (@res){ $country = $res[0]; $city = $res[4]; @@ -4679,9 +4683,10 @@ sub SectionProcessIp_geoip_city_maxmind { if (!$LoadedOverride){&LoadOverrideFile_geoip_city_maxmind();} if ($type eq 'geoippureperl') { my @record = (); - if ($TmpDomainLookup{$param}){ - @record = @{$TmpDomainLookup{$param}}; - }else{ + my @res; + if ($geoip_city_maxmind){@res = @{$TmpDomainLookup{$geoip_city_maxmind->get_ip_address($param)}};} + else {@res = @{$TmpDomainLookup{$param}};} + if (!@res){ @record=$geoip_city_maxmind->get_city_record($param) if $geoip_city_maxmind; } if ($Debug) { debug(" Plugin $PluginName: GetCityByName for $param: [@record]",5); } @@ -4741,9 +4746,10 @@ sub SectionProcessHostname_geoip_city_maxmind { if (!$LoadedOverride){&LoadOverrideFile_geoip_city_maxmind();} if ($type eq 'geoippureperl') { my @record = (); - if ($TmpDomainLookup{$param}){ - @record = @{$TmpDomainLookup{$param}}; - }else{ + my @res; + if ($geoip_city_maxmind){@res = @{$TmpDomainLookup{$geoip_city_maxmind->get_ip_address($param)}};} + else {@res = @{$TmpDomainLookup{$param}};} + if (!@res){ @record=$geoip_city_maxmind->get_city_record($param) if $geoip_city_maxmind; } if ($Debug) { debug(" Plugin $PluginName: GetCityByName for $param: [@record]",5); } diff --git a/wwwroot/cgi-bin/plugins/geoip_isp_maxmind.pm b/wwwroot/cgi-bin/plugins/geoip_isp_maxmind.pm index 69f718aa..b6256eb6 100644 --- a/wwwroot/cgi-bin/plugins/geoip_isp_maxmind.pm +++ b/wwwroot/cgi-bin/plugins/geoip_isp_maxmind.pm @@ -37,6 +37,10 @@ no strict "refs"; # AND THE NAME OF ALL FUNCTIONS THE PLUGIN MANAGE. my $PluginNeedAWStatsVersion="6.2"; my $PluginHooksFunctions="AddHTMLMenuLink AddHTMLGraph ShowInfoHost SectionInitHashArray SectionProcessIp SectionProcessHostname SectionReadHistory SectionWriteHistory"; +my $PluginName="geoip_isp_maxmind"; +my $LoadedOverride=0; +my $OverrideFile=""; +my %TmpDomainLookup; # -----> # <----- @@ -64,8 +68,8 @@ sub Init_geoip_isp_maxmind { # <----- # ENTER HERE CODE TO DO INIT PLUGIN ACTIONS - debug(" Plugin geoip_isp_maxmind: InitParams=$InitParams",1); - my ($mode,$datafile)=split(/\s+/,$InitParams,2); + debug(" Plugin $PluginName: InitParams=$InitParams",1); + my ($mode,$datafile,$override)=split(/\s+/,$InitParams,3); if (! $datafile) { $datafile="GeoIPIsp.dat"; } if ($type eq 'geoippureperl') { # With pureperl with always use GEOIP_STANDARD. @@ -76,12 +80,15 @@ sub Init_geoip_isp_maxmind { if ($mode eq '' || $mode eq 'GEOIP_MEMORY_CACHE') { $mode=Geo::IP::GEOIP_MEMORY_CACHE(); } else { $mode=Geo::IP::GEOIP_STANDARD(); } } - debug(" Plugin geoip_isp_maxmind: GeoIP initialized type=$type mode=$mode",1); + if ($override){$OverrideFile=$override;} + %TmpDomainLookup=(); + debug(" Plugin $PluginName: GeoIP initialized type=$type mode=$mode",1); if ($type eq 'geoippureperl') { $geoip_isp_maxmind = Geo::IP::PurePerl->open($datafile, $mode); } else { $geoip_isp_maxmind = Geo::IP->open($datafile, $mode); } + $LoadedOverride=0; # Fails on some GeoIP version # debug(" Plugin geoip_isp_maxmind: GeoIP initialized database_info=".$geoip_isp_maxmind->database_info()); # -----> @@ -100,11 +107,11 @@ sub AddHTMLMenuLink_geoip_isp_maxmind { my $menulink=$_[2]; my $menutext=$_[3]; # <----- - if ($Debug) { debug(" Plugin geoip_isp_maxmind: AddHTMLMenuLink"); } + if ($Debug) { debug(" Plugin $PluginName: AddHTMLMenuLink"); } if ($categ eq 'who') { - $menu->{'plugin_geoip_isp_maxmind'}=0.6; # Pos - $menulink->{'plugin_geoip_isp_maxmind'}=2; # Type of link - $menutext->{'plugin_geoip_isp_maxmind'}="ISP"; # Text + $menu->{"plugin_$PluginName"}=0.6; # Pos + $menulink->{"plugin_$PluginName"}=2; # Type of link + $menutext->{"plugin_$PluginName"}="ISP"; # Text } # -----> return 0; @@ -126,7 +133,7 @@ sub AddHTMLGraph_geoip_isp_maxmind { my $total_p; my $total_h; my $total_k; my $rest_p; my $rest_h; my $rest_k; - if ($Debug) { debug(" Plugin geoip_isp_maxmind: AddHTMLGraph $categ $menu $menulink $menutext"); } + if ($Debug) { debug(" Plugin $PluginName: AddHTMLGraph $categ $menu $menulink $menutext"); } my $title='ISP'; &tab_head("$title",19,0,'isp'); print "ISP : ".((scalar keys %_isp_h)-($_isp_h{'unknown'}?1:0)).""; @@ -148,9 +155,9 @@ sub AddHTMLGraph_geoip_isp_maxmind { print ""; my $isp=$key; $isp =~ s/_/ /g; print "".ucfirst($isp).""; - if ($ShowISP =~ /P/i) { print "".($_isp_p{$key}?$_isp_p{$key}:" ").""; } + if ($ShowISP =~ /P/i) { print "".($_isp_p{$key}?Format_Number($_isp_p{$key}):" ").""; } if ($ShowISP =~ /P/i) { print "".($_isp_p{$key}?"$p_p %":' ').""; } - if ($ShowISP =~ /H/i) { print "".($_isp_h{$key}?$_isp_h{$key}:" ").""; } + if ($ShowISP =~ /H/i) { print "".($_isp_h{$key}?Format_Number($_isp_h{$key}):" ").""; } if ($ShowISP =~ /H/i) { print "".($_isp_h{$key}?"$p_h %":' ').""; } if ($ShowISP =~ /B/i) { print "".Format_Bytes($_isp_k{$key}).""; } if ($ShowISP =~ /L/i) { print "".($_isp_p{$key}?Format_Date($_isp_l{$key},1):'-').""; } @@ -180,9 +187,9 @@ sub AddHTMLGraph_geoip_isp_maxmind { if ($TotalHits) { $p_h=int($rest_h/$TotalHits*1000)/10; } print ""; print "$Message[2]/$Message[0]"; - if ($ShowISP =~ /P/i) { print "".($rest_p?$rest_p:" ").""; } + if ($ShowISP =~ /P/i) { print "".($rest_p?Format_Number($rest_p):" ").""; } if ($ShowISP =~ /P/i) { print "".($rest_p?"$p_p %":' ').""; } - if ($ShowISP =~ /H/i) { print "".($rest_h?$rest_h:" ").""; } + if ($ShowISP =~ /H/i) { print "".($rest_h?Format_Number($rest_h):" ").""; } if ($ShowISP =~ /H/i) { print "".($rest_h?"$p_h %":' ').""; } if ($ShowISP =~ /B/i) { print "".Format_Bytes($rest_k).""; } if ($ShowISP =~ /L/i) { print " "; } @@ -224,10 +231,12 @@ sub ShowInfoHost_geoip_isp_maxmind { if ($NewLinkParams) { $NewLinkParams="${NewLinkParams}&"; } print ""; - print "GeoIP
ISP
"; + print "GeoIP
ISP
"; print ""; } elsif ($param) { + # try loading our override file if we haven't yet + if (!$LoadedOverride){&LoadOverrideFile_geoip_isp_maxmind();} my $ip=0; my $key; if ($param =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { # IPv4 address @@ -241,17 +250,19 @@ sub ShowInfoHost_geoip_isp_maxmind { print ""; if ($key && $ip==4) { my $isp; - if ($type eq 'geoippureperl') + if ($geoip_isp_maxmind){$isp = $TmpDomainLookup{$geoip_isp_maxmind->get_ip_address($param)};} + else {$isp = $TmpDomainLookup{$param};} + if (!$isp && $type eq 'geoippureperl') { # Function isp_by_addr does not exists in PurePerl but isp_by_name do same $isp=$geoip_isp_maxmind->isp_by_name($param) if $geoip_isp_maxmind; } - else + elsif (!$isp) { # Function isp_by_addr does not exits, so we use org_by_addr $isp=$geoip_isp_maxmind->org_by_addr($param) if $geoip_isp_maxmind; } - if ($Debug) { debug(" Plugin geoip_isp_maxmind: GetIspByIp for $param: [$isp]",5); } + if ($Debug) { debug(" Plugin $PluginName: GetIspByIp for $param: [$isp]",5); } if ($isp) { if (length($isp) <= $MAXLENGTH) { print "$isp"; @@ -267,15 +278,17 @@ sub ShowInfoHost_geoip_isp_maxmind { } if (! $key) { my $isp; - if ($type eq 'geoippureperl') + if ($geoip_isp_maxmind){$isp = $TmpDomainLookup{$geoip_isp_maxmind->get_ip_address($param)};} + else {$isp = $TmpDomainLookup{$param};} + if (!$isp && $type eq 'geoippureperl') { $isp=$geoip_isp_maxmind->isp_by_name($param) if $geoip_isp_maxmind; } - else + elsif (!$isp) { $isp=$geoip_isp_maxmind->isp_by_name($param) if $geoip_isp_maxmind; } - if ($Debug) { debug(" Plugin geoip_isp_maxmind: GetIspByHostname for $param: [$isp]",5); } + if ($Debug) { debug(" Plugin $PluginName: GetIspByHostname for $param: [$isp]",5); } if ($isp) { if (length($isp) <= $MAXLENGTH) { print "$isp"; @@ -303,7 +316,7 @@ sub ShowInfoHost_geoip_isp_maxmind { sub SectionInitHashArray_geoip_isp_maxmind { # my $param="$_[0]"; # <----- - if ($Debug) { debug(" Plugin geoip_isp_maxmind: Init_HashArray"); } + if ($Debug) { debug(" Plugin $PluginName: Init_HashArray"); } %_isp_p = %_isp_h = %_isp_k = %_isp_l =(); # -----> return 0; @@ -317,18 +330,21 @@ sub SectionInitHashArray_geoip_isp_maxmind { sub SectionProcessIp_geoip_isp_maxmind { my $param="$_[0]"; # Param must be an IP # <----- + if (!$LoadedOverride){&LoadOverrideFile_geoip_isp_maxmind();} my $isp; - if ($type eq 'geoippureperl') + if ($geoip_isp_maxmind){$isp = $TmpDomainLookup{$geoip_isp_maxmind->get_ip_address($param)};} + else {$isp = $TmpDomainLookup{$param};} + if (!$isp && $type eq 'geoippureperl') { # Function isp_by_addr does not exists in PurePerl but isp_by_name do same $isp=$geoip_isp_maxmind->isp_by_name($param) if $geoip_isp_maxmind; } - else + elsif(!$isp) { # Function isp_by_addr does not exits, so we use org_by_addr $isp=$geoip_isp_maxmind->org_by_addr($param) if $geoip_isp_maxmind; } - if ($Debug) { debug(" Plugin geoip_isp_maxmind: GetIspByIp for $param: [$isp]",5); } + if ($Debug) { debug(" Plugin $PluginName: GetIspByIp for $param: [$isp]",5); } if ($isp) { $isp =~ s/\s/_/g; $_isp_h{$isp}++; @@ -348,16 +364,19 @@ sub SectionProcessIp_geoip_isp_maxmind { sub SectionProcessHostname_geoip_isp_maxmind { my $param="$_[0]"; # Param must be an IP # <----- + if (!$LoadedOverride){&LoadOverrideFile_geoip_isp_maxmind();} my $isp; - if ($type eq 'geoippureperl') + if ($geoip_isp_maxmind){$isp = $TmpDomainLookup{$geoip_isp_maxmind->get_ip_address($param)};} + else {$isp = $TmpDomainLookup{$param};} + if (!$isp && $type eq 'geoippureperl') { $isp=$geoip_isp_maxmind->isp_by_name($param) if $geoip_isp_maxmind; } - else + elsif(!$isp) { $isp=$geoip_isp_maxmind->isp_by_name($param) if $geoip_isp_maxmind; } - if ($Debug) { debug(" Plugin geoip_isp_maxmind: GetIspByHostname for $param: [$isp]",5); } + if ($Debug) { debug(" Plugin $PluginName: GetIspByHostname for $param: [$isp]",5); } if ($isp) { $isp =~ s/\s/_/g; $_isp_h{$isp}++; @@ -380,7 +399,7 @@ sub SectionReadHistory_geoip_isp_maxmind { my $xmleb=shift; my $countlines=shift; # <----- - if ($Debug) { debug(" Plugin geoip_isp_maxmind: Begin of PLUGIN_geoip_isp_maxmind section"); } + if ($Debug) { debug(" Plugin $PluginName: Begin of PLUGIN_$PluginName section"); } my @field=(); my $count=0;my $countloaded=0; do { @@ -396,9 +415,9 @@ sub SectionReadHistory_geoip_isp_maxmind { @field=split(/\s+/,($xmlold?XMLDecodeFromHisto($_):$_)); $countlines++; } - until ($field[0] eq 'END_PLUGIN_geoip_isp_maxmind' || $field[0] eq "${xmleb}END_PLUGIN_geoip_isp_maxmind" || ! $_); - if ($field[0] ne 'END_PLUGIN_geoip_isp_maxmind' && $field[0] ne "${xmleb}END_PLUGIN_geoip_isp_maxmind") { error("History file is corrupted (End of section PLUGIN not found).\nRestore a recent backup of this file (data for this month will be restored to backup date), remove it (data for month will be lost), or remove the corrupted section in file (data for at least this section will be lost).","","",1); } - if ($Debug) { debug(" Plugin geoip_isp_maxmind: End of PLUGIN_geoip_isp_maxmind section ($count entries, $countloaded loaded)"); } + until ($field[0] eq "END_PLUGIN_$PluginName" || $field[0] eq "${xmleb}END_PLUGIN_$PluginName" || ! $_); + if ($field[0] ne "END_PLUGIN_$PluginName" && $field[0] ne "${xmleb}END_PLUGIN_$PluginName") { error("History file is corrupted (End of section PLUGIN not found).\nRestore a recent backup of this file (data for this month will be restored to backup date), remove it (data for month will be lost), or remove the corrupted section in file (data for at least this section will be lost).","","",1); } + if ($Debug) { debug(" Plugin $PluginName: End of PLUGIN_$PluginName ($count entries, $countloaded loaded)"); } # -----> return 0; } @@ -409,14 +428,14 @@ sub SectionReadHistory_geoip_isp_maxmind { #----------------------------------------------------------------------------- sub SectionWriteHistory_geoip_isp_maxmind { my ($xml,$xmlbb,$xmlbs,$xmlbe,$xmlrb,$xmlrs,$xmlre,$xmleb,$xmlee)=(shift,shift,shift,shift,shift,shift,shift,shift,shift); - if ($Debug) { debug(" Plugin geoip_isp_maxmind: SectionWriteHistory_geoip_isp_maxmind start - ".(scalar keys %_isp_h)); } + if ($Debug) { debug(" Plugin $PluginName: SectionWriteHistory_$PluginName start - ".(scalar keys %_isp_h)); } # <----- print HISTORYTMP "\n"; - if ($xml) { print HISTORYTMP "
$MAXNBOFSECTIONGIR\n"; } + if ($xml) { print HISTORYTMP "
$MAXNBOFSECTIONGIR\n"; } print HISTORYTMP "# Plugin key - Pages - Hits - Bandwidth - Last access\n"; #print HISTORYTMP "# The $MaxNbOfExtra[$extranum] first number of hits are first\n"; - $ValueInFile{'plugin_geoip_isp_maxmind'}=tell HISTORYTMP; - print HISTORYTMP "${xmlbb}BEGIN_PLUGIN_geoip_isp_maxmind${xmlbs}".(scalar keys %_isp_h)."${xmlbe}\n"; + $ValueInFile{"plugin_$PluginName"}=tell HISTORYTMP; + print HISTORYTMP "${xmlbb}BEGIN_PLUGIN_$PluginName${xmlbs}".(scalar keys %_isp_h)."${xmlbe}\n"; &BuildKeyList($MAXNBOFSECTIONGIR,1,\%_isp_h,\%_isp_h); my %keysinkeylist=(); foreach (@keylist) { @@ -433,11 +452,45 @@ sub SectionWriteHistory_geoip_isp_maxmind { #my $lastaccess=$_isp_l{$_}||''; print HISTORYTMP "${xmlrb}$_${xmlrs}0${xmlrs}", $_isp_h{$_}, "${xmlrs}0${xmlrs}0${xmlre}\n"; next; } - print HISTORYTMP "${xmleb}END_PLUGIN_geoip_isp_maxmind${xmlee}\n"; + print HISTORYTMP "${xmleb}END_PLUGIN_$PluginName${xmlee}\n"; # -----> return 0; } - +#----------------------------------------------------------------------------- +# PLUGIN FUNCTION: LoadOverrideFile +# Attempts to load a comma delimited file that will override the GeoIP database +# Useful for Intranet records +# CSV format: IP,"isp" +#----------------------------------------------------------------------------- +sub LoadOverrideFile_geoip_isp_maxmind{ + my $filetoload=""; + if ($OverrideFile){ + if (!open(GEOIPFILE, $OverrideFile)){ + debug("Plugin $PluginName: Unable to open override file: $OverrideFile"); + $LoadedOverride = 1; + return; + } + }else{ + my $conf = (exists(&Get_Config_Name) ? Get_Config_Name() : $SiteConfig); + if ($conf && open(GEOIPFILE,"$DirData/$PluginName.$conf.txt")) { $filetoload="$DirData/$PluginName.$conf.txt"; } + elsif (open(GEOIPFILE,"$DirData/$PluginName.txt")) { $filetoload="$DirData/$PluginName.txt"; } + else { debug("Did not find $PluginName file \"$DirData/$PluginName.txt\": $!"); } + } + # This is the fastest way to load with regexp that I know + while (){ + chomp $_; + s/\r//; + my @record = split(",", $_); + # replace quotes if they were used in the file + foreach (@record){ $_ =~ s/"//g; } + # store in hash + $TmpDomainLookup{$record[0]} = $record[1]; + } + close GEOIPFILE; + $LoadedOverride = 1; + debug(" Plugin $PluginName: Overload file loaded: ".(scalar keys %TmpDomainLookup)." entries found."); + return; +} 1; # Do not remove this line diff --git a/wwwroot/cgi-bin/plugins/geoip_org_maxmind.pm b/wwwroot/cgi-bin/plugins/geoip_org_maxmind.pm index d4b98e94..b09626c7 100644 --- a/wwwroot/cgi-bin/plugins/geoip_org_maxmind.pm +++ b/wwwroot/cgi-bin/plugins/geoip_org_maxmind.pm @@ -37,6 +37,10 @@ no strict "refs"; # AND THE NAME OF ALL FUNCTIONS THE PLUGIN MANAGE. my $PluginNeedAWStatsVersion="6.2"; my $PluginHooksFunctions="AddHTMLMenuLink AddHTMLGraph ShowInfoHost SectionInitHashArray SectionProcessIp SectionProcessHostname SectionReadHistory SectionWriteHistory"; +my $PluginName="geoip_org_maxmind"; +my $LoadedOverride=0; +my $OverrideFile=""; +my %TmpDomainLookup; # -----> # <----- @@ -64,8 +68,8 @@ sub Init_geoip_org_maxmind { # <----- # ENTER HERE CODE TO DO INIT PLUGIN ACTIONS - debug(" Plugin geoip_org_maxmind: InitParams=$InitParams",1); - my ($mode,$datafile)=split(/\s+/,$InitParams,2); + debug(" Plugin $PluginName: InitParams=$InitParams",1); + my ($mode,$datafile,$override)=split(/\s+/,$InitParams,3); if (! $datafile) { $datafile="GeoIPOrg.dat"; } if ($type eq 'geoippureperl') { # With pureperl with always use GEOIP_STANDARD. @@ -76,12 +80,15 @@ sub Init_geoip_org_maxmind { if ($mode eq '' || $mode eq 'GEOIP_MEMORY_CACHE') { $mode=Geo::IP::GEOIP_MEMORY_CACHE(); } else { $mode=Geo::IP::GEOIP_STANDARD(); } } - debug(" Plugin geoip_org_maxmind: GeoIP initialized type=$type mode=$mode",1); + if ($override){$OverrideFile=$override;} + %TmpDomainLookup=(); + debug(" Plugin $PluginName: GeoIP initialized type=$type mode=$mode",1); if ($type eq 'geoippureperl') { $geoip_org_maxmind = Geo::IP::PurePerl->open($datafile, $mode); } else { $geoip_org_maxmind = Geo::IP->open($datafile, $mode); } + $LoadedOverride=0; # Fails on some GeoIP version # debug(" Plugin geoip_org_maxmind: GeoIP initialized database_info=".$geoip_org_maxmind->database_info()); # -----> @@ -100,11 +107,11 @@ sub AddHTMLMenuLink_geoip_org_maxmind { my $menulink=$_[2]; my $menutext=$_[3]; # <----- - if ($Debug) { debug(" Plugin geoip_org_maxmind: AddHTMLMenuLink"); } + if ($Debug) { debug(" Plugin $PluginName: AddHTMLMenuLink"); } if ($categ eq 'who') { - $menu->{'plugin_geoip_org_maxmind'}=0.5; # Pos - $menulink->{'plugin_geoip_org_maxmind'}=2; # Type of link - $menutext->{'plugin_geoip_org_maxmind'}="Organizations"; # Text + $menu->{"plugin_$PluginName"}=0.5; # Pos + $menulink->{"plugin_$PluginName"}=2; # Type of link + $menutext->{"plugin_$PluginName"}="Organizations"; # Text } # -----> return 0; @@ -126,7 +133,7 @@ sub AddHTMLGraph_geoip_org_maxmind { my $total_p; my $total_h; my $total_k; my $rest_p; my $rest_h; my $rest_k; - if ($Debug) { debug(" Plugin geoip_org_maxmind: AddHTMLGraph $categ $menu $menulink $menutext"); } + if ($Debug) { debug(" Plugin $PluginName: AddHTMLGraph $categ $menu $menulink $menutext"); } my $title='Organizations'; &tab_head("$title",19,0,'org'); print "Organizations : ".((scalar keys %_org_h)-($_org_h{'unknown'}?1:0)).""; @@ -148,9 +155,9 @@ sub AddHTMLGraph_geoip_org_maxmind { print ""; my $org=$key; $org =~ s/_/ /g; print "".ucfirst($org).""; - if ($ShowISP =~ /P/i) { print "".($_org_p{$key}?$_org_p{$key}:" ").""; } + if ($ShowISP =~ /P/i) { print "".($_org_p{$key}?Format_Number($_org_p{$key}):" ").""; } if ($ShowISP =~ /P/i) { print "".($_org_p{$key}?"$p_p %":' ').""; } - if ($ShowISP =~ /H/i) { print "".($_org_h{$key}?$_org_h{$key}:" ").""; } + if ($ShowISP =~ /H/i) { print "".($_org_h{$key}?Format_Number($_org_h{$key}):" ").""; } if ($ShowISP =~ /H/i) { print "".($_org_h{$key}?"$p_h %":' ').""; } if ($ShowISP =~ /B/i) { print "".Format_Bytes($_org_k{$key}).""; } if ($ShowISP =~ /L/i) { print "".($_org_p{$key}?Format_Date($_org_l{$key},1):'-').""; } @@ -180,9 +187,9 @@ sub AddHTMLGraph_geoip_org_maxmind { if ($TotalHits) { $p_h=int($rest_h/$TotalHits*1000)/10; } print ""; print "$Message[2]/$Message[0]"; - if ($ShowISP =~ /P/i) { print "".($rest_p?$rest_p:" ").""; } + if ($ShowISP =~ /P/i) { print "".($rest_p?Format_Number($rest_p):" ").""; } if ($ShowISP =~ /P/i) { print "".($rest_p?"$p_p %":' ').""; } - if ($ShowISP =~ /H/i) { print "".($rest_h?$rest_h:" ").""; } + if ($ShowISP =~ /H/i) { print "".($rest_h?Format_Number($rest_h):" ").""; } if ($ShowISP =~ /H/i) { print "".($rest_h?"$p_h %":' ').""; } if ($ShowISP =~ /B/i) { print "".Format_Bytes($rest_k).""; } if ($ShowISP =~ /L/i) { print " "; } @@ -224,10 +231,11 @@ sub ShowInfoHost_geoip_org_maxmind { if ($NewLinkParams) { $NewLinkParams="${NewLinkParams}&"; } print ""; - print "GeoIP
Org
"; + print "GeoIP
Org
"; print ""; } elsif ($param) { + if (!$LoadedOverride){&LoadOverrideFile_geoip_org_maxmind();} my $ip=0; my $key; if ($param =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { # IPv4 address @@ -240,17 +248,19 @@ sub ShowInfoHost_geoip_org_maxmind { } print ""; if ($key && $ip==4) { - my $org; - if ($type eq 'geoippureperl') + my $org; + if ($geoip_org_maxmind){$org = $TmpDomainLookup{$geoip_org_maxmind->get_ip_address($param)};} + else {$org = $TmpDomainLookup{$param};} + if (!$org && $type eq 'geoippureperl') { # Function org_by_addr does not exists in PurePerl but org_by_name do same $org=$geoip_org_maxmind->org_by_name($param) if $geoip_org_maxmind; } - else + elsif(!$org) { $org=$geoip_org_maxmind->org_by_addr($param) if $geoip_org_maxmind; } - if ($Debug) { debug(" Plugin geoip_org_maxmind: GetOrgByIp for $param: [$org]",5); } + if ($Debug) { debug(" Plugin $PluginName: GetOrgByIp for $param: [$org]",5); } if ($org) { if (length($org) <= $MAXLENGTH) { print "$org"; @@ -266,15 +276,17 @@ sub ShowInfoHost_geoip_org_maxmind { } if (! $key) { my $org; - if ($type eq 'geoippureperl') + if ($geoip_org_maxmind){$org = $TmpDomainLookup{$geoip_org_maxmind->get_ip_address($param)};} + else {$org = $TmpDomainLookup{$param};} + if (!$org && $type eq 'geoippureperl') { $org=$geoip_org_maxmind->org_by_name($param) if $geoip_org_maxmind; } - else + elsif(!$org) { $org=$geoip_org_maxmind->org_by_name($param) if $geoip_org_maxmind; } - if ($Debug) { debug(" Plugin geoip_org_maxmind: GetOrgByHostname for $param: [$org]",5); } + if ($Debug) { debug(" Plugin $PluginName: GetOrgByHostname for $param: [$org]",5); } if ($org) { if (length($org) <= $MAXLENGTH) { print "$org"; @@ -302,7 +314,7 @@ sub ShowInfoHost_geoip_org_maxmind { sub SectionInitHashArray_geoip_org_maxmind { # my $param="$_[0]"; # <----- - if ($Debug) { debug(" Plugin geoip_org_maxmind: Init_HashArray"); } + if ($Debug) { debug(" Plugin $PluginName: Init_HashArray"); } %_org_p = %_org_h = %_org_k = %_org_l =(); # -----> return 0; @@ -316,17 +328,20 @@ sub SectionInitHashArray_geoip_org_maxmind { sub SectionProcessIp_geoip_org_maxmind { my $param="$_[0]"; # Param must be an IP # <----- + if (!$LoadedOverride){&LoadOverrideFile_geoip_org_maxmind();} my $org; - if ($type eq 'geoippureperl') + if ($geoip_org_maxmind){$org = $TmpDomainLookup{$geoip_org_maxmind->get_ip_address($param)};} + else {$org = $TmpDomainLookup{$param};} + if (!$org && $type eq 'geoippureperl') { # Function org_by_addr does not exists in PurePerl but org_by_name do same $org=$geoip_org_maxmind->org_by_name($param) if $geoip_org_maxmind; } - else + elsif(!$org) { $org=$geoip_org_maxmind->org_by_addr($param) if $geoip_org_maxmind; } - if ($Debug) { debug(" Plugin geoip_org_maxmind: GetOrgByIp for $param: [$org]",5); } + if ($Debug) { debug(" Plugin $PluginName: GetOrgByIp for $param: [$org]",5); } if ($org) { $org =~ s/\s/_/g; $_org_h{$org}++; @@ -346,16 +361,19 @@ sub SectionProcessIp_geoip_org_maxmind { sub SectionProcessHostname_geoip_org_maxmind { my $param="$_[0]"; # Param must be an IP # <----- + if (!$LoadedOverride){&LoadOverrideFile_geoip_org_maxmind();} my $org; - if ($type eq 'geoippureperl') + if ($geoip_org_maxmind){$org = $TmpDomainLookup{$geoip_org_maxmind->get_ip_address($param)};} + else {$org = $TmpDomainLookup{$param};} + if (!$org && $type eq 'geoippureperl') { $org=$geoip_org_maxmind->org_by_name($param) if $geoip_org_maxmind; } - else + elsif(!$org) { $org=$geoip_org_maxmind->org_by_name($param) if $geoip_org_maxmind; } - if ($Debug) { debug(" Plugin geoip_org_maxmind: GetOrgByHostname for $param: [$org]",5); } + if ($Debug) { debug(" Plugin $PluginName: GetOrgByHostname for $param: [$org]",5); } if ($org) { $org =~ s/\s/_/g; $_org_h{$org}++; @@ -378,7 +396,7 @@ sub SectionReadHistory_geoip_org_maxmind { my $xmleb=shift; my $countlines=shift; # <----- - if ($Debug) { debug(" Plugin geoip_org_maxmind: Begin of PLUGIN_geoip_org_maxmind section"); } + if ($Debug) { debug(" Plugin $PluginName: Begin of PLUGIN_$PluginName section"); } my @field=(); my $count=0;my $countloaded=0; do { @@ -394,9 +412,9 @@ sub SectionReadHistory_geoip_org_maxmind { @field=split(/\s+/,($xmlold?XMLDecodeFromHisto($_):$_)); $countlines++; } - until ($field[0] eq 'END_PLUGIN_geoip_org_maxmind' || $field[0] eq "${xmleb}END_PLUGIN_geoip_org_maxmind" || ! $_); - if ($field[0] ne 'END_PLUGIN_geoip_org_maxmind' && $field[0] ne "${xmleb}END_PLUGIN_geoip_org_maxmind") { error("History file is corrupted (End of section PLUGIN not found).\nRestore a recent backup of this file (data for this month will be restored to backup date), remove it (data for month will be lost), or remove the corrupted section in file (data for at least this section will be lost).","","",1); } - if ($Debug) { debug(" Plugin geoip_org_maxmind: End of PLUGIN_geoip_org_maxmind section ($count entries, $countloaded loaded)"); } + until ($field[0] eq "END_PLUGIN_$PluginName" || $field[0] eq "${xmleb}END_PLUGIN_$PluginName" || ! $_); + if ($field[0] ne "END_PLUGIN_$PluginName" && $field[0] ne "${xmleb}END_PLUGIN_$PluginName") { error("History file is corrupted (End of section PLUGIN not found).\nRestore a recent backup of this file (data for this month will be restored to backup date), remove it (data for month will be lost), or remove the corrupted section in file (data for at least this section will be lost).","","",1); } + if ($Debug) { debug(" Plugin $PluginName: End of PLUGIN_$PluginName section ($count entries, $countloaded loaded)"); } # -----> return 0; } @@ -407,14 +425,14 @@ sub SectionReadHistory_geoip_org_maxmind { #----------------------------------------------------------------------------- sub SectionWriteHistory_geoip_org_maxmind { my ($xml,$xmlbb,$xmlbs,$xmlbe,$xmlrb,$xmlrs,$xmlre,$xmleb,$xmlee)=(shift,shift,shift,shift,shift,shift,shift,shift,shift); - if ($Debug) { debug(" Plugin geoip_org_maxmind: SectionWriteHistory_geoip_org_maxmind start - ".(scalar keys %_org_h)); } + if ($Debug) { debug(" Plugin $PluginName: SectionWriteHistory_$PluginName start - ".(scalar keys %_org_h)); } # <----- print HISTORYTMP "\n"; - if ($xml) { print HISTORYTMP "
$MAXNBOFSECTIONGIR\n"; } + if ($xml) { print HISTORYTMP "
$MAXNBOFSECTIONGIR\n"; } print HISTORYTMP "# Plugin key - Pages - Hits - Bandwidth - Last access\n"; #print HISTORYTMP "# The $MaxNbOfExtra[$extranum] first number of hits are first\n"; $ValueInFile{'plugin_geoip_org_maxmind'}=tell HISTORYTMP; - print HISTORYTMP "${xmlbb}BEGIN_PLUGIN_geoip_org_maxmind${xmlbs}".(scalar keys %_org_h)."${xmlbe}\n"; + print HISTORYTMP "${xmlbb}BEGIN_PLUGIN_$PluginName${xmlbs}".(scalar keys %_org_h)."${xmlbe}\n"; &BuildKeyList($MAXNBOFSECTIONGIR,1,\%_org_h,\%_org_h); my %keysinkeylist=(); foreach (@keylist) { @@ -431,11 +449,45 @@ sub SectionWriteHistory_geoip_org_maxmind { #my $lastaccess=$_org_l{$_}||''; print HISTORYTMP "${xmlrb}$_${xmlrs}0${xmlrs}", $_org_h{$_}, "${xmlrs}0${xmlrs}0${xmlre}\n"; next; } - print HISTORYTMP "${xmleb}END_PLUGIN_geoip_org_maxmind${xmlee}\n"; + print HISTORYTMP "${xmleb}END_PLUGIN_$PluginName${xmlee}\n"; # -----> return 0; } - +#----------------------------------------------------------------------------- +# PLUGIN FUNCTION: LoadOverrideFile +# Attempts to load a comma delimited file that will override the GeoIP database +# Useful for Intranet records +# CSV format: IP,"organization" +#----------------------------------------------------------------------------- +sub LoadOverrideFile_geoip_org_maxmind{ + my $filetoload=""; + if ($OverrideFile){ + if (!open(GEOIPFILE, $OverrideFile)){ + debug("Plugin $PluginName: Unable to open override file: $OverrideFile"); + $LoadedOverride = 1; + return; + } + }else{ + my $conf = (exists(&Get_Config_Name) ? Get_Config_Name() : $SiteConfig); + if ($conf && open(GEOIPFILE,"$DirData/$PluginName.$conf.txt")) { $filetoload="$DirData/$PluginName.$conf.txt"; } + elsif (open(GEOIPFILE,"$DirData/$PluginName.txt")) { $filetoload="$DirData/$PluginName.txt"; } + else { debug("Did not find $PluginName file \"$DirData/$PluginName.txt\": $!"); } + } + # This is the fastest way to load with regexp that I know + while (){ + chomp $_; + s/\r//; + my @record = split(",", $_); + # replace quotes if they were used in the file + foreach (@record){ $_ =~ s/"//g; } + # store in hash + $TmpDomainLookup{$record[0]} = $record[1]; + } + close GEOIPFILE; + $LoadedOverride = 1; + debug(" Plugin $PluginName: Overload file loaded: ".(scalar keys %TmpDomainLookup)." entries found."); + return; +} 1; # Do not remove this line diff --git a/wwwroot/cgi-bin/plugins/geoip_region_maxmind.pm b/wwwroot/cgi-bin/plugins/geoip_region_maxmind.pm index 7d717368..5b1e3be8 100755 --- a/wwwroot/cgi-bin/plugins/geoip_region_maxmind.pm +++ b/wwwroot/cgi-bin/plugins/geoip_region_maxmind.pm @@ -38,6 +38,9 @@ no strict "refs"; # AND THE NAME OF ALL FUNCTIONS THE PLUGIN MANAGE. my $PluginNeedAWStatsVersion="6.5"; my $PluginHooksFunctions="AddHTMLMenuLink AddHTMLGraph ShowInfoHost SectionInitHashArray SectionProcessIp SectionProcessHostname SectionReadHistory SectionWriteHistory"; +my $PluginName="geoip_region_maxmind"; +my $LoadedOverride=0; +my $OverrideFile=""; # -----> # <----- @@ -150,8 +153,8 @@ sub Init_geoip_region_maxmind { # <----- # ENTER HERE CODE TO DO INIT PLUGIN ACTIONS - debug(" Plugin geoip_region_maxmind: InitParams=$InitParams",1); - my ($mode,$datafile)=split(/\s+/,$InitParams,2); + debug(" Plugin $PluginName: InitParams=$InitParams",1); + my ($mode,$datafile,$override)=split(/\s+/,$InitParams,3); if (! $datafile) { $datafile="GeoIPRegion.dat"; } if ($type eq 'geoippureperl') { # With pureperl with always use GEOIP_STANDARD. @@ -162,13 +165,15 @@ sub Init_geoip_region_maxmind { if ($mode eq '' || $mode eq 'GEOIP_MEMORY_CACHE') { $mode=Geo::IP::GEOIP_MEMORY_CACHE(); } else { $mode=Geo::IP::GEOIP_STANDARD(); } } + if ($override){$OverrideFile=$override;} %TmpDomainLookup=(); - debug(" Plugin geoip_region_maxmind: GeoIP initialized type=$type mode=$mode",1); + debug(" Plugin $PluginName: GeoIP initialized type=$type mode=$mode",1); if ($type eq 'geoippureperl') { $geoip_region_maxmind = Geo::IP::PurePerl->open($datafile, $mode); } else { $geoip_region_maxmind = Geo::IP->open($datafile, $mode); } + $LoadedOverride=0; # Fails with some geoip versions # debug(" Plugin geoip_region_maxmind: GeoIP initialized database_info=".$geoip_region_maxmind->database_info()); # -----> @@ -187,11 +192,11 @@ sub AddHTMLMenuLink_geoip_region_maxmind { my $menulink=$_[2]; my $menutext=$_[3]; # <----- - if ($Debug) { debug(" Plugin geoip_region_maxmind: AddHTMLMenuLink"); } + if ($Debug) { debug(" Plugin $PluginName: AddHTMLMenuLink"); } if ($categ eq 'who') { - $menu->{'plugin_geoip_region_maxmind'}=2.1; # Pos - $menulink->{'plugin_geoip_region_maxmind'}=2; # Type of link - $menutext->{'plugin_geoip_region_maxmind'}="Regions"; # Text + $menu->{"plugin_$PluginName"}=2.1; # Pos + $menulink->{"plugin_$PluginName"}=2; # Type of link + $menutext->{"plugin_$PluginName"}="Regions"; # Text } # -----> return 0; @@ -213,7 +218,7 @@ sub AddHTMLGraph_geoip_region_maxmind { my $total_p; my $total_h; my $total_k; my $rest_p; my $rest_h; my $rest_k; - if ($Debug) { debug(" Plugin geoip_region_maxmind: AddHTMLGraph"); } + if ($Debug) { debug(" Plugin $PluginName: AddHTMLGraph"); } my $title='Regions'; &tab_head("$title",19,0,'regions'); print "US and CA Regions : ".((scalar keys %_region_h)-($_region_h{'unknown'}?1:0)).""; @@ -246,9 +251,9 @@ sub AddHTMLGraph_geoip_region_maxmind { if ($TotalPages) { $p_p=int($_region_p{$key}/$TotalPages*1000)/10; } if ($TotalHits) { $p_h=int($_region_h{$key}/$TotalHits*1000)/10; } print "".$region{$countrycode}{uc($regioncode)}." ($regioncode)"; - if ($ShowRegions =~ /P/i) { print "".($_region_p{$key}?$_region_p{$key}:" ").""; } + if ($ShowRegions =~ /P/i) { print "".($_region_p{$key}?Format_Number($_region_p{$key}):" ").""; } if ($ShowRegions =~ /P/i) { print "".($_region_p{$key}?"$p_p %":' ').""; } - if ($ShowRegions =~ /H/i) { print "".($_region_h{$key}?$_region_h{$key}:" ").""; } + if ($ShowRegions =~ /H/i) { print "".($_region_h{$key}?Format_Number($_region_h{$key}):" ").""; } if ($ShowRegions =~ /H/i) { print "".($_region_h{$key}?"$p_h %":' ').""; } if ($ShowRegions =~ /B/i) { print "".Format_Bytes($_region_k{$key}).""; } if ($ShowRegions =~ /L/i) { print "".($_region_p{$key}?Format_Date($_region_l{$key},1):'-').""; } @@ -277,9 +282,9 @@ sub AddHTMLGraph_geoip_region_maxmind { if ($TotalPages) { $p_p=int($rest_p/$TotalPages*1000)/10; } if ($TotalHits) { $p_h=int($rest_h/$TotalHits*1000)/10; } print "$Message[2]/$Message[0]"; - if ($ShowRegions =~ /P/i) { print "".($rest_p?$rest_p:" ").""; } + if ($ShowRegions =~ /P/i) { print "".($rest_p?Format_Number($rest_p):" ").""; } if ($ShowRegions =~ /P/i) { print "".($rest_p?"$p_p %":' ').""; } - if ($ShowRegions =~ /H/i) { print "".($rest_h?$rest_h:" ").""; } + if ($ShowRegions =~ /H/i) { print "".($rest_h?Format_Number($rest_h):" ").""; } if ($ShowRegions =~ /H/i) { print "".($rest_h?"$p_h %":' ').""; } if ($ShowRegions =~ /B/i) { print "".Format_Bytes($rest_k).""; } if ($ShowRegions =~ /L/i) { print " "; } @@ -301,15 +306,16 @@ sub AddHTMLGraph_geoip_region_maxmind { sub GetCountryCodeByAddr_geoip_region_maxmind { my $param="$_[0]"; # <----- + if (!$LoadedOverride){&LoadOverrideFile_geoip_region_maxmind();} my $res=$TmpDomainLookup{$param}||''; if (! $res) { my ($res1,$res2,$countryregion)=(); ($res1,$res2)=$geoip_region_maxmind->region_by_name($param) if $geoip_region_maxmind; $res=lc($res1) || 'unknown'; $TmpDomainLookup{$param}=$res; - if ($Debug) { debug(" Plugin geoip_region_maxmind: GetCountryCodeByAddr for $param: [$res]",5); } + if ($Debug) { debug(" Plugin $PluginName: GetCountryCodeByAddr for $param: [$res]",5); } } - elsif ($Debug) { debug(" Plugin geoip_region_maxmind: GetCountryCodeByAddr for $param: Already resolved to [$res]",5); } + elsif ($Debug) { debug(" Plugin $PluginName: GetCountryCodeByAddr for $param: Already resolved to [$res]",5); } # -----> return $res; } @@ -324,15 +330,16 @@ sub GetCountryCodeByAddr_geoip_region_maxmind { sub GetCountryCodeByName_geoip_region_maxmind { my $param="$_[0]"; # <----- + if (!$LoadedOverride){&LoadOverrideFile_geoip_region_maxmind();} my $res=$TmpDomainLookup{$param}||''; if (! $res) { my ($res1,$res2,$countryregion)=(); ($res1,$res2)=$geoip_region_maxmind->region_by_name($param) if $geoip_region_maxmind; $res=lc($res1) || 'unknown'; $TmpDomainLookup{$param}=$res; - if ($Debug) { debug(" Plugin geoip_region_maxmind: GetCountryCodeByName for $param: [$res]",5); } + if ($Debug) { debug(" Plugin $PluginName: GetCountryCodeByName for $param: [$res]",5); } } - elsif ($Debug) { debug(" Plugin geoip_region_maxmind: GetCountryCodeByName for $param: Already resolved to [$res]",5); } + elsif ($Debug) { debug(" Plugin $PluginName: GetCountryCodeByName for $param: Already resolved to [$res]",5); } # -----> return $res; } @@ -367,10 +374,12 @@ sub ShowInfoHost_geoip_region_maxmind { if ($NewLinkParams) { $NewLinkParams="${NewLinkParams}&"; } print ""; - print "GeoIP
Region
"; + print "GeoIP
Region
"; print ""; } elsif ($param) { + # try loading our override file if we haven't yet + if (!$LoadedOverride){&LoadOverrideFile_geoip_region_maxmind();} my $ip=0; my $key; if ($param =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { # IPv4 address @@ -384,8 +393,16 @@ sub ShowInfoHost_geoip_region_maxmind { print ""; if ($key && $ip==4) { my ($res1,$res2,$countryregion)=(); - ($res1,$res2)=$geoip_region_maxmind->region_by_name($param) if $geoip_region_maxmind; - if ($Debug) { debug(" Plugin geoip_region_maxmind: GetRegionByIp for $param: [${res1}_${res2}]",5); } + my @res; + if ($geoip_region_maxmind){@res = @{$TmpDomainLookup{$geoip_region_maxmind->get_ip_address($param)}};} + else {@res = @{$TmpDomainLookup{$param}};} + if (@res){ + $res1 = $res[0]; + $res2 = $res[1]; + }else{ + ($res1,$res2)=$geoip_region_maxmind->region_by_name($param) if $geoip_region_maxmind; + } + if ($Debug) { debug(" Plugin $PluginName: GetRegionByIp for $param: [${res1}_${res2}]",5); } if (! $PluginsLoaded{'init'}{'geoip'}) { # Show country if ($res1 =~ /\w\w/) { print $DomainsHashIDLib{lc($res1)}||uc($res1); } @@ -410,8 +427,16 @@ sub ShowInfoHost_geoip_region_maxmind { } if (! $key) { my ($res1,$res2,$countryregion)=(); - ($res1,$res2)=$geoip_region_maxmind->region_by_name($param) if $geoip_region_maxmind; - if ($Debug) { debug(" Plugin geoip_region_maxmind: GetRegionByName for $param: [${res1}_${res2}]",5); } + my @res; + if ($geoip_region_maxmind){@res = @{$TmpDomainLookup{$geoip_region_maxmind->get_ip_address($param)}};} + else {@res = @{$TmpDomainLookup{$param}};} + if (@res){ + $res1 = $res[0]; + $res2 = $res[1]; + }else{ + ($res1,$res2)=$geoip_region_maxmind->region_by_name($param) if $geoip_region_maxmind; + } + if ($Debug) { debug(" Plugin $PluginName: GetRegionByName for $param: [${res1}_${res2}]",5); } if (! $PluginsLoaded{'init'}{'geoip'}) { # Show country if ($res1 =~ /\w\w/) { print $DomainsHashIDLib{lc($res1)}||uc($res1); } @@ -448,7 +473,7 @@ sub ShowInfoHost_geoip_region_maxmind { sub SectionInitHashArray_geoip_region_maxmind { # my $param="$_[0]"; # <----- - if ($Debug) { debug(" Plugin geoip_region_maxmind: Init_HashArray"); } + if ($Debug) { debug(" Plugin $PluginName: Init_HashArray"); } %_region_p = %_region_h = %_region_k = %_region_l =(); # -----> return 0; @@ -462,9 +487,18 @@ sub SectionInitHashArray_geoip_region_maxmind { sub SectionProcessIp_geoip_region_maxmind { my $param="$_[0]"; # Param must be an IP # <----- + if (!$LoadedOverride){&LoadOverrideFile_geoip_region_maxmind();} my ($res1,$res2,$countryregion)=(); - ($res1,$res2)=$geoip_region_maxmind->region_by_name($param) if $geoip_region_maxmind; - if ($Debug) { debug(" Plugin geoip_region_maxmind: GetRegionByIp for $param: [${res1}_${res2}]",5); } + my @res; + if ($geoip_region_maxmind){@res = @{$TmpDomainLookup{$geoip_region_maxmind->get_ip_address($param)}};} + else {@res = @{$TmpDomainLookup{$param}};} + if (@res){ + $res1 = $res[0]; + $res2 = $res[1]; + }else{ + ($res1,$res2)=$geoip_region_maxmind->region_by_name($param) if $geoip_region_maxmind; + } + if ($Debug) { debug(" Plugin $PluginName: GetRegionByIp for $param: [${res1}_${res2}]",5); } if ($res2 =~ /\w\w/) { $countryregion=lc("${res1}_${res2}"); } else { $countryregion='unknown'; } # if ($PageBool) { $_region_p{$countryregion}++; } @@ -481,9 +515,18 @@ sub SectionProcessIp_geoip_region_maxmind { sub SectionProcessHostname_geoip_region_maxmind { my $param="$_[0]"; # Param must be a hostname # <----- + if (!$LoadedOverride){&LoadOverrideFile_geoip_region_maxmind();} my ($res1,$res2,$countryregion)=(); - ($res1,$res2)=$geoip_region_maxmind->region_by_name($param) if $geoip_region_maxmind; - if ($Debug) { debug(" Plugin geoip_region_maxmind: GetRegionByName for $param: [${res1}_${res2}]",5); } + my @res; + if ($geoip_region_maxmind){@res = @{$TmpDomainLookup{$geoip_region_maxmind->get_ip_address($param)}};} + else {@res = @{$TmpDomainLookup{$param}};} + if (@res){ + $res1 = $res[0]; + $res2 = $res[1]; + }else{ + ($res1,$res2)=$geoip_region_maxmind->region_by_name($param) if $geoip_region_maxmind; + } + if ($Debug) { debug(" Plugin $PluginName: GetRegionByName for $param: [${res1}_${res2}]",5); } if ($res2 =~ /\w\w/) { $countryregion=lc("${res1}_${res2}"); } else { $countryregion='unknown'; } # if ($PageBool) { $_region_p{$countryregion}++; } @@ -504,7 +547,7 @@ sub SectionReadHistory_geoip_region_maxmind { my $xmleb=shift; my $countlines=shift; # <----- - if ($Debug) { debug(" Plugin geoip_region_maxmind: Begin of PLUGIN_geoip_region_maxmind section"); } + if ($Debug) { debug(" Plugin $PluginName: Begin of PLUGIN_$PluginName"); } my @field=(); my $count=0;my $countloaded=0; do { @@ -520,9 +563,9 @@ sub SectionReadHistory_geoip_region_maxmind { @field=split(/\s+/,($xmlold?XMLDecodeFromHisto($_):$_)); $countlines++; } - until ($field[0] eq 'END_PLUGIN_geoip_region_maxmind' || $field[0] eq "${xmleb}END_PLUGIN_geoip_region_maxmind" || ! $_); - if ($field[0] ne 'END_PLUGIN_geoip_region_maxmind' && $field[0] ne "${xmleb}END_PLUGIN_geoip_region_maxmind") { error("History file is corrupted (End of section PLUGIN not found).\nRestore a recent backup of this file (data for this month will be restored to backup date), remove it (data for month will be lost), or remove the corrupted section in file (data for at least this section will be lost).","","",1); } - if ($Debug) { debug(" Plugin geoip_region_maxmind: End of PLUGIN_geoip_region_maxmind section ($count entries, $countloaded loaded)"); } + until ($field[0] eq "END_PLUGIN_$PluginName" || $field[0] eq "${xmleb}END_PLUGIN_$PluginName" || ! $_); + if ($field[0] ne "END_PLUGIN_$PluginName" && $field[0] ne "${xmleb}END_PLUGIN_$PluginName") { error("History file is corrupted (End of section PLUGIN not found).\nRestore a recent backup of this file (data for this month will be restored to backup date), remove it (data for month will be lost), or remove the corrupted section in file (data for at least this section will be lost).","","",1); } + if ($Debug) { debug(" Plugin $PluginName: End of PLUGIN_$PluginName section ($count entries, $countloaded loaded)"); } # -----> return 0; } @@ -533,14 +576,14 @@ sub SectionReadHistory_geoip_region_maxmind { #----------------------------------------------------------------------------- sub SectionWriteHistory_geoip_region_maxmind { my ($xml,$xmlbb,$xmlbs,$xmlbe,$xmlrb,$xmlrs,$xmlre,$xmleb,$xmlee)=(shift,shift,shift,shift,shift,shift,shift,shift,shift); - if ($Debug) { debug(" Plugin geoip_region_maxmind: SectionWriteHistory_geoip_region_maxmind start - ".(scalar keys %_region_h)); } + if ($Debug) { debug(" Plugin $PluginName: SectionWriteHistory_$PluginName start - ".(scalar keys %_region_h)); } # <----- print HISTORYTMP "\n"; - if ($xml) { print HISTORYTMP "
$MAXNBOFSECTIONGIR\n"; } + if ($xml) { print HISTORYTMP "
$MAXNBOFSECTIONGIR\n"; } print HISTORYTMP "# Plugin key - Pages - Hits - Bandwidth - Last access\n"; #print HISTORYTMP "# The $MaxNbOfExtra[$extranum] first number of hits are first\n"; - $ValueInFile{'plugin_geoip_region_maxmind'}=tell HISTORYTMP; - print HISTORYTMP "${xmlbb}BEGIN_PLUGIN_geoip_region_maxmind${xmlbs}".(scalar keys %_region_h)."${xmlbe}\n"; + $ValueInFile{"plugin_$PluginName"}=tell HISTORYTMP; + print HISTORYTMP "${xmlbb}BEGIN_PLUGIN_$PluginName${xmlbs}".(scalar keys %_region_h)."${xmlbe}\n"; &BuildKeyList($MAXNBOFSECTIONGIR,1,\%_region_h,\%_region_h); my %keysinkeylist=(); foreach (@keylist) { @@ -557,11 +600,49 @@ sub SectionWriteHistory_geoip_region_maxmind { #my $lastaccess=$_region_l{$_}||''; print HISTORYTMP "${xmlrb}$_${xmlrs}0${xmlrs}", $_region_h{$_}, "${xmlrs}0${xmlrs}0${xmlre}\n"; next; } - print HISTORYTMP "${xmleb}END_PLUGIN_geoip_region_maxmind${xmlee}\n"; + print HISTORYTMP "${xmleb}END_PLUGIN_$PluginName${xmlee}\n"; # -----> return 0; } - +#----------------------------------------------------------------------------- +# PLUGIN FUNCTION: LoadOverrideFile +# Attempts to load a comma delimited file that will override the GeoIP database +# Useful for Intranet records +# CSV format: IP,2-char Country code, region +#----------------------------------------------------------------------------- +sub LoadOverrideFile_geoip_region_maxmind{ + my $filetoload=""; + if ($OverrideFile){ + if (!open(GEOIPFILE, $OverrideFile)){ + debug("Plugin $PluginName: Unable to open override file: $OverrideFile"); + $LoadedOverride = 1; + return; + } + }else{ + my $conf = (exists(&Get_Config_Name) ? Get_Config_Name() : $SiteConfig); + if ($conf && open(GEOIPFILE,"$DirData/$PluginName.$conf.txt")) { $filetoload="$DirData/$PluginName.$conf.txt"; } + elsif (open(GEOIPFILE,"$DirData/$PluginName.txt")) { $filetoload="$DirData/$PluginName.txt"; } + else { debug("Did not find $PluginName file \"$DirData/$PluginName.txt\": $!"); } + } + # This is the fastest way to load with regexp that I know + while (){ + chomp $_; + s/\r//; + my @record = split(",", $_); + # replace quotes if they were used in the file + foreach (@record){ $_ =~ s/"//g; } + # now we need to copy our file values in the order to mimic the lookup values + my @res = (); + $res[0] = $record[1]; # country code + $res[1] = $record[2]; # region code + # store in hash + $TmpDomainLookup{$record[0]} = [@res]; + } + close GEOIPFILE; + $LoadedOverride = 1; + debug(" Plugin $PluginName: Overload file loaded: ".(scalar keys %TmpDomainLookup)." entries found."); + return; +} 1; # Do not remove this line -- 2.47.3