From 19373f4b9bf0a3037a84a89ad5090ee40394eaa3 Mon Sep 17 00:00:00 2001 From: Richard Kuhnt Date: Thu, 6 Sep 2018 11:57:09 +0200 Subject: [PATCH] Add metalink support for aria2c (#2560) This should fix #2480 and fix https://github.com/lukesampson/scoop-extras/issues/1126 --- lib/autoupdate.ps1 | 53 +++++++++++++++++++++++++++++++++++++--------- lib/install.ps1 | 36 +++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 10 deletions(-) diff --git a/lib/autoupdate.ps1 b/lib/autoupdate.ps1 index 43fab708f..7f69b948e 100644 --- a/lib/autoupdate.ps1 +++ b/lib/autoupdate.ps1 @@ -91,6 +91,33 @@ function find_hash_in_json([String] $url, [String] $basename, [String] $jsonpath return format_hash $hash } +function find_hash_in_headers([String] $url) { + $hash = $null + + try { + $req = [System.Net.WebRequest]::Create($url) + $req.Referer = (strip_filename $url) + $req.AllowAutoRedirect = $false + $req.UserAgent = (Get-UserAgent) + $req.Timeout = 2000 + $req.Method = 'HEAD' + $res = $req.GetResponse() + if(([int]$response.StatusCode -ge 300) -and ([int]$response.StatusCode -lt 400)) { + if($res.Headers['Digest'] -match 'SHA-256=([^,]+)' -or $res.Headers['Digest'] -match 'SHA=([^,]+)' -or $res.Headers['Digest'] -match 'MD5=([^,]+)') { + $hash = ([System.Convert]::FromBase64String($matches[1]) | ForEach-Object { $_.ToString('x2') }) -join '' + debug $hash + } + } + $res.Close() + } catch [system.net.webexception] { + write-host -f darkred $_ + write-host -f darkred "URL $url is not valid" + return + } + + return format_hash $hash +} + function get_hash_for_app([String] $app, $config, [String] $version, [String] $url, [Hashtable] $substitutions) { $hash = $null @@ -131,16 +158,22 @@ function get_hash_for_app([String] $app, $config, [String] $version, [String] $u $hash = find_hash_in_textfile $hashfile_url $basename '"$basename":.*?"sha1":\s"([a-fA-F0-9]{40})"' } - if ($hashmode -eq 'extract') { - $hash = find_hash_in_textfile $hashfile_url $basename $config.find - } - - if ($hashmode -eq 'json') { - $hash = find_hash_in_json $hashfile_url $basename $config.jp - } - - if ($hashmode -eq 'rdf') { - $hash = find_hash_in_rdf $hashfile_url $basename + switch ($hashmode) { + 'extract' { + $hash = find_hash_in_textfile $hashfile_url $basename $config.find + } + 'json' { + $hash = find_hash_in_json $hashfile_url $basename $config.jp + } + 'rdf' { + $hash = find_hash_in_rdf $hashfile_url $basename + } + 'metalink' { + $hash = find_hash_in_headers $url + if(!$hash) { + $hash = find_hash_in_textfile "$url.meta4" + } + } } if($hash) { diff --git a/lib/install.ps1 b/lib/install.ps1 index 4783e82df..b058b5313 100644 --- a/lib/install.ps1 +++ b/lib/install.ps1 @@ -190,6 +190,31 @@ function aria_exit_code($exitcode) { return $codes[$exitcode] } +function get_filename_from_metalink($file) { + $bytes = get_magic_bytes_pretty $file '' + # check if file starts with '