OSDN Git Service

download.pl: remove broken mirror
[immortalwrt/immortalwrt.git] / scripts / download.pl
1 #!/usr/bin/env perl
2
3 # Copyright (C) 2006 OpenWrt.org
4 # Copyright (C) 2016 LEDE project
5 #
6 # This is free software, licensed under the GNU General Public License v2.
7 # See /LICENSE for more information.
8 #
9
10 use strict;
11 use warnings;
12 use File::Basename;
13 use File::Copy;
14 use Text::ParseWords;
15
16 @ARGV > 2 or die "Syntax: $0 <target dir> <filename> <hash> <url filename> [<mirror> ...]\n";
17
18 my $url_filename;
19 my $target = glob(shift @ARGV);
20 my $filename = shift @ARGV;
21 my $file_hash = shift @ARGV;
22 $url_filename = shift @ARGV unless $ARGV[0] =~ /:\/\//;
23 my $scriptdir = dirname($0);
24 my @mirrors;
25 my $ok;
26
27 $url_filename or $url_filename = $filename;
28
29 sub localmirrors {
30         my @mlist;
31         open LM, "$scriptdir/localmirrors" and do {
32             while (<LM>) {
33                         chomp $_;
34                         push @mlist, $_ if $_;
35                 }
36                 close LM;
37         };
38         open CONFIG, "<".$ENV{'TOPDIR'}."/.config" and do {
39                 while (<CONFIG>) {
40                         /^CONFIG_LOCALMIRROR="(.+)"/ and do {
41                                 chomp;
42                                 my @local_mirrors = split(/;/, $1);
43                                 push @mlist, @local_mirrors;
44                         };
45                 }
46                 close CONFIG;
47         };
48
49         my $mirror = $ENV{'DOWNLOAD_MIRROR'};
50         $mirror and push @mlist, split(/;/, $mirror);
51
52         return @mlist;
53 }
54
55 sub which($) {
56         my $prog = shift;
57         my $res = `command -v $prog`;
58         $res or return undef;
59         return $res;
60 }
61
62 sub hash_cmd() {
63         my $len = length($file_hash);
64         my $cmd;
65
66         $len == 64 and return "$ENV{'MKHASH'} sha256";
67         $len == 32 and return "$ENV{'MKHASH'} md5";
68         return undef;
69 }
70
71 sub download_cmd($) {
72         my $url = shift;
73         my $have_curl = 0;
74
75         if (open CURL, '-|', 'curl', '--version') {
76                 if (defined(my $line = readline CURL)) {
77                         $have_curl = 1 if $line =~ /^curl /;
78                 }
79                 close CURL;
80         }
81
82         return $have_curl
83                 ? (qw(curl -f --connect-timeout 20 --retry 5 --location --insecure), shellwords($ENV{CURL_OPTIONS} || ''), $url)
84                 : (qw(wget --tries=5 --timeout=20 --no-check-certificate --output-document=-), shellwords($ENV{WGET_OPTIONS} || ''), $url)
85         ;
86 }
87
88 my $hash_cmd = hash_cmd();
89 $hash_cmd or ($file_hash eq "skip") or die "Cannot find appropriate hash command, ensure the provided hash is either a MD5 or SHA256 checksum.\n";
90
91 sub download
92 {
93         my $mirror = shift;
94         my $download_filename = shift;
95
96         $mirror =~ s!/$!!;
97
98         if ($mirror =~ s!^file://!!) {
99                 if (! -d "$mirror") {
100                         print STDERR "Wrong local cache directory -$mirror-.\n";
101                         cleanup();
102                         return;
103                 }
104
105                 if (! -d "$target") {
106                         system("mkdir", "-p", "$target/");
107                 }
108
109                 if (! open TMPDLS, "find $mirror -follow -name $filename 2>/dev/null |") {
110                         print("Failed to search for $filename in $mirror\n");
111                         return;
112                 }
113
114                 my $link;
115
116                 while (defined(my $line = readline TMPDLS)) {
117                         chomp ($link = $line);
118                         if ($. > 1) {
119                                 print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
120                                 return;
121                         }
122                 }
123
124                 close TMPDLS;
125
126                 if (! $link) {
127                         print("No instances of $filename found in $mirror.\n");
128                         return;
129                 }
130
131                 print("Copying $filename from $link\n");
132                 copy($link, "$target/$filename.dl");
133
134                 $hash_cmd and do {
135                         if (system("cat '$target/$filename.dl' | $hash_cmd > '$target/$filename.hash'")) {
136                                 print("Failed to generate hash for $filename\n");
137                                 return;
138                         }
139                 };
140         } else {
141                 my @cmd = download_cmd("$mirror/$download_filename");
142                 print STDERR "+ ".join(" ",@cmd)."\n";
143                 open(FETCH_FD, '-|', @cmd) or die "Cannot launch curl or wget.\n";
144                 $hash_cmd and do {
145                         open MD5SUM, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
146                 };
147                 open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
148                 my $buffer;
149                 while (read FETCH_FD, $buffer, 1048576) {
150                         $hash_cmd and print MD5SUM $buffer;
151                         print OUTPUT $buffer;
152                 }
153                 $hash_cmd and close MD5SUM;
154                 close FETCH_FD;
155                 close OUTPUT;
156
157                 if ($? >> 8) {
158                         print STDERR "Download failed.\n";
159                         cleanup();
160                         return;
161                 }
162         }
163
164         $hash_cmd and do {
165                 my $sum = `cat "$target/$filename.hash"`;
166                 $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
167                 $sum = $1;
168
169                 if ($sum ne $file_hash) {
170                         print STDERR "Hash of the downloaded file does not match (file: $sum, requested: $file_hash) - deleting download.\n";
171                         cleanup();
172                         return;
173                 }
174         };
175
176         unlink "$target/$filename";
177         system("mv", "$target/$filename.dl", "$target/$filename");
178         cleanup();
179 }
180
181 sub cleanup
182 {
183         unlink "$target/$filename.dl";
184         unlink "$target/$filename.hash";
185 }
186
187 @mirrors = localmirrors();
188
189 foreach my $mirror (@ARGV) {
190         if ($mirror =~ /^\@SF\/(.+)$/) {
191                 # give sourceforge a few more tries, because it redirects to different mirrors
192                 for (1 .. 5) {
193                         push @mirrors, "https://downloads.sourceforge.net/$1";
194                 }
195         } elsif ($mirror =~ /^\@OPENWRT$/) {
196                 # use OpenWrt source server directly
197         } elsif ($mirror =~ /^\@IMMORTALWRT$/) {
198                 # use ImmortalWrt source server directly
199         } elsif ($mirror =~ /^\@DEBIAN\/(.+)$/) {
200                 push @mirrors, "https://mirrors.tencent.com/debian/$1";
201                 push @mirrors, "https://mirrors.aliyun.com/debian/$1";
202                 # push @mirrors, "https://mirrors.tuna.tsinghua.edu.cn/debian/$1";
203                 # push @mirrors, "https://mirrors.ustc.edu.cn/debian/$1";
204                 push @mirrors, "https://ftp.debian.org/debian/$1";
205                 push @mirrors, "https://mirror.leaseweb.com/debian/$1";
206                 push @mirrors, "https://mirror.netcologne.de/debian/$1";
207         } elsif ($mirror =~ /^\@APACHE\/(.+)$/) {
208                 push @mirrors, "https://mirrors.tencent.com/apache/$1";
209                 push @mirrors, "https://mirrors.aliyun.com/apache/$1";
210                 # push @mirrors, "https://mirrors.tuna.tsinghua.edu.cn/apache/$1";
211                 # push @mirrors, "https://mirrors.ustc.edu.cn/apache/$1";
212                 push @mirrors, "https://mirror.netcologne.de/apache.org/$1";
213                 push @mirrors, "https://mirror.aarnet.edu.au/pub/apache/$1";
214                 push @mirrors, "https://mirror.csclub.uwaterloo.ca/apache/$1";
215                 push @mirrors, "https://archive.apache.org/dist/$1";
216                 push @mirrors, "http://mirror.cogentco.com/pub/apache/$1";
217                 push @mirrors, "http://mirror.navercorp.com/apache/$1";
218                 push @mirrors, "http://ftp.jaist.ac.jp/pub/apache/$1";
219                 push @mirrors, "ftp://apache.cs.utah.edu/apache.org/$1";
220                 push @mirrors, "ftp://apache.mirrors.ovh.net/ftp.apache.org/dist/$1";
221         } elsif ($mirror =~ /^\@GITHUB\/(.+)$/) {
222                 my $dir = $1;
223                 my $i = 0;
224                 # replace the 2nd '/' with '@' for jsDelivr mirror
225                 push @mirrors, "https://cdn.jsdelivr.net/gh/". $dir =~ s{\/}{++$i == 2 ? '@' : $&}ger;
226                 push @mirrors, "https://raw.sevencdn.com/$dir";
227                 push @mirrors, "https://raw.fastgit.org/$dir";
228                 # give github a few more tries (different mirrors)
229                 for (1 .. 5) {
230                         push @mirrors, "https://raw.githubusercontent.com/$dir";
231                 }
232         } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
233                 push @mirrors, "https://mirrors.tencent.com/gnu/$1";
234                 push @mirrors, "https://mirrors.aliyun.com/gnu/$1";
235                 # push @mirrors, "https://mirrors.tuna.tsinghua.edu.cn/gnu/$1";
236                 # push @mirrors, "https://mirrors.cqu.edu.cn/gnu/$1";
237                 # push @mirrors, "https://mirrors.ustc.edu.cn/gnu/$1";
238                 push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnu/$1";
239                 push @mirrors, "https://mirror.netcologne.de/gnu/$1";
240                 push @mirrors, "http://ftp.kddilabs.jp/GNU/gnu/$1";
241                 push @mirrors, "http://www.nic.funet.fi/pub/gnu/gnu/$1";
242                 push @mirrors, "http://mirror.internode.on.net/pub/gnu/$1";
243                 push @mirrors, "http://mirror.navercorp.com/gnu/$1";
244                 push @mirrors, "ftp://mirrors.rit.edu/gnu/$1";
245                 push @mirrors, "ftp://download.xs4all.nl/pub/gnu/$1";
246                 push @mirrors, "https://ftp.gnu.org/gnu/$1";
247         } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
248                 push @mirrors, "https://mirror.netcologne.de/savannah/$1";
249                 push @mirrors, "https://mirror.csclub.uwaterloo.ca/nongnu/$1";
250                 push @mirrors, "http://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
251                 push @mirrors, "http://nongnu.uib.no/$1";
252                 push @mirrors, "http://ftp.igh.cnrs.fr/pub/nongnu/$1";
253                 push @mirrors, "ftp://cdimage.debian.org/mirror/gnu.org/savannah/$1";
254                 push @mirrors, "ftp://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
255         } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
256                 my @extra = ( $1 );
257                 if ($filename =~ /linux-\d+\.\d+(?:\.\d+)?-rc/) {
258                         push @extra, "$extra[0]/testing";
259                 } elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
260                         push @extra, "$extra[0]/longterm/v$1";
261                 }
262                 foreach my $dir (@extra) {
263                         # push @mirrors, "https://mirrors.ustc.edu.cn/kernel.org/$dir";
264                         # push @mirrors, "https://mirror.iscas.ac.cn/kernel.org/$dir";
265                         push @mirrors, "https://cdn.kernel.org/pub/$dir";
266                         push @mirrors, "https://download.xs4all.nl/ftp.kernel.org/pub/$dir";
267                         push @mirrors, "https://mirrors.mit.edu/kernel/$dir";
268                         push @mirrors, "http://ftp.nara.wide.ad.jp/pub/kernel.org/$dir";
269                         push @mirrors, "http://www.ring.gr.jp/archives/linux/kernel.org/$dir";
270                         push @mirrors, "ftp://ftp.riken.jp/Linux/kernel.org/$dir";
271                         push @mirrors, "ftp://www.mirrorservice.org/sites/ftp.kernel.org/pub/$dir";
272                 }
273         } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
274                 # push @mirrors, "https://mirrors.ustc.edu.cn/gnome/sources/$1";
275                 push @mirrors, "https://download.gnome.org/sources/$1";
276                 push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnome/sources/$1";
277                 push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
278                 push @mirrors, "http://ftp.kaist.ac.kr/gnome/sources/$1";
279                 push @mirrors, "http://www.mirrorservice.org/sites/ftp.gnome.org/pub/GNOME/sources/$1";
280                 push @mirrors, "http://mirror.internode.on.net/pub/gnome/sources/$1";
281                 push @mirrors, "http://ftp.belnet.be/ftp.gnome.org/sources/$1";
282                 push @mirrors, "ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1";
283                 push @mirrors, "ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1";
284         } else {
285                 push @mirrors, $mirror;
286         }
287 }
288
289 # push @mirrors, 'https://mirror01.download.immortalwrt.eu.org';
290 push @mirrors, 'https://mirror.immortalwrt.org/sources';
291 push @mirrors, 'https://sources.immortalwrt.org';
292 push @mirrors, 'https://sources.cdn.immortalwrt.org';
293 push @mirrors, 'https://sources.cdn.openwrt.org';
294 push @mirrors, 'https://sources.openwrt.org';
295 push @mirrors, 'https://mirror2.openwrt.org/sources';
296
297 if (-f "$target/$filename") {
298         $hash_cmd and do {
299                 if (system("cat '$target/$filename' | $hash_cmd > '$target/$filename.hash'")) {
300                         die "Failed to generate hash for $filename\n";
301                 }
302
303                 my $sum = `cat "$target/$filename.hash"`;
304                 $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
305                 $sum = $1;
306
307                 cleanup();
308                 exit 0 if $sum eq $file_hash;
309
310                 die "Hash of the local file $filename does not match (file: $sum, requested: $file_hash) - deleting download.\n";
311                 unlink "$target/$filename";
312         };
313 }
314
315 while (!-f "$target/$filename") {
316         my $mirror = shift @mirrors;
317         $mirror or die "No more mirrors to try - giving up.\n";
318
319         download($mirror, $url_filename);
320         if (!-f "$target/$filename" && $url_filename ne $filename) {
321                 download($mirror, $filename);
322         }
323 }
324
325 $SIG{INT} = \&cleanup;