VS Code Remote SSH 连接 Windows 服务器卡在"下载 VS Code 服务器":prcdn DNS 解析失败的诊断与 BITS 断点续传

193 阅读4分钟

环境

项目
本地系统Windows 11
远程系统Windows Server 2025 Datacenter (Build 26100)
VS CodeInsiders 1.110.0 (404102fc70f6f15e1aba226b6d56f4c75bd5fa4e)
Remote SSH 扩展ms-vscode-remote.remote-ssh 0.122.0
连接方式OpenSSH,公钥认证

现象

VS Code Remote SSH 连接远程 Windows 服务器时,永久卡在"正在下载 VS Code 服务器",进度不动,最终超时失败。SSH 连接本身正常(ssh 上科大A服务器 "echo connected" 能输出 connected)。

诊断过程

1. 定位 DNS 解析问题

VS Code Server 下载流程:update.code.visualstudio.com → 302 重定向 → vscode.download.prcdn.microsoft.com

在远程服务器上用 nslookup 测试:

nslookup vscode.download.prcdn.microsoft.com
*** UnKnown can't find vscode.download.prcdn.microsoft.com: Non-existent domain

根因找到:远程服务器的内网 DNS(192.168.1.5)无法解析 prcdn.microsoft.com 域名。

2. 排查可用的 CDN

微软 VS Code 有多个 CDN 域名。逐一在远程服务器上测试 DNS 解析:

CDN 域名DNS 解析说明
vscode.download.prcdn.microsoft.com失败默认重定向目标
az764295.vo.msecnd.net成功旧版 CDN,本地也无法解析
vscode.download.prss.microsoft.com成功解析到金山云 CDN 节点

在远程服务器上验证 prss 域名可达:

# 在远程服务器上执行
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
(Invoke-WebRequest -Uri 'https://vscode.download.prss.microsoft.com/dbazure/download/insider/<commit>/vscode-server-win32-x64.zip' -Method Head -UseBasicParsing -TimeoutSec 10).StatusCode
# 返回 200

3. 发现连接不稳定问题

虽然 prss CDN 可达,但直接用 WebClient.DownloadFile() 下载时,约在传输 25MB(~39%)时连接中断,zip 文件不完整,无法解压。多次尝试均在相同位置断开,说明是 CDN 边缘节点的连接保持问题。

4. 排除镜像方案

批量测试了 10+ 个国内镜像(华为云、阿里云、腾讯云、npmmirror、中科大、山大等),Insiders 版本均未被同步,全部 404 或返回 HTML 错误页。镜像方案仅适用于 Stable 版。

解决方案:BITS 断点续传

Windows 自带的 BITS(后台智能传输服务)原生支持断点续传,连接中断后自动恢复。将以下脚本保存到远程服务器并执行:

# install-vscode-server.ps1
# 用法:修改 $commit 为你的 VS Code commit ID,然后在远程服务器上执行

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$commit = "404102fc70f6f15e1aba226b6d56f4c75bd5fa4e"  # ← 替换为你的 commit ID
$url = "https://vscode.download.prss.microsoft.com/dbazure/download/insider/$commit/vscode-server-win32-x64.zip"
# Stable 版本把 insider 改为 stable

$dir = "$env:USERPROFILE\.vscode-server-insiders\bin\$commit"
# Stable 版本用 .vscode-server 而非 .vscode-server-insiders
$zip = "$dir\vscode-server.zip"

New-Item -ItemType Directory -Force $dir | Out-Null
Remove-Item $zip -Force -ErrorAction SilentlyContinue

# BITS 下载,自动断点续传
Write-Host "Downloading via BITS..."
$job = Start-BitsTransfer -Source $url -Destination $zip -Asynchronous
$retries = 0
while ($job.JobState -ne 'Transferred' -and $retries -lt 3) {
    if ($job.JobState -eq 'TransientError' -or $job.JobState -eq 'Error') {
        Write-Host "Transfer interrupted, resuming... (attempt $($retries+1))"
        Resume-BitsTransfer -BitsJob $job -Asynchronous
        $retries++
    }
    $pct = if ($job.BytesTotal -gt 0) { [math]::Round($job.BytesTransferred/$job.BytesTotal*100,1) } else { 0 }
    Write-Host "  $pct% ($($job.BytesTransferred) / $($job.BytesTotal))"
    Start-Sleep -Seconds 3
}
if ($job.JobState -eq 'Transferred') {
    Complete-BitsTransfer -BitsJob $job
    Write-Host "Download complete!"
} else {
    Write-Host "BITS failed: $($job.JobState)"; exit 1
}

# 解压
Write-Host "Extracting..."
Expand-Archive -Path $zip -DestinationPath $dir -Force
Remove-Item $zip -Force

# 如果解压后多了一层目录,提升到上层
$sub = Get-ChildItem $dir -Directory | Select-Object -First 1
if ($sub) {
    Get-ChildItem $sub.FullName | Move-Item -Destination $dir -Force
    Remove-Item $sub.FullName -Recurse -Force
}

Write-Host "Installed! Verify:"
Get-ChildItem $dir | Select-Object Name

获取 Commit ID

在本地 VS Code 中按 Ctrl+Shift+P → 输入 About → 找到"提交"后面的 40 位哈希值。 或者用命令行:

code-insiders --version   # 第二行就是 commit ID
# 或者 code --version(Stable 版)

执行方式

可以通过 SSH 从本地推送并执行:

scp install-vscode-server.ps1 "远程主机:D:/Users/用户名/install-vscode-server.ps1"
ssh 远程主机 "powershell -ExecutionPolicy Bypass -File D:\Users\用户名\install-vscode-server.ps1"

实际效果

Downloading via BITS...
  78.3% (50069504 / 63943408) - Transferring
  91.9% (58760060 / 63943408) - Transferring   ← 在此断连
Transfer interrupted, resuming... (attempt 1)  ← BITS 自动续传
  92% (58825596 / 63943408) - Connecting
  98.2% (62763418 / 63943408) - Transferring   ← 续传再次断连后再次恢复
Download complete!
Downloaded 63943408 bytes                       ← 完整文件
Extracting...
Installed!

安装完成后重启 VS Code 重新连接,不再卡在下载阶段。

现有方案对比

网上大量文章都是针对 Linux 远程服务器,方案为"手动 wget 下载 + scp 传输 + tar 解压"。本文场景的区别:

差异点网上常见方案本文场景
远程 OSLinuxWindows Server
失败原因服务器无外网有外网但 prcdn DNS 无法解析
下载格式.tar.gz.zip
传输问题CDN 连接 ~25MB 后中断
解决手段scp + tarBITS 断点续传
适用版本StableStable + Insiders(镜像站不同步 Insiders)

关键信息速查

  • VS Code Server 下载 URL 模板:https://vscode.download.prss.microsoft.com/dbazure/download/{quality}/{commit}/vscode-server-{os}-{arch}.zip
    • {quality}stableinsider
    • {os}-{arch}win32-x64linux-x64linux-arm64
  • 安装目录:%USERPROFILE%\.vscode-server-insiders\bin\{commit}\(Insiders)或 %USERPROFILE%\.vscode-server\bin\{commit}\(Stable)
  • VS Code Remote SSH 扩展中,remote.SSH.useOSSremote.SSH.ossDownloadUrl 可以覆盖 Linux 远程的下载源,但 Windows 远程的 PowerShell 下载脚本硬编码了 update.code.visualstudio.com,无法通过设置修改