]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - config/updxlrator/download
Change IMQ NAT behavior
[people/pmueller/ipfire-2.x.git] / config / updxlrator / download
CommitLineData
46c01c09
MT
1#!/usr/bin/perl
2#
3# This code is distributed under the terms of the GPL
4#
0a638396 5# (c) 2006-2008 marco.s - http://update-accelerator.advproxy.net
46c01c09 6#
0a638396 7# $Id: download,v 2.0 2008/04/06 00:00:00 marco.s Exp $
46c01c09
MT
8#
9
10use strict;
0a638396 11use HTTP::Date;
46c01c09 12
0a638396
CS
13my $swroot='/var/ipfire';
14my $apphome="$swroot/updatexlrator";
46c01c09 15my $logfile="/var/log/updatexlrator/download.log";
0a638396
CS
16my $logging=0;
17my $repository='/var/updatecache';
18my $login='';
19my $dlrate='';
20my $uuid='';
0a638396
CS
21my $useragent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
22my %xlratorsettings=();
23my %proxysettings=();
24my @http_header=();
25my $remote_mtime=0;
26my $updatefile='';
27my $unique=0;
28my $mirror=1;
46c01c09
MT
29
30my $sfOk="1";
31
0a638396
CS
32my $vendorid = @ARGV[0]; if ($vendorid eq '') { exit; }
33my $sourceurl = @ARGV[1]; if ($sourceurl eq '') { exit; }
34my $cfmirror = @ARGV[2]; if ($cfmirror eq '') { exit; }
46c01c09 35
0a638396 36umask(0002);
46c01c09 37
0a638396
CS
38$sourceurl =~ s@\%2f@/@ig;
39$sourceurl =~ s@\%7e@~@ig;
40$updatefile = substr($sourceurl,rindex($sourceurl,"/")+1);
41$updatefile =~ s@\%20@ @ig;
42$vendorid =~ tr/A-Z/a-z/;
46c01c09 43
0a638396 44unless (-d "$repository/download/$vendorid")
46c01c09 45{
c6919127
CS
46 system("mkdir -p $repository/download/$vendorid");
47 system("chown -R nobody.squid $repository/download/$vendorid");
48 system("chmod 775 $repository/download/$vendorid");
0a638396
CS
49}
50
51exit if (-e "$repository/download/$vendorid/$updatefile");
52
53system("touch $repository/download/$vendorid/$updatefile");
54
55if ($cfmirror)
56{
57 $uuid = `echo $updatefile | md5sum`;
58} else {
59 $uuid = `echo $sourceurl | md5sum`;
60}
61
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/;
64
65if (-e "$swroot/updatexlrator/settings")
66{
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" };
70}
71
72if (-e "$swroot/proxy/settings") { &readhash("$swroot/proxy/settings", \%proxysettings); }
73
74if (-e "$swroot/proxy/advanced/settings")
75{
76 %proxysettings=();
77 &readhash("$swroot/proxy/advanced/settings", \%proxysettings);
78}
79
80if (($proxysettings{'UPSTREAM_PROXY'}) && ($proxysettings{'UPSTREAM_USER'}))
46c01c09 81{
0a638396
CS
82 $login = "--proxy-user=\"$proxysettings{'UPSTREAM_USER'}\"";
83 if ($proxysettings{'UPSTREAM_PASSWORD'})
84 {
85 $login .= " --proxy-password=\"$proxysettings{'UPSTREAM_PASSWORD'}\"";
86 }
46c01c09
MT
87}
88
0a638396
CS
89if ($xlratorsettings{'MAX_DOWNLOAD_RATE'} eq '')
90{
91 &writelog("Retrieving file for local cache: $updatefile");
92} else {
93 &writelog("Retrieving file for local cache at max. " . $xlratorsettings{'MAX_DOWNLOAD_RATE'} . " kBit/s: $updatefile");
94}
46c01c09 95
0a638396 96$ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'};
c6919127 97@http_header = `wget $login --user-agent="$useragent" --spider -S $sourceurl 2>&1`;
0a638396 98$ENV{'http_proxy'} = '';
46c01c09 99
0a638396
CS
100foreach (@http_header)
101{
102 chomp;
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: $_"); }
105}
46c01c09 106
0a638396
CS
107$ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'};
108unlink "$repository/download/$vendorid/$updatefile";
c6919127 109$_ = system("wget $login $dlrate --user-agent=\"$useragent\" -q -nc -P $repository/download/$vendorid $sourceurl");
0a638396 110$ENV{'http_proxy'} = '';
46c01c09 111
0a638396
CS
112if ($_ == 0)
113{
114 &writelog("Download finished with result code: OK");
115
116 unless (-d "$repository/$vendorid")
117 {
c6919127
CS
118 system("mkdir -p $repository/$vendorid");
119 system("chown -R nobody.squid $repository/$vendorid");
120 system("chmod 775 $repository/$vendorid");
0a638396
CS
121 }
122
123 unless (-d "$repository/$vendorid/$uuid")
124 {
c6919127
CS
125 system("mkdir -p $repository/$vendorid/$uuid");
126 system("chown -R nobody.squid $repository/$vendorid/$uuid");
127 system("chmod 775 $repository/$vendorid/$uuid");
0a638396
CS
128 }
129
130 &writelog("Moving file to the cache directory: $vendorid/$uuid");
131 $updatefile =~ s@ @\\ @ig;
132 system("mv $repository/download/$vendorid/$updatefile $repository/$vendorid/$uuid");
133 # Workaround for IPCop's mv bug:
134 utime time,$remote_mtime,"$repository/$vendorid/$uuid/$updatefile";
135 $updatefile =~ s@\\ @ @ig;
136
137 &setcachestatus("$repository/$vendorid/$uuid/source.url",$sourceurl);
138 &setcachestatus("$repository/$vendorid/$uuid/status",$sfOk);
139 &setcachestatus("$repository/$vendorid/$uuid/checkup.log",time);
140 &setcachestatus("$repository/$vendorid/$uuid/access.log",time);
141
c6919127
CS
142 system("chown -R nobody.squid $repository/$vendorid/$uuid/*");
143 system("chmod 775 $repository/$vendorid/$uuid/*");
0a638396
CS
144
145} else {
146 &writelog("Download finished with result code: ERROR");
147 if (-e "$repository/download/$vendorid/$updatefile") { unlink ("$repository/download/$vendorid/$updatefile"); }
148}
149
150
151# -------------------------------------------------------------------
152
153sub readhash
154{
155 my $filename = $_[0];
156 my $hash = $_[1];
157 my ($var, $val);
158
159 if (-e $filename)
160 {
161 open(FILE, $filename) or die "Unable to read file $filename";
162 while (<FILE>)
163 {
164 chop;
165 ($var, $val) = split /=/, $_, 2;
166 if ($var)
167 {
168 $val =~ s/^\'//g;
169 $val =~ s/\'$//g;
170
171 # Untaint variables read from hash
172 $var =~ /([A-Za-z0-9_-]*)/; $var = $1;
173 $val =~ /([\w\W]*)/; $val = $1;
174 $hash->{$var} = $val;
175 }
176 }
177 close FILE;
178 }
179}
180
181# -------------------------------------------------------------------
46c01c09
MT
182
183sub writelog
184{
0a638396 185 if ($logging)
c6919127 186 {
0a638396
CS
187 open (LOGFILE,">>$logfile");
188 my @now = localtime(time);
189 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];
190 close LOGFILE;
191 }
192}
193
194# -------------------------------------------------------------------
195
196sub setcachestatus
197{
198 open (FILE,">$_[0]");
199 print FILE "$_[1]\n";
200 close FILE;
46c01c09
MT
201}
202
0a638396 203# -------------------------------------------------------------------