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