#!/usr/bin/perl use Net::SenderBase; use Getopt::Std; ## Command to options getopts('cgrf:i:l:m:d:', \%options); sub HELP_MESSAGE { print <>$file") or warn "Failed to open $LogFile. Message was $txt\n"; print LOG localtime() . " " . $txt . "\n"; close(LOG); } } my $MyCountry = $options{'m'} if defined $options{'m'}; my $HatedCountrys = $options{'f'} if defined $options{'f'}; my $RemoteIP = $options{'i'} if defined $options{'i'}; my $LogFile = $options{'l'} if defined $options{'l'}; my $File = $options{'d'} if defined $options{'d'}; my $From = $options{'b'} if defined $options{'b'}; my $Exit = 0; my $RemoteCountry = ""; my $FromCache = "No"; if (defined $options{'c'}) { use Cache::FileCache; my $cache_options_ref = { namespace => 'CountryCode', default_expires_in => '1 day', auto_purge_interval => '1 hour'}; my $cache; } # Covert all to upper $MyCountry =~ tr/a-z/A-Z/; $HatedCountrys =~ tr/a-z/A-Z/; @HatedCountries = split(/:/, $HatedCountrys); if (defined $options{'c'}) { $cache = Cache::FileCache->new($cache_options_ref); } ## Display help if -h is given if (defined $options{'h'}) { HELP_MESSAGE(); exit(0); } if ($MyCountry eq "" && $HatedCountrys eq "") { print "You forgot to define your country (-m \"US\") OR define a list of hated countried (-f \"JP:BR:TW\")\n"; LogIt("Error : You forgot to define your country (-m \"US\") OR define a list of hated countried (-f \"JP:BR:TW\")",$LogFile); HELP_MESSAGE(); exit(0); } if (defined $options{'d'} && defined $options{'i'}) { print "You can only use -d OR -i but not both.\n;"; LogIt("Error : You can only use -d OR -i but not both.",$LogFile); HELP_MESSAGE(); exit(0); } if (defined $options{'d'}) { open(FileData, $File); my $line = ; chomp($line); close FileData; my @InfoData = split(/;/, $line); my $ClientDomain = @InfoData[0]; $RemoteIP = $ClientDomain; } if (defined $options{'c'}) { $RemoteCountry = $cache->get( $RemoteIP ); } if ($RemoteCountry eq "") { my $query = Net::SenderBase::Query->new(Transport => 'dns', Address => $RemoteIP, Host => 'test.senderbase.org',); my $results = $query->results(); $RemoteCountry = $results->ip_country; } else { $FromCache = "Yes"; } if ($RemoteCountry eq "") { LogIt("Error : Error getting info about : $RemoteIP",$LogFile); exit($Exit); } if (defined $options{'c'} && $FromCache ne "Yes") { $cache->set( $RemoteIP, $RemoteCountry); } if ($RemoteCountry ne $MyCountry || grep (/$RemoteCountry/, @HatedCountries)) { $Exit = 3; } if (defined $options{'r'} && $Exit == 3) { $Txt = "Blocking"; } elsif (!defined $options{'r'} && $Exit == 3) { $Txt = "Would Have Blocked"; $Exit = 0; } elsif ($LogFile) { $Txt = "Allowing"; } LogIt("$Txt : $RemoteIP : $RemoteCountry : $FromCache",$LogFile); exit($Exit);