feat: smart LAN detection for sidebar links via X-Forwarded-For and router probe

This commit is contained in:
Gan, Jimmy
2026-02-26 03:11:13 +08:00
parent 22e9066ffe
commit 879ae4b6a3
2 changed files with 21 additions and 4 deletions
+17
View File
@@ -114,6 +114,23 @@ async def startup_event():
async def health():
return {"status": "ok"}
@app.get("/api/client-ip")
async def client_ip(request: Request):
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"])