]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - config/updxlrator/updxlrator
Fixed warning in updxlrator
[people/pmueller/ipfire-2.x.git] / config / updxlrator / updxlrator
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: updxlrator,v 2.0 2008/04/06 00:00:00 marco.s Exp $
46c01c09
MT
8#
9
10use strict;
0a638396 11use HTTP::Date;
46c01c09
MT
12
13$|=1;
14
15my $swroot="/var/ipfire";
381f2e71 16my $updcachedir="/var/updatecache";
0a638396
CS
17my $apphome="/var/ipfire/updatexlrator";
18my $logfile="/var/log/updatexlrator/cache.log";
0a638396
CS
19my $debug=(-e "$apphome/debug");
20my $http_port='81';
46c01c09 21my %netsettings=();
0a638396 22my %proxysettings=();
46c01c09 23my %xlratorsettings=();
46c01c09
MT
24my $logging=0;
25my $passive_mode=0;
26my $maxusage=0;
27my $nice='';
28my @tmp=();
46c01c09 29my $request='';
0a638396
CS
30my $xlrator_url=0;
31my $source_url='';
46c01c09
MT
32my $hostaddr='';
33my $username='';
34my $method='';
0a638396
CS
35my $unique = 0;
36my $mirror = 1;
46c01c09
MT
37
38readhash("${swroot}/ethernet/settings", \%netsettings);
39
40if (-e "$swroot/updatexlrator/settings")
41{
42 &readhash("$swroot/updatexlrator/settings", \%xlratorsettings);
43 if ($xlratorsettings{'ENABLE_LOG'} eq 'on') { $logging=1; };
44 if ($xlratorsettings{'PASSIVE_MODE'} eq 'on') { $passive_mode=1; };
45 $maxusage=$xlratorsettings{'MAX_DISK_USAGE'};
646750c5 46 if ($xlratorsettings{'LOW_DOWNLOAD_PRIORITY'} eq 'on') { $nice='/bin/nice --adjustment=15 '; };
46c01c09
MT
47}
48if (!$maxusage) { $maxusage=75; };
49
50
51while (<>) {
52
53 $request=$_;
46c01c09
MT
54
55 @tmp=split(/ /,$request);
56 chomp(@tmp);
57
0a638396
CS
58 $source_url = $tmp[0];
59 $hostaddr = $tmp[1]; while ($hostaddr =~ /.*\/$/) { chop $hostaddr; }
60 $username = $tmp[2]; if ($username eq '') { $username='-'; };
61 $method = $tmp[3];
62
63 $xlrator_url = $source_url;
46c01c09
MT
64
65 if (($method eq 'GET') || ($method eq 'HEAD'))
66 {
67
68 # -----------------------------------------------------------
69 # Section: Windows Update / Windows Downloads
70 # -----------------------------------------------------------
71
72 if (
0a638396
CS
73 (($source_url =~ m@^http://[^/]*\.microsoft\.com/.*\.(exe|psf|msi|msp|cab)$@i) ||
74 ($source_url =~ m@^http://[^/]*\.windowsupdate\.com/.*\.(exe|psf|msi|msp|cab)$@i))
75 && ($source_url !~ m@^http://[^/]*\.microsoft\.com/.*(/autoupd|selfupdate/).*\.cab@i)
76 && ($source_url !~ m@\&@)
46c01c09
MT
77 )
78 {
0a638396 79 $xlrator_url = &check_cache($source_url,$hostaddr,$username,"Microsoft",$unique);
46c01c09
MT
80 }
81
82 # -----------------------------------------------------------
83 # Section: Adobe Downloads
84 # -----------------------------------------------------------
85
0a638396
CS
86 if (
87 ($source_url =~ m@^http://(ar)?download\.adobe\.com/.*\.(exe|msi|bin|dmg|idx|gz)$@i) ||
88 ($source_url =~ m@^http://swupdl\.adobe\.com/updates/.*\.(exe|msi|bin|dmg|idx|gz|[a-z][a-z]_[A-Z][A-Z])$@i) ||
89 ($source_url =~ m@^http://swupmf\.adobe\.com/manifest/.*\.upd$@i)
90 )
91 {
92 $xlrator_url = &check_cache($source_url,$hostaddr,$username,"Adobe",$unique);
93 }
94
95 # -----------------------------------------------------------
96 # Section: Linux Downloads
97 # -----------------------------------------------------------
98
99 if ($source_url =~ m@^[h|f]t?tp://[^?]+\.(deb|rpm)$@i)
46c01c09 100 {
0a638396
CS
101 $xlrator_url = &check_cache($source_url,$hostaddr,$username,"Linux",$mirror);
102 }
103
104 # -----------------------------------------------------------
105 # Section: Trend Micro Downloads
106 # -----------------------------------------------------------
107
108 if (
109 ($source_url =~ m@^http://[^/]*\.trendmicro\.com/activeupdate/.*@i) &&
110 ($source_url !~ m@.*/tmnotify\.dat$@i) &&
111 ($source_url !~ m@.*/ini_xml\.zip$@i) &&
112 ($source_url !~ m@.*/server\.ini$@i)
113 )
114 {
115 $xlrator_url = &check_cache($source_url,$hostaddr,$username,"TrendMicro",$mirror);
46c01c09
MT
116 }
117
118 # -----------------------------------------------------------
119 # Section: Symantec Downloads
120 # -----------------------------------------------------------
121
0a638396 122 if ($source_url =~ m@^[h|f]tt?p://[^/]*\.symantec(liveupdate)?\.com/.*\.(exe|zip|vdb|xdb)$@i)
46c01c09 123 {
0a638396 124 $xlrator_url = &check_cache($source_url,$hostaddr,$username,"Symantec",$unique);
46c01c09 125 }
0a638396 126
5b2a12ff 127 # -----------------------------------------------------------
0a638396 128 # Section: Apple Downloads
46c01c09 129 # -----------------------------------------------------------
0a638396
CS
130
131 if (
132 (($source_url =~ m@^http://swcdn\.apple\.com/content/downloads/.*\.(tar)$@i) ||
133 ($source_url =~ m@^http://appldnld\.apple\.com\.edgesuite\.net/.*\.(exe|dmg)$@i) ||
134 ($source_url =~ m@^http://.*\.g.akamai.net/.*/3093/1/.*\.(tar|pkg|dmg|exe)$@i))
135 )
5b2a12ff 136 {
0a638396 137 $xlrator_url = &check_cache($source_url,$hostaddr,$username,"Apple",$unique);
5b2a12ff 138 }
0a638396
CS
139
140 # -----------------------------------------------------------
141 # Section: Avast Downloads
142 # -----------------------------------------------------------
143
144 if ($source_url =~ m@^http://download[\d]+\.avast\.com/.*\.(exe|vpu)$@i)
145 {
146 $xlrator_url = &check_cache($source_url,$hostaddr,$username,"Avast",$mirror);
186e3d2c 147 }
0a638396
CS
148
149 # -----------------------------------------------------------
186e3d2c 150
0a638396
CS
151 # -----------------------------------------------------------
152 # Section: Avira Downloads
153 # -----------------------------------------------------------
154
155 if ($source_url =~ m@^http://dl[0-9]\.avgate\.net/.*\.(htm|html|gz)$@i)
156 {
157 $xlrator_url = &check_cache($source_url,$hostaddr,$username,"Avira",$mirror);
2dfb38e6 158 }
4ae86474
AF
159
160 # -----------------------------------------------------------
0a638396 161 # Section: IPFire Downloads
4ae86474
AF
162 # -----------------------------------------------------------
163
0a638396 164 if ($source_url =~ m@^[f|h]t?tp://.*\.(ipfire)$@i)
4ae86474 165 {
0a638396 166 $xlrator_url = &check_cache($source_url,$hostaddr,$username,"IPFire",$mirror);
4ae86474
AF
167 }
168
0a638396 169 }
2dfb38e6 170
0a638396
CS
171 # -----------------------------------------------------------
172 # Section: AVG Downloads
173 # -----------------------------------------------------------
46c01c09 174
0a638396
CS
175 if ($source_url =~ m@^http://[^/]*\.(grisoft|avg)\.com/.*\.(bin)$@i)
176 {
177 $xlrator_url = &check_cache($source_url,$hostaddr,$username,"AVG",$mirror);
46c01c09
MT
178 }
179
0a638396 180 $request="$xlrator_url $hostaddr $username $method\n";
46c01c09
MT
181
182 print $request;
183}
184
185# -------------------------------------------------------------------
186
187sub readhash
188{
189 my $filename = $_[0];
190 my $hash = $_[1];
191 my ($var, $val);
192
193 if (-e $filename)
194 {
195 open(FILE, $filename) or die "Unable to read file $filename";
196 while (<FILE>)
197 {
198 chop;
199 ($var, $val) = split /=/, $_, 2;
200 if ($var)
201 {
202 $val =~ s/^\'//g;
203 $val =~ s/\'$//g;
204
205 # Untaint variables read from hash
206 $var =~ /([A-Za-z0-9_-]*)/; $var = $1;
207 $val =~ /([\w\W]*)/; $val = $1;
208 $hash->{$var} = $val;
209 }
210 }
211 close FILE;
212 }
213}
214
215# -------------------------------------------------------------------
216
217sub writelog
218{
0a638396
CS
219 if ($logging)
220 {
221 open(LOGFILE,">>$logfile");
222 print LOGFILE time." $_[0] $_[1] $_[2] $_[3] $_[4]\n";
223 close(LOGFILE);
224 }
46c01c09
MT
225}
226
227# -------------------------------------------------------------------
228
0a638396 229sub debuglog
46c01c09 230{
0a638396 231 if ($debug)
46c01c09 232 {
0a638396
CS
233 open(LOGFILE,">>/var/log/updatexlrator/debug.log");
234 my @now = localtime(time);
235 printf LOGFILE "%04d-%02d-%02d %02d:%02d:%02d [%d] [%s] %s\n",$now[5]+1900,$now[4]+1,$now[3],$now[2],$now[1],$now[0],$$,"updxlrator",$_[0];
236 close(LOGFILE);
46c01c09 237 }
0a638396
CS
238}
239
240# -------------------------------------------------------------------
241
242sub setcachestatus
243{
244 open (FILE,">>$_[0]");
245 print FILE "$_[1]\n";
246 close FILE;
247}
248
249# -------------------------------------------------------------------
250
251sub diskfree
252{
253 open(DF,"/bin/df --block-size=1 $_[0]|");
254 my @dfdata = <DF>;
46c01c09 255 close DF;
0a638396
CS
256 shift(@dfdata);
257 chomp(@dfdata);
258 my $dfstr = join(' ',@dfdata);
259 my ($device,$size,$used,$free,$percent,$mount) = split(' ',$dfstr);
260 if ($free =~ m/^(\d+)$/)
261 {
262 return $free;
263 }
46c01c09
MT
264}
265
266# -------------------------------------------------------------------
267
268sub diskusage
269{
270 open(DF,"/bin/df $_[0]|");
0a638396
CS
271 my @dfdata = <DF>;
272 close DF;
273 shift(@dfdata);
274 chomp(@dfdata);
275 my $dfstr = join(' ',@dfdata);
276 my ($device,$size,$used,$free,$percent,$mount) = split(' ',$dfstr);
277 if ($percent =~ m/^(\d+)%$/)
46c01c09 278 {
0a638396
CS
279 $percent =~ s/%$//;
280 return $percent;
46c01c09 281 }
46c01c09
MT
282}
283
284# -------------------------------------------------------------------
285
0a638396 286sub getmtime
46c01c09 287{
0a638396 288 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($_[0]);
46c01c09 289
0a638396
CS
290 return $mtime;
291}
46c01c09 292
0a638396 293# -------------------------------------------------------------------
46c01c09 294
0a638396
CS
295sub check_cache
296{
297 my $updsource="UPDCACHE";
298 my $updfile='';
299 my $cacheurl='';
300 my $vendorid='';
301 my $uuid='';
302 my @http_header=();
303 my $remote_size=0;
304 my $remote_mtime=0;
305 my $login='';
306 my $useragent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
307
308 my $sourceurl=$_[0];
309 my $cfmirror=$_[4];
310
311 $sourceurl =~ s@\%2f@/@ig;
312 $updfile = substr($sourceurl,rindex($sourceurl,"/")+1);
313 $updfile =~ s@\%20@ @ig;
314
315 if ($cfmirror)
46c01c09 316 {
0a638396
CS
317 $uuid = `echo $updfile | md5sum`;
318 } else {
319 $uuid = `echo $sourceurl | md5sum`;
46c01c09
MT
320 }
321
0a638396
CS
322 $uuid =~ s/[^0-9a-f]//g;
323 $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/;
324
325 $vendorid = $_[3];
326 $vendorid =~ tr/A-Z/a-z/;
327
328 &debuglog("Processing URL $sourceurl");
329 &debuglog("Vendor ID is $vendorid");
330 &debuglog("UUID is $uuid");
331
332 if (($proxysettings{'UPSTREAM_PROXY'}) && ($proxysettings{'UPSTREAM_USER'}))
46c01c09 333 {
0a638396
CS
334 $login = "--proxy-user=\"$proxysettings{'UPSTREAM_USER'}\"";
335 if ($proxysettings{'UPSTREAM_PASSWORD'})
46c01c09 336 {
0a638396 337 $login .= " --proxy-password=\"$proxysettings{'UPSTREAM_PASSWORD'}\"";
46c01c09
MT
338 }
339 }
46c01c09 340
0a638396 341 if ($proxysettings{'UPSTREAM_PROXY'}) { &debuglog("Using upstream proxy $proxysettings{'UPSTREAM_PROXY'}"); }
46c01c09 342
0a638396 343 $ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'};
da137f21 344 @http_header = `wget $login --user-agent="$useragent" --spider -S $sourceurl 2>&1`;
0a638396 345 $ENV{'http_proxy'} = '';
46c01c09 346
0a638396 347 foreach (@http_header)
46c01c09 348 {
0a638396
CS
349 chomp;
350 if (/^\s*Content-Length:\s/) { $remote_size = $_; $remote_size =~ s/[^0-9]//g; }
351 if (/^\s*Last-Modified:\s/) { $remote_mtime = $_; $remote_mtime =~ s/^\s*Last-Modified:\s//; $remote_mtime = HTTP::Date::str2time($remote_mtime) }
46c01c09
MT
352 }
353
0a638396
CS
354 if (
355 (-e "$updcachedir/$vendorid/$uuid/$updfile") &&
356 ($remote_size == (-s "$updcachedir/$vendorid/$uuid/$updfile")) &&
357 ($remote_mtime == &getmtime("$updcachedir/$vendorid/$uuid/$updfile"))
358 )
46c01c09 359 {
0a638396
CS
360 &debuglog("File exists in cache and is up to date");
361 &debuglog("Retrieving file from cache ($updsource)");
362 &setcachestatus("$updcachedir/$vendorid/$uuid/access.log",time);
0a638396 363 $cacheurl="http://$netsettings{'GREEN_ADDRESS'}:$http_port/updatecache/$vendorid/$uuid/$updfile";
46c01c09
MT
364 }
365 else
366 {
0a638396
CS
367 if (-e "$updcachedir/$vendorid/$uuid/$updfile")
368 {
369 &debuglog("Local filesize: " . (-s "$updcachedir/$vendorid/$uuid/$updfile"));
370 &debuglog("Local timestamp: " . &getmtime("$updcachedir/$vendorid/$uuid/$updfile"));
371 } else { &debuglog("File not found in cache"); }
46c01c09 372 $updsource="DLSOURCE";
0a638396
CS
373 &debuglog("Remote filesize: $remote_size");
374 &debuglog("Remote timestamp: $remote_mtime");
375 &debuglog("Free disk space: " . &diskfree($updcachedir));
376 &debuglog("Disk usage: " . &diskusage($updcachedir) . "% (max. $maxusage%)");
377 if (-e "$updcachedir/download/$vendorid/$updfile") { &debuglog("File download/$vendorid/$updfile exists"); }
378 &debuglog("Retrieving file from source ($updsource)");
379 if ((!$passive_mode) && (&diskusage($updcachedir) <= $maxusage) && ($remote_size <= &diskfree($updcachedir)) && (!-e "$updcachedir/download/$vendorid/$updfile"))
46c01c09 380 {
0a638396
CS
381 &debuglog("Running command $nice$apphome/bin/download $vendorid $sourceurl $cfmirror &");
382 system("$nice$apphome/bin/download $vendorid $sourceurl $cfmirror &");
46c01c09 383 }
0a638396 384 $cacheurl=$sourceurl;
46c01c09
MT
385 }
386
0a638396 387 &writelog($_[1],$_[2],$_[3],$updsource,$sourceurl);
46c01c09 388
0a638396 389 return $cacheurl;
46c01c09
MT
390}
391
392# -------------------------------------------------------------------