onesvm-browser-server/bench/proxy/up.sh
chii 983259836d chore: init workspace with onesvm-dev-md + casa-commander
docs: 联网搜索服务架构方案全套(plan-final/design-arch/选型决策/整合导览/MCP文档/部署预设/联调手册)
bench: 5 方案 + 代理 + 站点矩阵本机实测工程(无密钥)
部署目标:primary mgr1 先行测试(待批准后执行)
2026-09-01 15:19:52 +08:00

95 lines
3.2 KiB
Bash
Executable file

#!/usr/bin/env bash
# Fetch PROXY_SUB_URL into a temp runtime dir and start bench-proxy-mihomo.
# Subscription bytes never land in the git tree.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")" && pwd)"
# shellcheck disable=SC1091
export PYTHONPATH="$ROOT"
MIXED_PORT="${MIXED_PORT:-17890}"
API_PORT="${API_PORT:-19090}"
CONTAINER="${CONTAINER:-bench-proxy-mihomo}"
IMAGE_PRIMARY="${MIHOMO_IMAGE:-metacubex/mihomo:latest}"
IMAGE_MIRROR="${MIHOMO_IMAGE_MIRROR:-docker.m.daocloud.io/metacubex/mihomo:latest}"
RUNTIME_DIR="${RUNTIME_DIR:-/tmp/bench-proxy-runtime}"
if [[ -z "${PROXY_SUB_URL:-}" ]]; then
echo "error: PROXY_SUB_URL is required" >&2
exit 2
fi
mkdir -p "$RUNTIME_DIR"
chmod 700 "$RUNTIME_DIR"
SUB_RAW="$RUNTIME_DIR/sub.yaml"
CFG="$RUNTIME_DIR/config.yaml"
echo "[up] fetching subscription via PROXY_SUB_URL"
HTTP_CODE="$(curl -sS -L --max-time 45 -A "clash-meta/bench-proxy" \
-o "$SUB_RAW" -w "%{http_code}" "$PROXY_SUB_URL")"
echo "[up] HTTP $HTTP_CODE size=$(wc -c < "$SUB_RAW") bytes"
if [[ "$HTTP_CODE" != "200" ]]; then
echo "error: subscription fetch failed HTTP $HTTP_CODE" >&2
exit 1
fi
python3 - <<PY
import sys
sys.path.insert(0, "$ROOT")
from lib import rewrite_runtime_config, summarize_subscription
src = open("$SUB_RAW", encoding="utf-8", errors="replace").read()
summary = summarize_subscription(src)
print("[up] format=%s real_nodes=%s types=%s regions=%s" % (
summary["format"], summary["proxy_count_real"], summary["types"], summary["regions"]))
safe = {k: summary[k] for k in ("format", "proxy_count_raw", "proxy_count_real", "info_nodes", "types", "regions")}
open("$ROOT/sub-summary.json", "w", encoding="utf-8").write(
__import__("json").dumps(safe, ensure_ascii=False, indent=2) + "\n")
# container always listens on 17890/9090; host ports are mapped separately
cfg = rewrite_runtime_config(src, mixed_port=17890, controller="0.0.0.0:9090")
open("$CFG", "w", encoding="utf-8").write(cfg)
PY
ensure_image() {
if docker image inspect "$IMAGE_PRIMARY" >/dev/null 2>&1; then
echo "[up] using local image $IMAGE_PRIMARY" >&2
printf '%s' "$IMAGE_PRIMARY"
return
fi
echo "[up] pulling $IMAGE_PRIMARY" >&2
if docker pull "$IMAGE_PRIMARY" >&2; then
printf '%s' "$IMAGE_PRIMARY"
return
fi
echo "[up] primary pull failed; trying mirror $IMAGE_MIRROR" >&2
docker pull "$IMAGE_MIRROR" >&2
printf '%s' "$IMAGE_MIRROR"
}
IMAGE="$(ensure_image)"
docker rm -f "$CONTAINER" >/dev/null 2>&1 || true
echo "[up] starting $CONTAINER on mixed=$MIXED_PORT api=$API_PORT"
docker run -d --name "$CONTAINER" --restart=no \
-p "${MIXED_PORT}:17890" \
-p "${API_PORT}:9090" \
-v "${CFG}:/root/.config/mihomo/config.yaml:ro" \
"$IMAGE" -d /root/.config/mihomo >/dev/null
echo "[up] waiting for external-controller"
ok=0
for i in $(seq 1 40); do
if curl -sf --max-time 2 "http://127.0.0.1:${API_PORT}/version" >/dev/null; then
ok=1
break
fi
sleep 0.5
done
if [[ "$ok" != "1" ]]; then
echo "[up] controller not ready; last logs:" >&2
docker logs --tail 80 "$CONTAINER" >&2 || true
exit 1
fi
curl -sS "http://127.0.0.1:${API_PORT}/version"
echo
echo "[up] ready container=$CONTAINER mixed=http://127.0.0.1:${MIXED_PORT} api=http://127.0.0.1:${API_PORT}"