#!/usr/bin/env bash set -euo pipefail EXPECTED_PROXY="${OPENCLAW_EXPECTED_PROXY:-http://127.0.0.1:18789}" status_json="$(tailscale serve status --json 2>/dev/null || printf '{}\n')" parsed="$( python3 -c ' import json import sys try: data = json.load(sys.stdin) except json.JSONDecodeError: data = {} foreground = False if isinstance(data.get("Foreground"), dict) and data["Foreground"]: foreground = True _, data = next(iter(data["Foreground"].items())) web = data.get("Web") if isinstance(data, dict) else {} handlers = {} if isinstance(web, dict) and web: _, host_cfg = next(iter(web.items())) handlers = host_cfg.get("Handlers", {}) if isinstance(host_cfg, dict) else {} root_proxy = "" extras = [] for path, handler in handlers.items(): if not isinstance(handler, dict): continue proxy = handler.get("Proxy") if not isinstance(proxy, str): continue if path == "/": root_proxy = proxy else: extras.append((path, proxy)) print("foreground=" + ("1" if foreground else "0")) print("root=" + root_proxy) for path, proxy in extras: print("extra\t" + path + "\t" + proxy) ' <<<"$status_json" )" foreground=0 root_proxy="" extra_paths=() extra_targets=() while IFS= read -r line; do case "$line" in foreground=*) foreground="${line#foreground=}" ;; root=*) root_proxy="${line#root=}" ;; extra$'\t'*) rest="${line#extra$'\t'}" extra_paths+=("${rest%%$'\t'*}") extra_targets+=("${rest#*$'\t'}") ;; esac done <<<"$parsed" if [[ "$foreground" == "0" && "$root_proxy" == "$EXPECTED_PROXY" ]]; then exit 0 fi tailscale serve reset tailscale serve --bg "$EXPECTED_PROXY" >/dev/null for i in "${!extra_paths[@]}"; do tailscale serve --bg --set-path "${extra_paths[$i]}" "${extra_targets[$i]}" >/dev/null done