973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 1 | use std::{future::Future, net::SocketAddr, path::PathBuf, time::Duration}; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 2 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 3 | use axum::{Router, middleware}; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 4 | use listenfd::ListenFd; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 5 | use malice::{ |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 6 | idle::IdleTracker, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 7 | routes::{router, static_router}, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 8 | state::{AppState, ensure_dirs, load_or_create_master_key}, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 9 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 10 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 11 | fn env_flag(name: &str, default: bool) -> bool { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 12 | match std::env::var(name) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 13 | Ok(value) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 14 | let value = value.trim().to_ascii_lowercase(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 15 | match value.as_str() { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 16 | "1" | "true" | "yes" | "on" => true, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 17 | "0" | "false" | "no" | "off" => false, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 18 | _ => default, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 19 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 20 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 21 | Err(_) => default, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 22 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 23 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 24 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 25 | fn data_dir() -> PathBuf { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 26 | std::env::var("MALICE_DATA_DIR") |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 27 | .map(PathBuf::from) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 28 | .unwrap_or_else(|_| PathBuf::from("local-data")) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 29 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 30 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 31 | fn bind_addr() -> SocketAddr { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 32 | std::env::var("MALICE_BIND") |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 33 | .unwrap_or_else(|_| "127.0.0.1:3000".to_owned()) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 34 | .parse() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 35 | .expect("MALICE_BIND must be host:port") |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 36 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 37 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 38 | fn idle_exit_secs() -> Option<u64> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 39 | std::env::var("MALICE_IDLE_EXIT_SECS") |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 40 | .ok() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 41 | .and_then(|value| value.trim().parse().ok()) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 42 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 43 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 44 | fn web_dir() -> Option<String> { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 45 | if let Ok(dir) = std::env::var("MALICE_WEB_DIR") { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 46 | assert!( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 47 | std::path::Path::new(&dir).join("index.html").is_file(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 48 | "MALICE_WEB_DIR must point to built frontend assets containing index.html" |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 49 | ); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 50 | return Some(dir); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 51 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 52 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 53 | let local = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("web/dist"); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 54 | local |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 55 | .join("index.html") |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 56 | .is_file() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 57 | .then(|| local.to_string_lossy().into_owned()) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 58 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 59 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 60 | /// Takes over a systemd-activated listener from fd 3 when present, so the |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 61 | /// socket unit can hold the port before the process ever starts. Falls back |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 62 | /// to a plain bind otherwise. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 63 | async fn bind_listener() -> tokio::net::TcpListener { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 64 | let mut listenfd = ListenFd::from_env(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 65 | if let Some(listener) = listenfd |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 66 | .take_tcp_listener(0) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 67 | .expect("inspecting inherited sockets") |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 68 | { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 69 | listener |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 70 | .set_nonblocking(true) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 71 | .expect("setting inherited socket nonblocking"); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 72 | return tokio::net::TcpListener::from_std(listener) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 73 | .expect("adopting inherited socket into tokio"); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 74 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 75 | let addr = bind_addr(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 76 | tracing::info!("listening on {addr}"); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 77 | tokio::net::TcpListener::bind(addr) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 78 | .await |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 79 | .expect("binding the listener must succeed") |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 80 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 81 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 82 | /// Serves `app` on `listener`, exiting on ctrl-c or, when |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 83 | /// `MALICE_IDLE_EXIT_SECS` is set, after that long with no request in flight |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 84 | /// or started. `on_shutdown` runs after the exit signal either way. |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 85 | async fn serve( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 86 | listener: tokio::net::TcpListener, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 87 | app: Router, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 88 | on_shutdown: impl Future<Output = ()> + Send + 'static, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 89 | ) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 90 | let idle = idle_exit_secs().map(|secs| (IdleTracker::new(), secs)); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 91 | let app = match &idle { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 92 | Some((tracker, _)) => app.layer(middleware::from_fn_with_state( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 93 | tracker.clone(), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 94 | malice::idle::track, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 95 | )), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 96 | None => app, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 97 | }; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 98 | axum::serve(listener, app) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 99 | .with_graceful_shutdown(async move { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 100 | match idle { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 101 | Some((tracker, secs)) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 102 | tokio::select! { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 103 | _ = tokio::signal::ctrl_c() => {} |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 104 | _ = tracker.wait_idle(Duration::from_secs(secs)) => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 105 | tracing::info!("idle for {secs}s with no requests, exiting"); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 106 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 107 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 108 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 109 | None => { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 110 | let _ = tokio::signal::ctrl_c().await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 111 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 112 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 113 | on_shutdown.await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 114 | }) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 115 | .await |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 116 | .expect("serving must succeed"); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 117 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 118 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 119 | async fn run_hosted() { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 120 | let data_dir = data_dir(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 121 | ensure_dirs(&data_dir); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 122 | let master_key = |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 123 | load_or_create_master_key(&data_dir).expect("loading the master key must succeed"); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 124 | let cookie_secure = env_flag("MALICE_SESSION_COOKIE_SECURE", true); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 125 | let allow_http_homeserver = env_flag("MALICE_ALLOW_HTTP_HOMESERVER", false); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 126 | let state = AppState::new(data_dir, master_key, cookie_secure, allow_http_homeserver); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 127 | let app = router(state.clone(), web_dir()); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 128 | let listener = bind_listener().await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 129 | serve( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 130 | listener, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 131 | app, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 132 | async move { state.shutdown_gracefully().await }, |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 133 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 134 | .await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 135 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 136 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 137 | async fn run_static_only() { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 138 | let app = static_router(web_dir()); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 139 | let listener = bind_listener().await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 140 | serve(listener, app, async {}).await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 141 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 142 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 143 | #[tokio::main] |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 144 | async fn main() { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 145 | tracing_subscriber::fmt() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 146 | .with_env_filter( |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 147 | tracing_subscriber::EnvFilter::try_from_default_env() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 148 | .unwrap_or_else(|_| "malice=info,tower_http=info".into()), |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 149 | ) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 150 | .with_target(false) |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 151 | .compact() |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 152 | .init(); |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 153 | |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 154 | if env_flag("MALICE_STATIC_ONLY", false) { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 155 | run_static_only().await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 156 | } else { |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 157 | run_hosted().await; |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 158 | } |
973f8fbdaf80 | atagen | 2026-09-28 09:10 UTC | 159 | } |