]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - config/updxlrator/download
de356c4be37486b85867de08a80c0c52338a319f
[people/pmueller/ipfire-2.x.git] / config / updxlrator / download
1 #!/usr/bin/perl
2 #
3 # This code is distributed under the terms of the GPL
4 #
5 # (c) 2006-2008 marco.s - http://update-accelerator.advproxy.net
6 #
7 # $Id: download,v 2.0 2008/04/06 00:00:00 marco.s Exp $
8 #
9
10 use strict;
11 use HTTP::Date;
12
13 my $swroot='/var/ipfire';
14 my $apphome="$swroot/updatexlrator";
15 my $logfile="/var/log/updatexlrator/download.log";
16 my $logging=0;
17 my $repository='/var/updatecache';
18 my $login='';
19 my $dlrate='';
20 my $uuid='';
21 my $useragent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
22 my %xlratorsettings=();
23 my %proxysettings=();
24 my @http_header=();
25 my $remote_mtime=0;
26 my $updatefile='';
27 my $unique=0;
28 my $mirror=1;
29
30 my $sfOk="1";
31
32 my $vendorid = @ARGV[0]; if ($vendorid eq '') { exit; }
33 my $sourceurl = @ARGV[1]; if ($sourceurl eq '') { exit; }
34 my $cfmirror = @ARGV[2]; if ($cfmirror eq '') { exit; }
35
36 umask(0002);
37
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/;
43
44 unless (-d "$repository/download/$vendorid")
45 {
46 system("mkdir -p $repository/download/$vendorid");
47 system("chown -R nobody.squid $repository/download/$vendorid");
48 system("chmod 775 $repository/download/$vendorid");
49 }
50
51 exit if (-e "$repository/download/$vendorid/$updatefile");
52
53 system("touch $repository/download/$vendorid/$updatefile");
54
55 if ($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
65 if (-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
72 if (-e "$swroot/proxy/settings") { &readhash("$swroot/proxy/settings", \%proxysettings); }
73
74 if (-e "$swroot/proxy/advanced/settings")
75 {
76 %proxysettings=();
77 &readhash("$swroot/proxy/advanced/settings", \%proxysettings);
78 }
79
80 if (($proxysettings{'UPSTREAM_PROXY'}) && ($proxysettings{'UPSTREAM_USER'}))
81 {
82 $login = "--proxy-user=\"$proxysettings{'UPSTREAM_USER'}\"";
83 if ($proxysettings{'UPSTREAM_PASSWORD'})
84 {
85 $login .= " --proxy-password=\"$proxysettings{'UPSTREAM_PASSWORD'}\"";
86 }
87 }
88
89 if ($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 }
95
96 $ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'};
97 @http_header = `wget $login --user-agent="$useragent" --spider -S $sourceurl 2>&1`;
98 $ENV{'http_proxy'} = '';
99
100 foreach (@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 }
106
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'} = '';
111
112 if ($_ == 0)
113 {
114 &writelog("Download finished with result code: OK");
115
116 unless (-d "$repository/$vendorid")
117 {
118 system("mkdir -p $repository/$vendorid");
119 system("chown -R nobody.squid $repository/$vendorid");
120 system("chmod 775 $repository/$vendorid");
121 }
122
123 unless (-d "$repository/$vendorid/$uuid")
124 {
125 system("mkdir -p $repository/$vendorid/$uuid");
126 system("chown -R nobody.squid $repository/$vendorid/$uuid");
127 system("chmod 775 $repository/$vendorid/$uuid");
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
142 system("chown -R nobody.squid $repository/$vendorid/$uuid/*");
143 system("chmod 775 $repository/$vendorid/$uuid/*");
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
153 sub 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 # -------------------------------------------------------------------
182
183 sub writelog
184 {
185 if ($logging)
186 {
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
196 sub setcachestatus
197 {
198 open (FILE,">$_[0]");
199 print FILE "$_[1]\n";
200 close FILE;
201 }
202
203 # -------------------------------------------------------------------