Skip to content

Commit 2a5a0d4

Browse files
committed
Add visibility trigger
1 parent d8be4ef commit 2a5a0d4

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

docs/http_requests.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ All 33 endpoints across 5 route files plus `main.py`:
550550
| Method | Path | Description |
551551
|--------|------|-------------|
552552
| GET | `/` | Main HTML page with injected `window.appConfig` |
553+
| GET | `/vpn-status` | Check if client IP is within the WireGuard subnet |
553554

554555
### routes/stream.py
555556
| Method | Path | Description |

main.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import uvicorn
1111
from dotenv import load_dotenv
1212
from fastapi import FastAPI, Request
13-
from fastapi.responses import HTMLResponse
13+
from fastapi.responses import HTMLResponse, JSONResponse
1414
from fastapi.staticfiles import StaticFiles
1515
from fastapi.templating import Jinja2Templates
1616

@@ -205,6 +205,16 @@ def _is_on_wireguard(client_ip: str, wireguard_subnet: Optional[str]) -> bool:
205205
return True
206206

207207

208+
@app.get("/vpn-status")
209+
def vpn_status(request: Request) -> JSONResponse:
210+
"""Return whether the client is currently on the WireGuard VPN."""
211+
client = request.client
212+
client_ip = client.host if client else ""
213+
on_vpn = _is_on_wireguard(client_ip, config.wireguard_subnet)
214+
vpn_warning = config.wireguard_subnet is not None and not on_vpn
215+
return JSONResponse({"vpn_warning": vpn_warning})
216+
217+
208218
@app.get("/")
209219
def index(request: Request) -> HTMLResponse:
210220
"""Serve the main HTML page."""

static/app.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,6 +2342,22 @@ document.addEventListener('visibilitychange', async function () {
23422342
})();
23432343
// ── End Player Bar Drag-to-Snap ──────────────────────────────────────────────
23442344

2345+
// Re-check VPN status when the tab regains focus so the banner auto-dismisses
2346+
// if the user turns on WireGuard and switches back.
2347+
document.addEventListener('visibilitychange', () => {
2348+
if (document.visibilityState === 'visible') {
2349+
fetch('/vpn-status')
2350+
.then(r => r.json())
2351+
.then(data => {
2352+
const banner = document.getElementById('vpn-banner');
2353+
if (banner && !data.vpn_warning) {
2354+
banner.remove();
2355+
}
2356+
})
2357+
.catch(() => {}); // Silently ignore network errors
2358+
}
2359+
});
2360+
23452361
// Poll status every 3 seconds
23462362
setInterval(fetchStatus, 3000);
23472363

templates/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ <h2>
241241
<script src="/static/update-checker.js?v=1737842302"></script>
242242

243243
<!-- Main Application Script -->
244-
<script src="/static/app.js?v=1772075939"></script>
244+
<script src="/static/app.js?v=1772075940"></script>
245245
</body>
246246

247247
</html>

0 commit comments

Comments
 (0)