#该脚本用于下载指定的文件/n#需要在命令行中指定目标目录、文件名、文件哈希值和下载链接等参数/n#也可以指定多个镜像地址,程序会按顺序尝试下载/n#如果下载成功,会将文件保存到目标目录中,并进行哈希值校验/n#如果哈希值不匹配,会删除下载的文件并报错/n#如果已经存在相同文件名的文件且哈希值匹配,程序会直接退出/n#如果哈希值匹配但文件名不同,程序会将下载的文件重命名为目标文件名/n/nuse strict;/nuse warnings;/nuse File::Basename; #用于获取文件名和目录名/nuse File::Copy; #用于复制和移动文件/nuse Text::ParseWords; #用于解析命令行参数/n/n#检查是否指定了足够的参数/n@ARGV > 2 or die 'Syntax: $0 [ ...]/n';/n/n#获取目标目录、文件名、哈希值和下载链接等参数/nmy $url_filename;/nmy $target = glob(shift @ARGV);/nmy $filename = shift @ARGV;/nmy $file_hash = shift @ARGV;/n$url_filename = shift @ARGV unless $ARGV[0] =~ /:///////;/nmy $scriptdir = dirname($0);/nmy @mirrors;/nmy $ok;/n/n#判断是否需要检查证书/nmy $check_certificate = $ENV{DOWNLOAD_CHECK_CERTIFICATE} eq 'y';/n#获取自定义的下载工具/nmy $custom_tool = $ENV{DOWNLOAD_TOOL_CUSTOM};/n#定义下载工具变量,后面会根据情况选择具体的工具/nmy $download_tool;/n/n#判断哈希值类型,并生成相应的哈希命令/nmy $hash_cmd = hash_cmd();/n$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';/n/n#定义函数,用于获取本地镜像列表/nsub localmirrors {/n/tmy @mlist;/n/topen LM, '$scriptdir/localmirrors' and do {/n/t/twhile () {/n/t/t/tchomp $;/n/t/t/tpush @mlist, $ if $;/n/t/t}/n/t/tclose LM;/n/t};/n/topen CONFIG, '<' . $ENV{'TOPDIR'} . '/.config' and do {/n/t/twhile () {/n/t/t/t/^CONFIG_LOCALMIRROR=/'(.+)/'/ and do {/n/t/t/t/tchomp;/n/t/t/t/tmy @local_mirrors = split(/;/, $1);/n/t/t/t/tpush @mlist, @local_mirrors;/n/t/t/t};/n/t/t}/n/t/tclose CONFIG;/n/t};/n/n/tmy $mirror = $ENV{'DOWNLOAD_MIRROR'};/n/t$mirror and push @mlist, split(/;/, $mirror);/n/n/treturn @mlist;/n}/n/n#定义函数,用于判断命令是否存在/nsub which ($) {/n/tmy $prog = shift;/n/tmy $res = command -v $prog;/n/t$res or return undef;/n/treturn $res;/n}/n/n#定义函数,用于获取哈希命令/nsub hash_cmd() {/n/tmy $len = length($file_hash);/n/tmy $cmd;/n/n/t$len == 64 and return '$ENV{MKHASH} sha256';/n/t$len == 32 and return '$ENV{MKHASH} md5';/n/treturn undef;/n}/n/n#定义函数,用于判断工具是否存在/nsub tool_present {/n/tmy $tool_name = shift;/n/tmy $compare_line = shift;/n/tmy $present = 0;/n/n/tif (open TOOL, '$tool_name --version 2>/dev/null |') {/n/t/tif (defined(my $line = readline TOOL)) {/n/t/t/t$present = 1 if $line =~ /^$compare_line /;/n/t/t}/n/t/tclose TOOL;/n/t}/n/n/treturn $present/n}/n/n#定义函数,用于选择下载工具/nsub select_tool {/n/t$custom_tool =~ tr//'//d;/n/tif ($custom_tool) {/n/t/treturn $custom_tool;/n/t}/n/n/t# 尝试使用 curl/n/tif (tool_present('curl', 'curl')) {/n/t/treturn 'curl';/n/t}/n/n/t# 未找到工具,使用 wget/n/treturn 'wget';/n}/n/n#定义函数,用于生成下载命令/nsub download_cmd {/n/tmy $url = shift;/n/tmy $filename = shift;/n/n/tif ($download_tool eq 'curl') {/n/t/treturn (qw(curl -f --connect-timeout 20 --retry 5 --location)),/n/t/t/t$check_certificate ? () : '--insecure',/n/t/t/tshellwords($ENV{CURL_OPTIONS} || ''),/n/t/t/t$url;/n/t} elsif ($download_tool eq 'wget') {/n/t/treturn (qw(wget --tries=5 --timeout=20 --output-document=-)),/n/t/t/t$check_certificate ? () : '--no-check-certificate',/n/t/t/tshellwords($ENV{WGET_OPTIONS} || ''),/n/t/t/t$url;/n/t} elsif ($download_tool eq 'aria2c') {/n/t/tmy $additional_mirrors = join(' ', map '$/$filename', @);/n/t/tmy @chArray = ('a'..'z', 'A'..'Z', 0..9);/n/t/tmy $rfn = join '', '$filename', map{ $chArray[int rand @chArray] } 0..9;/n/n/t/t@mirrors=();/n/n/t/treturn join(' ', '[ -d $ENV{TMPDIR}/aria2c ] || mkdir $ENV{TMPDIR}/aria2c;',/n/t/t/t'touch $ENV{TMPDIR}/aria2c/$rfn_spp;',/n/t/t/tqw(aria2c --stderr -c -x2 -s10 -j10 -k1M), $url, $additional_mirrors,/n/t/t/t$check_certificate ? () : '--check-certificate=false',/n/t/t/t'--server-stat-of=$ENV{TMPDIR}/aria2c/$rfn_spp',/n/t/t/t'--server-stat-if=$ENV{TMPDIR}/aria2c/$rfn_spp',/n/t/t/t'--daemon=false --no-conf', shellwords($ENV{ARIA2C_OPTIONS} || ''),/n/t/t/t'-d $ENV{TMPDIR}/aria2c -o $rfn;',/n/t/t/t'cat $ENV{TMPDIR}/aria2c/$rfn;',/n/t/t/t'rm $ENV{TMPDIR}/aria2c/$rfn $ENV{TMPDIR}/aria2c/$rfn_spp');/n/t} else {/n/t/treturn join(' ', $download_tool, $url);/n/t}/n}/n/n#定义函数,用于下载文件并验证哈希值/nsub download/n{/n/tmy $mirror = shift;/n/tmy $download_filename = shift;/n/tmy @additional_mirrors = @_;/n/n/t$mirror =~ s!/$!!;/n/n/tif ($mirror =~ s!^file://!!) {/n/t/tif (! -d '$mirror') {/n/t/t/tprint STDERR '本地缓存目录错误Wrong local cache directory -$mirror-.//n';/n/t/t/tcleanup();/n/t/t/treturn;/n/t/t}/n/n/t/tif (! -d '$target') {/n/t/t/tsystem('mkdir', '-p', '$target/');/n/t/t}/n/n/t/tif (! open TMPDLS, 'find $mirror -follow -name $filename 2>/dev/null |') {/n/t/t/tprint('Failed to search for $filename in $mirror//n');/n/t/t/treturn;/n/t/t}/n/n/t/tmy $link;/n/n/t/twhile (defined(my $line = readline TMPDLS)) {/n/t/t/tchomp ($link = $line);/n/t/t/tif ($. > 1) {/n/t/t/t/tprint('$. or more instances of $filename in $mirror found . Only one instance allowed.//n');/n/t/t/t/treturn;/n/t/t/t}/n/t/t}/n/n/t/tclose TMPDLS;/n/n/t/tif (! $link) {/n/t/t/tprint('No instances of $filename found in $mirror.//n');/n/t/t/treturn;/n/t/t}/n/n/t/tprint('Copying $filename from $link//n');/n/t/tcopy($link, '$target/$filename.dl');/n/n/t/t$hash_cmd and do {/n/t/t/tif (system('cat /'$target/$filename.dl/' | $hash_cmd > /'$target/$filename.hash/'')) {/n/t/t/t/tprint('Failed to generate hash for $filename//n');/n/t/t/t/treturn;/n/t/t/t}/n/t/t};/n/t} else {/n/t/tmy @cmd = download_cmd('$mirror/$download_filename', $download_filename, @additional_mirrors);/n/t/tprint STDERR '+ ' . join(' ',@cmd) . '//n';/n/t/topen(FETCH_FD, '-|', @cmd) or die 'Cannot launch aria2c, curl or wget.//n';/n/t/t$hash_cmd and do {/n/t/t/topen MD5SUM, '| $hash_cmd > /'$target/$filename.hash/'' or die 'Cannot launch $hash_cmd.//n';/n/t/t};/n/t/topen OUTPUT, '> $target/$filename.dl' or die 'Cannot create file $target/$filename.dl: $!//n';/n/t/tmy $buffer;/n/t/twhile (read FETCH_FD, $buffer, 1048576) {/n/t/t/t$hash_cmd and print MD5SUM $buffer;/n/t/t/tprint OUTPUT $buffer;/n/t/t}/n/t/t$hash_cmd and close MD5SUM;/n/t/tclose FETCH_FD;/n/t/tclose OUTPUT;/n/n/t/tif ($? >> 8) {/n/t/t/tprint STDERR '下载失败.//n';/n/t/t/tcleanup();/n/t/t/treturn;/n/t/t}/n/t}/n/n/t$hash_cmd and do {/n/t/tmy $sum = cat '$target/$filename.hash';/n/t/t$sum =~ /^(/w+)/s*/ or die 'Could not generate file hash//n';/n/t/t$sum = $1;/n/n/t/tif ($sum ne $file_hash) {/n/t/t/tprint STDERR 'Hash of the downloaded file does not match (file: $sum, requested: $file_hash) - deleting download.//n';/n/t/t/tcleanup();/n/t/t/treturn;/n/t/t}/n/t};/n/n/tunlink '$target/$filename';/n/tsystem('mv', '$target/$filename.dl', '$target/$filename');/n/tcleanup();/n}/n/n#定义函数,用于清理临时文件/nsub cleanup/n{/n/tunlink '$target/$filename.dl';/n/tunlink '$target/$filename.hash';/n}/n/n#获取本地镜像列表/n@mirrors = localmirrors();/n/n#遍历所有镜像地址/nforeach my $mirror (@ARGV) {/n/tif ($mirror =~ /^//@SF///(.+)$/) {/n/t/t# 尝试从 SourceForge 下载/n/t/tfor (1 .. 5) {/n/t/t/tpush @mirrors, 'https://downloads.sourceforge.net/$1';/n/t/t}/n/t} elsif ($mirror =~ /^//@OPENWRT$/) {/n/t/t# 使用 OpenWrt 源服务器直接下载/n/t} elsif ($mirror =~ /^//@DEBIAN///(.+)$/) {/n/t/t# 尝试从 Debian 镜像下载/n/t/tpush @mirrors, 'https://ftp.debian.org/debian/$1';/n/t/tpush @mirrors, 'https://mirror.leaseweb.com/debian/$1';/n/t/tpush @mirrors, 'https://mirror.netcologne.de/debian/$1';/n/t/tpush @mirrors, 'https://mirrors.tuna.tsinghua.edu.cn/debian/$1';/n/t/tpush @mirrors, 'https://mirrors.ustc.edu.cn/debian/$1'/n/t} elsif ($mirror =~ /^//@APACHE///(.+)$/) {/n/t/t# 尝试从 Apache 镜像下载/n/t/tpush @mirrors, 'https://dlcdn.apache.org/$1';/n/t/tpush @mirrors, 'https://mirror.netcologne.de/apache.org/$1';/n/t/tpush @mirrors, 'https://mirror.aarnet.edu.au/pub/apache/$1';/n/t/tpush @mirrors, 'https://mirror.csclub.uwaterloo.ca/apache/$1';/n/t/tpush @mirrors, 'https://archive.apache.org/dist/$1';/n/t/tpush @mirrors, 'http://mirror.cogentco.com/pub/apache/$1';/n/t/tpush @mirrors, 'http://mirror.navercorp.com/apache/$1';/n/t/tpush @mirrors, 'http://ftp.jaist.ac.jp/pub/apache/$1';/n/t/tpush @mirrors, 'ftp://apache.cs.utah.edu/apache.org/$1';/n/t/tpush @mirrors, 'ftp://apache.mirrors.ovh.net/ftp.apache.org/dist/$1';/n/t/tpush @mirrors, 'https://mirrors.tuna.tsinghua.edu.cn/apache/$1';/n/t/tpush @mirrors, 'https://mirrors.ustc.edu.cn/apache/$1';/n/t} elsif ($mirror =~ /^//@GITHUB///(.+)$/) {/n/t/t# 尝试从 GitHub 下载/n/t/tfor (1 .. 5) {/n/t/t/tpush @mirrors, 'https://raw.githubusercontent.com/$1';/n/t/t}/n/t} elsif ($mirror =~ /^//@GNU///(.+)$/) {/n/t/t# 尝试从 GNU 镜像下载/n/t/tpush @mirrors, 'https://mirror.csclub.uwaterloo.ca/gnu/$1';/n/t/tpush @mirrors, 'https://mirror.netcologne.de/gnu/$1';/n/t/tpush @mirrors, 'http://ftp.kddilabs.jp/GNU/gnu/$1';/n/t/tpush @mirrors, 'http://www.nic.funet.fi/pub/gnu/gnu/$1';/n/t/tpush @mirrors, 'http://mirror.internode.on.net/pub/gnu/$1';/n/t/tpush @mirrors, 'http://mirror.navercorp.com/gnu/$1';/n/t/tpush @mirrors, 'ftp://mirrors.rit.edu/gnu/$1';/n/t/tpush @mirrors, 'ftp://download.xs4all.nl/pub/gnu/$1';/n/t/tpush @mirrors, 'https://ftp.gnu.org/gnu/$1';/n/t/tpush @mirrors, 'https://mirrors.tuna.tsinghua.edu.cn/gnu/$1';/n/t/tpush @mirrors, 'https://mirrors.ustc.edu.cn/gnu/$1';/n/t} elsif ($mirror =~ /^//@SAVANNAH///(.+)$/) {/n/t/t# 尝试从 Savannah 镜像下载/n/t/tpush @mirrors, 'https://mirror.netcologne.de/savannah/$1';/n/t/tpush @mirrors, 'https://mirror.csclub.uwaterloo.ca/nongnu/$1';/n/t/tpush @mirrors, 'http://ftp.acc.umu.se/mirror/gnu.org/savannah/$1';/n/t/tpush @mirrors, 'http://nongnu.uib.no/$1';/n/t/tpush @mirrors, 'http://ftp.igh.cnrs.fr/pub/nongnu/$1';/n/t/tpush @mirrors, 'ftp://cdimage.debian.org/mirror/gnu.org/savannah/$1';/n/t/tpush @mirrors, 'ftp://ftp.acc.umu.se/mirror/gnu.org/savannah/$1';/n/t} elsif ($mirror =~ /^//@KERNEL///(.+)$/) {/n/t/tmy @extra = ( $1 );/n/t/tif ($filename =~ /linux-//d+//.//d+(?://.//d+)?-rc/) {/n/t/t/tpush @extra, '$extra[0]/testing';/n/t/t} elsif ($filename =~ /linux-(//d+//.//d+(?://.//d+)?)/) {/n/t/t/tpush @extra, '$extra[0]/longterm/v$1';/n/t/t}/n/t/tforeach my $dir (@extra) {/n/t/t/tpush @mirrors, 'https://cdn.kernel.org/pub/$dir';/n/t/t/tpush @mirrors, 'https://download.xs4all.nl/ftp.kernel.org/pub/$dir';/n/t/t/tpush @mirrors, 'https://mirrors.mit.edu/kernel/$dir';/n/t/t/tpush @mirrors, 'http://ftp.nara.wide.ad.jp/pub/kernel.org/$dir';/n/t/t/tpush @mirrors, 'http://www.ring.gr.jp/archives/linux/kernel.org/$dir';/n/t/t/tpush @mirrors, 'ftp://ftp.riken.jp/Linux/kernel.org/$dir';/n/t/t/tpush @mirrors, 'ftp://www.mirrorservice.org/sites/ftp.kernel.org/pub/$dir';/n/t/t/tpush @mirrors, 'https://mirrors.tuna.tsinghua.edu.cn/kernel/$dir';/n/t/t/tpush @mirrors, 'https://mirrors.ustc.edu.cn/kernel.org/$dir';/n/t/t}/n/t} elsif ($mirror =~ /^//@GNOME///(.+)$/) {/n/t/t# 尝试从 GNOME 镜像下载/n/t/tpush @mirrors, 'https://download.gnome.org/sources/$1';/n/t/tpush @mirrors, 'https://mirror.csclub.uwaterloo.ca/gnome/sources/$1';/n/t/tpush @mirrors, 'http://ftp.acc.umu.se/pub/GNOME/sources/$1';/n/t/tpush @mirrors, 'http://ftp.kaist.ac.kr/gnome/sources/$1';/n/t/tpush @mirrors, 'http://www.mirrorservice.org/sites/ftp.gnome.org/pub/GNOME/sources/$1';/n/t/tpush @mirrors, 'http://mirror.internode.on.net/pub/gnome/sources/$1';/n/t/tpush @mirrors, 'http://ftp.belnet.be/ftp.gnome.org/sources/$1';/n/t/tpush @mirrors, 'ftp://ftp.cse.buffalo.edu/pub/Gnome/sources/$1';/n/t/tpush @mirrors, 'ftp://ftp.nara.wide.ad.jp/pub/X11/GNOME/sources/$1';/n/t/tpush @mirrors, 'https://mirrors.ustc.edu.cn/gnome/sources/$1';/n/t} else {/n/t/tpush @mirrors, $mirror;/n/t}/n}/n/n# 添加 OpenWrt 源服务器地址/npush @mirrors, 'https://sources.cdn.openwrt.org';/npush @mirrors, 'https://sources.openwrt.org';/npush @mirrors, 'https://mirror2.openwrt.org/sources';/n/n# 检查目标目录中是否存在目标文件/nif (-f '$target/$filename') {/n/t$hash_cmd and do {/n/t/tif (system('cat /'$target/$filename/' | $hash_cmd > /'$target/$filename.hash/'')) {/n/t/t/tdie 'Failed to generate hash for $filename//n';/n/t/t}/n/n/t/tmy $sum = cat '$target/$filename.hash';/n/t/t$sum =~ /^(/w+)/s*/ or die 'Could not generate file hash//n';/n/t/t$sum = $1;/n/n/t/tcleanup();/n/t/texit 0 if $sum eq $file_hash;/n/n/t/tdie 'Hash of the local file $filename does not match (file: $sum, requested: $file_hash) - deleting download.//n';/n/t/tunlink '$target/$filename';/n/t};/n}/n/n# 选择下载工具/n$download_tool = select_tool();/n/n# 循环尝试从镜像下载文件,直到下载成功/nwhile (!-f '$target/$filename') {/n/tmy $mirror = shift @mirrors;/n/t$mirror or die 'No more mirrors to try - giving up.//n';/n/n/tdownload($mirror, $url_filename, @mirrors);/n/tif (!-f '$target/$filename' && $url_filename ne $filename) {/n/t/tdownload($mirror, $filename, @mirrors);/n/t}/n}/n/n# 设置 Ctrl+C 信号处理函数/n$SIG{INT} = //&cleanup;/n

OpenWrt 下载脚本:使用 curl、wget 或 aria2c 下载文件并验证哈希值