-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.py
More file actions
369 lines (323 loc) · 14 KB
/
Copy pathapp.py
File metadata and controls
369 lines (323 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
"""ph-intercept - DNS game (Pi-hole, AdGuard Home, and Technitium)."""
import asyncio
from contextlib import asynccontextmanager
from pathlib import Path
import httpx
from starlette.applications import Starlette
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Request
from starlette.responses import HTMLResponse, JSONResponse, StreamingResponse
from starlette.routing import Mount, Route
from starlette.staticfiles import StaticFiles
from starlette.templating import Jinja2Templates
from core.config import (
BG_MODE, BG_IMAGE, PROVIDER, RETURN_URL, SKY_PRESET, SKY_PRESETS,
TWO_PLAYER_LOCAL_CONFIGURED, TWO_PLAYER_ENABLED, P2_DASHBOARD, P2_VERIFY_SSL,
PIHOLE2_URL, PIHOLE2_PASSWORD,
ADGUARD2_BASE, ADGUARD2_USERNAME, ADGUARD2_PASSWORD,
TECHNITIUM2_BASE, TECHNITIUM2_TOKEN, TECHNITIUM2_USER, TECHNITIUM2_PASSWORD,
)
from core.multiplayer import get_status as mp_status, set_mode as mp_set_mode
if PROVIDER == "adguard":
from core.config import ADGUARD_DASHBOARD as _DASHBOARD, ADGUARD_VERIFY_SSL as _VERIFY_SSL
from core.adguard import (
add_ws_client, get_stats, query_poller, remove_ws_client,
reset_watermark, toggle_blocking, trigger_filter_update, drop_session,
)
elif PROVIDER == "technitium":
from core.config import TECHNITIUM_DASHBOARD as _DASHBOARD, TECHNITIUM_VERIFY_SSL as _VERIFY_SSL
from core.technitium import (
add_ws_client, get_stats, query_poller, remove_ws_client,
reset_watermark, toggle_blocking, trigger_filter_update, drop_session,
)
else:
from core.config import PIHOLE_DASHBOARD as _DASHBOARD, PIHOLE_VERIFY_SSL as _VERIFY_SSL
from core.pihole import (
add_ws_client, remove_ws_client, reset_watermark, toggle_blocking, drop_session, query_poller,
get_pihole_stats as get_stats, trigger_gravity_update as trigger_filter_update,
)
if TWO_PLAYER_LOCAL_CONFIGURED:
# The second instance mirrors the primary provider.
if PROVIDER == "adguard":
from core.adguard import (
add_p2_ws_client, remove_p2_ws_client, reset_p2_watermark,
get_p2_stats, drop_p2_session, query_p2_poller,
toggle_p2_blocking, trigger_p2_gravity_update,
)
elif PROVIDER == "technitium":
from core.technitium import (
add_p2_ws_client, remove_p2_ws_client, reset_p2_watermark,
get_p2_stats, drop_p2_session, query_p2_poller,
toggle_p2_blocking, trigger_p2_gravity_update,
probe_p2 as technitium_probe_p2,
)
else:
from core.pihole import (
add_p2_ws_client, remove_p2_ws_client, reset_p2_watermark,
get_p2_pihole_stats as get_p2_stats, drop_p2_session, query_p2_poller,
toggle_p2_blocking, trigger_p2_gravity_update,
)
_http_client: httpx.AsyncClient | None = None
_http_client2: httpx.AsyncClient | None = None
@asynccontextmanager
async def lifespan(_app):
global _http_client, _http_client2
_http_client = httpx.AsyncClient(timeout=1.5, headers={"User-Agent": "ph-intercept"}, verify=_VERIFY_SSL)
tasks = [asyncio.create_task(query_poller(_http_client))]
if TWO_PLAYER_LOCAL_CONFIGURED:
_http_client2 = httpx.AsyncClient(timeout=1.5, headers={"User-Agent": "ph-intercept"}, verify=P2_VERIFY_SSL)
tasks.append(asyncio.create_task(query_p2_poller(_http_client2)))
yield
for t in tasks:
t.cancel()
for t in tasks:
try:
await t
except asyncio.CancelledError:
pass
try:
async with asyncio.timeout(1.0):
await drop_session(_http_client)
except Exception:
pass
await _http_client.aclose()
if _http_client2:
try:
async with asyncio.timeout(1.0):
await drop_p2_session(_http_client2)
except Exception:
pass
await _http_client2.aclose()
_base = Path(__file__).parent
templates = Jinja2Templates(directory=_base / "templates")
_CSP = (
"default-src 'self'; "
"script-src 'self' 'unsafe-inline'; "
"style-src 'self' 'unsafe-inline'; "
"img-src * data: blob:; "
"font-src 'self'; "
"connect-src 'self'; "
"frame-ancestors *"
)
class ResponseHeaderMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request, call_next):
response = await call_next(request)
response.headers["X-Content-Type-Options"] = "nosniff"
response.headers["Referrer-Policy"] = "no-referrer"
response.headers["Content-Security-Policy"] = _CSP
if request.url.path.startswith("/static/"):
if request.url.path.endswith(('.woff2', '.woff', '.ttf', '.otf')):
response.headers["Cache-Control"] = "public, max-age=31536000, immutable"
else:
response.headers["Cache-Control"] = "public, max-age=3600"
return response
async def index(request: Request) -> HTMLResponse:
preset = SKY_PRESETS.get(SKY_PRESET, SKY_PRESETS["summer_triangle"])
return templates.TemplateResponse(request, "index.html", {
"bg_mode": BG_MODE,
"provider": PROVIDER,
"pihole_dashboard": _DASHBOARD,
"p2_dashboard": P2_DASHBOARD,
"two_player_enabled": TWO_PLAYER_ENABLED,
"bg_config": {
"bg_mode": BG_MODE,
"bg_image": BG_IMAGE,
"sky_ra": preset["ra"],
"sky_dec": preset["dec"],
"sky_preset": SKY_PRESET,
"return_url": RETURN_URL,
},
# Full preset table so the client can switch sky presets in-app without a round trip.
"sky_presets": SKY_PRESETS,
})
async def pihole_stats(_request: Request) -> JSONResponse:
data = await get_stats(_http_client)
if not data:
return JSONResponse({})
return JSONResponse({
"percent": data.get("percent"),
"queries": data.get("queries"),
"blocked": data.get("blocked"),
"no_error": data.get("no_error"),
"gravity": data.get("gravity"),
"blocking": data.get("blocking"),
"block_timer": data.get("block_timer"),
})
async def pihole_toggle(request: Request) -> JSONResponse:
body = await request.json()
timer = body.get("timer")
if timer is not None:
try:
timer = int(timer)
if timer <= 0:
timer = None
except (TypeError, ValueError):
timer = None
return JSONResponse(await toggle_blocking(_http_client, bool(body.get("enable", True)), timer))
async def two_player_status(_request: Request) -> JSONResponse:
return JSONResponse(mp_status(TWO_PLAYER_LOCAL_CONFIGURED))
_P2_PLACEHOLDER = "CHANGE.ME"
async def _p2_config_error() -> str | None:
"""Validate the second-instance config and probe reachability for the active
provider. Returns an error string, or None when the instance is usable."""
if PROVIDER == "adguard":
if not ADGUARD2_BASE:
return "ADGUARD2_URL is not set in your configuration"
if _P2_PLACEHOLDER in ADGUARD2_BASE:
return "ADGUARD2_URL still has the default placeholder value. Update it in your compose.yaml"
if not (ADGUARD2_BASE.startswith("http://") or ADGUARD2_BASE.startswith("https://")):
return "ADGUARD2_URL must start with http:// or https://"
if not ADGUARD2_PASSWORD:
return "ADGUARD2_PASSWORD is not set in your configuration"
if _http_client2:
try:
resp = await _http_client2.get(
f"{ADGUARD2_BASE}/status",
auth=(ADGUARD2_USERNAME, ADGUARD2_PASSWORD),
)
if resp.status_code == 401:
return "AdGuard 2 authentication failed. Check ADGUARD2_USERNAME and ADGUARD2_PASSWORD"
if resp.status_code != 200:
return "Could not reach AdGuard 2. Check ADGUARD2_URL"
except Exception:
return "Could not reach AdGuard 2. Check ADGUARD2_URL"
return None
if PROVIDER == "technitium":
if not TECHNITIUM2_BASE:
return "TECHNITIUM2_URL is not set in your configuration"
if _P2_PLACEHOLDER in TECHNITIUM2_BASE:
return "TECHNITIUM2_URL still has the default placeholder value. Update it in your compose.yaml"
if not (TECHNITIUM2_BASE.startswith("http://") or TECHNITIUM2_BASE.startswith("https://")):
return "TECHNITIUM2_URL must start with http:// or https://"
if not (TECHNITIUM2_TOKEN or (TECHNITIUM2_USER and TECHNITIUM2_PASSWORD)):
return "Technitium 2 needs TECHNITIUM2_TOKEN, or TECHNITIUM2_USER and TECHNITIUM2_PASSWORD"
if _http_client2:
return await technitium_probe_p2(_http_client2)
return None
if not PIHOLE2_URL:
return "PIHOLE2_URL is not set in your configuration"
if _P2_PLACEHOLDER in PIHOLE2_URL:
return "PIHOLE2_URL still has the default placeholder value. Update it in your compose.yaml"
if not (PIHOLE2_URL.startswith("http://") or PIHOLE2_URL.startswith("https://")):
return "PIHOLE2_URL must start with http:// or https://"
if not PIHOLE2_PASSWORD:
return "PIHOLE2_PASSWORD is not set in your configuration"
if _http_client2:
try:
resp = await _http_client2.post(
f"{PIHOLE2_URL}/auth",
json={"password": PIHOLE2_PASSWORD},
)
if not resp.json().get("session", {}).get("valid"):
return "Pi-hole 2 authentication failed. Check PIHOLE2_PASSWORD"
except Exception:
return "Could not reach Pi-hole 2. Check PIHOLE2_URL"
return None
async def two_player_set_mode(request: Request) -> JSONResponse:
body = await request.json()
mode = body.get("mode", "")
if mode == "local":
err = await _p2_config_error()
if err:
return JSONResponse({"error": err}, status_code=400)
try:
return JSONResponse(mp_set_mode(mode, TWO_PLAYER_LOCAL_CONFIGURED))
except ValueError as exc:
return JSONResponse({"error": str(exc)}, status_code=400)
async def pihole2_stats(_request: Request) -> JSONResponse:
if not TWO_PLAYER_LOCAL_CONFIGURED or not _http_client2:
return JSONResponse({})
data = await get_p2_stats(_http_client2)
if not data:
return JSONResponse({})
return JSONResponse({
"percent": data.get("percent"),
"queries": data.get("queries"),
"blocked": data.get("blocked"),
"no_error": data.get("no_error"),
"gravity": data.get("gravity"),
"blocking": data.get("blocking"),
"block_timer": data.get("block_timer"),
})
async def pihole2_events(request: Request) -> StreamingResponse:
if not TWO_PLAYER_LOCAL_CONFIGURED:
return StreamingResponse(iter([]), media_type="text/event-stream")
async def generate():
q: asyncio.Queue = asyncio.Queue(maxsize=60)
add_p2_ws_client(q)
reset_p2_watermark()
yield ": ok\n\n"
try:
while True:
payload = await q.get()
if payload is None:
break
yield f"data: {payload}\n\n"
except asyncio.CancelledError:
pass
finally:
remove_p2_ws_client(q)
return StreamingResponse(
generate(),
media_type="text/event-stream",
headers={"X-Accel-Buffering": "no", "Cache-Control": "no-cache"},
)
async def pihole_gravity_update(request: Request) -> JSONResponse:
return JSONResponse(await trigger_filter_update(_http_client))
async def pihole2_toggle(request: Request) -> JSONResponse:
if not TWO_PLAYER_LOCAL_CONFIGURED or not _http_client2:
return JSONResponse({"error": "not configured"}, status_code=400)
body = await request.json()
timer = body.get("timer")
if timer is not None:
try:
timer = int(timer)
if timer <= 0:
timer = None
except (TypeError, ValueError):
timer = None
return JSONResponse(await toggle_p2_blocking(_http_client2, bool(body.get("enable", True)), timer))
async def pihole2_gravity_update(_request: Request) -> JSONResponse:
if not TWO_PLAYER_LOCAL_CONFIGURED or not _http_client2:
return JSONResponse({"error": "not configured"}, status_code=400)
return JSONResponse(await trigger_p2_gravity_update(_http_client2))
async def pihole_events(request: Request) -> StreamingResponse:
async def generate():
q: asyncio.Queue = asyncio.Queue(maxsize=60)
add_ws_client(q)
reset_watermark()
yield ": ok\n\n"
try:
while True:
payload = await q.get()
if payload is None:
break
yield f"data: {payload}\n\n"
except asyncio.CancelledError:
pass
finally:
remove_ws_client(q)
return StreamingResponse(
generate(),
media_type="text/event-stream",
headers={"X-Accel-Buffering": "no", "Cache-Control": "no-cache"},
)
app = Starlette(
lifespan=lifespan,
routes=[
Route("/", index),
Route("/api/pihole/stats", pihole_stats),
Route("/api/pihole/toggle", pihole_toggle, methods=["POST"]),
Route("/api/pihole/gravity-update", pihole_gravity_update, methods=["POST"]),
Route("/api/2p/status", two_player_status),
Route("/api/2p/mode", two_player_set_mode, methods=["POST"]),
Route("/api/pihole/events", pihole_events),
Route("/api/pihole2/stats", pihole2_stats),
Route("/api/pihole2/toggle", pihole2_toggle, methods=["POST"]),
Route("/api/pihole2/gravity-update", pihole2_gravity_update, methods=["POST"]),
Route("/api/pihole2/events", pihole2_events),
Mount("/static", StaticFiles(directory=_base / "static"), name="static"),
Mount("/bg", StaticFiles(directory=_base / "static" / "bg", check_dir=False), name="bg"),
],
)
app.add_middleware(ResponseHeaderMiddleware)