feat: 基础层——phonenumberslite/resolve_region_code/CA字典bug/XFF服务端优先

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
浅念
2026-10-08 16:48:55 +08:00
parent 1d4e282b14
commit 1c4d204906
5 changed files with 47 additions and 5 deletions
+6 -2
View File
@@ -22,9 +22,9 @@ async def ws_chat(websocket: WebSocket):
# Collect client info
client_ip = websocket.client.host if websocket.client else ""
forwarded = websocket.headers.get("x-forwarded-for", "")
forwarded = websocket.headers.get("x-forwarded-for", "").split(",")[0].strip()
if forwarded:
client_ip = forwarded.split(",")[0].strip()
client_ip = forwarded
user_agent = websocket.headers.get("user-agent", "")
client_info = {"ip": client_ip, "user_agent": user_agent}
@@ -58,6 +58,10 @@ async def ws_chat(websocket: WebSocket):
init_msg.get("source", "standalone")
)
merged_info = {**client_info, **(init_msg.get("client_info") or {})}
# 服务端 XFF 优先于客户端自报 ip(客户端自报可伪造,可伪造面最小);
# 无 XFF 时保留客户端自报,user_agent 等其他字段不受影响
if forwarded:
merged_info["ip"] = forwarded
conv = await manager.create_conversation(
source=source,
client_info=merged_info,