download.pl 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. if (open CURL, "curl --version 2>/dev/null |") {
  65. if (defined(my $line = readline CURL)) {
  66. $have_curl = 1 if $line =~ /^curl /;
  67. }
  68. close CURL;
  69. }
  70. return $have_curl
  71. ? (qw(curl -f --connect-timeout 20 --retry 5 --location),
  72. $check_certificate ? () : '--insecure',
  73. shellwords($ENV{CURL_OPTIONS} || ''),
  74. $url)
  75. : (qw(wget --tries=5 --timeout=20 --output-document=-),
  76. $check_certificate ? () : '--no-check-certificate',
  77. shellwords($ENV{WGET_OPTIONS} || ''),
  78. $url)
  79. ;
  80. }
  81. my $hash_cmd = hash_cmd();
  82. $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";
  83. sub download
  84. {
  85. my $mirror = shift;
  86. my $download_filename = shift;
  87. $mirror =~ s!/$!!;
  88. if ($mirror =~ s!^file://!!) {
  89. if (! -d "$mirror") {
  90. print STDERR "Wrong local cache directory -$mirror-.\n";
  91. cleanup();
  92. return;
  93. }
  94. if (! -d "$target") {
  95. system("mkdir", "-p", "$target/");
  96. }
  97. if (! open TMPDLS, "find $mirror -follow -name $filename 2>/dev/null |") {
  98. print("Failed to search for $filename in $mirror\n");
  99. return;
  100. }
  101. my $link;
  102. while (defined(my $line = readline TMPDLS)) {
  103. chomp ($link = $line);
  104. if ($. > 1) {
  105. print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
  106. return;
  107. }
  108. }
  109. close TMPDLS;
  110. if (! $link) {
  111. print("No instances of $filename found in $mirror.\n");
  112. return;
  113. }
  114. print("Copying $filename from $link\n");
  115. copy($link, "$target/$filename.dl");
  116. $hash_cmd and do {
  117. if (system("cat '$target/$filename.dl' | $hash_cmd > '$target/$filename.hash'")) {
  118. print("Failed to generate hash for $filename\n");
  119. return;
  120. }
  121. };
  122. } else {
  123. my @cmd = download_cmd("$mirror/$download_filename");
  124. print STDERR "+ ".join(" ",@cmd)."\n";
  125. open(FETCH_FD, '-|', @cmd) or die "Cannot launch curl or wget.\n";
  126. $hash_cmd and do {
  127. open MD5SUM, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
  128. };
  129. open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
  130. my $buffer;
  131. while (read FETCH_FD, $buffer, 1048576) {
  132. $hash_cmd and print MD5SUM $buffer;
  133. print OUTPUT $buffer;
  134. }
  135. $hash_cmd and close MD5SUM;
  136. close FETCH_FD;
  137. close OUTPUT;
  138. if ($? >> 8) {
  139. print STDERR "Download failed.\n";
  140. cleanup();
  141. return;
  142. }
  143. }
  144. $hash_cmd and do {
  145. my $sum = `cat "$target/$filename.hash"`;
  146. $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
  147. $sum = $1;
  148. if ($sum ne $file_hash) {
  149. print STDERR "Hash of the downloaded file does not match (file: $sum, requested: $file_hash) - deleting download.\n";
  150. cleanup();
  151. return;
  152. }
  153. };
  154. unlink "$target/$filename";
  155. system("mv", "$target/$filename.dl", "$target/$filename");
  156. cleanup();
  157. }
  158. sub cleanup
  159. {
  160. unlink "$target/$filename.dl";
  161. unlink "$target/$filename.hash";
  162. }
  163. @mirrors = localmirrors();
  164. foreach my $mirror (@ARGV) {
  165. if ($mirror =~ /^\@SF\/(.+)$/) {
  166. # give sourceforge a few more tries, because it redirects to different mirrors
  167. for (1 .. 5) {
  168. push @mirrors, "https://downloads.sourceforge.net/$1";
  169. }
  170. } elsif ($mirror =~ /^\@OPENWRT$/) {
  171. # use OpenWrt source server directly
  172. } elsif ($mirror =~ /^\@DEBIAN\/(.+)$/) {
  173. push @mirrors, "https://ftp.debian.org/debian/$1";
  174. push @mirrors, "https://mirror.leaseweb.com/debian/$1";
  175. push @mirrors, "https://mirror.netcologne.de/debian/$1";
  176. } elsif ($mirror =~ /^\@APACHE\/(.+)$/) {
  177. push @mirrors, "https://mirror.netcologne.de/apache.org/$1";
  178. push @mirrors, "https://mirror.aarnet.edu.au/pub/apache/$1";
  179. push @mirrors, "https://mirror.csclub.uwaterloo.ca/apache/$1";
  180. push @mirrors, "https://archive.apache.org/dist/$1";
  181. push @mirrors, "http://mirror.cogentco.com/pub/apache/$1";
  182. push @mirrors, "http://mirror.navercorp.com/apache/$1";
  183. push @mirrors, "http://ftp.jaist.ac.jp/pub/apache/$1";
  184. push @mirrors, "ftp://apache.cs.utah.edu/apache.org/$1";
  185. push @mirrors, "ftp://apache.mirrors.ovh.net/ftp.apache.org/dist/$1";
  186. } elsif ($mirror =~ /^\@GITHUB\/(.+)$/) {
  187. # give github a few more tries (different mirrors)
  188. for (1 .. 5) {
  189. push @mirrors, "https://raw.githubusercontent.com/$1";
  190. }
  191. } elsif ($mirror =~ /^\@GNU\/(.+)$/) {
  192. push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnu/$1";
  193. push @mirrors, "https://mirror.netcologne.de/gnu/$1";
  194. push @mirrors, "http://ftp.kddilabs.jp/GNU/gnu/$1";
  195. push @mirrors, "http://www.nic.funet.fi/pub/gnu/gnu/$1";
  196. push @mirrors, "http://mirror.internode.on.net/pub/gnu/$1";
  197. push @mirrors, "http://mirror.navercorp.com/gnu/$1";
  198. push @mirrors, "ftp://mirrors.rit.edu/gnu/$1";
  199. push @mirrors, "ftp://download.xs4all.nl/pub/gnu/$1";
  200. push @mirrors, "https://ftp.gnu.org/gnu/$1";
  201. } elsif ($mirror =~ /^\@SAVANNAH\/(.+)$/) {
  202. push @mirrors, "https://mirror.netcologne.de/savannah/$1";
  203. push @mirrors, "https://mirror.csclub.uwaterloo.ca/nongnu/$1";
  204. push @mirrors, "http://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
  205. push @mirrors, "http://nongnu.uib.no/$1";
  206. push @mirrors, "http://ftp.igh.cnrs.fr/pub/nongnu/$1";
  207. push @mirrors, "ftp://cdimage.debian.org/mirror/gnu.org/savannah/$1";
  208. push @mirrors, "ftp://ftp.acc.umu.se/mirror/gnu.org/savannah/$1";
  209. } elsif ($mirror =~ /^\@KERNEL\/(.+)$/) {
  210. my @extra = ( $1 );
  211. if ($filename =~ /linux-\d+\.\d+(?:\.\d+)?-rc/) {
  212. push @extra, "$extra[0]/testing";
  213. } elsif ($filename =~ /linux-(\d+\.\d+(?:\.\d+)?)/) {
  214. push @extra, "$extra[0]/longterm/v$1";
  215. }
  216. foreach my $dir (@extra) {
  217. push @mirrors, "https://cdn.kernel.org/pub/$dir";
  218. push @mirrors, "https://download.xs4all.nl/ftp.kernel.org/pub/$dir";
  219. push @mirrors, "https://mirrors.mit.edu/kernel/$dir";
  220. push @mirrors, "http://ftp.nara.wide.ad.jp/pub/kernel.org/$dir";
  221. push @mirrors, "http://www.ring.gr.jp/archives/linux/kernel.org/$dir";
  222. push @mirrors, "ftp://ftp.riken.jp/Linux/kernel.org/$dir";
  223. push @mirrors, "ftp://www.mirrorservice.org/sites/ftp.kernel.org/pub/$dir";
  224. }
  225. } elsif ($mirror =~ /^\@GNOME\/(.+)$/) {
  226. push @mirrors, "https://download.gnome.org/sources/$1";
  227. push @mirrors, "https://mirror.csclub.uwaterloo.ca/gnome/sources/$1";
  228. push @mirrors, "http://ftp.acc.umu.se/pub/GNOME/sources/$1";
  229. push @mirrors, "http://ftp.kaist.ac.kr/gnome/sources/$1";
  230. push @mirrors, "http://www.mirrorservice.org/sites/ftp.gnome.org/pub/GNOME/sources/$1";
  231. push @mirrors, "http://mirror.internode.on.net/pub/gnome/sources/$1";
  232. push @mirrors, "http://ftp.belnet.be/ftp.gnome.org/sources/$1";
  233. push @mirrors, "ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1";
  234. push @mirrors, "ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1";
  235. } else {
  236. push @mirrors, $mirror;
  237. }
  238. }
  239. push @mirrors, 'https://sources.cdn.openwrt.org';
  240. push @mirrors, 'https://sources.openwrt.org';
  241. push @mirrors, 'https://mirror2.openwrt.org/sources';
  242. if (-f "$target/$filename") {
  243. $hash_cmd and do {
  244. if (system("cat '$target/$filename' | $hash_cmd > '$target/$filename.hash'")) {
  245. die "Failed to generate hash for $filename\n";
  246. }
  247. my $sum = `cat "$target/$filename.hash"`;
  248. $sum =~ /^(\w+)\s*/ or die "Could not generate file hash\n";
  249. $sum = $1;
  250. cleanup();
  251. exit 0 if $sum eq $file_hash;
  252. die "Hash of the local file $filename does not match (file: $sum, requested: $file_hash) - deleting download.\n";
  253. unlink "$target/$filename";
  254. };
  255. }
  256. while (!-f "$target/$filename") {
  257. my $mirror = shift @mirrors;
  258. $mirror or die "No more mirrors to try - giving up.\n";
  259. download($mirror, $url_filename);
  260. if (!-f "$target/$filename" && $url_filename ne $filename) {
  261. download($mirror, $filename);
  262. }
  263. }
  264. $SIG{INT} = \&cleanup;