131 lines
3.3 KiB
PowerShell
131 lines
3.3 KiB
PowerShell
# 启动 Mihomo (Clash Meta) 代理
|
|
|
|
# ========== 1. 设置脚本目录 ==========
|
|
$scriptDir = $PSScriptRoot
|
|
if ( [string]::IsNullOrWhiteSpace($scriptDir))
|
|
{
|
|
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
|
}
|
|
|
|
|
|
# ========== 2. 定义路径 ==========
|
|
|
|
$configDir = Join-Path $scriptDir "config"
|
|
$configFile = Join-Path $configDir "config.yaml"
|
|
$exe = Join-Path $configDir "mihomo.exe"
|
|
|
|
Set-Location -LiteralPath $configDir
|
|
|
|
$geoipFiles = @(
|
|
"geoip.dat",
|
|
"geosite.dat",
|
|
"geoip.metadb"
|
|
)
|
|
|
|
# ========== 3. 颜色输出函数 ==========
|
|
function Write-ColorOutput
|
|
{
|
|
param(
|
|
[string]$Message,
|
|
[string]$Color = "White"
|
|
)
|
|
Write-Host $Message -ForegroundColor $Color
|
|
}
|
|
|
|
# ========== 4. 检查必要文件 ==========
|
|
if (-not (Test-Path -LiteralPath $exe))
|
|
{
|
|
Write-ColorOutput "❌ 错误:mihomo 程序不存在:$exe" "Red"
|
|
exit 1
|
|
}
|
|
|
|
if (-not (Test-Path -LiteralPath $configFile))
|
|
{
|
|
Write-ColorOutput "❌ 错误:配置文件不存在:$configFile" "Red"
|
|
exit 1
|
|
}
|
|
|
|
# 检查 GeoIP 文件
|
|
$missingFiles = @()
|
|
foreach ($file in $geoipFiles)
|
|
{
|
|
$filePath = Join-Path $configDir $file
|
|
if (-not (Test-Path -LiteralPath $filePath))
|
|
{
|
|
$missingFiles += $file
|
|
}
|
|
}
|
|
|
|
if ($missingFiles.Count -gt 0)
|
|
{
|
|
Write-ColorOutput "⚠️ 警告:以下 GeoIP 数据文件缺失,程序将尝试联网下载:" "Yellow"
|
|
foreach ($file in $missingFiles)
|
|
{
|
|
Write-ColorOutput " - $file" "Yellow"
|
|
}
|
|
Write-ColorOutput " 请确保网络通畅,或运行 install.bash 手动下载。" "Yellow"
|
|
Write-ColorOutput ""
|
|
}
|
|
|
|
# ========== 5. 等待网络连接 ==========
|
|
$timeout = 180
|
|
$interval = 3
|
|
$elapsed = 0
|
|
|
|
Write-ColorOutput "⏳ 等待网络连接..." "Cyan"
|
|
|
|
while ($elapsed -lt $timeout)
|
|
{
|
|
try
|
|
{
|
|
$pingResult = Test-Connection -ComputerName "www.baidu.com" -Count 1 -Quiet -ErrorAction Stop
|
|
if ($pingResult)
|
|
{
|
|
Write-ColorOutput "✅ 网络已连接。" "Green"
|
|
break
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
# ping 失败,继续等待
|
|
}
|
|
Start-Sleep -Seconds $interval
|
|
$elapsed += $interval
|
|
Write-ColorOutput " 等待中... ($elapsed 秒)" "Gray"
|
|
}
|
|
|
|
if ($elapsed -ge $timeout)
|
|
{
|
|
Write-ColorOutput "❌ 等待网络超时(${timeout}秒),已停止启动。" "Red"
|
|
exit 1
|
|
}
|
|
|
|
# ========== 6. 检查配置格式 ==========
|
|
Write-ColorOutput ""
|
|
Write-ColorOutput "🔍 正在检查配置格式..." "Cyan"
|
|
|
|
$process = Start-Process -FilePath $exe -ArgumentList "-d `"$configDir`" -f `"$configFile`" -t" -Wait -PassThru -NoNewWindow
|
|
|
|
if ($process.ExitCode -ne 0)
|
|
{
|
|
Write-ColorOutput "❌ 配置格式检查失败(退出码:$( $process.ExitCode )),已停止启动。" "Red"
|
|
exit $process.ExitCode
|
|
}
|
|
|
|
Write-ColorOutput "✅ 配置格式检查成功!" "Green"
|
|
|
|
# ========== 7. 启动代理 ==========
|
|
Write-ColorOutput ""
|
|
Write-ColorOutput "🚀 正在启动 mihomo..." "Cyan"
|
|
Write-ColorOutput " 配置文件:$configFile" "Gray"
|
|
Write-ColorOutput " 工作目录:$configDir" "Gray"
|
|
Write-ColorOutput ""
|
|
Write-ColorOutput "📌 提示:按 Ctrl+C 可停止代理" "Yellow"
|
|
Write-ColorOutput ""
|
|
|
|
# 启动主进程,前台运行
|
|
& $exe -d $configDir -f $configFile
|
|
|
|
# 如果主进程退出,显示信息
|
|
Write-ColorOutput ""
|
|
Write-ColorOutput "⚠️ mihomo 进程已退出。" "Yellow" |