#!/usr/bin/perl
#NFC use FCGI;
use strict;
use File::stat;
use CGI qw/:standard/;
use Geo::IP;
use geo_cc;
my $cc;
my %env;
my $count = 0;
print header();
my %archreleases = ( "2#i386"=>1,
"3#i386"=>1,"3#x86_64"=>1,"3#ia64"=>1,"3#s390"=>1,"3#s390x"=>1,"3#SRPMS"=>1,
"4#i386"=>1,"4#x86_64"=>1,"4#ia64"=>1,"4#s390"=>1,"4#s390x"=>1,"4#alpha"=>1,"4#SRPMS"=>1,
"5#i386"=>1,"5#x86_64"=>1,);
my @arches3 = ("i386","x86_64","ia64","s390","s390x","SRPMS");
my @arches2 = ("i386");
my %arches = ("i386"=>1,"x86_64"=>1,"ia64"=>1,"s390"=>1,"s390x"=>1,"alpha"=>1,"SRPMS"=>1);
my %releases = ("5"=>"5","5.0"=>"5","5.1"=>"5","5.2"=>"5","5.3"=>"5.3","5.4"=>"5.4","4"=>"4","4.1"=>"4","4.2"=>"4","4.3"=>"4","4.4"=>"4","4.5"=>"4","4.6"=>"4","4.7"=>"4","4.8"=>"4.8","3"=>"3","3.1"=>"3","3.2"=>"3","3.3"=>"3","3.4"=>"3","3.5"=>"3","3.6"=>"3","3.7"=>"3","3.8"=>"3","3.9"=>"3.9","2"=>"2","2.1"=>"2");
my $release = 4;
my $debug = 0;
my $dirmode = 0; #list dirs rather than files
my $my_input = $ENV{QUERY_STRING};
my %params;
my ($pair,$field,$value);
my @fv_pairs = split /\&/ , $my_input;
foreach $pair (@fv_pairs) {
if($pair=~m/([^=]+)=(.*)/) {
$field = $1;
$value = $2;
$value =~ s/\+/ /g;
$value =~ s/%([\dA-Fa-f]{2})/pack("C", hex($1))/eg;
$params{$field}=$value;
}
}
if($debug >= 2){
my ($k,$val);
print "Params:
";
while (($k, $val) = each(%params))
{
print"$k - $val
\n";
}
}
my $country;
if ( exists $params{"debug"} )
{
if ($params{"debug"} > 0 && $params{"debug"} < 4)
{
$debug = $params{"debug"};
}
}
if( !exists $params{"arch"})
{
print "Must specify arch as parameter\n";
exit;
}
if (!exists $params{"file"})
{
$dirmode = 1;
}
if( exists $params{"release"} )
{
if (exists($releases{$params{"release"}}))
{
$release = $releases{$params{"release"}};
}
else {
print $params{"release"}," hasnt been released \n";
exit;
}
if ($debug >= 1) { print "Release: $params{'release'} -> $release
"; };
}
if (!$arches{$params{"arch"}})
{
print "Bad arch - not in list - ";
my $k;
foreach $k ( keys(%arches)) {
print"$k ";
}
print "\n";
exit;
}
my $arch = $params{"arch"};
my $isofile;
if (exists( $params{"file"} ))
{
$isofile = $params{"file"};
}
else
{
$isofile = "iso";
}
#$arch =~ tr/A-Z/a-z/;
my $ipaddr="not set";
my $ccuse = '';
if( exists $params{"cc"} ) # use the param one instead of the real one
{
$country = $params{"cc"};
if($country eq 'c1' || $country eq 'c2' || $country eq 'c3' || $country eq 'c4')
{
$ccuse = $country;
}
if ($debug)
{
print "using cc from para - $country\n
";
}
}
else
{
if (exists $params{"ip"} ) # use ip as a para and push through geoip
{
$ipaddr = $params{"ip"};
}
else
{
$ipaddr = $ENV{'REMOTE_ADDR'};
}
my $gi = Geo::IP->open("/var/lib/GeoIP/GeoIP.dat",GEOIP_MEMORY_CACHE);
$country = $gi->country_code_by_addr($ipaddr);
if ($debug) { print "ip $ipaddr - country $country\n
" };
}
# look up country in cclist to get centos mirrorlist ...
if($ccuse eq '')
{
$country =~ tr/a-z/A-Z/;
$cc = cc_hash($country);
$cc =~ tr/A-Z/a-z/;
}
else
{
$cc = $ccuse;
}
my @cclist = get_cc_list($cc);
my $ttt = time();
if ($debug) { print "using cc-$cc arch-$arch cclist-@cclist\n
"};
my $done = 0;
my $open = 0;
my $listed = 0;
my $dvdlisted = 0;
my $ccnum = 1;
if($isofile =~ /4\.2/) { $release = '4.2' };
my $isodir = "/home/centos-mirrorlists/$release/isos/".$arch."/".$isofile;
my $dvdisodir = "/home/centos-mirrorlists/$release/isos/".$arch."/dvdiso";
my $cc;
my $outstr = "";
my $dvdoutstr = "";
my $checkstr = "";
my $filestr = "";
my $near =0;
foreach $cc (@cclist){
my $file = $isodir."\.".$cc;
if ($debug) { print "File - $file\n";};
if (open(ISO,"< $file"))
{
if (!$outstr){
my $sb = stat($file);
$filestr = sprintf "Mirrors Verified %s UTC
",scalar localtime $sb->mtime;
}
if($debug){$outstr .= "country - $country, cclist - @cclist, ccnum - $ccnum, near - $near, cc - $cc\n
";};
if (@cclist > 1)
{
if($ccnum == 1 && ( $cc =~ /$country/i ))
{
$outstr .= "Actual Country -
";
}
}
if($ccnum==1 && $cc !~ /$country/i)
{
$outstr .= "
Nearby Countries -
";
$near =1;
}
if(($ccnum == 2) && $near == 0 )
{
$outstr .= "
Nearby Countries -
";
$near =1;
}
while ()
{
$outstr .= "$_<\/a>
";
++$listed;
}
close(ISO);
++$ccnum;
}
}
if ($isofile eq "iso"){ # - we are doing a dir - so list dvd isos as well
$ccnum = 1;
$near = 0;
foreach $cc (@cclist){
my $file = $dvdisodir."\.".$cc;
if ($debug) { print "File - $file\n";};
if (open(ISO,"< $file"))
{
if($debug){$dvdoutstr .= "country - $country, cclist - @cclist, ccnum - $ccnum, near - $near, cc - $cc\n
";};
if (@cclist > 1)
{
if($ccnum == 1 && ( $cc =~ /$country/i ))
{
$dvdoutstr .= "Actual Country -
";
}
}
if($ccnum==1 && $cc !~ /$country/i)
{
$dvdoutstr .= "
Nearby Countries -
";
$near =1;
}
if(($ccnum == 2) && $near == 0 )
{
$dvdoutstr .= "
Nearby Countries -
";
$near =1;
}
while ()
{
$dvdoutstr .= "$_<\/a>
";
++$dvdlisted;
}
close(ISO);
++$ccnum;
}
}
}
if ($ccnum == 1) #no country dvd file - use c1 (later use c2 c3)
{
my $file = $dvdisodir."\.c1";
if (open(ISO,"< $file"))
{
if($debug){$dvdoutstr .= "country - $country, cclist - @cclist, ccnum - $ccnum, near - $near, cc - $cc\n
";};
while ()
{
$dvdoutstr .= "$_<\/a>
";
++$dvdlisted;
}
close(ISO);
}
}
if (open(HEAD,")
{
s:\"HEADER\.images:\"/HEADER\.images:g;
print;
}
}
if ($listed)
{
print "
In order to conserve the limited bandwidth available .iso images are not downloadable from mirror.centos.org
";
if ($dvdlisted) {
print "The following mirrors should have dvd .iso images available for download:
";
print $dvdoutstr;
print "
";
}
print "The following mirrors should have the cd .iso images available:
";
print $outstr;
print "
";
print $filestr;
print "
Or use bittorrent to download the .torrent files
provided.
Bittorrent is a peer to peer download system described at http://bittorrent.com/
An rpm for CentOS-3 or CentOS-4 is available at
http://dag.wieers.com/packages/bittorrent/.
(Use the latest el3 version
for CentOS-3, or the latest el4 version for CentOS-4)
";
}
else
{
#should redirect to notonmirror.html
print "
";
if (open(NOT,")
{
print;
}
}
else
{
print ".iso files are not downloadable directly from our main mirrors
\n";
}
print "
";
}
if (open(FOOT,")
{
print;
}
}
exit;