fix: read real client IP from X-Forwarded-For behind Caddy/Docker
Deploy Dashboard / deploy (push) Successful in 36s

This commit is contained in:
Gan, Jimmy
2026-02-26 03:29:28 +08:00
parent 6ed57f56f5
commit a4018ed3ca
+14 -14
View File
@@ -116,20 +116,20 @@ async def health():
@app.get("/api/client-ip") @app.get("/api/client-ip")
async def client_ip(request: Request): async def client_ip(request: Request):
ip = request.client.host ip = request.headers.get("x-forwarded-for", "").split(",")[0].strip() or request.client.host
if ip.startswith("192.168.31."): lan = ip.startswith("192.168.31.") or (ip.startswith("100.") and _router_reachable())
return {"ip": ip, "lan": True} return {"ip": ip, "lan": lan}
if ip.startswith("100."):
import socket def _router_reachable():
try: import socket
s = socket.socket() try:
s.settimeout(1) s = socket.socket()
s.connect(("192.168.31.1", 80)) s.settimeout(1)
s.close() s.connect(("192.168.31.1", 80))
return {"ip": ip, "lan": True} s.close()
except Exception: return True
pass except Exception:
return {"ip": ip, "lan": False} return False
# Public auth endpoints # Public auth endpoints
app.include_router(auth.router, prefix="/api/auth", tags=["auth"]) app.include_router(auth.router, prefix="/api/auth", tags=["auth"])