#!/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 - </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}"