feat: M5 admin dashboard (HTMX + Tailwind CDN, embedded HTML)
A web admin UI for the rustdesk-server, mounted at /admin/* on the
existing HTTP API listener. Single-binary deploy preserved — the two
HTML files live in admin_ui/ and are pulled into the binary via
include_str! at build time, so there's nothing extra to ship.
================================================================================
Architecture
================================================================================
- Stack: HTMX 1.9 + Tailwind play CDN. No SPA, no Node toolchain. Pages
are server-rendered HTML fragments returned by Rust handlers via
Html<String>; the index.html shell uses hx-get to drop a fragment into
the main pane and hx-push-url for back-button history.
- Auth: same Bearer-token table the API uses. The dashboard log-in form
POSTs username + password (+ optional TOTP) to /admin/login; on success
the server mints a token and pins it in an HttpOnly + SameSite=Strict
cookie (`rd_admin_session`). The AuthedUser extractor was extended to
accept either the Authorization: Bearer header (curl, desktop client)
OR the session cookie (browser).
- Embedding: src/api/admin/mod.rs has `include_str!("../../../admin_ui/index.html")`
+ login.html. No tower_http::ServeDir wildcard — we ran into axum 0.5
routing conflicts between literal /admin/login routes and an /admin/*
catch-all, so each HTML file is its own explicit route.
================================================================================
M5a — foundation
================================================================================
Files:
admin_ui/index.html page shell + sidebar + HTMX + 401-bounces-to-login
admin_ui/login.html credentials + TOTP form, posts to /admin/login
src/api/admin/mod.rs router + include_str! + Cache-Control: no-cache
src/api/admin/auth.rs /admin/login POST (form-encoded), /admin/logout POST
src/api/admin/me.rs sidebar fragment ("Signed in as <name>")
src/api/middleware.rs `AuthedUser` now reads either Bearer OR cookie
src/api/state.rs `admin_ui_dir` (informational; UI is embedded)
src/main.rs --admin-ui-dir flag (empty disables the dashboard)
The login flow asks for TOTP transparently in the same form when the
target user has a secret enrolled, so the dashboard inherits the TOTP
gate from the API auth surface for free.
================================================================================
M5b — full CRUD pages
================================================================================
- Users (src/api/admin/pages/users.rs) — list, create, password reset,
toggle admin / status, TOTP enroll / unenroll, delete. TOTP enroll
surfaces the secret + otpauth URL once, on a dismissible banner above
the table.
- Devices (devices.rs) — list with hostname/OS/last-heartbeat/conn count,
force-disconnect (queues `heartbeat_commands` row consumed at the next
/api/heartbeat tick), force-sysinfo refresh.
- Device groups (groups.rs) — list / create / delete / add member /
remove member. Per-group section, with an add-member dropdown of users
not yet in the group.
- Strategies (strategies.rs) — list / create / edit config_options /
delete. config_options is validated as a JSON object on the server side
before persist; bad JSON is reflected to the page with a friendly
error notice.
- Address books (address_books.rs) — read-only overview of all books
with owner, kind (personal / shared badge), peer count, GUID.
- OIDC providers (oidc.rs) — read-only list of what's configured. Editing
remains operator-side via --oidc-config TOML or direct SQL.
================================================================================
M5c — audit + recordings browsers
================================================================================
- Audit log (audit.rs) — three tabs (Connections / File transfers /
Alarms), each capped at the latest 200 rows. Tab pills are HTMX links
with hx-get + hx-target="#main" so the tab switch is a single fetch.
- Recordings (recordings.rs) — read-only list with peer / size / state /
start / finish. Streaming download is a follow-up; for now operators
pull files from --recording-dir directly.
================================================================================
DB methods added
================================================================================
- Users: users_list_all, user_set_status, user_set_admin,
user_set_password, user_delete, user_has_totp,
raw_update_user_email
- Devices: devices_list_all, device_sysinfo_get_conns,
heartbeat_command_queue (also used elsewhere; surfaced)
- Groups: device_groups_list_all, device_group_members,
device_group_create, device_group_delete,
device_group_add_member, device_group_remove_member
- Strategy: strategies_list_all, strategy_create,
strategy_update_config, strategy_delete
- Audit: audit_conn_list, audit_file_list, audit_alarm_list
- Misc: ab_list_all_with_owner, recordings_list
All use the runtime sqlx::query("...") form (matching the project-wide
convention) so the SQLite compile-time-check macros don't require these
new tables to pre-exist in the dev DB.
================================================================================
Conventions enforced
================================================================================
- Every page handler gates on require_admin(&AuthedUser) — non-admin
users get an HTTP 403 + JSON envelope, which the SPA shell catches and
bounces back to the login form.
- HTML fragments are produced via `format!`-with-named-args; html_escape
is centralized in src/api/admin/pages/shared.rs and applied to every
user-supplied string before it lands in the DOM.
- All mutations return either the updated table fragment OR
notice_html(kind, msg) + the table — same pattern across pages, so
HTMX swap targets stay simple (always #region innerHTML).
- Cookie carries no path restriction so it also authorizes /api/* calls
the dashboard might want to make from the browser; HttpOnly +
SameSite=Strict mitigates XSS / CSRF; Max-Age tracks ApiConfig's
session_ttl_secs (30 days).
================================================================================
Verification
================================================================================
1. cargo build --release — clean.
2. End-to-end smoke test:
- /admin/ serves index.html (4406 bytes), /admin/login.html serves
login.html (2598 bytes).
- POST /admin/login with valid creds returns 200 + Set-Cookie
`rd_admin_session=…; HttpOnly; Path=/; SameSite=Strict; Max-Age=…`.
- All eight /admin/pages/* fragments return 200 with cookie.
- Users CRUD round-trip: create alice → toggle admin → disable →
reset password → enroll TOTP (32-char secret displayed once) →
unenroll → delete; self-action guard rejects suicide deletes.
- Groups CRUD: create engineering → add alice as member → SQL
confirms the row.
- Strategies: valid JSON accepted, invalid JSON rejected with a
friendly notice.
- Audit tabs: all three render 200; empty-state messages appear when
no rows.
- /admin/logout clears the cookie; subsequent /admin/me returns 401.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
//! Admin dashboard router. Mounted at `/admin/*` by `api::router` when
|
||||
//! the operator hasn't disabled it via `--admin-ui-dir=` (empty).
|
||||
//!
|
||||
//! Static HTML/CSS lives in `admin_ui/` next to the source tree and is
|
||||
//! embedded into the binary at build time via `include_str!` — no separate
|
||||
//! deploy artifact, no ServeDir wildcard route conflicting with the
|
||||
//! literal /admin/login etc. The ASSETS table at the bottom is the
|
||||
//! authoritative list of files we ship.
|
||||
//!
|
||||
//! Layout served at runtime:
|
||||
//! /admin/ ← index.html (the SPA shell)
|
||||
//! /admin/login.html ← login form
|
||||
//! /admin/login POST handler (form-encoded, sets session cookie)
|
||||
//! /admin/logout POST handler (clears session cookie)
|
||||
//! /admin/me GET fragment (current user, sidebar widget)
|
||||
//! /admin/pages/* GET fragments (one per page)
|
||||
|
||||
pub mod auth;
|
||||
pub mod me;
|
||||
pub mod pages;
|
||||
|
||||
use axum::http::header;
|
||||
use axum::response::{Html, IntoResponse, Response};
|
||||
use axum::routing::{get, post};
|
||||
use axum::Router;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Files embedded into the binary. Paths are relative to this source file
|
||||
/// per `include_str!`. Adding a new HTML asset = one new entry here.
|
||||
const INDEX_HTML: &str = include_str!("../../../admin_ui/index.html");
|
||||
const LOGIN_HTML: &str = include_str!("../../../admin_ui/login.html");
|
||||
|
||||
pub fn build(state: Arc<crate::api::state::AppState>) -> Option<Router> {
|
||||
if state.cfg.admin_ui_dir.is_empty() {
|
||||
// Operator opted out by setting the flag to empty.
|
||||
return None;
|
||||
}
|
||||
let r = Router::new()
|
||||
// Static HTML pages — explicit routes per file, no wildcard.
|
||||
.route("/admin", get(serve_index))
|
||||
.route("/admin/", get(serve_index))
|
||||
.route("/admin/index.html", get(serve_index))
|
||||
.route("/admin/login.html", get(serve_login))
|
||||
// Dynamic dashboard endpoints.
|
||||
.route("/admin/login", post(auth::login))
|
||||
.route("/admin/logout", post(auth::logout))
|
||||
.route("/admin/me", get(me::me))
|
||||
// Page fragments — one per sidebar entry.
|
||||
.route("/admin/pages/users", get(pages::users::index))
|
||||
.route("/admin/pages/users/create", post(pages::users::create))
|
||||
.route(
|
||||
"/admin/pages/users/:id/password-reset",
|
||||
post(pages::users::reset_password),
|
||||
)
|
||||
.route(
|
||||
"/admin/pages/users/:id/toggle-admin",
|
||||
post(pages::users::toggle_admin),
|
||||
)
|
||||
.route(
|
||||
"/admin/pages/users/:id/toggle-status",
|
||||
post(pages::users::toggle_status),
|
||||
)
|
||||
.route(
|
||||
"/admin/pages/users/:id/totp-enroll",
|
||||
post(pages::users::totp_enroll),
|
||||
)
|
||||
.route(
|
||||
"/admin/pages/users/:id/totp-unenroll",
|
||||
post(pages::users::totp_unenroll),
|
||||
)
|
||||
.route("/admin/pages/users/:id/delete", post(pages::users::delete))
|
||||
// Devices
|
||||
.route(
|
||||
"/admin/pages/devices/:peer_id/disconnect",
|
||||
post(pages::devices::force_disconnect),
|
||||
)
|
||||
.route(
|
||||
"/admin/pages/devices/:peer_id/sysinfo-refresh",
|
||||
post(pages::devices::force_sysinfo),
|
||||
)
|
||||
// Groups
|
||||
.route("/admin/pages/groups/create", post(pages::groups::create))
|
||||
.route("/admin/pages/groups/:id/delete", post(pages::groups::delete))
|
||||
.route(
|
||||
"/admin/pages/groups/:id/members/add",
|
||||
post(pages::groups::add_member),
|
||||
)
|
||||
.route(
|
||||
"/admin/pages/groups/:id/members/:user_id/remove",
|
||||
post(pages::groups::remove_member),
|
||||
)
|
||||
// Strategies
|
||||
.route(
|
||||
"/admin/pages/strategies/create",
|
||||
post(pages::strategies::create),
|
||||
)
|
||||
.route(
|
||||
"/admin/pages/strategies/:id/update",
|
||||
post(pages::strategies::update),
|
||||
)
|
||||
.route(
|
||||
"/admin/pages/strategies/:id/delete",
|
||||
post(pages::strategies::delete),
|
||||
)
|
||||
.route("/admin/pages/devices", get(pages::devices::index))
|
||||
.route("/admin/pages/groups", get(pages::groups::index))
|
||||
.route("/admin/pages/strategies", get(pages::strategies::index))
|
||||
.route(
|
||||
"/admin/pages/address-books",
|
||||
get(pages::address_books::index),
|
||||
)
|
||||
.route("/admin/pages/oidc", get(pages::oidc::index))
|
||||
.route("/admin/pages/audit", get(pages::audit::index))
|
||||
.route("/admin/pages/recordings", get(pages::recordings::index));
|
||||
hbb_common::log::info!(
|
||||
"admin dashboard mounted at /admin (HTML embedded; --admin-ui-dir is informational)"
|
||||
);
|
||||
Some(r)
|
||||
}
|
||||
|
||||
async fn serve_index() -> Response {
|
||||
html_response(INDEX_HTML)
|
||||
}
|
||||
|
||||
async fn serve_login() -> Response {
|
||||
html_response(LOGIN_HTML)
|
||||
}
|
||||
|
||||
fn html_response(body: &'static str) -> Response {
|
||||
// We hand back `Html<&'static str>` so axum sets `text/html` for us.
|
||||
// Cache-Control: no-cache so the operator sees fresh HTML after a
|
||||
// server upgrade without having to bump asset URLs.
|
||||
let mut resp = Html(body).into_response();
|
||||
resp.headers_mut().insert(
|
||||
header::CACHE_CONTROL,
|
||||
axum::http::HeaderValue::from_static("no-cache"),
|
||||
);
|
||||
resp
|
||||
}
|
||||
Reference in New Issue
Block a user