download.pl 10.0 KB

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