]>
Commit | Line | Data |
---|---|---|
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 | # |
4f4092e3 CS |
7 | # Portions (c) 2008 by dotzball - http://www.blockouttraffic.de |
8 | # | |
9 | # $Id: download,v 2.1 2008/07/23 00:00:00 marco.s Exp $ | |
46c01c09 MT |
10 | # |
11 | ||
12 | use strict; | |
0a638396 | 13 | use HTTP::Date; |
46c01c09 | 14 | |
4f4092e3 CS |
15 | require '/var/ipfire/updatexlrator/updxlrator-lib.pl'; |
16 | ||
46c01c09 | 17 | my $logfile="/var/log/updatexlrator/download.log"; |
0a638396 CS |
18 | my $logging=0; |
19 | my $repository='/var/updatecache'; | |
20 | my $login=''; | |
21 | my $dlrate=''; | |
22 | my $uuid=''; | |
0a638396 CS |
23 | my %xlratorsettings=(); |
24 | my %proxysettings=(); | |
25 | my @http_header=(); | |
4f4092e3 | 26 | my $remote_size=0; |
0a638396 CS |
27 | my $remote_mtime=0; |
28 | my $updatefile=''; | |
29 | my $unique=0; | |
30 | my $mirror=1; | |
46c01c09 | 31 | |
4f4092e3 CS |
32 | my %dlinfo=(); |
33 | my $wgetContinueFlag=""; | |
46c01c09 | 34 | |
4f4092e3 CS |
35 | my $vendorid = $ARGV[0]; if (!defined($vendorid) || $vendorid eq '') { exit; } |
36 | my $sourceurl = $ARGV[1]; if (!defined($sourceurl) || $sourceurl eq '') { exit; } | |
37 | my $cfmirror = $ARGV[2]; if (!defined($cfmirror) || $cfmirror eq '') { exit; } | |
38 | my $restartdl = defined($ARGV[3]) ? $ARGV[3] : 0; | |
46c01c09 | 39 | |
0a638396 | 40 | umask(0002); |
46c01c09 | 41 | |
4f4092e3 | 42 | $sourceurl =~ s@\%2b@+@ig; |
0a638396 CS |
43 | $sourceurl =~ s@\%2f@/@ig; |
44 | $sourceurl =~ s@\%7e@~@ig; | |
45 | $updatefile = substr($sourceurl,rindex($sourceurl,"/")+1); | |
46 | $updatefile =~ s@\%20@ @ig; | |
47 | $vendorid =~ tr/A-Z/a-z/; | |
46c01c09 | 48 | |
0a638396 | 49 | unless (-d "$repository/download/$vendorid") |
46c01c09 | 50 | { |
c6919127 | 51 | system("mkdir -p $repository/download/$vendorid"); |
c6919127 | 52 | system("chmod 775 $repository/download/$vendorid"); |
0a638396 CS |
53 | } |
54 | ||
4f4092e3 CS |
55 | if($restartdl == 0) |
56 | { | |
57 | # this is a new download | |
58 | exit if (-e "$repository/download/$vendorid/$updatefile"); | |
59 | ||
60 | # dotzball: Why is this necessary? | |
61 | system("touch $repository/download/$vendorid/$updatefile"); | |
62 | $wgetContinueFlag = "-nc"; | |
63 | ||
64 | } | |
65 | else | |
66 | { | |
67 | # this is a restart of a previous (unfinished) download | |
68 | # -> continue download | |
69 | $wgetContinueFlag = "-c"; | |
70 | &writelog("Continue download: $updatefile"); | |
71 | } | |
0a638396 | 72 | |
0a638396 CS |
73 | |
74 | if ($cfmirror) | |
75 | { | |
76 | $uuid = `echo $updatefile | md5sum`; | |
77 | } else { | |
78 | $uuid = `echo $sourceurl | md5sum`; | |
79 | } | |
80 | ||
81 | $uuid =~ s/[^0-9a-f]//g; | |
82 | $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/; | |
83 | ||
4f4092e3 | 84 | if (-e "$UPDXLT::swroot/updatexlrator/settings") |
0a638396 | 85 | { |
4f4092e3 | 86 | &UPDXLT::readhash("$UPDXLT::swroot/updatexlrator/settings", \%xlratorsettings); |
0a638396 CS |
87 | if ($xlratorsettings{'MAX_DOWNLOAD_RATE'} ne '') { $dlrate = "--limit-rate=" . int($xlratorsettings{'MAX_DOWNLOAD_RATE'} / 8) . "k" }; |
88 | } | |
89 | ||
4f4092e3 | 90 | if (-e "$UPDXLT::swroot/proxy/settings") { &UPDXLT::readhash("$UPDXLT::swroot/proxy/settings", \%proxysettings); } |
0a638396 | 91 | |
4f4092e3 | 92 | if (-e "$UPDXLT::swroot/proxy/advanced/settings") |
0a638396 CS |
93 | { |
94 | %proxysettings=(); | |
4f4092e3 | 95 | &UPDXLT::readhash("$UPDXLT::swroot/proxy/advanced/settings", \%proxysettings); |
0a638396 CS |
96 | } |
97 | ||
98 | if (($proxysettings{'UPSTREAM_PROXY'}) && ($proxysettings{'UPSTREAM_USER'})) | |
46c01c09 | 99 | { |
0a638396 CS |
100 | $login = "--proxy-user=\"$proxysettings{'UPSTREAM_USER'}\""; |
101 | if ($proxysettings{'UPSTREAM_PASSWORD'}) | |
102 | { | |
103 | $login .= " --proxy-password=\"$proxysettings{'UPSTREAM_PASSWORD'}\""; | |
104 | } | |
46c01c09 MT |
105 | } |
106 | ||
0a638396 CS |
107 | if ($xlratorsettings{'MAX_DOWNLOAD_RATE'} eq '') |
108 | { | |
109 | &writelog("Retrieving file for local cache: $updatefile"); | |
110 | } else { | |
111 | &writelog("Retrieving file for local cache at max. " . $xlratorsettings{'MAX_DOWNLOAD_RATE'} . " kBit/s: $updatefile"); | |
112 | } | |
46c01c09 | 113 | |
0a638396 | 114 | $ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'}; |
4f4092e3 | 115 | @http_header = `$UPDXLT::wget $login --user-agent="$UPDXLT::useragent" --spider -S $sourceurl 2>&1`; |
0a638396 | 116 | $ENV{'http_proxy'} = ''; |
46c01c09 | 117 | |
0a638396 CS |
118 | foreach (@http_header) |
119 | { | |
120 | chomp; | |
4f4092e3 CS |
121 | if (/^\s*Content-Length:\s/) { s/[^0-9]//g; $remote_size=$_; &writelog("Remote file size: $_ bytes"); } |
122 | if (/^\s*Last-Modified:\s/) | |
123 | { | |
124 | s/^\s*Last-Modified:\s//; | |
125 | $remote_mtime = HTTP::Date::str2time($_); | |
126 | &writelog("Remote file date: $_"); | |
127 | } | |
0a638396 | 128 | } |
46c01c09 | 129 | |
0a638396 | 130 | $ENV{'http_proxy'} = $proxysettings{'UPSTREAM_PROXY'}; |
4f4092e3 CS |
131 | |
132 | unless($restartdl) | |
133 | { | |
134 | # this is a new download | |
135 | # -> download from scratch | |
136 | unlink "$repository/download/$vendorid/$updatefile"; | |
137 | unlink "$repository/download/$vendorid/$updatefile.info"; | |
138 | } | |
139 | ||
140 | # save file informations while downloading | |
141 | $dlinfo{'VENDORID'} = $vendorid; | |
142 | $dlinfo{'SRCURL'} = $sourceurl; | |
143 | $dlinfo{'FILENAME'} = $updatefile; | |
144 | $dlinfo{'CFMIRROR'} = $cfmirror; | |
145 | $dlinfo{'REMOTETIME'} = $remote_mtime; | |
146 | $dlinfo{'REMOTESIZE'} = $remote_size; | |
147 | $dlinfo{'STATUS'} = "1"; | |
148 | &UPDXLT::writehash("$repository/download/$vendorid/$updatefile.info", \%dlinfo); | |
149 | ||
150 | my $cmd = "$UPDXLT::wget $login $dlrate --user-agent=\"$UPDXLT::useragent\" -q -P $repository/download/$vendorid $wgetContinueFlag $sourceurl"; | |
151 | ||
152 | $_ = system("$cmd"); | |
0a638396 | 153 | $ENV{'http_proxy'} = ''; |
46c01c09 | 154 | |
0a638396 CS |
155 | if ($_ == 0) |
156 | { | |
157 | &writelog("Download finished with result code: OK"); | |
158 | ||
159 | unless (-d "$repository/$vendorid") | |
160 | { | |
2b5b6b9e | 161 | system("mkdir -p $repository/$vendorid"); |
c6919127 | 162 | system("chmod 775 $repository/$vendorid"); |
0a638396 CS |
163 | } |
164 | ||
165 | unless (-d "$repository/$vendorid/$uuid") | |
166 | { | |
c6919127 | 167 | system("mkdir -p $repository/$vendorid/$uuid"); |
c6919127 | 168 | system("chmod 775 $repository/$vendorid/$uuid"); |
0a638396 CS |
169 | } |
170 | ||
171 | &writelog("Moving file to the cache directory: $vendorid/$uuid"); | |
172 | $updatefile =~ s@ @\\ @ig; | |
173 | system("mv $repository/download/$vendorid/$updatefile $repository/$vendorid/$uuid"); | |
174 | # Workaround for IPCop's mv bug: | |
175 | utime time,$remote_mtime,"$repository/$vendorid/$uuid/$updatefile"; | |
176 | $updatefile =~ s@\\ @ @ig; | |
177 | ||
4f4092e3 CS |
178 | &UPDXLT::setcachestatus("$repository/$vendorid/$uuid/source.url",$sourceurl); |
179 | &UPDXLT::setcachestatus("$repository/$vendorid/$uuid/status",$UPDXLT::sfOk); | |
180 | &UPDXLT::setcachestatus("$repository/$vendorid/$uuid/checkup.log",time); | |
181 | &UPDXLT::setcachestatus("$repository/$vendorid/$uuid/access.log",time); | |
0a638396 | 182 | |
f17e1488 | 183 | system("/usr/local/bin/updxsetperms"); |
c6919127 | 184 | system("chmod 775 $repository/$vendorid/$uuid/*"); |
0a638396 | 185 | |
4f4092e3 CS |
186 | unlink ("$repository/download/$vendorid/$updatefile.info"); |
187 | ||
0a638396 CS |
188 | } else { |
189 | &writelog("Download finished with result code: ERROR"); | |
190 | if (-e "$repository/download/$vendorid/$updatefile") { unlink ("$repository/download/$vendorid/$updatefile"); } | |
191 | } | |
192 | ||
193 | ||
0a638396 | 194 | # ------------------------------------------------------------------- |
46c01c09 MT |
195 | |
196 | sub writelog | |
197 | { | |
0a638396 | 198 | if ($logging) |
c6919127 | 199 | { |
0a638396 CS |
200 | open (LOGFILE,">>$logfile"); |
201 | my @now = localtime(time); | |
202 | 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]; | |
203 | close LOGFILE; | |
204 | } | |
205 | } | |
206 | ||
207 | # ------------------------------------------------------------------- |