Dmitry Beresnev commited on
Commit ·
0337912
1
Parent(s): f755f75
fix gateway token
Browse files341 unmodified lines3423433443453463478 unmodified lines356357358359360361109 unmodified lines47147247347447547647724 unmodified lines502503504505506507341 unmodified linesreturn urlunsplit((parts.scheme, parts.netloc, parts.path, urlencode(query), parts.fragment))def resolve_gateway_token() -> str:if OPENCLAW_GATEWAY_TOKEN:return OPENCLAW_GATEWAY_TOKEN8 unmodified linesreturn ""def build_strategy_frame(data: pd.DataFrame, fast_period: int, slow_period: int) -> pd.DataFrame:frame = pd.DataFrame(index=data.index)frame["Close"] = data["Close"]109 unmodified linesproxy_ok = Falselocal_status = "Unavailable"proxy_status = "Unavailable"gateway_token = resolve_gateway_token()std_ui_url = with_token(OPENCLAW_STANDARD_UI_URL, gateway_token)try:ui_resp = requests.get(std_ui_url, timeout=2, allow_redirects=True)24 unmodified lineswith std_ui_embed_col:public_ui_url = with_token(OPENCLAW_STANDARD_UI_PUBLIC_URL, gateway_token)if public_ui_url:components.iframe(public_ui_url, height=560, scrolling=True)st.markdown(f"[Open Standard UI]({public_ui_url})")else:341 unmodified lines3423433443453463473483493503513523533543553568 unmodified lines365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429109 unmodified lines53954054154254354454524 unmodified lines570571572573574575576341 unmodified linesreturn urlunsplit((parts.scheme, parts.netloc, parts.path, urlencode(query), parts.fragment))def mask_token_in_url(url: str) -> str:parts = urlsplit(url)query = dict(parse_qsl(parts.query, keep_blank_values=True))token = query.get("token", "")if token:query["token"] = f"{token[:4]}...{token[-4:]}" if len(token) > 8 else "***"return urlunsplit((parts.scheme, parts.netloc, parts.path, urlencode(query), parts.fragment))def resolve_gateway_token() -> str:if OPENCLAW_GATEWAY_TOKEN:return OPENCLAW_GATEWAY_TOKEN8 unmodified linesreturn ""def _collect_auth_tokens(obj: object) -> list[str]:found: list[str] = []if isinstance(obj, dict):# Direct auth token object: {"auth": {"token": "..."}}auth_obj = obj.get("auth")if isinstance(auth_obj, dict):tok = auth_obj.get("token")if isinstance(tok, str) and tok.strip():found.append(tok.strip())for value in obj.values():found.extend(_collect_auth_tokens(value))elif isinstance(obj, list):for item in obj:found.extend(_collect_auth_tokens(item))return founddef resolve_ui_token() -> str:# 1) Explicit env override always wins.if OPENCLAW_GATEWAY_TOKEN:return OPENCLAW_GATEWAY_TOKEN# 2) Collect candidate tokens from runtime state.candidates: list[str] = []try:state_cfg = json.loads(OPENCLAW_STATE_CONFIG_PATH.read_text(encoding="utf-8"))candidates.extend(_collect_auth_tokens(state_cfg))gw_tok = (state_cfg.get("gateway", {}).get("auth", {}).get("token", ""))if isinstance(gw_tok, str) and gw_tok.strip():candidates.append(gw_tok.strip())except Exception:pass# Deduplicate while preserving order.deduped = []seen = set()for t in candidates:if t not in seen:seen.add(t)deduped.append(t)# 3) Validate token candidates against browser UI endpoint.for token in deduped:try:probe_url = with_token(OPENCLAW_STANDARD_UI_URL, token)resp = requests.get(probe_url, timeout=2, allow_redirects=True)if resp.status_code < 400:return tokenexcept Exception:continue# 4) Fallback to legacy gateway token resolution.return resolve_gateway_token()def build_strategy_frame(data: pd.DataFrame, fast_period: int, slow_period: int) -> pd.DataFrame:frame = pd.DataFrame(index=data.index)frame["Close"] = data["Close"]109 unmodified linesproxy_ok = Falselocal_status = "Unavailable"proxy_status = "Unavailable"gateway_token = resolve_ui_token()std_ui_url = with_token(OPENCLAW_STANDARD_UI_URL, gateway_token)try:ui_resp = requests.get(std_ui_url, timeout=2, allow_redirects=True)24 unmodified lineswith std_ui_embed_col:public_ui_url = with_token(OPENCLAW_STANDARD_UI_PUBLIC_URL, gateway_token)if public_ui_url:st.caption(f"Embedded URL: {mask_token_in_url(public_ui_url)}")components.iframe(public_ui_url, height=560, scrolling=True)st.markdown(f"[Open Standard UI]({public_ui_url})")else:
app.py+70-1