fix: disable gateway listeners in server mode

This commit is contained in:
“Rogee”
2026-08-21 13:36:33 +08:00
parent c93fb6b9d3
commit 4f8640acf2
6 changed files with 122 additions and 27 deletions
+54 -1
View File
@@ -5,6 +5,9 @@ image=${1:-mohomo-docker:smoke}
suffix="$$"
container="mohomo-docker-smoke-${suffix}"
volume="mohomo-docker-smoke-${suffix}"
cookie=""
login_html=""
config_html=""
case "$container:$volume" in
mohomo-docker-smoke-*':mohomo-docker-smoke-'*) ;;
@@ -14,6 +17,9 @@ esac
cleanup() {
docker container rm --force "$container" >/dev/null 2>&1 || true
docker volume rm "$volume" >/dev/null 2>&1 || true
[ -z "$cookie" ] || rm -f "$cookie"
[ -z "$login_html" ] || rm -f "$login_html"
[ -z "$config_html" ] || rm -f "$config_html"
}
trap cleanup EXIT INT TERM
@@ -48,8 +54,44 @@ until curl --fail --silent --show-error "http://127.0.0.1:${web_port}/" >/dev/nu
done
docker exec "$container" grep -Fx 'OPERATING_MODE=server' /opt/clash/.ssclash/settings >/dev/null
docker exec "$container" grep -Fx 'PROXY_MODE=none' /opt/clash/.ssclash/settings >/dev/null
docker exec "$container" grep -Fx 'mixed-port: 7890' /opt/clash/config.yaml >/dev/null
docker exec --detach "$container" /opt/clash/bin/clash -d /opt/clash
docker exec "$container" /usr/local/bin/ssclash setpass container-smoke-only >/dev/null
cookie=$(mktemp)
login_html=$(mktemp)
config_html=$(mktemp)
curl --fail --silent --show-error --cookie-jar "$cookie" \
"http://127.0.0.1:${web_port}/login" > "$login_html"
login_csrf=$(sed -n 's/.*name="csrf" value="\([^"]*\)".*/\1/p' "$login_html" | head -1)
test -n "$login_csrf"
curl --fail --silent --show-error \
--cookie "$cookie" \
--cookie-jar "$cookie" \
--request POST \
--data-urlencode "csrf=${login_csrf}" \
--data-urlencode 'password=container-smoke-only' \
"http://127.0.0.1:${web_port}/login" >/dev/null
curl --fail --silent --show-error \
--cookie "$cookie" \
"http://127.0.0.1:${web_port}/config" > "$config_html"
api_csrf=$(sed -n 's/.*name="csrf-token" content="\([^"]*\)".*/\1/p' "$config_html" | head -1)
test -n "$api_csrf"
start_response=$(curl --fail --silent --show-error \
--cookie "$cookie" \
--header "X-CSRF-Token: ${api_csrf}" \
--header 'Content-Type: application/json' \
--data '{"action":"start"}' \
"http://127.0.0.1:${web_port}/api/service")
printf '%s' "$start_response" | grep -F '"ok":true' >/dev/null
status_response=$(curl --fail --silent --show-error \
--cookie "$cookie" \
--header "X-CSRF-Token: ${api_csrf}" \
"http://127.0.0.1:${web_port}/api/status")
printf '%s' "$status_response" | grep -F '"running":true' >/dev/null
printf '%s' "$status_response" | grep -F '"operatingMode":"server"' >/dev/null
attempt=0
until curl --fail --silent --show-error \
@@ -65,4 +107,15 @@ until curl --fail --silent --show-error \
sleep 1
done
if docker exec "$container" grep -Eq '^(tproxy-port|redir-port|tun):' /opt/clash/config.yaml; then
docker logs "$container" >&2
echo "gateway listener leaked into server-only config" >&2
exit 1
fi
if docker logs "$container" 2>&1 | grep -Ei '\[(error|fatal)\]|operation not permitted' >/dev/null; then
docker logs "$container" >&2
echo "container emitted an error during Web-managed startup" >&2
exit 1
fi
echo "container smoke test passed: web_port=${web_port} proxy_port=${proxy_port}"