download.pl 11 KB

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