]>
git.ipfire.org Git - people/teissler/ipfire-2.x.git/blob - config/updxlrator/download
3 # This code is distributed under the terms of the GPL
5 # (c) 2006-2008 marco.s - http://update-accelerator.advproxy.net
7 # $Id: download,v 2.0 2008/04/06 00:00:00 marco.s Exp $
13 my $swroot='/var/ipfire';
14 my $apphome="$swroot/updatexlrator";
15 my $logfile="/var/log/updatexlrator/download.log";
17 my $repository='/var/updatecache';
21 my $wget="$apphome/bin/wget";
22 my $useragent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
23 my %xlratorsettings=();
33 my $vendorid = @ARGV[0]; if ($vendorid eq '') { exit; }
34 my $sourceurl = @ARGV[1]; if ($sourceurl eq '') { exit; }
35 my $cfmirror = @ARGV[2]; if ($cfmirror eq '') { exit; }
39 $sourceurl =~ s@\
%2f@
/@ig;
40 $sourceurl =~ s@\
%7e@
~@ig;
41 $updatefile = substr($sourceurl,rindex($sourceurl,"/")+1);
42 $updatefile =~ s@\
%20@
@ig;
43 $vendorid =~ tr/A-Z/a-z/;
45 unless (-d
"$repository/download/$vendorid")
47 system("mkdir $repository/download/$vendorid");
48 system("$apphome/bin/setperms download/$vendorid");
51 exit if (-e
"$repository/download/$vendorid/$updatefile");
53 system("touch $repository/download/$vendorid/$updatefile");
57 $uuid = `echo $updatefile | md5sum`;
59 $uuid = `echo $sourceurl | md5sum`;
62 $uuid =~ s/[^0-9a-f]//g;
63 $uuid =~ s/([a-f\d]{8})([a-f\d]{4})([a-f\d]{4})([a-f\d]{4})([a-f\d]{12})/$1-$2-$3-$4-$5/;
65 if (-e
"$swroot/updatexlrator/settings")
67 &readhash
("$swroot/updatexlrator/settings", \
%xlratorsettings);
68 if ($xlratorsettings{'ENABLE_LOG'} eq 'on') { $logging=1; };
69 if ($xlratorsettings{'MAX_DOWNLOAD_RATE'} ne '') { $dlrate = "--limit-rate=" . int($xlratorsettings{'MAX_DOWNLOAD_RATE'} / 8) . "k" };
72 if (-e
"$swroot/proxy/settings") { &readhash
("$swroot/proxy/settings", \
%proxysettings); }
74 if (-e
"$swroot/proxy/advanced/settings")
77 &readhash
("$swroot/proxy/advanced/settings", \
%proxysettings);
80 if (($proxysettings{'UPSTREAM_PROXY'}) && ($proxysettings{'UPSTREAM_USER'}))
82 $login = "--proxy-user=\"$proxysettings{'UPSTREAM_USER'}\"";
83 if ($proxysettings{'UPSTREAM_PASSWORD'})
85 $login .= " --proxy-password=\"$proxysettings{'UPSTREAM_PASSWORD'}\"";
89 if ($xlratorsettings{'MAX_DOWNLOAD_RATE'} eq '')
91 &writelog
("Retrieving file for local cache: $updatefile");
93 &writelog
("Retrieving file for local cache at max. " . $xlratorsettings{'MAX_DOWNLOAD_RATE'} . " kBit/s: $updatefile");
96 $ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'};
97 @http_header = `$wget $login --user-agent="$useragent" --spider -S $sourceurl 2>&1`;
98 $ENV{'http_proxy'} = '';
100 foreach (@http_header)
103 if (/^\s*Content-Length:\s/) { s/[^0-9]//g; &writelog
("Remote file size: $_ bytes"); }
104 if (/^\s*Last-Modified:\s/) { s/^\s*Last-Modified:\s//; $remote_mtime = HTTP
::Date
::str2time
($_); &writelog
("Remote file date: $_"); }
107 $ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'};
108 unlink "$repository/download/$vendorid/$updatefile";
109 $_ = system("$wget $login $dlrate --user-agent=\"$useragent\" -q -nc -P $repository/download/$vendorid $sourceurl");
110 $ENV{'http_proxy'} = '';
114 &writelog
("Download finished with result code: OK");
116 unless (-d
"$repository/$vendorid")
118 system("mkdir $repository/$vendorid");
119 system("$apphome/bin/setperms $vendorid");
122 unless (-d
"$repository/$vendorid/$uuid")
124 system("mkdir $repository/$vendorid/$uuid");
125 system("$apphome/bin/setperms $vendorid/$uuid");
128 &writelog
("Moving file to the cache directory: $vendorid/$uuid");
129 $updatefile =~ s@ @
\\ @ig;
130 system("mv $repository/download/$vendorid/$updatefile $repository/$vendorid/$uuid");
131 # Workaround for IPCop's mv bug:
132 utime time,$remote_mtime,"$repository/$vendorid/$uuid/$updatefile";
133 $updatefile =~ s@
\\ @
@ig;
135 &setcachestatus
("$repository/$vendorid/$uuid/source.url",$sourceurl);
136 &setcachestatus
("$repository/$vendorid/$uuid/status",$sfOk);
137 &setcachestatus
("$repository/$vendorid/$uuid/checkup.log",time);
138 &setcachestatus
("$repository/$vendorid/$uuid/access.log",time);
140 system("$apphome/bin/setperms $vendorid/$uuid/*");
143 &writelog
("Download finished with result code: ERROR");
144 if (-e
"$repository/download/$vendorid/$updatefile") { unlink ("$repository/download/$vendorid/$updatefile"); }
148 # -------------------------------------------------------------------
152 my $filename = $_[0];
158 open(FILE
, $filename) or die "Unable to read file $filename";
162 ($var, $val) = split /=/, $_, 2;
168 # Untaint variables read from hash
169 $var =~ /([A-Za-z0-9_-]*)/; $var = $1;
170 $val =~ /([\w\W]*)/; $val = $1;
171 $hash->{$var} = $val;
178 # -------------------------------------------------------------------
184 open (LOGFILE
,">>$logfile");
185 my @now = localtime(time);
186 printf LOGFILE
"%04d-%02d-%02d %02d:%02d:%02d [%d] %s\n",$now[5]+1900,$now[4]+1,$now[3],$now[2],$now[1],$now[0],$$,$_[0];
191 # -------------------------------------------------------------------
195 open (FILE
,">$_[0]");
196 print FILE
"$_[1]\n";
200 # -------------------------------------------------------------------