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")
async def client_ip(request: Request):
ip = request.client.host
if ip.startswith("192.168.31."):
return {"ip": ip, "lan": True}
if ip.startswith("100."):
import socket
try:
s = socket.socket()
s.settimeout(1)
s.connect(("192.168.31.1", 80))
s.close()
return {"ip": ip, "lan": True}
except Exception:
pass
return {"ip": ip, "lan": False}
ip = request.headers.get("x-forwarded-for", "").split(",")[0].strip() or request.client.host
lan = ip.startswith("192.168.31.") or (ip.startswith("100.") and _router_reachable())
return {"ip": ip, "lan": lan}
def _router_reachable():
import socket
try:
s = socket.socket()
s.settimeout(1)
s.connect(("192.168.31.1", 80))
s.close()
return True
except Exception:
return False
# Public auth endpoints
app.include_router(auth.router, prefix="/api/auth", tags=["auth"])