fix: read real client IP from X-Forwarded-For behind Caddy/Docker
Deploy Dashboard / deploy (push) Successful in 36s
Deploy Dashboard / deploy (push) Successful in 36s
This commit is contained in:
@@ -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."):
|
||||
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 {"ip": ip, "lan": True}
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
return {"ip": ip, "lan": False}
|
||||
return False
|
||||
|
||||
# Public auth endpoints
|
||||
app.include_router(auth.router, prefix="/api/auth", tags=["auth"])
|
||||
|
||||
Reference in New Issue
Block a user