mirror of
https://github.com/ScoopInstaller/Scoop.git
synced 2025-12-12 11:06:34 +00:00
Add metalink support for aria2c (#2560)
This should fix #2480 and fix https://github.com/lukesampson/scoop-extras/issues/1126
This commit is contained in:
@@ -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,17 +158,23 @@ 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') {
|
||||
switch ($hashmode) {
|
||||
'extract' {
|
||||
$hash = find_hash_in_textfile $hashfile_url $basename $config.find
|
||||
}
|
||||
|
||||
if ($hashmode -eq 'json') {
|
||||
'json' {
|
||||
$hash = find_hash_in_json $hashfile_url $basename $config.jp
|
||||
}
|
||||
|
||||
if ($hashmode -eq 'rdf') {
|
||||
'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) {
|
||||
# got one!
|
||||
|
||||
@@ -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 '<?xml'
|
||||
if(!($bytes.StartsWith('3c3f786d6c'))) {
|
||||
return $null
|
||||
}
|
||||
|
||||
# Add System.Xml for reading metalink files
|
||||
Add-Type -AssemblyName 'System.Xml'
|
||||
$xr = [System.Xml.XmlReader]::Create($file)
|
||||
$filename = $null
|
||||
try {
|
||||
$xr.ReadStartElement('metalink')
|
||||
if($xr.ReadToFollowing('file') -and $xr.MoveToFirstAttribute()) {
|
||||
$filename = $xr.Value
|
||||
}
|
||||
} catch [System.Xml.XmlException] {
|
||||
return $null
|
||||
} finally {
|
||||
$xr.Close()
|
||||
}
|
||||
|
||||
return $filename
|
||||
}
|
||||
|
||||
function dl_with_cache_aria2($app, $version, $manifest, $architecture, $dir, $cookies = $null, $use_cache = $true, $check_hash = $true) {
|
||||
$data = @{}
|
||||
$urls = @(url $manifest $architecture)
|
||||
@@ -212,6 +237,11 @@ function dl_with_cache_aria2($app, $version, $manifest, $architecture, $dir, $co
|
||||
"--console-log-level=warn"
|
||||
"--enable-color=false"
|
||||
"--no-conf=true"
|
||||
"--follow-metalink=true"
|
||||
"--metalink-preferred-protocol=https"
|
||||
"--min-tls-version=TLSv1.2"
|
||||
"--stop-with-process=$PID"
|
||||
"--continue"
|
||||
)
|
||||
|
||||
if($cookies) {
|
||||
@@ -300,6 +330,12 @@ function dl_with_cache_aria2($app, $version, $manifest, $architecture, $dir, $co
|
||||
|
||||
foreach($url in $urls) {
|
||||
|
||||
$metalink_filename = get_filename_from_metalink $data.$url.source
|
||||
if($metalink_filename) {
|
||||
Remove-Item $data.$url.source -Force
|
||||
Rename-Item -Force (Join-Path -Path $cachedir -ChildPath $metalink_filename) $data.$url.source
|
||||
}
|
||||
|
||||
# run hash checks
|
||||
if($check_hash) {
|
||||
$manifest_hash = hash_for_url $manifest $url $architecture
|
||||
|
||||
Reference in New Issue
Block a user