-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
518 lines (482 loc) · 21.1 KB
/
server.py
File metadata and controls
518 lines (482 loc) · 21.1 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, HTTPException, Form, Response, Request, Depends
from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse
from passlib.context import CryptContext
import asyncio
import main # Assumes your MVG API logic is in main.py
import json
import os
import secrets
import pyotp
import logging
from datetime import datetime, timedelta
# ------------------------------
# Logging filter to reduce spam
# ------------------------------
logging.getLogger("uvicorn.access").addFilter(
lambda record: "/admin/check-auth" not in record.getMessage() and "/ws" not in record.getMessage()
)
# ------------------------------
# FastAPI & security
# ------------------------------
app = FastAPI()
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
# ------------------------------
# GLOBALS
# ------------------------------
SETTINGS = {}
ACTIVE_SESSIONS = {}
TEMP_ACCESS = None
cached_departures = []
# SCALING FACTOR
SCALING = 0.8 # 1.0 = 100%, 1.5 = 150%, etc.
# ------------------------------
# SETTINGS LOADING/SAVING
# ------------------------------
def load_settings():
global SETTINGS
defaults = {
"stations": ["Klinikum Großhadern", "Max-Lebschke Platz"],
"update_interval": 30,
"admin_user": "admin",
"admin_hash": "",
"totp_devices": []
}
if os.path.exists("settings.json"):
try:
with open("settings.json", "r", encoding="utf-8") as f:
SETTINGS = {**defaults, **json.load(f)}
return
except:
pass
SETTINGS = defaults
def save_settings():
with open("settings.json", "w", encoding="utf-8") as f:
json.dump(SETTINGS, f, indent=4)
load_settings()
# ------------------------------
# SESSION DEPENDENCY
# ------------------------------
async def get_session(request: Request):
session_id = request.cookies.get("admin_session")
if not session_id or session_id not in ACTIVE_SESSIONS:
return None
return ACTIVE_SESSIONS[session_id]
# ------------------------------
# UPDATE DEPARTURES TASK
# ------------------------------
async def update_departures():
global cached_departures
while True:
try:
data = main.mvg_api(SETTINGS["stations"], api_type="departures", combine_departures=True)
cached_departures = data
await manager.broadcast(cached_departures)
except Exception as e:
print(f"MVG API Error: {e}")
await asyncio.sleep(SETTINGS["update_interval"])
@app.on_event("startup")
async def startup_event():
asyncio.create_task(update_departures())
# ------------------------------
# ADMIN LOGIN / 2FA
# ------------------------------
@app.get("/admin/login", response_class=HTMLResponse)
async def login_page():
return """
<html>
<head><title>Admin Login</title>
<style>
body { font-family: sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; background: #f0f2f5; margin: 0; }
.box { background: white; padding: 30px; border-radius: 12px; box-shadow: 0 4px 10px rgba(0,0,0,0.1); width: 320px; text-align: center; }
input { width: 100%; padding: 10px; margin: 10px 0; border: 1px solid #ddd; border-radius: 5px; box-sizing: border-box; }
.btn-blue { width: 100%; padding: 10px; background: #0055a4; color: white; border: none; border-radius: 5px; cursor: pointer; font-size: 16px; }
.btn-temp { width: 100%; padding: 8px; background: #fff; color: #d97706; border: 1px solid #d97706; border-radius: 5px; cursor: pointer; margin-top: 20px; font-size: 11px; }
.hint { font-size: 11px; color: #777; margin-top: 10px; }
</style>
</head>
<body>
<div class="box">
<h2>Admin Login</h2>
<form action="/admin/login" method="post">
<input type="text" name="username" placeholder="Username or TOTP Code" required>
<input type="password" name="password" placeholder="Password">
<button type="submit" class="btn-blue">Login</button>
<div class="hint">Tip: Enter your 6-digit Authenticator code as 'Username' for Quick Login.</div>
</form>
<form action="/admin/request-temp" method="post">
<button type="submit" class="btn-temp">Request Console Access (10m)</button>
</form>
</div>
</body>
</html>
"""
@app.post("/admin/login")
async def process_login(response: Response, username: str = Form(...), password: str = Form("")):
if username.isdigit() and len(username) == 6:
for device in SETTINGS["totp_devices"]:
if pyotp.TOTP(device["secret"]).verify(username):
session_id = secrets.token_hex(16)
ACTIVE_SESSIONS[session_id] = {"user": f"QuickAuth ({device['name']})", "approved": True}
resp = RedirectResponse(url="/admin", status_code=303)
resp.set_cookie(key="admin_session", value=session_id, httponly=True)
return resp
is_temp = TEMP_ACCESS and username == TEMP_ACCESS["user"] and password == TEMP_ACCESS["pass"] and datetime.now() < TEMP_ACCESS["expires"]
is_perm = (username == SETTINGS.get("admin_user") and SETTINGS.get("admin_hash") and pwd_context.verify(password, SETTINGS["admin_hash"]))
if not (is_temp or is_perm):
raise HTTPException(401, "Invalid Credentials")
session_id = secrets.token_hex(16)
plain_code = str(secrets.randbelow(9000) + 1000)
ACTIVE_SESSIONS[session_id] = {
"user": username,
"approved": False,
"hashed_code": pwd_context.hash(plain_code),
"display_code": plain_code,
"processing": False
}
resp = RedirectResponse(url="/admin/verify", status_code=303)
resp.set_cookie(key="admin_session", value=session_id, httponly=True)
return resp
@app.get("/admin/verify", response_class=HTMLResponse)
async def verify_page(session: dict = Depends(get_session)):
if not session: return RedirectResponse("/admin/login")
if session["approved"]: return RedirectResponse("/admin")
return f"""
<html>
<head>
<script>
setInterval(() => {{
fetch('/admin/check-auth').then(r => r.json()).then(d => {{ if(d.ok) location.href='/admin'; }})
}}, 2000);
</script>
<style>
body{{font-family:sans-serif;display:flex;justify-content:center;align-items:center;height:100vh;background:#fffbe6;margin:0;}}
.card{{text-align:center;padding:40px;background:#fff;border-radius:12px;box-shadow:0 4px 10px rgba(0,0,0,0.05); width: 350px;}}
input{{padding:10px;width:100%;margin:10px 0;border-radius:5px;border:1px solid #ddd;box-sizing:border-box;}}
button{{padding:10px;width:100%;background:#0055a4;color:#fff;border:none;border-radius:5px;cursor:pointer;}}
.btn-console {{ background: #333; margin-top: 20px; font-size: 12px; }}
.code-box {{ font-size: 24px; font-weight: bold; color: #d97706; margin: 10px 0; }}
</style>
</head>
<body>
<div class="card">
<h2>Two-Factor Authentication</h2>
<form action="/admin/verify-totp" method="post">
<p>Enter 6-digit App Code:</p>
<input type="text" name="totp_code" placeholder="000000" autofocus autocomplete="off">
<button type="submit">Verify App</button>
</form>
<hr style="margin:25px 0; border:0; border-top:1px solid #eee;">
<p>Option 2: Use Server Console</p>
<div class="code-box">{session['display_code']}</div>
<form action="/admin/trigger-console" method="post">
<button type="submit" class="btn-console">Authorize via Console</button>
</form>
</div>
</body>
</html>
"""
@app.post("/admin/trigger-console")
async def trigger_console(session: dict = Depends(get_session)):
if not session or session.get("processing"):
return RedirectResponse("/admin/verify", status_code=303)
async def console_input_loop():
session["processing"] = True
print(f"\n[LOGIN ATTEMPT] User: {session['user']}")
print(f"To approve, enter the code shown on the screen: {session['display_code']}")
loop = asyncio.get_event_loop()
try:
u_input = await loop.run_in_executor(None, lambda: input("Enter Code: "))
if pwd_context.verify(u_input.strip(), session["hashed_code"]):
session["approved"] = True
print(">>> Access GRANTED.")
else:
print(">>> Access DENIED: Invalid code.")
except Exception as e:
print(f"Console error: {e}")
finally:
session["processing"] = False
asyncio.create_task(console_input_loop())
return RedirectResponse("/admin/verify", status_code=303)
@app.post("/admin/verify-totp")
async def verify_totp(totp_code: str = Form(...), session: dict = Depends(get_session)):
if not session: return RedirectResponse("/admin/login")
for device in SETTINGS["totp_devices"]:
if pyotp.TOTP(device["secret"]).verify(totp_code):
session["approved"] = True
return RedirectResponse("/admin", status_code=303)
return HTMLResponse("Invalid Code. <a href='/admin/verify'>Try again</a>")
@app.get("/admin/check-auth")
async def check_auth(session: dict = Depends(get_session)):
return {"ok": session["approved"] if session else False}
@app.post("/admin/request-temp")
async def request_temp():
global TEMP_ACCESS
user, pw = f"temp_{secrets.token_hex(2)}", secrets.token_urlsafe(8)
TEMP_ACCESS = {"user": user, "pass": pw, "expires": datetime.now() + timedelta(minutes=10)}
print(f"\n\n{'='*20}\nTEMP LOGIN DETAILS:\nUser: {user}\nPass: {pw}\n{'='*20}\n")
return HTMLResponse(f"Credentials printed to console. <a href='/admin/login'>Back to Login</a>")
# ------------------------------
# ADMIN PANEL
# ------------------------------
@app.get("/admin", response_class=HTMLResponse)
async def admin_panel(session: dict = Depends(get_session)):
if not session or not session["approved"]: return RedirectResponse("/admin/login")
new_secret = pyotp.random_base32()
otp_uri = pyotp.totp.TOTP(new_secret).provisioning_uri(name=f"MVG_{secrets.token_hex(2)}", issuer_name="LocalServer")
qr_url = f"https://api.qrserver.com/v1/create-qr-code/?size=150x150&data={otp_uri}"
device_list_html = "".join([f"<li><b>{d['name']}</b> <a href='/admin/delete-device/{i}' style='color:red; float:right;'>Delete</a></li>" for i, d in enumerate(SETTINGS["totp_devices"])])
return f"""
<html>
<head><title>Admin Settings</title><style>
body{{font-family:sans-serif;max-width:500px;margin:40px auto; background:#f4f4f9;}}
.sect{{border:1px solid #ddd;padding:20px;border-radius:10px; background:white; margin-bottom:20px;}}
input{{width:100%;padding:8px;box-sizing:border-box;margin-top:5px; border:1px solid #ccc; border-radius:4px;}}
.btn{{width:100%;padding:10px;margin-top:15px;background:#0055a4;color:white;border:none;border-radius:5px;cursor:pointer; font-weight:bold;}}
ul{{padding:0; list-style:none;}} li{{background:#eee; padding:10px; margin-bottom:5px; border-radius:5px; overflow:hidden;}}
</style></head>
<body>
<h2>General Settings</h2>
<form action="/admin/save" method="post" class="sect">
<label>Stations (comma separated):</label><input type="text" name="stations" value="{", ".join(SETTINGS['stations'])}">
<br><br><label>Refresh Interval (seconds):</label><input type="number" name="interval" value="{SETTINGS['update_interval']}">
<br><br><label>Change Admin Password:</label><input type="password" name="new_password" placeholder="Leave blank to keep current">
<button type="submit" class="btn">Save Settings</button>
</form>
<h2>Authenticator Devices</h2>
<div class="sect">
<ul>{device_list_html or "<li>No devices linked</li>"}</ul>
<hr>
<form action="/admin/add-device" method="post" style="text-align:center;">
<p><b>Link New Device</b></p>
<img src="{qr_url}"><br>
<input type="text" name="device_name" placeholder="Device Name (e.g. My Phone)" required>
<input type="hidden" name="new_secret" value="{new_secret}">
<button type="submit" class="btn" style="background:#28a745;">Add This Device</button>
</form>
</div>
<br><a href="/" style="text-decoration:none; color:#666;">← Back to Board</a>
</body>
</html>
"""
@app.post("/admin/save")
async def save_admin(stations: str = Form(...), interval: int = Form(...), new_password: str = Form(None), session: dict = Depends(get_session)):
if not session or not session["approved"]: return RedirectResponse("/admin/login")
SETTINGS["stations"] = [s.strip() for s in stations.split(",")]
SETTINGS["update_interval"] = max(5, interval)
if new_password: SETTINGS["admin_hash"] = pwd_context.hash(new_password)
save_settings()
return RedirectResponse("/admin", status_code=303)
@app.post("/admin/add-device")
async def add_device(device_name: str = Form(...), new_secret: str = Form(...), session: dict = Depends(get_session)):
if not session or not session["approved"]: return RedirectResponse("/admin/login")
SETTINGS["totp_devices"].append({"name": device_name, "secret": new_secret})
save_settings()
return RedirectResponse("/admin", status_code=303)
@app.get("/admin/delete-device/{index}")
async def delete_device(index: int, session: dict = Depends(get_session)):
if not session or not session["approved"]: return RedirectResponse("/admin/login")
if 0 <= index < len(SETTINGS["totp_devices"]):
SETTINGS["totp_devices"].pop(index)
save_settings()
return RedirectResponse("/admin", status_code=303)
# ------------------------------
# WEBSOCKET MANAGER
# ------------------------------
class ConnectionManager:
def __init__(self):
self.active_connections = []
async def connect(self, ws: WebSocket):
await ws.accept()
self.active_connections.append(ws)
if cached_departures:
await ws.send_json({"deps": cached_departures})
def disconnect(self, ws: WebSocket):
if ws in self.active_connections: self.active_connections.remove(ws)
async def broadcast(self, msg: list):
for conn in self.active_connections:
try: await conn.send_json({"deps": msg})
except: pass
manager = ConnectionManager()
# ------------------------------
# MAIN BOARD WITH SCALING
# ------------------------------
@app.get("/", response_class=HTMLResponse)
async def index():
DEST_PADDING = 40 # distance in pixels from the right column
html_content = f"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
:root {{
--scale: {SCALING};
--dest-padding: {DEST_PADDING}px;
}}
body {{
font-family: Arial, sans-serif;
background: #fff;
margin: 0;
padding: 0;
}}
table {{
border-collapse: collapse;
width: 100%;
table-layout: auto;
font-size: calc(14px * var(--scale));
}}
td {{
padding: calc(1.5vh * var(--scale)) calc(2vw * var(--scale));
border-bottom: 2px solid #eee;
vertical-align: middle;
}}
.line-row {{
display: flex;
align-items: center;
gap: calc(15px * var(--scale));
}}
.icon-u {{
width: calc(60px * var(--scale));
height: calc(60px * var(--scale));
background: #0055a4;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
border-radius: 8px;
font-size: calc(35px * var(--scale));
}}
.icon-bus {{
width: calc(55px * var(--scale));
height: calc(55px * var(--scale));
background: #fff;
color: #00656e;
display: flex;
align-items: center;
justify-content: center;
font-weight: 800;
font-size: calc(16px * var(--scale));
border-radius: 50%;
border: calc(4px * var(--scale)) solid #00656e;
}}
.badge {{
color: #fff;
padding: calc(5px * var(--scale)) calc(15px * var(--scale));
border-radius: 8px;
font-weight: bold;
min-width: calc(70px * var(--scale));
text-align: center;
font-size: calc(28px * var(--scale));
}}
.bg-u {{ background: #0055a4; }}
.bg-bus {{ background: #00656e; }}
.status {{
font-size: calc(1.2em * var(--scale));
font-weight: bold;
margin-top: calc(5px * var(--scale));
display: block;
}}
.ontime {{ color: #008542; }}
.delayed {{ color: #d00; }}
.scheduled {{ color: #666; font-style: italic; }}
/* DESTINATION COLUMN */
.dest {{
font-size: calc(2.5em * var(--scale));
font-weight: 600;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding-right: var(--dest-padding);
}}
/* TIME COLUMN */
td.time-cell {{
width: calc(50px * var(--scale)); /* fixed width for time column */
text-align: right;
padding-left: 2px;
padding-right: 2px;
white-space: nowrap;
}}
.time-box {{
display: block; /* fill td */
padding-right: 10px
}}
.clock {{
font-size: calc(1.5em * var(--scale));
color: #444;
font-weight: bold;
display: block; /* first line */
line-height: 1;
}}
.min {{
font-weight: bold;
font-size: calc(2.2em * var(--scale));
color: #000;
display: block; /* second line */
line-height: 1;
margin-top: 2px;
white-space: nowrap;
}}
.jetzt {{
color: #008542;
animation: blink 2s infinite;
}}
@keyframes blink {{
0% {{ opacity: 1; }}
50% {{ opacity: 0.5; }}
100% {{ opacity: 1; }}
}}
</style>
</head>
<body>
<table><tbody id="board"></tbody></table>
<script>
let lastData = [];
function updateUI() {{
if (!lastData.length) return;
const now = Date.now() / 1000;
let html = "";
lastData.filter(dep => (dep.time - now) > -60).forEach(dep => {{
const diff = dep.time - now;
const minutes = Math.max(0, Math.round(diff / 60));
const timeObj = new Date(dep.time * 1000);
const clockStr = timeObj.getHours().toString().padStart(2,'0') + ":" + timeObj.getMinutes().toString().padStart(2,'0');
let bCls = dep.type.toLowerCase().includes("u-bahn") ? "bg-u" : "bg-bus";
let icon = dep.type.toLowerCase().includes("u-bahn") ? '<div class="icon-u">U</div>' : '<div class="icon-bus">BUS</div>';
let status = dep.realtime ? (dep.delay > 0 ? `<span class="status delayed">+${{dep.delay}} min</span>` : (dep.delay < 0 ? `<span class="status ontime">${{dep.delay}} min</span>` : '<span class="status ontime">pünktlich</span>')) : '<span class="status scheduled">Planmäßig*</span>';
html += `<tr>
<td style="width:28%"><div class="line-row">${{icon}}<div class="badge ${{bCls}}">${{dep.line}}</div></div>${{status}}</td>
<td class="dest">${{dep.destination}}</td>
<td class="time-cell">
<div class="time-box">
<span class="clock">${{clockStr}}</span>
<span class="min">${{minutes === 0 ? '<span class="jetzt">Jetzt</span>' : minutes + ' min'}}</span>
</div>
</td>
</tr>`;
}});
document.getElementById('board').innerHTML = html;
}}
function connect() {{
const ws = new WebSocket((location.protocol==='https:'?'wss:':'ws:')+'//'+location.host+'/ws');
ws.onmessage = (e) => {{
const data = JSON.parse(e.data);
lastData = data.deps;
updateUI();
}};
ws.onclose = () => setTimeout(connect, 2000);
}}
setInterval(updateUI, 1000);
connect();
</script>
</body>
</html>
"""
return HTMLResponse(content=html_content)
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await manager.connect(websocket)
try:
while True: await websocket.receive_text()
except WebSocketDisconnect:
manager.disconnect(websocket)