#
# This code is distributed under the terms of the GPL
#
-# (c) 2006 marco.s
+# (c) 2006-2008 marco.s - http://update-accelerator.advproxy.net
#
-# $Id: checkup,v 1.0 2006/08/30 00:00:00 marco.s Exp $
+# $Id: checkup,v 2.0 2007/06/17 00:00:00 marco.s Exp $
#
use strict;
-use IO::Socket;
use HTTP::Date;
my $swroot='/var/ipfire';
-my $scriptpath=substr($0,0,rindex($0,"/"));
my $apphome="/var/ipfire/updatexlrator";
my $logfile="/var/log/updatexlrator/checkup.log";
-my $debug=(-e "$apphome/debug");
-my $repository='/srv/web/ipfire/html/updatecache';
+my $repository='/var/updatecache';
+my $useragent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
+my %proxysettings=();
my %xlratorsettings=();
my $download=0;
my $updatefile='';
my $sourceurl='';
-my $remote_size=0;
-my $local_size=0;
-my $remote_mtime=0;
-my $local_mtime=0;
+my @sources=();
my @updatelist=();
-my @metadata=();
-
-@updatelist = <$repository/*>;
+my $logging=0;
my $sfUnknown = "0";
my $sfOk = "1";
my $sfOutdated = "2";
+my $sfNoSource = "3";
if (-e "$swroot/updatexlrator/settings")
{
&readhash("$swroot/updatexlrator/settings", \%xlratorsettings);
if ($xlratorsettings{'FULL_AUTOSYNC'} eq 'on') { $download=1; };
+ if ($xlratorsettings{'ENABLE_LOG'} eq 'on') { $logging=1; };
}
-foreach (@updatelist)
-{
- if (!-d $_)
- {
- $updatefile = substr($_,rindex($_,"/")+1);
- if (-e "$repository/metadata/$updatefile")
- {
- open (FILE,"$repository/metadata/$updatefile");
- @metadata = <FILE>;
- close FILE;
- chomp(@metadata);
- $sourceurl = $metadata[0];
+if (-e "$swroot/proxy/settings") { &readhash("$swroot/proxy/settings", \%proxysettings); }
- $remote_size = &getdownloadsize($sourceurl);
- $local_size = (-s "$repository/$updatefile");
+if (-e "$swroot/proxy/advanced/settings")
+{
+ %proxysettings=();
+ &readhash("$swroot/proxy/advanced/settings", \%proxysettings);
+}
- $remote_mtime = &getlastmod($sourceurl);
- $local_mtime = &getmtime("$repository/$updatefile");
+foreach (<$repository/*>)
+{
+ if (-d $_)
+ {
+ unless (/^$repository\/download$/) { push(@sources,$_); }
+ }
+}
- if ($remote_mtime eq 0)
- {
- $metadata[2] = $sfUnknown;
- if ($debug) { &writelog("$updatefile - WARNING: Source not found"); }
- print "$updatefile - WARNING: Source not found\n";
- }
- elsif (($local_mtime eq $remote_mtime) && ($local_size == $remote_size))
- {
- $metadata[2] = $sfOk;
- $metadata[3] = time;
- if ($debug) { &writelog("$updatefile"); }
- print "$updatefile\n";
- }
- else
- {
- $metadata[2] = $sfOutdated;
- $metadata[3] = time;
- if ($debug) { &writelog("$updatefile - WARNING: Out of date"); }
- print "$updatefile - WARNING: Out of date\n";
- if ($download)
- {
- if ($debug)
- {
- 1 while $remote_size =~ s/^(-?\d+)(\d{3})/$1.$2/;
- print "Please wait, retrieving file ($remote_size Byte) from source ...";
- `$scriptpath/../bin/wget -nd -nv -O $repository/$updatefile $sourceurl >>$logfile 2>&1`;
- print "\n";
- } else
- {
- `$scriptpath/../bin/wget -nd -nv -O $repository/$updatefile $sourceurl 2>&1`;
- }
- $local_mtime = &getmtime("$repository/$updatefile");
- if ($local_mtime eq $remote_mtime) { $metadata[2] = $sfOk; }
- }
- }
- open (FILE,">$repository/metadata/$updatefile");
- foreach (@metadata) { print FILE "$_\n"; }
+foreach (@sources)
+{
+ @updatelist=<$_/*>;
+ foreach(@updatelist)
+ {
+ if (-e "$_/source.url")
+ {
+ open (FILE,"$_/source.url");
+ $sourceurl=<FILE>;
close FILE;
+ chomp($sourceurl);
+ $updatefile = substr($sourceurl,rindex($sourceurl,'/')+1,length($sourceurl));
+ &checksource($_);
}
}
}
# -------------------------------------------------------------------
-sub getlastmod
+sub writelog
{
- my $remote=0;
- my @response=();
- my $lastmoddate=0;
-
- my $url = $_[0];
-
- $url =~ s@^(.*)://([^/]*)@@;
-
- my $proto = $1;
- my $fqhn = $2;
-
- if ((-e "$swroot/red/active") && ($proto eq 'http'))
- {
- $remote = IO::Socket::INET->new(
- PeerHost => $fqhn,
- PeerPort => 'http(80)',
- Timeout => 1
- );
- }
-
- if ($remote)
+ print "$_[0]\n";
+ if ($logging)
{
- print $remote "HEAD $url HTTP/1.0\n";
- print $remote "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\n";
- print $remote "Host: $fqhn\n";
- print $remote "Accept: */*\n\n";
- while (<$remote>) { push(@response,$_); }
- close $remote;
- if ($response[0] =~ /^HTTP\/\d+\.\d+\s\d+\sOK\s*$/)
- {
- foreach (@response)
- {
- if (/^Last-Modified: /i)
- {
- s/^Last-Modified: //i;
- $lastmoddate=HTTP::Date::str2time($_);
- }
- }
- }
+ open (LOGFILE,">>$logfile");
+ my @now = localtime(time);
+ printf LOGFILE "%04d-%02d-%02d %02d:%02d:%02d %s\n",$now[5]+1900,$now[4]+1,$now[3],$now[2],$now[1],$now[0],$_[0];
+ close LOGFILE;
}
- return $lastmoddate;
}
# -------------------------------------------------------------------
-sub getdownloadsize
+sub setcachestatus
{
- my $remote=0;
- my @response=();
- my $contentlength=0;
+ open (FILE,">$_[0]");
+ print FILE "$_[1]\n";
+ close FILE;
+}
- my $url = $_[0];
+# -------------------------------------------------------------------
- $url =~ s@^(.*)://([^/]*)@@;
+sub checksource
+{
+ my @http_header=();
+ my $http_result='000 n/a';
+ my $returncode=0;
+ my $localfile='';
+ my $remote_size=0;
+ my $remote_mtime=0;
+ my $login='';
+ my $url='';
+ my $cdir=$_[0];
+
+ open (FILE,"$cdir/source.url");
+ $url=<FILE>;
+ close FILE;
+ chomp($url);
+
+ $localfile = $cdir . substr($url,rindex($url,'/'),length($url));
+
+ if (($proxysettings{'UPSTREAM_PROXY'}) && ($proxysettings{'UPSTREAM_USER'}))
+ {
+ $login = "--proxy-user=\"$proxysettings{'UPSTREAM_USER'}\"";
+ if ($proxysettings{'UPSTREAM_PASSWORD'})
+ {
+ $login .= " --proxy-password=\"$proxysettings{'UPSTREAM_PASSWORD'}\"";
+ }
+ }
- my $proto = $1;
- my $fqhn = $2;
+ $ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'};
+ @http_header = `wget $login --user-agent="$useragent" --spider -S $url 2>&1`;
+ $ENV{'http_proxy'} = '';
+ &writelog(@http_header);
- if ((-e "$swroot/red/active") && ($proto eq 'http'))
+ foreach (@http_header)
{
- $remote = IO::Socket::INET->new(
- PeerHost => $fqhn,
- PeerPort => 'http(80)',
- Timeout => 1
- );
+ chomp;
+ if (/^\s*HTTP\/\d+\.\d+\s\d+\s+\w+/) { $http_result = $_; $http_result =~ s/^\s*HTTP\/\d+\.\d+\s+//; }
+ if (/^\s*Content-Length:\s/) { $remote_size = $_; $remote_size =~ s/[^0-9]//g; }
+ if (/^\s*Last-Modified:\s/) { $remote_mtime = $_; $remote_mtime =~ s/^\s*Last-Modified:\s//; $remote_mtime = HTTP::Date::str2time($remote_mtime) }
}
- if ($remote)
+ &writelog($localfile);
+ &writelog("HTTP result: $http_result");
+ &writelog("Source size: $remote_size");
+ &writelog("Cached size: " . (-s $localfile));
+ &writelog("Source time: $remote_mtime");
+ &writelog("Cached time: " . getmtime($localfile));
+
+ if ($http_result =~ /\d+\s+OK$/)
{
- print $remote "HEAD $url HTTP/1.0\n";
- print $remote "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\n";
- print $remote "Host: $fqhn\n";
- print $remote "Accept: */*\n\n";
- while (<$remote>) { push(@response,$_); }
- close $remote;
- if ($response[0] =~ /^HTTP\/\d+\.\d+\s\d+\sOK\s*$/)
+ if (($remote_size == -s $localfile) && ($remote_mtime == getmtime($localfile)))
{
- foreach (@response)
+ &writelog("Status: Ok");
+ &setcachestatus("$cdir/status",$sfOk);
+ } else {
+ &writelog("Status: Outdated");
+ &setcachestatus("$cdir/status",$sfOutdated);
+ if ($download)
{
- if (/^Content-Length: /i)
- {
- s/^Content-Length: //i;
- $contentlength=int($_);
- }
+ &writelog("Retrieving file from source: $remote_size bytes");
+ $_ = system("wget $login --user-agent=\"$useragent\" -q -O $localfile $url");
+ &writelog("Download finished with code: $_");
+ if ($_ == 0) { &setcachestatus("$cdir/status",$sfOk); }
}
}
+ } else {
+ $_ = $http_result;
+ s/\D+//;
+ if ($_ eq '404')
+ {
+ &writelog("Status: No source");
+ &setcachestatus("$cdir/status",$sfNoSource);
+ } else {
+ &writelog("Status: Error");
+ &setcachestatus("$cdir/status",$sfUnknown);
+ }
}
- return $contentlength;
-}
-
-# -------------------------------------------------------------------
-
-sub writelog
-{
- open (LOGFILE,">>$logfile");
- my @now = localtime(time);
- printf LOGFILE "%02d:%02d:%02d %s\n",$now[2],$now[1],$now[0],$_[0];
- close LOGFILE;
+
+ &setcachestatus("$cdir/checkup.log",time);
}
# -------------------------------------------------------------------