173 lines
6.5 KiB
PowerShell
173 lines
6.5 KiB
PowerShell
Add-Type -AssemblyName System.Windows.Forms
|
|
Add-Type -AssemblyName System.Drawing
|
|
|
|
# === 配置 ===
|
|
$PORT = 8788
|
|
$NPM_PATH = "C:\Users\12914\AppData\Roaming\npm"
|
|
$NODE_PATH = "C:\Program Files\nodejs"
|
|
|
|
# === 辅助函数 ===
|
|
function Get-ProxyStatus {
|
|
$conn = Get-NetTCPConnection -LocalPort $PORT -ErrorAction SilentlyContinue
|
|
if ($conn) {
|
|
$proc = Get-Process -Id $conn.OwningProcess -ErrorAction SilentlyContinue
|
|
return @{ Running = $true; PID = $conn.OwningProcess; ProcessName = $proc.ProcessName }
|
|
}
|
|
return @{ Running = $false; PID = 0; ProcessName = "" }
|
|
}
|
|
|
|
function Update-Status {
|
|
$status = Get-ProxyStatus
|
|
if ($status.Running) {
|
|
$lblStatus.Text = "状态: 运行中 | PID: $($status.PID)"
|
|
$lblStatus.ForeColor = [System.Drawing.Color]::FromArgb(34, 139, 34)
|
|
$btnStart.Enabled = $false
|
|
$btnStop.Enabled = $true
|
|
$btnRestart.Enabled = $true
|
|
} else {
|
|
$lblStatus.Text = "状态: 未运行"
|
|
$lblStatus.ForeColor = [System.Drawing.Color]::FromArgb(178, 34, 34)
|
|
$btnStart.Enabled = $true
|
|
$btnStop.Enabled = $false
|
|
$btnRestart.Enabled = $false
|
|
}
|
|
}
|
|
|
|
function Start-Proxy {
|
|
$env:Path = $env:Path + ";$NODE_PATH;$NPM_PATH"
|
|
$psi = New-Object System.Diagnostics.ProcessStartInfo
|
|
$psi.FileName = "cmd.exe"
|
|
$psi.Arguments = "/c mimo2codex --no-update-check"
|
|
$psi.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Minimized
|
|
$psi.UseShellExecute = $true
|
|
[System.Diagnostics.Process]::Start($psi) | Out-Null
|
|
Start-Sleep -Seconds 3
|
|
Update-Status
|
|
$status = Get-ProxyStatus
|
|
if ($status.Running) {
|
|
[System.Windows.Forms.MessageBox]::Show("代理已启动!`n端口: $PORT`n地址: http://127.0.0.1:$PORT", "成功", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
|
|
} else {
|
|
[System.Windows.Forms.MessageBox]::Show("启动可能失败,请检查 Node.js 是否安装正确。", "警告", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Warning)
|
|
}
|
|
}
|
|
|
|
function Stop-Proxy {
|
|
$status = Get-ProxyStatus
|
|
if ($status.Running) {
|
|
Stop-Process -Id $status.PID -Force -ErrorAction SilentlyContinue
|
|
Start-Sleep -Seconds 2
|
|
}
|
|
Update-Status
|
|
[System.Windows.Forms.MessageBox]::Show("代理已停止。", "已停止", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
|
|
}
|
|
|
|
function Restart-Proxy {
|
|
Stop-Proxy
|
|
Start-Sleep -Seconds 1
|
|
Start-Proxy
|
|
}
|
|
|
|
function Open-Admin {
|
|
$psi = New-Object System.Diagnostics.ProcessStartInfo
|
|
$psi.FileName = "cmd.exe"
|
|
$psi.Arguments = "/c start http://127.0.0.1:$PORT/admin/"
|
|
$psi.UseShellExecute = $true
|
|
[System.Diagnostics.Process]::Start($psi) | Out-Null
|
|
}
|
|
|
|
# === 创建窗口 ===
|
|
$form = New-Object System.Windows.Forms.Form
|
|
$form.Text = "MiMo2Codex 代理管理器"
|
|
$form.Size = New-Object System.Drawing.Size(420, 320)
|
|
$form.StartPosition = "CenterScreen"
|
|
$form.FormBorderStyle = "FixedDialog"
|
|
$form.MaximizeBox = $false
|
|
$form.BackColor = [System.Drawing.Color]::FromArgb(245, 245, 248)
|
|
|
|
# === 标题 ===
|
|
$lblTitle = New-Object System.Windows.Forms.Label
|
|
$lblTitle.Text = "MiMo2Codex 代理管理器"
|
|
$lblTitle.Font = New-Object System.Drawing.Font("Microsoft YaHei", 16, [System.Drawing.FontStyle]::Bold)
|
|
$lblTitle.Location = New-Object System.Drawing.Point(20, 15)
|
|
$lblTitle.Size = New-Object System.Drawing.Size(380, 35)
|
|
$lblTitle.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
|
|
$lblTitle.ForeColor = [System.Drawing.Color]::FromArgb(33, 33, 33)
|
|
$form.Controls.Add($lblTitle)
|
|
|
|
# === 状态标签 ===
|
|
$lblStatus = New-Object System.Windows.Forms.Label
|
|
$lblStatus.Text = "正在检查..."
|
|
$lblStatus.Font = New-Object System.Drawing.Font("Microsoft YaHei", 11)
|
|
$lblStatus.Location = New-Object System.Drawing.Point(20, 60)
|
|
$lblStatus.Size = New-Object System.Drawing.Size(380, 30)
|
|
$lblStatus.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
|
|
$form.Controls.Add($lblStatus)
|
|
|
|
# === 分割线 ===
|
|
$lblLine = New-Object System.Windows.Forms.Label
|
|
$lblLine.Text = ""
|
|
$lblLine.BorderStyle = [System.Windows.Forms.BorderStyle]::Fixed3D
|
|
$lblLine.Location = New-Object System.Drawing.Point(20, 95)
|
|
$lblLine.Size = New-Object System.Drawing.Size(380, 2)
|
|
$form.Controls.Add($lblLine)
|
|
|
|
# === 信息标签 ===
|
|
$lblInfo = New-Object System.Windows.Forms.Label
|
|
$lblInfo.Text = "端口: $PORT`n地址: http://127.0.0.1:$PORT`n上游: https://opencode.ai/zen/go/v1"
|
|
$lblInfo.Font = New-Object System.Drawing.Font("Consolas", 9)
|
|
$lblInfo.Location = New-Object System.Drawing.Point(20, 105)
|
|
$lblInfo.Size = New-Object System.Drawing.Size(380, 55)
|
|
$lblInfo.TextAlign = [System.Drawing.ContentAlignment]::MiddleLeft
|
|
$form.Controls.Add($lblInfo)
|
|
|
|
# === 按钮样式函数 ===
|
|
function New-Button($text, $x, $y, $color) {
|
|
$btn = New-Object System.Windows.Forms.Button
|
|
$btn.Text = $text
|
|
$btn.Font = New-Object System.Drawing.Font("Microsoft YaHei", 10)
|
|
$btn.Location = New-Object System.Drawing.Point($x, $y)
|
|
$btn.Size = New-Object System.Drawing.Size(120, 40)
|
|
$btn.BackColor = $color
|
|
$btn.ForeColor = [System.Drawing.Color]::White
|
|
$btn.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
|
|
$btn.FlatAppearance.BorderSize = 0
|
|
return $btn
|
|
}
|
|
|
|
# === 按钮 ===
|
|
$btnStart = New-Button "启动代理" 20 175 ([System.Drawing.Color]::FromArgb(34, 139, 34))
|
|
$btnStart.Add_Click({ Start-Proxy })
|
|
$form.Controls.Add($btnStart)
|
|
|
|
$btnStop = New-Button "停止代理" 150 175 ([System.Drawing.Color]::FromArgb(178, 34, 34))
|
|
$btnStop.Add_Click({ Stop-Proxy })
|
|
$form.Controls.Add($btnStop)
|
|
|
|
$btnRestart = New-Button "重启代理" 280 175 ([System.Drawing.Color]::FromArgb(70, 130, 180))
|
|
$btnRestart.Add_Click({ Restart-Proxy })
|
|
$form.Controls.Add($btnRestart)
|
|
|
|
$btnAdmin = New-Button "管理面板" 20 225 ([System.Drawing.Color]::FromArgb(100, 100, 100))
|
|
$btnAdmin.Add_Click({ Open-Admin })
|
|
$form.Controls.Add($btnAdmin)
|
|
|
|
$btnRefresh = New-Button "刷新状态" 150 225 ([System.Drawing.Color]::FromArgb(100, 100, 100))
|
|
$btnRefresh.Add_Click({ Update-Status })
|
|
$form.Controls.Add($btnRefresh)
|
|
|
|
$btnExit = New-Button "退出" 280 225 ([System.Drawing.Color]::FromArgb(80, 80, 80))
|
|
$btnExit.Add_Click({ $form.Close() })
|
|
$form.Controls.Add($btnExit)
|
|
|
|
# === 初始化状态 ===
|
|
Update-Status
|
|
|
|
# === 定时刷新 ===
|
|
$timer = New-Object System.Windows.Forms.Timer
|
|
$timer.Interval = 5000
|
|
$timer.Add_Tick({ Update-Status })
|
|
$timer.Start()
|
|
|
|
# === 显示窗口 ===
|
|
[System.Windows.Forms.Application]::Run($form)
|