File size: 2,962 Bytes
fdc7a1e
 
 
 
 
5cd7ce4
 
 
 
 
 
 
fdc7a1e
 
5cd7ce4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49e3c8a
 
 
 
 
 
 
fdc7a1e
 
 
 
 
 
 
 
 
 
 
 
 
 
5cd7ce4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49e3c8a
 
5cd7ce4
 
 
fdc7a1e
 
 
5cd7ce4
 
 
 
 
 
 
49e3c8a
5cd7ce4
fdc7a1e
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import json
import os
from pathlib import Path


def _split_csv_env(name: str) -> list[str]:
    raw = os.getenv(name, "")
    if not raw:
        return []
    return [part.strip() for part in raw.split(",") if part.strip()]


def main() -> int:
    token = os.getenv("OPENCLAW_GATEWAY_TOKEN", "").strip()
    bind_mode = os.getenv("OPENCLAW_GATEWAY_BIND", "lan").strip() or "lan"
    control_ui_base_path = os.getenv("OPENCLAW_CONTROL_UI_BASE_PATH", "/openclaw").strip() or "/openclaw"
    allowed_origins = _split_csv_env("OPENCLAW_ALLOWED_ORIGINS")
    if not allowed_origins:
        # Safe defaults for local diagnostics + HF public Space URL.
        allowed_origins = [
            "http://127.0.0.1:7860",
            "http://localhost:7860",
            "https://researchengineering-agi-assistant.hf.space",
        ]
    trusted_proxies = _split_csv_env("OPENCLAW_TRUSTED_PROXIES")
    if not trusted_proxies:
        trusted_proxies = ["127.0.0.1", "::1"]
    allow_insecure_auth = os.getenv("OPENCLAW_CONTROL_UI_ALLOW_INSECURE_AUTH", "1").strip() in {
        "1",
        "true",
        "True",
        "yes",
        "on",
    }
    disable_device_auth = os.getenv("OPENCLAW_CONTROL_UI_DISABLE_DEVICE_AUTH", "1").strip() in {
        "1",
        "true",
        "True",
        "yes",
        "on",
    }

    state_path = Path("/app/.openclaw/state/openclaw.json")
    state_path.parent.mkdir(parents=True, exist_ok=True)

    data = {}
    if state_path.exists():
        try:
            data = json.loads(state_path.read_text(encoding="utf-8"))
        except Exception:
            data = {}

    gateway = data.get("gateway", {})
    if not isinstance(gateway, dict):
        gateway = {}
    gateway["bind"] = bind_mode

    if token:
        auth = gateway.get("auth", {})
        if not isinstance(auth, dict):
            auth = {}
        auth["token"] = token
        gateway["auth"] = auth

    control_ui = gateway.get("controlUi", {})
    if not isinstance(control_ui, dict):
        control_ui = {}
    control_ui["basePath"] = control_ui_base_path
    control_ui["allowedOrigins"] = allowed_origins
    control_ui["allowInsecureAuth"] = allow_insecure_auth
    # Break-glass for reverse-proxied hosted setups that cannot complete pairing reliably.
    control_ui["dangerouslyDisableDeviceAuth"] = disable_device_auth
    gateway["controlUi"] = control_ui
    gateway["trustedProxies"] = trusted_proxies

    data["gateway"] = gateway

    state_path.write_text(json.dumps(data, indent=2) + "\n", encoding="utf-8")
    print(
        "[bootstrap] gateway settings applied:"
        f" bind={bind_mode}"
        f" basePath={control_ui_base_path}"
        f" allowedOrigins={allowed_origins}"
        f" trustedProxies={trusted_proxies}"
        f" allowInsecureAuth={allow_insecure_auth}"
        f" dangerouslyDisableDeviceAuth={disable_device_auth}"
    )
    return 0


if __name__ == "__main__":
    raise SystemExit(main())