Skip to main content

rpfm_server/
main.rs

1//---------------------------------------------------------------------------//
2// Copyright (c) 2017-2026 Ismael Gutiérrez González. All rights reserved.
3//
4// This file is part of the Rusted PackFile Manager (RPFM) project,
5// which can be found here: https://github.com/Frodo45127/rpfm.
6//
7// This file is licensed under the MIT license, which can be found here:
8// https://github.com/Frodo45127/rpfm/blob/master/LICENSE.
9//---------------------------------------------------------------------------//
10
11//! # `rpfm_server`
12//!
13//! Backend process for [Rusted PackFile Manager][rpfm]. Hosts the heavy work
14//! that the Qt6 UI ([`rpfm_ui`][ui]) and AI / MCP clients drive remotely:
15//! Pack I/O, schema decoding, diagnostics, search, dependencies, optimisation
16//! and so on.
17//!
18//! [rpfm]: https://github.com/Frodo45127/rpfm
19//! [ui]: https://crates.io/crates/rpfm_ui
20//!
21//! ## Architecture
22//!
23//! The server is built on [`axum`] (HTTP + WebSocket) and [`tokio`]. It binds
24//! to `127.0.0.1:45127` by default and exposes three endpoints:
25//!
26//! | Endpoint    | Method | Purpose                                                                          |
27//! |-------------|--------|----------------------------------------------------------------------------------|
28//! | `/ws`       | GET    | WebSocket upgrade. Carries the [`rpfm_ipc`] command/response protocol.           |
29//! | `/version`  | GET    | REST: report the server build version + pid.    |
30//! | `/sessions` | GET    | REST: list every active session (used by the UI session picker).                 |
31//! | `/mcp`      | *      | MCP `StreamableHttpService` exposing the same surface to AI / MCP clients.       |
32//!
33//! Every client connection is wrapped in a [`session::Session`] managed by a
34//! [`session::SessionManager`]. Each session owns a dedicated background
35//! thread (see [`background_thread`]) that processes commands serially against
36//! its own in-memory state (open packs, dependency cache, settings cache),
37//! so multiple concurrent clients can't step on each other.
38//!
39//! ## Modules
40//!
41//! - [`background_thread`] — central command dispatcher; one async loop per session.
42//! - [`comms`] — generic mpsc-based request/response abstraction used to talk
43//!   to the background thread.
44//! - [`server_websocket`] — `/ws` upgrade handler and message multiplexer.
45//! - [`server_mcp`] — `/mcp` endpoint: tools, prompts, resources for MCP clients.
46//! - [`session`] — `SessionManager`, `Session`, lifecycle and timeout handling.
47//! - [`settings`] — JSON-backed settings store with batch-write optimisation.
48//! - [`updater`] — self-update checks against GitHub releases.
49//!
50//! ## Telemetry
51//!
52//! Logging, panic capture and action telemetry are wired through
53//! [`rpfm_telemetry`]. The Sentry guard returned by [`Logger::init`] is held
54//! for the process lifetime in [`main`].
55
56// Under windows, hide the server window by default.
57#![windows_subsystem = "windows"]
58
59use axum::{extract::State, routing::get, Json, Router};
60use rmcp::transport::streamable_http_server::{session::local::LocalSessionManager, StreamableHttpService};
61use tokio::net::TcpListener;
62use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Layer};
63
64use std::net::SocketAddr;
65use std::path::PathBuf;
66use std::sync::Arc;
67
68use rpfm_ipc::helpers::SessionInfo;
69use rpfm_ipc::messages::{Command, Response};
70use rpfm_ipc::settings_keys::{ANONYMOUS_TELEMETRY_ID, ENABLE_CRASH_REPORTS, ENABLE_USAGE_TELEMETRY};
71
72use rpfm_telemetry::{Logger, SentryLayer, SENTRY_DSN, info, release_name, warn};
73
74use crate::server_mcp::McpServer;
75use crate::session::SessionManager;
76use crate::settings::{error_path, init_config_path, Settings};
77use crate::server_websocket::ws_handler;
78
79pub mod background_thread;
80pub mod ceo_builder;
81pub mod comms;
82pub mod server_mcp;
83pub mod server_websocket;
84pub mod session;
85pub mod settings;
86pub mod updater;
87#[cfg(test)] mod updater_test;
88
89use mimalloc::MiMalloc;
90
91#[global_allocator]
92static GLOBAL: MiMalloc = MiMalloc;
93
94//-------------------------------------------------------------------------------//
95//                                  Constants
96//-------------------------------------------------------------------------------//
97
98/// Sentry DSN used for crash reports.
99const SENTRY_DSN_KEY: &str = match option_env!("RPFM_SERVER_SENTRY_DSN") {
100    Some(dsn) => dsn,
101    None => "",
102};
103
104/// PostHog project API key used for telemetry and feedback.
105const POSTHOG_API_KEY_VALUE: &str = match option_env!("RPFM_SERVER_POSTHOG_API_KEY") {
106    Some(key) => key,
107    None => "",
108};
109
110/// Default IP address the HTTP server binds to (`127.0.0.1` / loopback).
111const DEFAULT_ADDRESS: [u8; 4] = [127, 0, 0, 1];
112
113/// Default TCP port the HTTP server listens on.
114const DEFAULT_PORT: u16 = 45127;
115
116/// Organisation domain used to derive the OS-specific config directory
117/// (mirrors `QCoreApplication::organizationDomain` on the UI side).
118const ORG_DOMAIN: &str = "com";
119
120/// Organisation name used to derive the OS-specific config directory.
121const ORG_NAME: &str = "FrodoWazEre";
122
123/// Application name used to derive the OS-specific config directory.
124const APP_NAME: &str = "rpfm";
125
126//-------------------------------------------------------------------------------//
127//                                  Functions
128//-------------------------------------------------------------------------------//
129
130/// Process entry point.
131///
132/// Initialises the Sentry/telemetry guard, primes the telemetry toggles from
133/// persisted settings, builds the [`session::SessionManager`], wires the
134/// `axum` router (`/ws`, `/sessions`, `/mcp`) and starts the listener on
135/// [`DEFAULT_ADDRESS`]:[`DEFAULT_PORT`].
136///
137/// Returns when the listener stops accepting (typically after every session
138/// has been cleaned up — the cleanup task in [`session::SessionManager`]
139/// terminates the process when the session set drains).
140#[tokio::main]
141async fn main() {
142
143    // Sentry client guard, so we can reuse it later on and keep it in scope for the entire duration of the program.
144    // Must be initialized before the tracing subscriber so the SentryLayer can capture spans.
145    *SENTRY_DSN.write().unwrap() = SENTRY_DSN_KEY.to_owned();
146    rpfm_telemetry::set_posthog_api_key(POSTHOG_API_KEY_VALUE);
147    let guard = Logger::init(&{
148        init_config_path().expect("Error while trying to initialize config path. We're fucked.");
149        error_path().unwrap_or_else(|_| PathBuf::from("."))
150    }, true, false, release_name!()).expect("Failed to initialize logging system.");
151
152    // Setup tracing subscriber for logging, redirecting to stderr to avoid interfering with MCP.
153    // The SentryLayer captures tracing spans/events as Sentry breadcrumbs and performance spans.
154    tracing_subscriber::registry()
155        .with(tracing_subscriber::fmt::layer()
156            .with_writer(std::io::stderr)
157            .with_filter(tracing_subscriber::filter::LevelFilter::INFO))
158        .with(SentryLayer::default())
159        .init();
160
161    if guard.is_enabled() {
162        info!("Sentry logging support for RPFM SERVER enabled. Starting...");
163    } else {
164        info!("Sentry logging support for RPFM SERVER disabled. Starting...");
165    }
166
167    // Read telemetry settings from disk before any sessions spin up so early commands
168    // are counted and crash reports respect the user's choice. Background threads will
169    // refresh these whenever the settings change.
170    if let Ok(settings) = Settings::init(false) {
171        rpfm_telemetry::set_usage_telemetry_enabled(settings.bool(ENABLE_USAGE_TELEMETRY));
172        rpfm_telemetry::set_crash_reports_enabled(settings.bool(ENABLE_CRASH_REPORTS));
173
174        let id = settings.string(ANONYMOUS_TELEMETRY_ID);
175        if !id.is_empty() {
176            rpfm_telemetry::set_distinct_id(&id);
177        }
178    }
179
180    // Attach breakdown dimensions to every PostHog event in the next flush.
181    rpfm_telemetry::set_event_property("release", serde_json::Value::from(env!("CARGO_PKG_VERSION")));
182    rpfm_telemetry::set_event_property("os", serde_json::Value::from(std::env::consts::OS));
183    rpfm_telemetry::set_event_property("is_beta", serde_json::Value::from(rpfm_telemetry::is_beta()));
184
185    // Create the session manager to handle per-client sessions,
186    // and start the background cleanup task for expired sessions.
187    let session_manager: Arc<SessionManager> = Arc::new(SessionManager::default());
188    SessionManager::start_cleanup_task(session_manager.clone());
189
190    // Create an MCP service with its own session for MCP clients.
191    let sm = session_manager.clone();
192    let http_service = StreamableHttpService::new(
193        move || {
194            let session = sm.create_session();
195            Ok(McpServer::new(session))
196        },
197        LocalSessionManager::default().into(),
198        Default::default(),
199    );
200
201    // Setup the endpoints for the server.
202    let app = Router::new()
203        .route("/ws", get(ws_handler))
204        .route("/version", get(version_handler))
205        .route("/sessions", get(sessions_handler))
206        .nest_service("/mcp", http_service)
207        .with_state(session_manager);
208
209    let addr = SocketAddr::from((DEFAULT_ADDRESS, DEFAULT_PORT));
210    match TcpListener::bind(addr).await {
211        Ok(listener) => {
212            info!("Listening on {}", addr);
213            axum::serve(listener, app).await.unwrap();
214        }
215        Err(err) => {
216            warn!("Failed to bind to address {}: {}\n\nThis usually means you got another copy of the server running. Either use that one, or stop it and try again.", addr, err);
217        }
218    }
219}
220
221/// REST endpoint reporting the server's build version and process id.
222///
223/// Returns a JSON object: `{ "version": "5.0.0", "pid": 1234 }`.
224async fn version_handler() -> Json<serde_json::Value> {
225    Json(serde_json::json!({
226        "version": env!("CARGO_PKG_VERSION"),
227        "pid": std::process::id(),
228    }))
229}
230
231/// REST endpoint to get information about all active sessions.
232///
233/// Returns a JSON array of [`SessionInfo`] objects containing:
234/// - `session_id`: Unique session identifier
235/// - `connection_count`: Number of active WebSocket connections
236/// - `timeout_remaining_secs`: Seconds until session cleanup (if disconnected)
237/// - `is_shutting_down`: Whether session is marked for shutdown
238///
239/// This endpoint is used by the UI's session management dialog to display
240/// available sessions and allow users to connect to specific ones.
241async fn sessions_handler(State(session_manager): State<Arc<SessionManager>>) -> Json<Vec<SessionInfo>> {
242    let sessions = session_manager.get_sessions_info();
243    info!("Sessions endpoint queried: {} active session(s)", sessions.len());
244    Json(sessions)
245}