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:
+14
-14
@@ -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"])
|
||||||
|
|||||||
Reference in New Issue
Block a user