]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - config/updxlrator/updxlrator
3 # This code is distributed under the terms of the GPL
5 # (c) 2006-2008 marco.s - http://update-accelerator.advproxy.net
7 # $Id: updxlrator,v 2.0 2008/04/06 00:00:00 marco.s Exp $
15 my $swroot="/var/ipfire";
16 my $updcachedir="/var/updatecache";
17 my $apphome="/var/ipfire/updatexlrator";
18 my $logfile="/var/log/updatexlrator/cache.log";
19 my $debug=(-e
"$apphome/debug");
23 my %xlratorsettings=();
38 readhash
("${swroot}/ethernet/settings", \
%netsettings);
40 if (-e
"$swroot/updatexlrator/settings")
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'};
46 if ($xlratorsettings{'LOW_DOWNLOAD_PRIORITY'} eq 'on') { $nice='/bin/nice --adjustment=15 '; };
48 if (!$maxusage) { $maxusage=75; };
55 @tmp=split(/ /,$request);
58 $source_url = $tmp[0];
59 $hostaddr = $tmp[1]; while ($hostaddr =~ /.*\/$/) { chop $hostaddr; }
60 $username = $tmp[2]; if ($username eq '') { $username='-'; };
63 $xlrator_url = $source_url;
65 if (($method eq 'GET') || ($method eq 'HEAD'))
68 # -----------------------------------------------------------
69 # Section: Windows Update / Windows Downloads
70 # -----------------------------------------------------------
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@\
&@
)
79 $xlrator_url = &check_cache
($source_url,$hostaddr,$username,"Microsoft",$unique);
82 # -----------------------------------------------------------
83 # Section: Adobe Downloads
84 # -----------------------------------------------------------
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)
92 $xlrator_url = &check_cache
($source_url,$hostaddr,$username,"Adobe",$unique);
95 # -----------------------------------------------------------
96 # Section: Linux Downloads
97 # -----------------------------------------------------------
99 if ($source_url =~ m@
^[h
|f
]t?tp
://[^?
]+\
.(deb
|rpm
)$@i)
101 $xlrator_url = &check_cache
($source_url,$hostaddr,$username,"Linux",$mirror);
104 # -----------------------------------------------------------
105 # Section: Trend Micro Downloads
106 # -----------------------------------------------------------
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)
115 $xlrator_url = &check_cache
($source_url,$hostaddr,$username,"TrendMicro",$mirror);
118 # -----------------------------------------------------------
119 # Section: Symantec Downloads
120 # -----------------------------------------------------------
122 if ($source_url =~ m@
^[h
|f
]tt?p
://[^/]*\.symantec(liveupdate)?\.com/.*\
.(exe
|zip
|vdb
|xdb
)$@i)
124 $xlrator_url = &check_cache
($source_url,$hostaddr,$username,"Symantec",$unique);
127 # -----------------------------------------------------------
128 # Section: Apple Downloads
129 # -----------------------------------------------------------
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))
137 $xlrator_url = &check_cache
($source_url,$hostaddr,$username,"Apple",$unique);
140 # -----------------------------------------------------------
141 # Section: Avast Downloads
142 # -----------------------------------------------------------
144 if ($source_url =~ m@
^http
://download
[\d
]+\
.avast\
.com
/.*\
.(exe
|vpu
)$@i)
146 $xlrator_url = &check_cache
($source_url,$hostaddr,$username,"Avast",$mirror);
149 # -----------------------------------------------------------
151 # -----------------------------------------------------------
152 # Section: Avira Downloads
153 # -----------------------------------------------------------
155 if ($source_url =~ m@
^http
://dl[0-9]\
.avgate\
.net
/.*\
.(htm
|html
|gz
)$@i)
157 $xlrator_url = &check_cache
($source_url,$hostaddr,$username,"Avira",$mirror);
160 # -----------------------------------------------------------
161 # Section: IPFire Downloads
162 # -----------------------------------------------------------
164 if ($source_url =~ m@
^[f
|h
]t?tp
://.*\
.(ipfire
)$@i)
166 $xlrator_url = &check_cache
($source_url,$hostaddr,$username,"IPFire",$mirror);
171 # -----------------------------------------------------------
172 # Section: AVG Downloads
173 # -----------------------------------------------------------
175 if ($source_url =~ m@
^http
://[^/]*\.(grisoft|avg)\.com/.*\
.(bin
)$@i)
177 $xlrator_url = &check_cache
($source_url,$hostaddr,$username,"AVG",$mirror);
180 $request="$xlrator_url $hostaddr $username $method\n";
185 # -------------------------------------------------------------------
189 my $filename = $_[0];
195 open(FILE
, $filename) or die "Unable to read file $filename";
199 ($var, $val) = split /=/, $_, 2;
205 # Untaint variables read from hash
206 $var =~ /([A-Za-z0-9_-]*)/; $var = $1;
207 $val =~ /([\w\W]*)/; $val = $1;
208 $hash->{$var} = $val;
215 # -------------------------------------------------------------------
221 open(LOGFILE
,">>$logfile");
222 print LOGFILE
time." $_[0] $_[1] $_[2] $_[3] $_[4]\n";
227 # -------------------------------------------------------------------
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];
240 # -------------------------------------------------------------------
244 open (FILE
,">>$_[0]");
245 print FILE
"$_[1]\n";
249 # -------------------------------------------------------------------
253 open(DF
,"/bin/df --block-size=1 $_[0]|");
258 my $dfstr = join(' ',@dfdata);
259 my ($device,$size,$used,$free,$percent,$mount) = split(' ',$dfstr);
260 if ($free =~ m/^(\d+)$/)
266 # -------------------------------------------------------------------
270 open(DF
,"/bin/df $_[0]|");
275 my $dfstr = join(' ',@dfdata);
276 my ($device,$size,$used,$free,$percent,$mount) = split(' ',$dfstr);
277 if ($percent =~ m/^(\d+)%$/)
284 # -------------------------------------------------------------------
288 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($_[0]);
293 # -------------------------------------------------------------------
297 my $updsource="UPDCACHE";
306 my $useragent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
311 $sourceurl =~ s@\
%2f@
/@ig;
312 $updfile = substr($sourceurl,rindex($sourceurl,"/")+1);
313 $updfile =~ s@\
%20@
@ig;
317 $uuid = `echo $updfile | md5sum`;
319 $uuid = `echo $sourceurl | md5sum`;
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/;
326 $vendorid =~ tr/A-Z/a-z/;
328 &debuglog
("Processing URL $sourceurl");
329 &debuglog
("Vendor ID is $vendorid");
330 &debuglog
("UUID is $uuid");
332 if (($proxysettings{'UPSTREAM_PROXY'}) && ($proxysettings{'UPSTREAM_USER'}))
334 $login = "--proxy-user=\"$proxysettings{'UPSTREAM_USER'}\"";
335 if ($proxysettings{'UPSTREAM_PASSWORD'})
337 $login .= " --proxy-password=\"$proxysettings{'UPSTREAM_PASSWORD'}\"";
341 if ($proxysettings{'UPSTREAM_PROXY'}) { &debuglog
("Using upstream proxy $proxysettings{'UPSTREAM_PROXY'}"); }
343 $ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'};
344 @http_header = `wget $login --user-agent="$useragent" --spider -S $sourceurl 2>&1`;
345 $ENV{'http_proxy'} = '';
347 foreach (@http_header)
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) }
355 (-e
"$updcachedir/$vendorid/$uuid/$updfile") &&
356 ($remote_size == (-s
"$updcachedir/$vendorid/$uuid/$updfile")) &&
357 ($remote_mtime == &getmtime
("$updcachedir/$vendorid/$uuid/$updfile"))
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);
363 $cacheurl="http://$netsettings{'GREEN_ADDRESS'}:$http_port/updatecache/$vendorid/$uuid/$updfile";
367 if (-e
"$updcachedir/$vendorid/$uuid/$updfile")
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"); }
372 $updsource="DLSOURCE";
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"))
381 &debuglog
("Running command $nice$apphome/bin/download $vendorid $sourceurl $cfmirror &");
382 system("$nice$apphome/bin/download $vendorid $sourceurl $cfmirror &");
384 $cacheurl=$sourceurl;
387 &writelog
($_[1],$_[2],$_[3],$updsource,$sourceurl);
392 # -------------------------------------------------------------------