# SSH 隧道管理器 一键安装(Windows) # 用法: powershell -c "irm https://gitea.xbay.cc/leefer/ssh-e2e/raw/branch/main/install.ps1 | iex" $ErrorActionPreference = "Stop" $BaseUrl = "https://gitea.xbay.cc/leefer/ssh-e2e" $AppDir = Join-Path $env:USERPROFILE "ssh-e2e" function Say($m) { Write-Host "===> $m" -ForegroundColor Green } function Die($m) { Write-Host "错误: $m" -ForegroundColor Red; Read-Host "按回车退出"; exit 1 } # ---------- 下载源码 ---------- Say "下载最新代码到 $AppDir ..." New-Item -ItemType Directory -Force -Path $AppDir | Out-Null $gotCode = $false if (Get-Command git -ErrorAction SilentlyContinue) { # git 会把正常进度写到 stderr,EAP=Stop 会误杀,此段临时放开 $ErrorActionPreference = "Continue" if (Test-Path (Join-Path $AppDir ".git")) { git -C $AppDir fetch --all --quiet git -C $AppDir reset --hard origin/main --quiet if ($LASTEXITCODE -eq 0) { $gotCode = $true } } else { git clone --depth 1 --quiet "$BaseUrl.git" $AppDir if ($LASTEXITCODE -eq 0) { $gotCode = $true } } $ErrorActionPreference = "Stop" if (-not $gotCode -and (Test-Path (Join-Path $AppDir "main.py"))) { $gotCode = $true } } if (-not $gotCode) { # git 不可用或失败:下载 zip 归档 $zip = Join-Path $env:TEMP "ssh-e2e-main.zip" [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 Invoke-WebRequest "$BaseUrl/archive/main.zip" -OutFile $zip -UseBasicParsing $tmp = Join-Path $env:TEMP "ssh-e2e-extract" if (Test-Path $tmp) { Remove-Item $tmp -Recurse -Force } Expand-Archive $zip -DestinationPath $tmp $inner = Get-ChildItem $tmp | Where-Object PSIsContainer | Select-Object -First 1 Get-ChildItem $inner.FullName | Move-Item -Destination $AppDir -Force Remove-Item $tmp, $zip -Recurse -Force -ErrorAction SilentlyContinue } if (-not (Test-Path (Join-Path $AppDir "main.py"))) { Die "代码下载失败,请检查网络" } # ---------- Python 检查 ---------- $py = Get-Command python -ErrorAction SilentlyContinue if (-not $py) { Die "未找到 python,请先安装 Python 3.10+(https://www.python.org/downloads/)" } # ---------- 虚拟环境 ---------- if (-not (Test-Path (Join-Path $AppDir ".venv\Scripts\pythonw.exe"))) { Say "创建虚拟环境并安装依赖(首次约 1-2 分钟)..." python -m venv (Join-Path $AppDir ".venv") if ($LASTEXITCODE -ne 0) { Die "创建虚拟环境失败" } & (Join-Path $AppDir ".venv\Scripts\python.exe") -m pip install --quiet --disable-pip-version-check -r (Join-Path $AppDir "requirements.txt") if ($LASTEXITCODE -ne 0) { Die "依赖安装失败,请检查网络" } } elseif (-not (Test-Path (Join-Path $AppDir ".venv\Lib\site-packages\pystray"))) { & (Join-Path $AppDir ".venv\Scripts\python.exe") -m pip install --quiet --disable-pip-version-check -r (Join-Path $AppDir "requirements.txt") } # ---------- ssh-tunnel 命令(命令行向导,与 GUI 共用配置) ---------- $cmdPath = Join-Path $AppDir "ssh-tunnel.cmd" if (-not (Test-Path $cmdPath)) { Say "缺少 ssh-tunnel.cmd(仓库已更新,请重新运行安装)" } $userPath = [Environment]::GetEnvironmentVariable("Path", "User") if ($userPath -notlike "*$AppDir*") { [Environment]::SetEnvironmentVariable("Path", ($userPath.TrimEnd(";") + ";" + $AppDir), "User") Say "已把 $AppDir 加入用户 PATH — 新开终端后可直接输入 ssh-tunnel" } # ---------- 桌面快捷方式 ---------- try { $ws = New-Object -ComObject WScript.Shell $lnk = $ws.CreateShortcut([IO.Path]::Combine([Environment]::GetFolderPath("Desktop"), "SSH 隧道管理器.lnk")) $lnk.TargetPath = Join-Path $AppDir ".venv\Scripts\pythonw.exe" $lnk.Arguments = '"' + (Join-Path $AppDir "main.py") + '"' $lnk.WorkingDirectory = $AppDir $lnk.IconLocation = Join-Path $AppDir "ui\app.ico" $lnk.Save() Say "已创建桌面快捷方式" } catch { } # ---------- 启动 ---------- Say "启动 SSH 隧道管理器 ..." Start-Process -FilePath (Join-Path $AppDir ".venv\Scripts\pythonw.exe") ` -ArgumentList ('"' + (Join-Path $AppDir "main.py") + '"') -WorkingDirectory $AppDir Say "完成!窗口即将弹出;点最小化/关闭会驻留系统托盘。"