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

@@ -2,7 +2,7 @@
WSL2 + Claude Code CLI + Unity MCP + Rust Token Killer **全栈一键部署方案**,适用于 Windows 11 开发环境。
支持 **v2rayN 代理自动检测**、**WSL2 代理透传**、**防火墙自动放行**
支持 **WSL2 镜像网络模式**`autoProxy=true`WSL2 自动继承 Windows 代理状态,无需脚本干预
## 组件清单
@@ -47,14 +47,8 @@ cd path\to\claude-dev-stack
cp .env.example .env
notepad .env
# 3. 运行部署脚本(灵眸模式自动跳过代理检测)
# 3. 运行部署脚本
pwsh .\deploy.ps1
# 手动指定代理端口(使用官方 Anthropic API 时需要)
pwsh .\deploy.ps1 -ProxyPort 10809
# 不使用代理(直连)
pwsh .\deploy.ps1 -ProxyPort -1
```
首次安装 WSL2 特性后可能需要**重启系统**,重启后重新执行脚本。
@@ -71,45 +65,25 @@ pwsh .\deploy.ps1 -SkipWSL
# 在 WSL2 Ubuntu 终端中执行
cp .env.example .env
nano .env
# 如需代理(端口由 deploy.ps1 写入 ~/.mcp-proxy.env或手动指定
PROXY_PORT=10809 bash wsl-setup.sh
bash wsl-setup.sh
```
---
## 代理说明v2rayN
## 代理说明
### 自动检测顺序
### WSL2 镜像模式自动继承 Windows 代理
1. Windows 系统代理注册表v2rayN「设为系统代理」时写入
2. v2rayN 配置文件(`%APPDATA%\v2rayN\guiNConfig.json`
3. 探测常用端口:`10809, 10808, 7890, 1080, 8080`
脚本自动配置 `~/.wslconfig` 启用 WSL2 镜像网络模式:
### WSL2 代理透传原理
```
WSL2 Ubuntu
→ [Windows 主机 vEthernet(WSL) IP]:10809
→ v2rayN
→ 互联网
```ini
[wsl2]
networkingMode=mirrored
autoProxy=true
```
**必须**在 v2rayN 中开启:
> 参数设置 → **「允许来自局域网的连接」** ✅
脚本自动完成:
- 写入 WSL2 `~/.mcp-proxy.env`(每次 shell 启动自动生效)
- 配置 WSL2 `git` + `npm` 代理
- `apt-get` 安装阶段同样走代理
### 代理覆盖范围
| 操作 | 是否走代理 |
|------|-----------|
| Windows `git clone` / `npm install` | ✅ |
| WSL2 `apt-get` / `git clone` / `npm install` / `cargo install` | ✅ |
| MCP Server ↔ AI 客户端stdio | ❌ 本地通信,无需代理 |
| MCP Server ↔ Unity Pluginlocalhost | ❌ 本地通信,无需代理 |
`autoProxy=true`WSL2 自动继承 Windows 系统代理状态。
**是否使用代理由 Windows 侧决定**(例如通过 v2rayN「设为系统代理」无需脚本干预。
---
@@ -133,7 +107,6 @@ WSL2 Ubuntu
> | `CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC` | `1` | 关闭遥测/自动更新检查 |
>
> - Token 写入 `settings.json` 的 `env` 块(不写入 `~/.bashrc`,避免与 `ANTHROPIC_API_KEY` 冲突)
> - 部署脚本自动跳过代理检测(国内直连无需代理)
> - 旧 `ANTHROPIC_API_KEY` / `ANTHROPIC_BASE_URL` 变量从 `~/.bashrc` 自动清除
### 备选Anthropic 官方 API
@@ -367,19 +340,17 @@ wsl -d Ubuntu -- bash -c "cd ~/.mcp-servers/unity-mcp-server && git pull && npm
**问题npm install 安装超时(国内网络)**
```bash
# 方案一:通过 v2rayN 代理(推荐)
pwsh .\deploy.ps1 -ProxyPort 10809
# 方案一:在 Windows 侧开启系统代理v2rayN「设为系统代理」WSL2 自动继承
# 方案二WSL2 内配置镜像
npm config set registry https://registry.npmmirror.com
```
**问题WSL2 无法连接 v2rayN 代理**
**问题WSL2 无法访问外网**
```
解决:
1. v2rayN → 参数设置 → 勾选「允许来自局域网的连接」
2. 确认防火墙未拦截 v2rayN 监听端口
3. WSL2 内测试curl -I https://github.com --proxy http://$(ip route | grep default | awk '{print $3}'):10809
1. 确认 Windows 侧代理已开启系统代理(v2rayN → 参数设置 → 勾选「允许来自局域网的连接」
2. 确认 ~/.wslconfig 包含 networkingMode=mirrored 和 autoProxy=true
3. 重启 WSL2wsl --shutdown再重新启动
```
**问题Unity 版本兼容性**

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