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 b03b20aee6
commit c4ebfb6432
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"])
@@ -12,10 +12,10 @@
const TS_IP = "100.78.131.124";
let lanReachable = $state(false);
if (typeof window !== "undefined" && location.hostname !== LAN_IP) {
fetch(`http://${LAN_IP}:4000/`, { mode: "no-cors", signal: AbortSignal.timeout(1500) })
.then(() => { lanReachable = true; })
.catch(() => {});
if (typeof window !== "undefined") {
fetch("/api/client-ip").then(r => r.json()).then(d => {
if (d.lan) lanReachable = true;
}).catch(() => {});
}
const media = [