Retry Node downloads with Windows trust
This commit is contained in:
@@ -6,9 +6,11 @@ import re
|
||||
import hashlib
|
||||
import os
|
||||
import shutil
|
||||
import ssl
|
||||
import subprocess
|
||||
import tempfile
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import zipfile
|
||||
from dataclasses import dataclass
|
||||
@@ -834,9 +836,16 @@ def install_node(
|
||||
) -> tuple[Path, Path, str]:
|
||||
root = Path(install_root) if install_root is not None else default_maven_install_root()
|
||||
open_url = opener or urllib.request.urlopen
|
||||
allow_windows_fallback = opener is None and os.name == "nt"
|
||||
on_output(f"正在读取 Node.js 官方版本索引,项目要求: {' 且 '.join(requirements)}")
|
||||
with open_url(f"{NODE_DIST_BASE}/index.json", timeout=30) as response:
|
||||
release = resolve_node_release(response.read(), requirements)
|
||||
index_data = _read_url_bytes(
|
||||
f"{NODE_DIST_BASE}/index.json",
|
||||
open_url,
|
||||
30,
|
||||
on_output,
|
||||
allow_windows_fallback=allow_windows_fallback,
|
||||
)
|
||||
release = resolve_node_release(index_data, requirements)
|
||||
|
||||
version = release.version
|
||||
directory_name = f"node-v{version}-win-x64"
|
||||
@@ -855,8 +864,21 @@ def install_node(
|
||||
temp_dir = Path(temporary)
|
||||
archive_path = temp_dir / archive_name
|
||||
on_output(f"已自动选择 Node.js {version},正在从官方地址下载...")
|
||||
_download_to_file(archive_url, archive_path, open_url, on_output)
|
||||
expected_checksum = _download_node_checksum(checksum_url, archive_name, open_url)
|
||||
_download_to_file_with_windows_fallback(
|
||||
archive_url,
|
||||
archive_path,
|
||||
open_url,
|
||||
on_output,
|
||||
allow_windows_fallback=allow_windows_fallback,
|
||||
)
|
||||
checksum_data = _read_url_bytes(
|
||||
checksum_url,
|
||||
open_url,
|
||||
30,
|
||||
on_output,
|
||||
allow_windows_fallback=allow_windows_fallback,
|
||||
)
|
||||
expected_checksum = _parse_node_checksum(checksum_data, archive_name)
|
||||
actual_checksum = _sha256(archive_path)
|
||||
if actual_checksum.lower() != expected_checksum.lower():
|
||||
raise RuntimeError("Node.js 安装包 SHA-256 校验失败,已停止安装。")
|
||||
@@ -872,13 +894,8 @@ def install_node(
|
||||
return node_executable, npm_executable, version
|
||||
|
||||
|
||||
def _download_node_checksum(
|
||||
url: str,
|
||||
archive_name: str,
|
||||
opener: Callable[..., BinaryIO],
|
||||
) -> str:
|
||||
with opener(url, timeout=30) as response:
|
||||
text = response.read().decode("ascii", errors="strict")
|
||||
def _parse_node_checksum(data: bytes, archive_name: str) -> str:
|
||||
text = data.decode("ascii", errors="strict")
|
||||
for line in text.splitlines():
|
||||
parts = line.split()
|
||||
if len(parts) >= 2 and parts[-1].lstrip("*") == archive_name:
|
||||
@@ -888,6 +905,97 @@ def _download_node_checksum(
|
||||
raise RuntimeError(f"Node.js 校验清单中未找到 {archive_name}。")
|
||||
|
||||
|
||||
def _read_url_bytes(
|
||||
url: str,
|
||||
opener: Callable[..., BinaryIO],
|
||||
timeout: int,
|
||||
on_output: Callable[[str], None],
|
||||
*,
|
||||
allow_windows_fallback: bool,
|
||||
) -> bytes:
|
||||
try:
|
||||
with opener(url, timeout=timeout) as response:
|
||||
return response.read()
|
||||
except Exception as exc:
|
||||
if not allow_windows_fallback or not _is_certificate_verification_failure(exc):
|
||||
raise
|
||||
on_output("Python 未信任当前网络证书,正在改用 Windows 系统证书存储重试下载...")
|
||||
with tempfile.TemporaryDirectory(prefix="autodeploy-windows-download-") as temporary:
|
||||
destination = Path(temporary) / "download.bin"
|
||||
_download_with_windows_certificates(url, destination, timeout)
|
||||
return destination.read_bytes()
|
||||
|
||||
|
||||
def _download_to_file_with_windows_fallback(
|
||||
url: str,
|
||||
destination: Path,
|
||||
opener: Callable[..., BinaryIO],
|
||||
on_output: Callable[[str], None],
|
||||
*,
|
||||
allow_windows_fallback: bool,
|
||||
) -> None:
|
||||
try:
|
||||
_download_to_file(url, destination, opener, on_output)
|
||||
except Exception as exc:
|
||||
if not allow_windows_fallback or not _is_certificate_verification_failure(exc):
|
||||
raise
|
||||
destination.unlink(missing_ok=True)
|
||||
on_output("Python 未信任当前网络证书,正在改用 Windows 系统证书存储重试下载...")
|
||||
_download_with_windows_certificates(url, destination, 60)
|
||||
|
||||
|
||||
def _is_certificate_verification_failure(exc: BaseException) -> bool:
|
||||
if isinstance(exc, ssl.SSLCertVerificationError):
|
||||
return True
|
||||
reason = getattr(exc, "reason", None)
|
||||
if isinstance(reason, ssl.SSLCertVerificationError):
|
||||
return True
|
||||
message = str(exc).upper()
|
||||
return "CERTIFICATE_VERIFY_FAILED" in message or "SELF-SIGNED CERTIFICATE" in message
|
||||
|
||||
|
||||
def _download_with_windows_certificates(url: str, destination: Path, timeout: int) -> None:
|
||||
powershell = shutil.which("powershell.exe") or str(
|
||||
Path(os.environ.get("SystemRoot", r"C:\Windows"))
|
||||
/ "System32"
|
||||
/ "WindowsPowerShell"
|
||||
/ "v1.0"
|
||||
/ "powershell.exe"
|
||||
)
|
||||
script = (
|
||||
"& { param([string]$Url,[string]$Destination,[int]$TimeoutSeconds) "
|
||||
"$ErrorActionPreference='Stop'; $ProgressPreference='SilentlyContinue'; "
|
||||
"Invoke-WebRequest -UseBasicParsing -Uri $Url -OutFile $Destination -TimeoutSec $TimeoutSeconds }"
|
||||
)
|
||||
completed = subprocess.run(
|
||||
[
|
||||
powershell,
|
||||
"-NoProfile",
|
||||
"-NonInteractive",
|
||||
"-ExecutionPolicy",
|
||||
"Bypass",
|
||||
"-Command",
|
||||
script,
|
||||
url,
|
||||
str(destination),
|
||||
str(timeout),
|
||||
],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
text=True,
|
||||
errors="replace",
|
||||
shell=False,
|
||||
creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0) if os.name == "nt" else 0,
|
||||
)
|
||||
if completed.returncode != 0 or not destination.is_file() or destination.stat().st_size == 0:
|
||||
detail = completed.stdout.strip() or f"退出码 {completed.returncode}"
|
||||
raise RuntimeError(
|
||||
"Windows 系统证书存储也无法完成 HTTPS 下载。"
|
||||
"请将网络代理的根证书安装到 Windows 受信任的根证书颁发机构。"
|
||||
f"\n{detail}"
|
||||
)
|
||||
|
||||
|
||||
def _node_version_matches(version: str, requirement: str, lts: object) -> bool:
|
||||
text = requirement.strip().lower()
|
||||
if text in {"", "*", "node", "latest"}:
|
||||
|
||||
Reference in New Issue
Block a user