121 lines
3.2 KiB
Bash
121 lines
3.2 KiB
Bash
#!/usr/bin/env bash
|
|
# Soft-guard: if Clash selector is on VLESS and VLESS autopick is disabled,
|
|
# switch back to DY_SHELLCRASH_DEFAULT_NODE (TUIC). Does not touch DB/business.
|
|
# Never rewrites YAML; only PUT /proxies/{group} name=.
|
|
set +e
|
|
|
|
INTERVAL="${DY_SHELLCRASH_GUARD_INTERVAL:-60}"
|
|
COOLDOWN="${DY_SHELLCRASH_GUARD_COOLDOWN:-120}"
|
|
CONTROLLER="${DY_SHELLCRASH_CONTROLLER:-http://127.0.0.1:9999}"
|
|
DEFAULT_NODE="${DY_SHELLCRASH_DEFAULT_NODE:-1d2da142-singbox_tuic5}"
|
|
ALLOW_VLESS="${DY_SHELLCRASH_ALLOW_VLESS:-}"
|
|
STATE_DIR="${DY_SHELLCRASH_GUARD_STATE:-/run/douyinweb-shellcrash-guard}"
|
|
LAST_FIX=0
|
|
|
|
log() { printf '[sc-tuic-guard] %s\n' "$*"; }
|
|
|
|
mkdir -p "$STATE_DIR" 2>/dev/null || true
|
|
|
|
allow_vless() {
|
|
case "$(printf '%s' "$ALLOW_VLESS" | tr 'A-Z' 'a-z')" in
|
|
1|true|yes|on) return 0 ;;
|
|
*) return 1 ;;
|
|
esac
|
|
}
|
|
|
|
is_vless() {
|
|
printf '%s' "$1" | grep -qi 'vless'
|
|
}
|
|
|
|
now_node() {
|
|
curl -sS -m 4 "$CONTROLLER/proxies" 2>/dev/null | python3 -c '
|
|
import sys,json
|
|
try:
|
|
d=json.load(sys.stdin)
|
|
except Exception:
|
|
sys.exit(1)
|
|
ps=d.get("proxies") or {}
|
|
for g in ("\u🚀 节点选择".replace("\u",""),):
|
|
pass
|
|
# Prefer rocket selector, then GLOBAL / PROXY / plain 节点选择
|
|
cands=[]
|
|
for k,v in ps.items():
|
|
if isinstance(v,dict) and v.get("type")=="Selector" and v.get("now"):
|
|
cands.append((k,v.get("now")))
|
|
for prefer in ("GLOBAL","PROXY","节点选择"):
|
|
for k,n in cands:
|
|
if prefer in k or k==prefer:
|
|
print(n); sys.exit(0)
|
|
for k,n in cands:
|
|
if "节点" in k or "Select" in k or "PROXY" in k.upper():
|
|
print(n); sys.exit(0)
|
|
if cands:
|
|
print(cands[0][1]); sys.exit(0)
|
|
sys.exit(2)
|
|
' 2>/dev/null
|
|
}
|
|
|
|
select_node() {
|
|
local node="$1"
|
|
python3 - "$CONTROLLER" "$node" <<'PY'
|
|
import json, sys, urllib.parse, urllib.request
|
|
ctrl, node = sys.argv[1], sys.argv[2]
|
|
try:
|
|
with urllib.request.urlopen(ctrl + "/proxies", timeout=4) as r:
|
|
ps = json.load(r).get("proxies") or {}
|
|
except Exception:
|
|
sys.exit(0)
|
|
groups = []
|
|
for k, v in ps.items():
|
|
if isinstance(v, dict) and v.get("type") == "Selector":
|
|
if k in ("GLOBAL", "PROXY") or "节点" in k or "Select" in k:
|
|
groups.append(k)
|
|
if not groups:
|
|
groups = ["GLOBAL"]
|
|
for g in groups:
|
|
enc = urllib.parse.quote(g, safe="")
|
|
req = urllib.request.Request(
|
|
ctrl + "/proxies/" + enc,
|
|
data=json.dumps({"name": node}).encode(),
|
|
method="PUT",
|
|
headers={"Content-Type": "application/json"},
|
|
)
|
|
try:
|
|
urllib.request.urlopen(req, timeout=4)
|
|
except Exception:
|
|
pass
|
|
PY
|
|
}
|
|
|
|
guard_once() {
|
|
if allow_vless; then
|
|
return 0
|
|
fi
|
|
local now
|
|
now=$(now_node) || return 0
|
|
if ! is_vless "$now"; then
|
|
return 0
|
|
fi
|
|
local ts
|
|
ts=$(date +%s)
|
|
if [ $((ts - LAST_FIX)) -lt "$COOLDOWN" ]; then
|
|
log "VLESS selected ($now) but cooldown; skip"
|
|
return 0
|
|
fi
|
|
log "VLESS selected ($now) -> revert to $DEFAULT_NODE"
|
|
select_node "$DEFAULT_NODE"
|
|
LAST_FIX=$ts
|
|
echo "$ts $now -> $DEFAULT_NODE" >>"$STATE_DIR/fixes.log" 2>/dev/null || true
|
|
}
|
|
|
|
if [ "${1:-}" = "once" ]; then
|
|
guard_once
|
|
exit 0
|
|
fi
|
|
|
|
log "interval=${INTERVAL}s default=${DEFAULT_NODE} allow_vless=${ALLOW_VLESS:-0}"
|
|
while true; do
|
|
guard_once || true
|
|
sleep "$INTERVAL" || sleep 60
|
|
done
|