阅读时可先确认获取与安装前提,再查看本页列出的配置和接口说明。
下载说明继续:总结
总结
自愈策略的三层架构:
| 层级 | 故障类型 | 自愈手段 |
|---|---|---|
| 第一层 | 节点资源不可用 | 多层级策略组 + 高可靠兜底节点资源 |
| 第二层 | 进程崩溃 | 内置守护 + 外部监控 + 自动重启 |
| 第三层 | 配置方案文件失效 | 备份配置方案文件 + 订阅缓存 + 自动切换 |
安装完成以后,大部分故障能在这两步内自动恢复。最低投入方案:启用 Clash Verge 内置进程守护 + 配置方案文件多层策略组(fallback → DIRECT)。
下载说明继续:代理为什么需要”自愈”?
代理为什么需要”自愈”?
代理服务的故障类型五花八门:节点资源间歇性掉线、DNS 解析失败、订阅链接失效、内核组件进程崩溃、配置方案文件文件损坏……每次故障都依赖用户手动排查重启,既打断工作流程也影响体验。
“代理故障自愈”的目标是:让 Clash Verge 在多数常见故障场景下可以自动检测、自行恢复,用户甚至感知不到发生过故障。
本文从三类故障(节点资源故障、进程故障、配置方案文件故障)出发,逐一设计自愈策略。
下载说明继续:第一层:节点资源故障自愈
第一层:节点资源故障自愈
多层级策略组构建安全网
单靠 url-test 或 fallback 一种策略组不够。构建多层回退链路:
proxy-groups:
# 第一层:首选高速节点(url-test 自动选最优)
- name: "高速组"
type: url-test
proxies:
- "日本-01"
- "新加坡-01"
- "香港-01"
url: "http://www.gstatic.com/generate_204"
interval: 60
timeout: 3000
# 第二层:稳定节点(fallback,不频繁切换)
- name: "稳定组"
type: fallback
proxies:
- "美国-01"
- "英国-01"
url: "http://www.gstatic.com/generate_204"
interval: 120
timeout: 5000
# 第三层:终极代理(所有节点挂掉时的兜底)
- name: "终极代理"
type: select
proxies:
- "高速组" # 引用第一层
- "稳定组" # 高速组全挂 → 自动切到稳定组
- DIRECT # 全部挂 → 直连,至少不断网
rules:
- MATCH,终极代理
下载客户端后,稳定组也挂掉时兜底 DIRECT。当高速组健康检查发现所有节点资源不可用时,url-test 返回空,select 策略组自动切换到稳定组。
配置方案文件 relay 节点资源做终极备份
在配置方案文件中预留一个高可靠节点资源(如自建 VPS 的 Shadowsocks),作为所有机场节点资源都失效时的逃生通道:
proxies:
- name: "逃生通道"
type: ss
server: backup-vps.com
port: 8388
cipher: aes-256-gcm
password: "backup-password"
proxy-groups:
- name: "终极代理"
type: fallback
proxies:
- "高速组"
- "稳定组"
- "逃生通道" # 最后保底
节点资源健康检查的精准配置方案文件
不同用途的节点资源应该实际使用方式不同的健康检查 URL:
proxy-groups:
- name: "日常代理"
type: url-test
url: "http://www.gstatic.com/generate_204"
interval: 120
timeout: 3000
- name: "Google 专线"
type: url-test
url: "https://www.google.com" # 必须能访问 Google
interval: 300
timeout: 5000
- name: "AI 服务"
type: url-test
url: "https://api.openai.com" # 专门检测 AI 服务可达性
interval: 180
timeout: 5000
下载说明继续:第二层:进程故障自愈
第二层:进程故障自愈
Clash Verge 内置进程守护
Clash Verge Rev 自带进程守护。在设置中确认:
– 内核组件崩溃自动重启:开启
– 异常退出通知:开启(方便了解崩溃频率)
外部进程监控脚本
如果内置守护不够(某些场景下 Clash Verge GUI 可能也会崩溃),加一层外部监控:
#!/bin/bash
# clash-watchdog.sh —— 外部进程守护脚本
CLASH_API="http://127.0.0.1:9090"
CLASH_GUI="clash-verge-rev"
CHECK_INTERVAL=30 # 检查间隔(秒)
MAX_RESTART=5 # 每小时最多重启次数
RESTART_LOG="/var/log/clash-watchdog.log"
restart_count=0
last_hour=$(date +%H)
while true; do
current_hour=$(date +%H)
# 每小时重置计数
if [ "$current_hour" != "$last_hour" ]; then
restart_count=0
last_hour=$current_hour
fi
# 检查 Clash 内核是否存活
if ! curl -s --connect-timeout 3 "$CLASH_API" > /dev/null 2>&1; then
echo "[$(date)] Clash 内核无响应" >> "$RESTART_LOG"
if [ $restart_count -lt $MAX_RESTART ]; then
echo "[$(date)] 尝试重启..." >> "$RESTART_LOG"
# 方式1:通过 Clash Verge API 重启(如果有)
# curl -X POST http://127.0.0.1:9097/restart
# 方式2:杀掉并重新启动
pkill -f mihomo
sleep 2
$CLASH_GUI &
restart_count=$((restart_count + 1))
else
echo "[$(date)] 达到最大重启次数,停止重试" >> "$RESTART_LOG"
fi
fi
sleep $CHECK_INTERVAL
done
设置开机自启:
# systemd service (Linux) # /etc/systemd/system/clash-watchdog.service [Unit] Description=Clash Watchdog After=network.target [Service] Type=simple ExecStart=/usr/local/bin/clash-watchdog.sh Restart=always User=your-user [Install] WantedBy=multi-user.target
心跳检测 + 自动切换配置方案文件
如果主配置方案文件完全失效(如订阅链接全挂),自动切换到备份配置方案文件:
#!/bin/bash
# auto-failover.sh —— 配置故障自动切换
PRIMARY_CONFIG="$HOME/.config/clash-verge-rev/profiles/config.yaml"
BACKUP_CONFIG="$HOME/.config/clash-verge-rev/profiles/backup.yaml"
API="http://127.0.0.1:9090"
# 检查 Clash 代理功能是否正常
check_proxy() {
curl -s -x http://127.0.0.1:7890 --connect-timeout 5 https://www.google.com > /dev/null 2>&1
return $?
}
# 主逻辑
if ! check_proxy; then
echo "[$(date)] 代理不可用,尝试切换配置"
# 检查当前用的是哪个配置
current_config=$(curl -s "$API/configs" | grep -o '"path":"[^"]*"' | head -1)
if echo "$current_config" | grep -q "config.yaml"; then
# 切换到备份配置
curl -X PUT "$API/configs?force=true" -H "Content-Type: application/json" -d '{"path": "backup.yaml"}'
echo "[$(date)] 已切换到备份配置"
fi
fi
下载说明继续:第三层:配置方案文件故障自愈
第三层:配置方案文件故障自愈
订阅失效的零中断切换
当一个 Provider 软件版本升级失败时,实际使用方式本地缓存。如果多个 Provider 全部失效,回退到直连模式:
proxy-providers:
main:
type: http
url: "https://airport-a.com/sub"
path: ./profiles/proxies/main.yaml
interval: 3600
backup-provider:
type: http
url: "https://airport-b.com/sub"
path: ./profiles/proxies/backup.yaml
interval: 3600
proxy-groups:
- name: "代理"
type: select
use:
- main
- backup-provider
proxies:
- DIRECT # 两个 Provider 都失效 → 兜底直连
配置方案文件文件语法自动校验
每次配置方案文件修改后自动运行 YAML 校验,避免语法错误导致 Clash 启动失败:
#!/bin/bash
# validate-config.sh —— 配置修改后自动校验
CONFIG_DIR="$HOME/.config/clash-verge-rev/profiles"
validate_and_reload() {
local config_file="$1"
# YAML 语法校验
if python3 -c "import yaml; yaml.safe_load(open('$config_file'))" 2>/dev/null; then
echo "✓ $config_file 语法正确"
# 通过 API 重载配置
curl -s -X PUT "http://127.0.0.1:9090/configs?force=true" -H "Content-Type: application/json" -d "{\\\"path\\\": \\\"$config_file\\\"}" > /dev/null
else
echo "✗ $config_file 语法错误,保持当前配置不变"
return 1
fi
}
# 监听配置目录变更
# Linux 使用 inotifywait
# macOS 使用 fswatch
# Windows 使用 FileSystemWatcher (PowerShell)
while inotifywait -r -e modify,create "$CONFIG_DIR" 2>/dev/null; do
sleep 1 # 等待文件写入完成
validate_and_reload "$CONFIG_DIR/config.yaml"
done
下载说明继续:集成方案:自愈总控脚本
集成方案:自愈总控脚本
将所有自愈逻辑整合到一个守护脚本中:
#!/bin/bash
# clash-autoheal.sh —— Clash 自愈总控
API="http://127.0.0.1:9090"
LOG="/var/log/clash-autoheal.log"
CHECK_INTERVAL=60
log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG"; }
# 检查 1:内核是否存活
check_kernel() {
curl -s --connect-timeout 3 "$API" > /dev/null 2>&1
}
# 检查 2:代理是否工作
check_proxy() {
curl -s -x http://127.0.0.1:7890 --connect-timeout 5 https://www.google.com > /dev/null 2>&1
}
# 检查 3:当前有多少可用节点
count_live_nodes() {
curl -s "$API/proxies" | python3 -c "
import json, sys
data = json.load(sys.stdin)
live = 0
for name, p in data.get('proxies', {}).items():
if p['type'] in ('Shadowsocks', 'Vmess', 'Trojan', 'Hysteria2'):
h = p.get('history', [])
if h and h[-1].get('delay', 0) > 0:
live += 1
print(live)
" 2>/dev/null
}
# 恢复操作
restore_proxy() {
log "执行恢复操作"
# 操作1:重启 Clash 内核
pkill -f mihomo && sleep 2
clash-verge-rev &
# 操作2:如果 30 秒后仍不可用,切 fallback 配置
sleep 30
if ! check_proxy; then
log "内核恢复失败,切换配置"
curl -s -X PUT "$API/configs?force=true" -H "Content-Type: application/json" -d '{"path": "backup.yaml"}'
fi
}
# 主循环
while true; do
if ! check_kernel; then
log "内核异常,触发恢复"
restore_proxy
elif ! check_proxy; then
live=$(count_live_nodes)
log "代理不通,存活节点: $live"
if [ "$live" -eq 0 ]; then
log "无存活节点,触发恢复"
restore_proxy
fi
fi
sleep $CHECK_INTERVAL
done
下载说明继续:各平台专属方案
各平台专属方案
Windows 计划任务 + PowerShell
# clash-healthcheck.ps1
$api = "http://127.0.0.1:9090"
try {
$null = Invoke-WebRequest -Uri $api -TimeoutSec 3 -UseBasicParsing
} catch {
Write-EventLog -LogName Application -Source "ClashAutoHeal" `
-EventId 1 -EntryType Warning -Message "Clash 内核无响应,尝试重启"
Start-Process "clash-verge-rev"
}
在任务计划程序中创建每 5 分钟执行的触发器。
macOS launchd
Label com.user.clash-autoheal ProgramArguments /usr/local/bin/clash-autoheal.sh RunAtLoad KeepAlive StandardOutPath /tmp/clash-autoheal.log
完成上述步骤后,即可在客户端中继续验证本页涉及的功能。