# ========================================================= # MAX Windows Toolkit - 安装 v2rayN 便携版 # 中文界面,统一风格 # ========================================================= $Common = "C:\Setup\windows\lib\common.ps1" if (Test-Path $Common) { . $Common } Write-Header "安装 v2rayN 便携版" Show-AdminWarn $V2rayNUrl = "$Global:RepoBase/apps/V2RayN.zip" $DownloadDir = "C:\Setup\apps" $InstallDir = "C:\Tools\v2rayN" $ZipFile = Join-Path $DownloadDir "V2RayN.zip" try { New-Item -ItemType Directory -Path $DownloadDir -Force -ErrorAction Stop | Out-Null New-Item -ItemType Directory -Path "C:\Tools" -Force -ErrorAction Stop | Out-Null } catch {} # --------------------------------------------------------- # 辅助函数 # --------------------------------------------------------- # 停止 v2rayN 相关进程 function Stop-v2rayNProcesses { Write-Info "正在关闭 v2rayN 相关进程..." $ProcessNames = @("v2rayN", "xray", "v2ray", "sing-box", "hysteria", "tuic-client") foreach ($Name in $ProcessNames) { Get-Process -Name $Name -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue } } # 查找 v2rayN.exe function Find-v2rayNExe { param([string]$Path) $Exe = Get-ChildItem -Path $Path -Filter "v2rayN.exe" -Recurse -ErrorAction SilentlyContinue | Select-Object -First 1 if ($Exe) { return $Exe.FullName } return $null } # 创建快捷方式 function Create-Shortcut { param([string]$TargetPath, [string]$ShortcutPath, [string]$WorkingDirectory, [string]$Description) try { $WScriptShell = New-Object -ComObject WScript.Shell $Shortcut = $WScriptShell.CreateShortcut($ShortcutPath) $Shortcut.TargetPath = $TargetPath $Shortcut.WorkingDirectory = $WorkingDirectory $Shortcut.Description = $Description $Shortcut.Save() Write-OK "快捷方式已创建: $ShortcutPath" } catch { Write-Warn "创建快捷方式失败: $ShortcutPath" Write-Muted $_.Exception.Message } } # 安装 v2rayN function Install-v2rayN { Write-Info "正在下载 v2rayN..." if (-not (Download-File -Url $V2rayNUrl -OutFile $ZipFile)) { Write-Err "下载 v2rayN 失败" Write-Muted "请检查链接: $V2rayNUrl" return } Stop-v2rayNProcesses if (Test-Path $InstallDir) { $BackupDir = "C:\Tools\v2rayN_backup_$(Get-Date -Format 'yyyyMMdd_HHmmss')" Write-Warn "发现现有 v2rayN,正在备份到: $BackupDir" try { Move-Item -Path $InstallDir -Destination $BackupDir -Force } catch { Write-Err "备份现有 v2rayN 文件夹失败" Write-Muted $_.Exception.Message return } } New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null Write-Info "正在解压 v2rayN 到: $InstallDir" try { Expand-Archive -Path $ZipFile -DestinationPath $InstallDir -Force } catch { Write-Err "解压 V2RayN.zip 失败" Write-Muted $_.Exception.Message return } Write-Info "正在解除文件锁定..." try { Get-ChildItem -Path $InstallDir -Recurse -ErrorAction SilentlyContinue | Unblock-File -ErrorAction SilentlyContinue } catch {} $V2rayNExe = Find-v2rayNExe -Path $InstallDir if ([string]::IsNullOrWhiteSpace($V2rayNExe)) { Write-Err "解压后未找到 v2rayN.exe" Write-Warn "请检查 ZIP 包结构" return } $V2rayNWorkDir = Split-Path $V2rayNExe -Parent Write-OK "v2rayN 可执行文件已找到: $V2rayNExe" # 创建桌面和开始菜单快捷方式 $DesktopShortcut = Join-Path ([Environment]::GetFolderPath("Desktop")) "v2rayN.lnk" $StartMenuShortcut = Join-Path $env:ProgramData "Microsoft\Windows\Start Menu\Programs\v2rayN.lnk" Create-Shortcut -TargetPath $V2rayNExe -ShortcutPath $DesktopShortcut -WorkingDirectory $V2rayNWorkDir -Description "v2rayN" Create-Shortcut -TargetPath $V2rayNExe -ShortcutPath $StartMenuShortcut -WorkingDirectory $V2rayNWorkDir -Description "v2rayN" Write-OK "v2rayN 安装完成" } # 添加开机启动 function Add-v2rayNStartup { $V2rayNExe = Find-v2rayNExe -Path $InstallDir if ([string]::IsNullOrWhiteSpace($V2rayNExe)) { Write-Err "未找到 v2rayN.exe,请先安装" return } $StartupPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" Set-ItemProperty -Path $StartupPath -Name "v2rayN" -Type String -Value "`"$V2rayNExe`"" Write-OK "v2rayN 开机启动项已添加" } # 移除开机启动 function Remove-v2rayNStartup { Remove-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" -Name "v2rayN" -ErrorAction SilentlyContinue Write-OK "v2rayN 开机启动项已移除" } # 启动 v2rayN function Start-v2rayN { $V2rayNExe = Find-v2rayNExe -Path $InstallDir if ([string]::IsNullOrWhiteSpace($V2rayNExe)) { Write-Err "未找到 v2rayN.exe,请先安装" return } try { Start-Process -FilePath $V2rayNExe -WorkingDirectory (Split-Path $V2rayNExe -Parent) Write-OK "v2rayN 已启动" } catch { Write-Err "启动 v2rayN 失败" Write-Muted $_.Exception.Message } } # 打开文件夹 function Open-v2rayNFolder { if (Test-Path $InstallDir) { Start-Process explorer.exe $InstallDir } else { Write-Warn "v2rayN 文件夹不存在: $InstallDir" } } # 卸载 v2rayN function Remove-v2rayN { Write-Warn "此操作将删除 v2rayN 文件夹:" Write-Muted $InstallDir Write-Info "" $Confirm = Read-Host "是否继续?输入 YES 确认" if ($Confirm -ne "YES") { Write-Warn "已取消" return } Stop-v2rayNProcesses Remove-v2rayNStartup try { Remove-Item -Path $InstallDir -Recurse -Force -ErrorAction SilentlyContinue $DesktopShortcut = Join-Path ([Environment]::GetFolderPath("Desktop")) "v2rayN.lnk" $StartMenuShortcut = Join-Path $env:ProgramData "Microsoft\Windows\Start Menu\Programs\v2rayN.lnk" Remove-Item $DesktopShortcut -Force -ErrorAction SilentlyContinue Remove-Item $StartMenuShortcut -Force -ErrorAction SilentlyContinue Write-OK "v2rayN 已卸载" } catch { Write-Err "卸载 v2rayN 失败" Write-Muted $_.Exception.Message } } # --------------------------------------------------------- # 主菜单循环 # --------------------------------------------------------- :v2rayLoop while ($true) { Clear-Host Write-Header "安装 v2rayN 便携版" Write-Info "1. 安装/更新 v2rayN" Write-Info "2. 启动 v2rayN" Write-Info "3. 打开 v2rayN 文件夹" Write-Info "4. 添加 v2rayN 开机启动" Write-Info "5. 移除 v2rayN 开机启动" Write-Info "6. 卸载 v2rayN" Write-Info "0. 退出" Write-Info "" $Choice = Read-Host "请选择" Write-Line switch ($Choice) { "1" { Install-v2rayN; Pause-Max } "2" { Start-v2rayN; Pause-Max } "3" { Open-v2rayNFolder; Pause-Max } "4" { Add-v2rayNStartup; Pause-Max } "5" { Remove-v2rayNStartup; Pause-Max } "6" { Remove-v2rayN; Pause-Max } "0" { break :v2rayLoop } default { Write-Err "无效选项"; Pause-Max } } }