diff --git a/ai-station-portal-admin b/ai-station-portal-admin index 8fdf4c4..c52daab 100755 --- a/ai-station-portal-admin +++ b/ai-station-portal-admin @@ -321,6 +321,24 @@ def password_change(username): ) return 0 + +def system_power(action): + commands = { + 'reboot': '/usr/bin/systemctl', + 'poweroff': '/usr/bin/systemctl', + } + if action not in commands: + return 2 + + subprocess.run( + [commands[action], '--no-block', action], + check=True, + capture_output=True, + text=True, + timeout=10, + ) + return 0 + def main(): if len(sys.argv) != 3: return 2 action, value = sys.argv[1:] @@ -334,6 +352,7 @@ def main(): if value not in {'yes', 'no'}: return 2 run('/usr/bin/timedatectl', 'set-ntp', 'true' if value == 'yes' else 'false'); return 0 if action == 'password-change': return password_change(value) + if action == 'system-power': return system_power(value) if action == 'network-read': return network_read(value) if action == 'network-status': return network_status() if action == 'network-apply': return network_apply(value) diff --git a/app.py b/app.py index e0e21f3..4b7b409 100644 --- a/app.py +++ b/app.py @@ -373,6 +373,39 @@ async def update_admin_password( return RedirectResponse("/admin/login?result=password-changed", status_code=303) + +@app.post("/admin/system/reboot") +async def admin_reboot(request: Request, csrf_token: str = Form(...)): + if not admin_session_valid(request): + return RedirectResponse("/admin/login", status_code=303) + + session_token = request.session.get("password_csrf", "") + if not session_token or not hmac.compare_digest(csrf_token, session_token): + return RedirectResponse("/admin?error=system-csrf", status_code=303) + + ok, _ = run_helper("system-power", "reboot") + if not ok: + return RedirectResponse("/admin?error=system-power", status_code=303) + + return RedirectResponse("/admin", status_code=303) + + +@app.post("/admin/system/poweroff") +async def admin_poweroff(request: Request, csrf_token: str = Form(...)): + if not admin_session_valid(request): + return RedirectResponse("/admin/login", status_code=303) + + session_token = request.session.get("password_csrf", "") + if not session_token or not hmac.compare_digest(csrf_token, session_token): + return RedirectResponse("/admin?error=system-csrf", status_code=303) + + ok, _ = run_helper("system-power", "poweroff") + if not ok: + return RedirectResponse("/admin?error=system-power", status_code=303) + + return RedirectResponse("/admin", status_code=303) + + @app.post("/admin/settings/network") async def update_network( request: Request, diff --git a/templates/admin.html b/templates/admin.html index c51d637..b372624 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -275,6 +275,30 @@ + + + Управление станцией + + Перезагрузка или полное выключение сервера. + + + + + Перезагрузить + + + + + + Выключить + + + + + + Сеть
Перезагрузка или полное выключение сервера.