-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstart_suite.py
More file actions
executable file
·327 lines (265 loc) · 9.87 KB
/
start_suite.py
File metadata and controls
executable file
·327 lines (265 loc) · 9.87 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
#!/usr/bin/env python3
# =============================================================================
# Pi-hole Suite REST API (OPTIONAL COMPONENT)
# =============================================================================
# This is an *optional* companion API for the Pi-hole + Unbound setup.
# It is NOT required for the core DNS stack (Pi-hole + Unbound) to work.
#
# What it provides:
# GET /health - uptime / version
# GET /version - app version
# GET /urls - detected service URLs
# GET /pihole - Pi-hole version + FTL status
# GET /unbound - Unbound port + dig test
# GET /dns?limit=N - last N DNS log entries
# GET /leases?limit=N - DHCP leases
# GET /stats - summary stats
#
# Authentication: X-API-Key header (SUITE_API_KEY env var required)
# Default port: 8090 (SUITE_PORT env var)
# Default bind: 127.0.0.1 (SUITE_HOST env var)
#
# Start: python3 start_suite.py
# Deps: pip install -r requirements.txt
# Docs: GET /docs (Swagger UI auto-generated by FastAPI)
# =============================================================================
import os
import re
import subprocess
import time
from typing import Any
from urllib.request import Request, urlopen
from fastapi import Depends, FastAPI, Header, HTTPException
import uvicorn
APP_VERSION = "1.0.0"
START_TIME = time.time()
def _env(name: str, default: str | None = None) -> str:
value = os.getenv(name)
if value is None or value == "":
if default is None:
raise RuntimeError(f"Missing required environment variable: {name}")
return default
return value
def require_api_key(x_api_key: str | None = Header(default=None, alias="X-API-Key")) -> None:
expected = _env("SUITE_API_KEY", default="")
if expected == "":
raise HTTPException(status_code=500, detail="Server not configured: SUITE_API_KEY missing")
if x_api_key != expected:
raise HTTPException(status_code=401, detail="Unauthorized")
app = FastAPI(title="Pi-hole Suite API", version=APP_VERSION)
def _read_lines(path: str, limit: int) -> list[str]:
try:
with open(path, "r", encoding="utf-8", errors="replace") as f:
lines = f.readlines()
return lines[-limit:]
except OSError:
return []
def _run(cmd: list[str], timeout: float = 2.0) -> dict[str, Any]:
try:
p = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout, check=False)
return {
"ok": True,
"cmd": cmd,
"returncode": p.returncode,
"stdout": p.stdout.strip(),
"stderr": p.stderr.strip(),
}
except (OSError, subprocess.TimeoutExpired) as e:
return {"ok": False, "cmd": cmd, "error": str(e)}
def _guess_host_ip() -> str:
# Best-effort: pick first host IP; used for URL display.
# Never blocks for long.
r = _run(["hostname", "-I"], timeout=1.0)
if r.get("ok") and r.get("stdout"):
first = r["stdout"].split()[0]
if first:
return first
r = _run(["ip", "-4", "route", "get", "1.1.1.1"], timeout=1.0)
if r.get("ok") and r.get("stdout"):
m = re.search(r"src\s+(\d+\.\d+\.\d+\.\d+)", r["stdout"])
if m:
return m.group(1)
return "127.0.0.1"
def _http_ok(url: str, timeout: float = 1.5) -> bool:
try:
req = Request(url, method="GET")
with urlopen(req, timeout=timeout) as resp:
return 200 <= resp.status < 500
except Exception:
return False
def _parse_pihole_log(limit: int) -> list[dict[str, Any]]:
# Best-effort parser for classic Pi-hole log format.
# If file isn't present/accessible, returns empty list.
candidates = [
"/var/log/pihole/pihole.log",
"/var/log/pihole.log",
]
lines: list[str] = []
for p in candidates:
lines = _read_lines(p, limit * 3)
if lines:
break
out: list[dict[str, Any]] = []
# Example patterns differ by versions; keep it permissive.
# Jan 1 12:00:00 dnsmasq[1234]: query[A] example.com from 192.168.1.2
rx = re.compile(
r"^(?P<ts>\w{3}\s+\d+\s+\d+:\d+:\d+).*(query\[[A-Z]+\])\s+(?P<q>[^\s]+)\s+from\s+(?P<client>[^\s]+)"
)
for line in reversed(lines):
m = rx.search(line)
if not m:
continue
out.append(
{
"timestamp": m.group("ts"),
"client": m.group("client"),
"query": m.group("q"),
"action": "query",
}
)
if len(out) >= limit:
break
return list(reversed(out))
def _parse_dhcp_leases(limit: int) -> list[dict[str, Any]]:
# Pi-hole DHCP leases file: /etc/pihole/dhcp.leases
# Format: <expiry_epoch> <mac> <ip> <hostname> <clientid>
# We only have expiry; lease_start may be unknown.
import datetime
path = "/etc/pihole/dhcp.leases"
lines = _read_lines(path, limit * 3)
out: list[dict[str, Any]] = []
def to_iso(epoch: str) -> str | None:
try:
ts = int(epoch)
except ValueError:
return None
return datetime.datetime.fromtimestamp(ts, tz=datetime.timezone.utc).isoformat()
for line in reversed(lines):
parts = line.strip().split()
if len(parts) < 4:
continue
expiry, mac, ip, hostname = parts[0:4]
out.append(
{
"ip": ip,
"mac": mac,
"hostname": hostname,
"lease_start": None,
"lease_end": to_iso(expiry) or expiry,
}
)
if len(out) >= limit:
break
return list(reversed(out))
def _read_pihole_v6_upstreams() -> list[str]:
toml = "/etc/pihole/pihole.toml"
try:
with open(toml, "r", encoding="utf-8", errors="replace") as f:
t = f.read()
except OSError:
return []
# Very small parser: look for upstreams = ["127.0.0.1#5335", ...]
m = re.search(r"^upstreams\s*=\s*\[(.*?)\]", t, flags=re.M)
if not m:
return []
inner = m.group(1)
return [s.strip().strip('"') for s in inner.split(",") if s.strip()]
@app.get("/health", dependencies=[Depends(require_api_key)])
def health() -> dict[str, Any]:
return {
"ok": True,
"message": "Pi-hole Suite API is running",
"version": APP_VERSION,
"uptime_seconds": int(time.time() - START_TIME),
}
@app.get("/version", dependencies=[Depends(require_api_key)])
def version() -> dict[str, Any]:
return {
"app": "pihole-suite",
"version": APP_VERSION,
"uptime_seconds": int(time.time() - START_TIME),
}
@app.get("/urls", dependencies=[Depends(require_api_key)])
def urls() -> dict[str, Any]:
host_ip = _guess_host_ip()
suite_port = int(_env("SUITE_PORT", default="8090"))
netalertx_port = int(_env("NETALERTX_PORT", default="20211"))
return {
"host_ip": host_ip,
"pihole_admin": f"http://{host_ip}/admin",
"netalertx": f"http://{host_ip}:{netalertx_port}",
"suite_local": f"http://127.0.0.1:{suite_port}",
"note": "Suite binds to 127.0.0.1 by default; access remotely via a reverse proxy if needed.",
}
@app.get("/pihole", dependencies=[Depends(require_api_key)])
def pihole() -> dict[str, Any]:
v = _run(["pihole", "-v"], timeout=2.0)
ftl = _run(["systemctl", "is-active", "pihole-FTL"], timeout=2.0)
upstreams = _read_pihole_v6_upstreams()
return {
"pihole_version": v.get("stdout", "") if v.get("ok") else "unknown",
"ftl_active": (ftl.get("ok") and ftl.get("returncode") == 0),
"upstreams": upstreams,
}
@app.get("/unbound", dependencies=[Depends(require_api_key)])
def unbound() -> dict[str, Any]:
unbound_port = int(_env("UNBOUND_PORT", default="5335"))
svc = _run(["systemctl", "is-active", "unbound"], timeout=2.0)
dig = _run(
[
"dig",
"+short",
f"@127.0.0.1",
"-p",
str(unbound_port),
"+time=1",
"+tries=1",
"cloudflare.com",
],
timeout=2.0,
)
ok = bool(dig.get("ok") and dig.get("stdout"))
return {
"port": unbound_port,
"service_active": (svc.get("ok") and svc.get("returncode") == 0),
"dig_ok": ok,
"dig_result": dig.get("stdout", ""),
}
@app.get("/netalertx", dependencies=[Depends(require_api_key)])
def netalertx() -> dict[str, Any]:
port = int(_env("NETALERTX_PORT", default="20211"))
url = f"http://127.0.0.1:{port}/"
reachable = _http_ok(url)
return {
"port": port,
"url_local": url,
"http_reachable": reachable,
}
@app.get("/dns", dependencies=[Depends(require_api_key)])
def dns(limit: int = 50) -> list[dict[str, Any]]:
limit = max(1, min(limit, 500))
return _parse_pihole_log(limit)
@app.get("/devices", dependencies=[Depends(require_api_key)])
def devices() -> list[dict[str, Any]]:
# Placeholder: device discovery depends on NetAlertX/Pi.Alert APIs and is
# environment-specific.
return []
@app.get("/leases", dependencies=[Depends(require_api_key)])
def leases(limit: int = 200) -> list[dict[str, Any]]:
limit = max(1, min(limit, 2000))
return _parse_dhcp_leases(limit)
@app.get("/stats", dependencies=[Depends(require_api_key)])
def stats() -> dict[str, Any]:
recent = _parse_pihole_log(100)
return {
"total_dns_logs": len(recent),
"total_devices": 0,
"recent_queries": len(recent),
"note": "DNS stats are derived from best-effort log parsing; may be empty depending on Pi-hole logging/permissions.",
}
def main() -> None:
port = int(_env("SUITE_PORT", default="8090"))
host = _env("SUITE_HOST", default="127.0.0.1")
uvicorn.run(app, host=host, port=port, log_level=os.getenv("SUITE_LOG_LEVEL", "info").lower())
if __name__ == "__main__":
main()