refactor: 移除代理检测逻辑(WSL2 镜像模式自动继承 Windows 代理)

WSL2 mirror mode + autoProxy=true 下,WSL2 自动继承 Windows 代理状态
无需脚本干预,是否使用代理由 Windows 侧决定

移除内容:
- -ProxyPort 参数
- Get-V2RayNProxyPort / Set-WindowsProxy / Clear-WindowsProxy 函数
- Step 0 代理检测配置块
- ~/.mcp-proxy.env 生成及所有 source 调用
- git proxy save/restore 逻辑
- npm proxy 配置
- 最终摘要代理信息行

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-05-28 12:09:08 +08:00
parent 464639ef1a
commit 0b71a33bd2
2 changed files with 18 additions and 204 deletions

View File

@@ -4,7 +4,6 @@
WSL2 + Claude Code CLI + Unity MCP + Rust Token Killer 全栈一键部署脚本
.DESCRIPTION
自动完成以下步骤:
0. 检测 v2rayN 代理端口,配置 Windows & WSL2 两侧代理
1. 启用 WSL2 功能 & 安装 Ubuntu 发行版
2. 检测/安装 Windows 本机 Node.jsAI 客户端 MCP 需要)
3. WSL2 内安装 Node.js LTSClaude Code CLI 需要)
@@ -13,8 +12,6 @@
6. 配置 Windows 防火墙放行 MCP Bridge 端口
7. 安装 Rust 工具链 & Token Killer (rtk)
8. 写入 PowerShell Profile 快捷命令
.PARAMETER ProxyPort
v2rayN HTTP 代理端口0 = 自动检测;-1 = 跳过代理
.PARAMETER BridgePort
Unity MCP Bridge 端口,默认 7890
.PARAMETER InstallDir
@@ -30,7 +27,6 @@
已有 WSL2 可普通终端运行,加 -SkipWSL 跳过 WSL2 安装检查。
#>
param(
[int] $ProxyPort = 0,
[int] $BridgePort = 7890,
[string]$InstallDir = "$env:USERPROFILE\unity-mcp-server",
[string]$UnityHubPath = "C:\Program Files\Unity Hub\Unity Hub.exe",
@@ -93,104 +89,10 @@ $UseLmuAuth = ($ANTHROPIC_AUTH_TOKEN -ne "" -and $ANTHROPIC_BASE_URL -ne "https:
Write-Host ""
Write-Host "╔══════════════════════════════════════════════════════════╗" -ForegroundColor Cyan
Write-Host "║ WSL2 + Claude Code CLI + Unity MCP + RTK 全栈部署 ║" -ForegroundColor Cyan
Write-Host "║ AnkleBreaker Unity MCP · v2rayN 代理自动检测" -ForegroundColor Cyan
Write-Host "║ AnkleBreaker Unity MCP · WSL2 Mirror Mode " -ForegroundColor Cyan
Write-Host "╚══════════════════════════════════════════════════════════╝" -ForegroundColor Cyan
Write-Host ""
# ══════════════════════════════════════════════════════════════
# Step 0: v2rayN 代理检测与配置
# ══════════════════════════════════════════════════════════════
Write-Step "0/8 v2rayN 代理检测"
# ── 检测代理端口 ──────────────────────────────────────────────
function Get-V2RayNProxyPort {
# 1. Windows 系统代理注册表v2rayN"设为系统代理"时写入)
try {
$ie = Get-ItemProperty `
"HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" `
-ErrorAction SilentlyContinue
if ($ie.ProxyEnable -eq 1 -and $ie.ProxyServer -match ":(\d+)") {
return [int]$Matches[1]
}
} catch {}
# 2. v2rayN 配置文件
$cfgPaths = @(
"$env:APPDATA\v2rayN\guiNConfig.json",
"$env:LOCALAPPDATA\v2rayN\guiNConfig.json"
)
foreach ($p in $cfgPaths) {
if (Test-Path $p) {
try {
$j = Get-Content $p -Raw | ConvertFrom-Json
if ($j.localPort) { return [int]$j.localPort }
if ($j.httpPort) { return [int]$j.httpPort }
} catch {}
}
}
# 3. 探测常用端口
foreach ($port in @(10809, 10808, 7890, 1080, 8080)) {
try {
$tcp = New-Object System.Net.Sockets.TcpClient
$ar = $tcp.BeginConnect("127.0.0.1", $port, $null, $null)
if ($ar.AsyncWaitHandle.WaitOne(300, $false)) {
$tcp.EndConnect($ar); $tcp.Close(); return $port
}
$tcp.Close()
} catch {}
}
return 0
}
function Set-WindowsProxy {
param([int]$Port)
$url = "http://127.0.0.1:$Port"
git config --global http.proxy $url 2>$null
git config --global https.proxy $url 2>$null
& npm config set proxy $url --location global 2>$null
& npm config set https-proxy $url --location global 2>$null
$env:http_proxy = $url; $env:https_proxy = $url
$env:HTTP_PROXY = $url; $env:HTTPS_PROXY = $url
Write-OK "Windows git/npm/env 代理 -> $url"
}
function Clear-WindowsProxy {
git config --global --unset http.proxy 2>$null
git config --global --unset https.proxy 2>$null
& npm config delete proxy --location global 2>$null
& npm config delete https-proxy --location global 2>$null
"http_proxy","https_proxy","HTTP_PROXY","HTTPS_PROXY" |
ForEach-Object { Remove-Item "Env:\$_" -ErrorAction SilentlyContinue }
}
$resolvedProxyPort = 0
# 灵眸 AI 国内直连无需代理,主动跳过
if ($UseLmuAuth -and $ProxyPort -ne -1) {
Write-Info "灵眸 AI 模式:国内直连,自动跳过代理配置"
Write-Info "如需为其他服务git/npm使用代理请手动指定 -ProxyPort"
$ProxyPort = -1
}
if ($ProxyPort -eq -1) {
Write-Warn "已跳过代理配置(-ProxyPort -1"
} elseif ($ProxyPort -gt 0) {
$resolvedProxyPort = $ProxyPort
Set-WindowsProxy -Port $resolvedProxyPort
Write-OK "使用指定代理端口: $resolvedProxyPort"
} else {
$resolvedProxyPort = Get-V2RayNProxyPort
if ($resolvedProxyPort -gt 0) {
Set-WindowsProxy -Port $resolvedProxyPort
Write-OK "自动检测到 v2rayN 代理端口: $resolvedProxyPort"
} else {
Write-Warn "未检测到活跃代理,将使用直连网络"
Write-Warn "如需代理,请指定: pwsh deploy.ps1 -ProxyPort 10809"
Write-Warn "⚠ 确保 v2rayN 已开启「允许来自局域网的连接」WSL2 需要)"
}
}
# ══════════════════════════════════════════════════════════════
# Step 1: WSL2 安装
# ══════════════════════════════════════════════════════════════
@@ -302,39 +204,6 @@ if ($needRestart) {
Start-Sleep 3
}
# ── 配置 WSL2 侧代理(持久化到 ~/.bashrc ───────────────────
if ($resolvedProxyPort -gt 0) {
Write-Info "配置 WSL2 代理(端口 $resolvedProxyPort..."
# mirrored 模式下 127.0.0.1 即 Windows直接用即可
$proxyEnvContent = "# WSL2 mirrored networking: 127.0.0.1 = Windows host`nexport http_proxy=`"http://127.0.0.1:$resolvedProxyPort`"`nexport https_proxy=`"http://127.0.0.1:$resolvedProxyPort`"`nexport HTTP_PROXY=`"http://127.0.0.1:$resolvedProxyPort`"`nexport HTTPS_PROXY=`"http://127.0.0.1:$resolvedProxyPort`"`nexport no_proxy=`"localhost,127.0.0.1,::1`"`n"
$tmpFile = "$env:TEMP\mcp-proxy.env"
$bytes = [System.Text.Encoding]::UTF8.GetBytes($proxyEnvContent)
[System.IO.File]::WriteAllBytes($tmpFile, $bytes)
Copy-Item $tmpFile "\\wsl.localhost\$WSL_DISTRO\tmp\mcp-proxy-deploy.env" -Force 2>$null
Invoke-WSL "cp /tmp/mcp-proxy-deploy.env ~/.mcp-proxy.env && chmod 644 ~/.mcp-proxy.env && echo OK" | Out-Null
# 写入 ~/.bashrc幂等
Invoke-WSL @'
if ! grep -q 'mcp-proxy.env' ~/.bashrc 2>/dev/null; then
printf '\n# Unity MCP WSL2 proxy (generated by deploy.ps1)\n[ -f ~/.mcp-proxy.env ] && . ~/.mcp-proxy.env\n' >> ~/.bashrc
fi
# 立即应用代理并配置 git
. ~/.mcp-proxy.env 2>/dev/null || true
if [ -n "$http_proxy" ]; then
git config --global http.proxy "$http_proxy"
git config --global https.proxy "$https_proxy"
echo "WSL2 git proxy set: $http_proxy"
fi
true
'@ -IgnoreError | Out-Null
Write-OK "WSL2 代理已写入 ~/.mcp-proxy.env + ~/.bashrc"
Write-Warn "⚠ 请确保 v2rayN 已开启「允许来自局域网的连接」!"
}
# ══════════════════════════════════════════════════════════════
# Step 2: Windows 本机 Node.js 检查AI 客户端 MCP 需要)
# ══════════════════════════════════════════════════════════════
@@ -367,13 +236,6 @@ if ($winNode) {
}
}
# 对 Windows npm 设置代理
if ($resolvedProxyPort -gt 0 -and (Get-Command npm -ErrorAction SilentlyContinue)) {
$proxyUrl = "http://127.0.0.1:$resolvedProxyPort"
& npm config set proxy $proxyUrl --location global 2>$null
& npm config set https-proxy $proxyUrl --location global 2>$null
}
# ══════════════════════════════════════════════════════════════
# Step 3: WSL2 系统依赖 & Node.js
# ══════════════════════════════════════════════════════════════
@@ -396,7 +258,6 @@ if ($nodeVer -match "v\d+") {
} else {
Write-Info "安装 WSL2 Node.js LTS (via nvm)..."
$installNodeCmd = @'
. ~/.mcp-proxy.env 2>/dev/null || true
export DEBIAN_FRONTEND=noninteractive
sudo apt-get install -y -qq curl ca-certificates 2>/dev/null
# 安装 nvm
@@ -448,7 +309,6 @@ if ($claudeVer -notmatch "MISSING") {
$installClaudeCmd = @'
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
. ~/.mcp-proxy.env 2>/dev/null || true
npm install -g @anthropic-ai/claude-code --quiet
which claude
claude --version
@@ -619,7 +479,6 @@ $wslMcpDir = "~/.mcp-servers/unity-mcp-server"
$wslMcpCmd = @'
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
. ~/.mcp-proxy.env 2>/dev/null || true
mkdir -p $HOME/.mcp-servers
if [ -d $HOME/.mcp-servers/unity-mcp-server/.git ]; then
git -C $HOME/.mcp-servers/unity-mcp-server pull --quiet 2>/dev/null || true
@@ -707,7 +566,6 @@ Merge-McpConfig "$env:APPDATA\Code\User\mcp.json" $mcpEntry
$wslMcpCfgCmd = @"
export NVM_DIR="`$HOME/.nvm"
[ -s "`$NVM_DIR/nvm.sh" ] && . "`$NVM_DIR/nvm.sh"
. ~/.mcp-proxy.env 2>/dev/null || true
#
claude mcp remove unity-mcp --scope user 2>/dev/null || true
claude mcp add --scope user unity-mcp node $wslScript -e UNITY_BRIDGE_PORT=$BridgePort
@@ -772,8 +630,6 @@ if ($rustVer -notmatch "MISSING") {
Write-Info "安装 Rust 工具链..."
$installRustCmd = @"
sudo apt-get install -y -qq build-essential pkg-config libssl-dev 2>/dev/null || true
# mirrored 127.0.0.1rustup env
. ~/.mcp-proxy.env 2>/dev/null || true
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs -o /tmp/rustup-init.sh
sh /tmp/rustup-init.sh -y --default-toolchain stable --no-modify-path
source ~/.cargo/env
@@ -792,15 +648,7 @@ if ($rtkVer -notmatch "MISSING") {
Write-Info "安装 rtk (Rust Token Killer)..."
$installRtkCmd = @"
. ~/.cargo/env 2>/dev/null || true
# mirrored CARGO_NET_GIT_FETCH_WITH_CLI cargo git
# git clone github git proxy https
_saved_http=`$(git config --global http.proxy 2>/dev/null)
_saved_https=`$(git config --global https.proxy 2>/dev/null)
git config --global --unset http.proxy 2>/dev/null || true
git config --global --unset https.proxy 2>/dev/null || true
CARGO_NET_GIT_FETCH_WITH_CLI=true cargo install --git https://github.com/rtk-ai/rtk 2>&1 | tail -5
[ -n "`$_saved_http" ] && git config --global http.proxy "`$_saved_http" || true
[ -n "`$_saved_https" ] && git config --global https.proxy "`$_saved_https" || true
"@
Invoke-WSL $installRtkCmd -IgnoreError
$rtkCheck = Invoke-WSL ". ~/.cargo/env 2>/dev/null; rtk --version 2>/dev/null || echo FAILED" -IgnoreError
@@ -871,11 +719,6 @@ foreach ($it in $items) {
Write-Host (" {0,-16} {1}" -f $it[0], $(if ($it[1]) { $it[1] } else { "(未安装)" })) -ForegroundColor White
}
if ($resolvedProxyPort -gt 0) {
Write-Host ""
Write-Host " v2rayN 代理 127.0.0.1:$resolvedProxyPort (WSL2 通过 Windows 主机 IP 访问)" -ForegroundColor DarkGray
}
Write-Host ""
Write-Host " ━━━━ 后续步骤 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Cyan
Write-Host " 1) 在 Unity 项目中安装 MCP Plugin每个项目一次" -ForegroundColor White