-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
255 lines (208 loc) · 9.21 KB
/
Copy pathserver.py
File metadata and controls
255 lines (208 loc) · 9.21 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
"""
server.py
=========
Local aiohttp HTTP API used as the IPC channel between the GUI and the
networking engine.
Endpoints
---------
GET /api/devices – snapshot of every known device (JSON list)
POST /api/monitor – body: {"mac": "...", "monitored": true|false}
POST /api/scan – trigger an immediate ARP sweep
GET /api/health – liveness probe
CRITICAL (Blueprint 3.E):
Do NOT use `await request.json()`.
If the client forgets `Content-Type: application/json` it blows up
with a silent HTTP 400. We always parse manually:
raw = await request.text()
data = json.loads(raw)
"""
import asyncio
import json
import threading
from typing import Optional
from aiohttp import web
from device_registry import REGISTRY
# Filled in by main.py before start() is called.
SPOOFER = None # type: Optional[object] (spoofer.Spoofer)
SCANNER = None # type: Optional[object] (scanner.Scanner)
# ----------------------------------------------------------------------
# Handlers
# ----------------------------------------------------------------------
async def health(_request: web.Request) -> web.Response:
return web.json_response({"ok": True})
async def list_devices(_request: web.Request) -> web.Response:
devices = REGISTRY.get_all()
# Strip private "_prev_*" keys – not interesting to the GUI.
public = []
for d in devices:
speed_limit_kbps = (d["speed_limit"] / 1024) if d["speed_limit"] else None
public.append({
"ip": d["ip"],
"mac": d["mac"],
"name": d["name"],
"dl_bytes": d["dl_bytes"],
"ul_bytes": d["ul_bytes"],
"dl_speed": round(d["dl_speed"], 1),
"ul_speed": round(d["ul_speed"], 1),
"is_monitored": d["is_monitored"],
"is_blocked": d["is_blocked"],
"speed_limit_kbps": round(speed_limit_kbps, 1) if speed_limit_kbps else None,
"first_seen": d["first_seen"],
"last_seen": d["last_seen"],
})
return web.json_response({"devices": public})
async def toggle_monitor(request: web.Request) -> web.Response:
"""
Toggle the is_monitored flag for a device.
Body (JSON, parsed MANUALLY per Blueprint 3.E):
{"mac": "aa:bb:cc:dd:ee:ff", "monitored": true|false}
"""
# ---- Manual JSON parsing as requested to avoid strict aiohttp errors ----
try:
raw = await request.text()
print(f"[API] [TOGGLE] /api/monitor received: {raw!r}")
data = json.loads(raw) if raw else {}
except json.JSONDecodeError as e:
print(f"[API] !! /api/monitor JSON decode error: {e}")
return web.json_response(
{"ok": False, "error": f"invalid JSON: {e}"}, status=400)
mac = (data.get("mac") or "").lower().strip()
# Handle both "monitored" and "is_monitored" keys just in case
monitored = bool(data.get("monitored") if "monitored" in data else data.get("is_monitored"))
if not mac:
return web.json_response(
{"ok": False, "error": "missing 'mac'"}, status=400)
# Capture previous state
prev = REGISTRY.get_by_mac(mac)
if prev is None:
print(f"[API] !! Toggle failed: Unknown MAC {mac}")
return web.json_response(
{"ok": False, "error": f"unknown mac {mac}"}, status=404)
prev_monitored = prev["is_monitored"]
print(f"[API] [TOGGLE] mac={mac} | {prev_monitored} -> {monitored}")
# Update Registry
updated = REGISTRY.set_monitored(mac, monitored)
if updated is None:
return web.json_response(
{"ok": False, "error": "registry update failed"}, status=500)
# CRITICAL: If transitioning from monitored -> unmonitored, trigger ARP RESTORE
if prev_monitored and not monitored:
# Mark unmonitored timestamp for L2Forwarder grace period
REGISTRY.set_unmonitored_timestamp(mac)
if SPOOFER is not None:
print(f"[API] [RESTORE] Triggering instant ARP restore for {updated['ip']} ...")
# Run in thread to not block the API response
def _restore_worker():
try:
SPOOFER.restore_arp(updated["ip"], updated["mac"])
except Exception as e:
print(f"[API] !! [RESTORE] Worker failed for {mac}: {e}")
threading.Thread(target=_restore_worker, name=f"Restore-{mac}", daemon=True).start()
else:
print("[API] !! [RESTORE] Warning: SPOOFER instance is None, cannot restore ARP!")
return web.json_response({"ok": True, "device": updated})
async def trigger_scan(_request: web.Request) -> web.Response:
if SCANNER is None:
return web.json_response(
{"ok": False, "error": "scanner not ready"}, status=503)
# Off-load the actual sniff to a worker thread.
loop = asyncio.get_running_loop()
new_count = await loop.run_in_executor(None, SCANNER.scan_once)
return web.json_response({"ok": True, "new_devices": new_count})
async def toggle_block(request: web.Request) -> web.Response:
"""
Toggle the is_blocked flag for a device.
Body (JSON, parsed MANUALLY per Blueprint 3.E):
{"mac": "aa:bb:cc:dd:ee:ff", "blocked": true|false}
"""
try:
raw = await request.text()
print(f"[API] /api/block raw body: {raw!r}")
data = json.loads(raw) if raw else {}
except json.JSONDecodeError as e:
print(f"[API] /api/block JSON decode error: {e}")
return web.json_response(
{"ok": False, "error": f"invalid JSON: {e}"}, status=400)
mac = (data.get("mac") or "").lower().strip()
blocked = bool(data.get("blocked"))
if not mac:
return web.json_response(
{"ok": False, "error": "missing 'mac'"}, status=400)
print(f"[API] /api/block mac={mac} -> blocked={blocked}")
updated = REGISTRY.set_blocked(mac, blocked)
if updated is None:
return web.json_response(
{"ok": False, "error": f"unknown mac {mac}"}, status=404)
return web.json_response({"ok": True, "device": updated})
async def set_speed_limit(request: web.Request) -> web.Response:
"""
Set speed limit for a device.
Body (JSON, parsed MANUALLY per Blueprint 3.E):
{"mac": "aa:bb:cc:dd:ee:ff", "speed_limit_kbps": 512.5 or null}
speed_limit_kbps: speed limit in KB/s, or null for unlimited
"""
try:
raw = await request.text()
print(f"[API] /api/speed raw body: {raw!r}")
data = json.loads(raw) if raw else {}
except json.JSONDecodeError as e:
print(f"[API] /api/speed JSON decode error: {e}")
return web.json_response(
{"ok": False, "error": f"invalid JSON: {e}"}, status=400)
mac = (data.get("mac") or "").lower().strip()
speed_limit_kbps = data.get("speed_limit_kbps")
if not mac:
return web.json_response(
{"ok": False, "error": "missing 'mac'"}, status=400)
# Convert KB/s to bytes/s, or None if unlimited
if speed_limit_kbps is None:
speed_limit_bps = None
else:
try:
speed_limit_bps = float(speed_limit_kbps) * 1024.0
if speed_limit_bps <= 0:
return web.json_response(
{"ok": False, "error": "speed_limit_kbps must be positive or null"}, status=400)
except (TypeError, ValueError):
return web.json_response(
{"ok": False, "error": "speed_limit_kbps must be a number or null"}, status=400)
print(f"[API] /api/speed mac={mac} -> speed_limit={speed_limit_bps} bytes/sec")
updated = REGISTRY.set_speed_limit(mac, speed_limit_bps)
if updated is None:
return web.json_response(
{"ok": False, "error": f"unknown mac {mac}"}, status=404)
return web.json_response({"ok": True, "device": updated})
# ----------------------------------------------------------------------
# Server bootstrap
# ----------------------------------------------------------------------
def build_app() -> web.Application:
app = web.Application()
app.router.add_get ("/api/health", health)
app.router.add_get ("/api/devices", list_devices)
app.router.add_post("/api/monitor", toggle_monitor)
app.router.add_post("/api/scan", trigger_scan)
app.router.add_post("/api/block", toggle_block)
app.router.add_post("/api/speed", set_speed_limit)
return app
def run_server_in_thread(host: str = "127.0.0.1", port: int = 8765) -> threading.Thread:
"""
Spin up the aiohttp server in its own thread with its own asyncio loop,
so we can keep the main thread free for the Tk GUI.
"""
def _serve():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
app = build_app()
runner = web.AppRunner(app)
loop.run_until_complete(runner.setup())
site = web.TCPSite(runner, host, port)
loop.run_until_complete(site.start())
print(f"[SERVER] aiohttp listening on http://{host}:{port}")
try:
loop.run_forever()
finally:
loop.run_until_complete(runner.cleanup())
loop.close()
t = threading.Thread(target=_serve, name="APIServer", daemon=True)
t.start()
return t