fix: 修复代理问题
This commit is contained in:
@@ -335,9 +335,16 @@ async def _run_greeting_scheduler() -> None:
|
||||
async def lifespan(app: FastAPI):
|
||||
set_message_callback(_on_ws_message)
|
||||
_callback_key = (os.getenv("WECHAT_WS_KEY") or os.getenv("KEY") or os.getenv("WS_KEY") or "").strip()
|
||||
if CALLBACK_BASE_URL and _callback_key:
|
||||
ok = await _register_message_callback(_callback_key)
|
||||
if ok:
|
||||
if CALLBACK_BASE_URL:
|
||||
keys_to_register = set(store.list_all_keys())
|
||||
if _callback_key:
|
||||
keys_to_register.add(_callback_key)
|
||||
for k in keys_to_register:
|
||||
if k and k.strip():
|
||||
ok = await _register_message_callback(k.strip())
|
||||
if ok:
|
||||
logger.info("启动时已注册回调 key=***%s", k[-4:] if len(k) >= 4 else "****")
|
||||
if keys_to_register:
|
||||
logger.info("消息接收已切换为实时回调入口,不再启动 WS GetSyncMsg")
|
||||
else:
|
||||
asyncio.create_task(start_ws_sync())
|
||||
@@ -448,6 +455,50 @@ async def api_check_proxy(proxy: Optional[str] = Query(None, description="可选
|
||||
|
||||
preview = _preview(proxy_url)
|
||||
logger.info("check-proxy: source=%s, proxy_preview=%s", source, preview)
|
||||
is_socks = proxy_url.strip().lower().startswith("socks5")
|
||||
if is_socks:
|
||||
try:
|
||||
from httpx_socks import AsyncProxyTransport
|
||||
# httpx_socks/urllib 仅识别 socks5,将 socks5h 转为 socks5
|
||||
transport_url = proxy_url.strip()
|
||||
if transport_url.lower().startswith("socks5h://"):
|
||||
transport_url = "socks5://" + transport_url[10:]
|
||||
transport = AsyncProxyTransport.from_url(transport_url)
|
||||
async with httpx.AsyncClient(trust_env=False, timeout=15.0, transport=transport) as client:
|
||||
resp = await client.get(PROXY_CHECK_URL)
|
||||
if resp.status_code == 200:
|
||||
logger.info("check-proxy: ok (socks), status=%s", resp.status_code)
|
||||
return {
|
||||
"ok": True,
|
||||
"source": source,
|
||||
"proxy_preview": preview,
|
||||
"check_url": PROXY_CHECK_URL,
|
||||
"status_code": resp.status_code,
|
||||
}
|
||||
logger.warning("check-proxy: fail (socks), status=%s", resp.status_code)
|
||||
return {
|
||||
"ok": False,
|
||||
"source": source,
|
||||
"proxy_preview": preview,
|
||||
"error": f"请求测试页返回 {resp.status_code}",
|
||||
"status_code": resp.status_code,
|
||||
}
|
||||
except ImportError:
|
||||
logger.info("check-proxy: socks5 已配置,跳过连通性检测(需 pip install httpx-socks 方可检测)")
|
||||
return {
|
||||
"ok": True,
|
||||
"source": source,
|
||||
"proxy_preview": preview,
|
||||
"note": "socks5 代理已配置;连通性检测需安装 pip install httpx-socks",
|
||||
}
|
||||
except Exception as e:
|
||||
logger.warning("check-proxy: socks exception %s", e)
|
||||
return {
|
||||
"ok": False,
|
||||
"source": source,
|
||||
"proxy_preview": preview,
|
||||
"error": str(e),
|
||||
}
|
||||
try:
|
||||
async with httpx.AsyncClient(trust_env=False, timeout=15.0, proxy=proxy_url) as client:
|
||||
resp = await client.get(PROXY_CHECK_URL)
|
||||
|
||||
Reference in New Issue
Block a user