Finding: QQ connect checks the WS proxy route while HTTPX parses the HTTPS proxy. connect/reconnect run the httpx token/gateway-URL client (reads HTTPS_PROXY itself) before the WebSocket (explicit WSS_PROXY). A clean WSS_PROXY beside a malformed secret HTTPS_PROXY made describe_proxy_error return None, so 'QQ startup failed: Invalid port: <password>' reached the fatal status and log.
50 lines
2.1 KiB
Python
50 lines
2.1 KiB
Python
"""QQBot shared utilities — User-Agent, HTTP helpers, config coercion."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import platform
|
|
import sys
|
|
from typing import Any, Dict, List
|
|
|
|
from .constants import QQBOT_VERSION
|
|
|
|
|
|
def _get_hermes_version() -> str:
|
|
"""Return the hermes-agent package version, or 'dev' if unavailable."""
|
|
try:
|
|
from importlib.metadata import version
|
|
return version("hermes-agent")
|
|
except Exception:
|
|
return "dev"
|
|
|
|
|
|
def build_user_agent() -> str:
|
|
"""``QQBotAdapter/<qqbot_version> (Python/<py_version>; <os>; Hermes/<hermes_version>)``."""
|
|
v = sys.version_info
|
|
return (f"QQBotAdapter/{QQBOT_VERSION} (Python/{v.major}.{v.minor}.{v.micro}; "
|
|
f"{platform.system().lower()}; Hermes/{_get_hermes_version()})")
|
|
|
|
|
|
def get_api_headers() -> dict[str, str]:
|
|
"""Standard QQBot API headers. ``q.qq.com`` requires ``Accept: application/json``
|
|
— without it the server returns a JavaScript anti-bot challenge page."""
|
|
return {"Content-Type": "application/json", "Accept": "application/json", "User-Agent": build_user_agent()}
|
|
|
|
|
|
def coerce_list(value: Any) -> list[str]:
|
|
"""Coerce a comma-separated string / list / tuple / set / scalar into a trimmed string list."""
|
|
if value is None:
|
|
return []
|
|
items = value.split(",") if isinstance(value, str) else value if isinstance(value, (list, tuple, set)) else [value]
|
|
return [s for s in (str(item).strip() for item in items) if s]
|
|
|
|
|
|
def describe_connect_proxy_error(exc: BaseException, ws_proxy: str | None) -> str | None:
|
|
"""``describe_proxy_error`` for a connect/reconnect failure, which may come from either client:
|
|
the httpx token/gateway-URL client reads the proxy environment itself (httpx's own
|
|
``trust_env``, NO_PROXY applied) while the WebSocket dials the explicit *ws_proxy*. A clean
|
|
WSS_PROXY beside a secret HTTPS_PROXY must still redact httpx's ``Invalid port: '<pw>'``."""
|
|
from gateway.platforms.base import describe_proxy_error, env_proxy_for_redaction
|
|
from .constants import API_BASE, TOKEN_URL
|
|
return describe_proxy_error(exc, env_proxy_for_redaction(TOKEN_URL, API_BASE)) or describe_proxy_error(exc, ws_proxy)
|