diff --git a/apps/gpservice/src/cli.rs b/apps/gpservice/src/cli.rs index 2256e92..f8595e0 100644 --- a/apps/gpservice/src/cli.rs +++ b/apps/gpservice/src/cli.rs @@ -1,4 +1,7 @@ -use std::sync::Arc; +use std::sync::{ + Arc, + atomic::{AtomicBool, Ordering}, +}; use std::{collections::HashMap, io::Write}; use anyhow::bail; @@ -58,9 +61,17 @@ impl Cli { let (ws_req_tx, ws_req_rx) = mpsc::channel::(32); // Channel for receiving the VPN state from the VPN task let (vpn_state_tx, vpn_state_rx) = watch::channel(VpnState::Disconnected); + let gui_restart_requested = Arc::new(AtomicBool::new(false)); let mut vpn_task = VpnTask::new(ws_req_rx, vpn_state_tx); - let ws_server = WsServer::new(api_key.clone(), ws_req_tx, vpn_state_rx, lock_file.clone(), redaction); + let ws_server = WsServer::new( + api_key.clone(), + ws_req_tx, + Arc::clone(&gui_restart_requested), + vpn_state_rx, + lock_file.clone(), + redaction, + ); let (shutdown_tx, mut shutdown_rx) = mpsc::channel::<()>(4); let shutdown_tx_clone = shutdown_tx.clone(); @@ -92,7 +103,7 @@ impl Cli { let minimized = self.minimized; tokio::spawn(async move { - launch_gui(envs, api_key, minimized).await; + launch_gui(envs, api_key, minimized, gui_restart_requested).await; let _ = shutdown_tx.send(()).await; }); } @@ -201,7 +212,12 @@ mod signals { } } -async fn launch_gui(envs: Option>, api_key: Vec, mut minimized: bool) { +async fn launch_gui( + envs: Option>, + api_key: Vec, + mut minimized: bool, + restart_requested: Arc, +) { loop { let gui_launcher = GuiLauncher::new(env!("CARGO_PKG_VERSION"), &api_key) .envs(envs.clone()) @@ -209,13 +225,13 @@ async fn launch_gui(envs: Option>, api_key: Vec, mut match gui_launcher.launch().await { Ok(exit_status) => { - // Exit code 99 means that the GUI needs to be restarted - if exit_status.code() != Some(99) { + let should_restart = restart_requested.swap(false, Ordering::SeqCst); + if !should_restart { info!("GUI exited with code {:?}", exit_status.code()); break; } - info!("GUI exited with code 99, restarting"); + info!("GUI restart requested, restarting"); minimized = false; } Err(err) => { diff --git a/apps/gpservice/src/handlers.rs b/apps/gpservice/src/handlers.rs index f2df569..9830bbb 100644 --- a/apps/gpservice/src/handlers.rs +++ b/apps/gpservice/src/handlers.rs @@ -19,7 +19,10 @@ use axum::{ use common::binary_paths; use futures::{SinkExt, StreamExt}; use gpapi::{ - service::{event::WsEvent, request::UpdateGuiRequest}, + service::{ + event::WsEvent, + request::{RestartGuiRequest, UpdateGuiRequest}, + }, utils::checksum::verify_checksum, }; use log::{info, warn}; @@ -37,6 +40,17 @@ pub(crate) async fn active_gui(State(ctx): State>) -> impl ctx.send_event(WsEvent::ActiveGui).await; } +pub(crate) async fn restart_gui(State(ctx): State>, body: Bytes) -> Result<(), StatusCode> { + if let Err(err) = ctx.decrypt::(body.to_vec()) { + warn!("Failed to decrypt restart GUI payload: {}", err); + return Err(StatusCode::BAD_REQUEST); + } + + ctx.request_gui_restart(); + + Ok(()) +} + pub async fn update_gui(State(ctx): State>, body: Bytes) -> Result<(), StatusCode> { let payload = match ctx.decrypt::(body.to_vec()) { Ok(payload) => payload, diff --git a/apps/gpservice/src/routes.rs b/apps/gpservice/src/routes.rs index 5c71053..cd01833 100644 --- a/apps/gpservice/src/routes.rs +++ b/apps/gpservice/src/routes.rs @@ -11,6 +11,7 @@ pub(crate) fn routes(ctx: Arc) -> Router { Router::new() .route("/health", get(handlers::health)) .route("/active-gui", post(handlers::active_gui)) + .route("/restart-gui", post(handlers::restart_gui)) .route("/update-gui", post(handlers::update_gui)) .route("/ws", get(handlers::ws_handler)) .with_state(ctx) diff --git a/apps/gpservice/src/ws_server.rs b/apps/gpservice/src/ws_server.rs index 3c3e1ce..6826166 100644 --- a/apps/gpservice/src/ws_server.rs +++ b/apps/gpservice/src/ws_server.rs @@ -1,4 +1,7 @@ -use std::sync::Arc; +use std::sync::{ + Arc, + atomic::{AtomicBool, Ordering}, +}; use axum::extract::ws::Message; use common::binary_paths; @@ -26,6 +29,7 @@ use crate::{routes, ws_connection::WsConnection}; pub(crate) struct WsServerContext { crypto: Arc, ws_req_tx: mpsc::Sender, + gui_restart_requested: Arc, vpn_state_rx: watch::Receiver, redaction: Arc, connections: RwLock>>, @@ -35,12 +39,14 @@ impl WsServerContext { pub fn new( api_key: Vec, ws_req_tx: mpsc::Sender, + gui_restart_requested: Arc, vpn_state_rx: watch::Receiver, redaction: Arc, ) -> Self { Self { crypto: Arc::new(Crypto::new(api_key)), ws_req_tx, + gui_restart_requested, vpn_state_rx, redaction, connections: Default::default(), @@ -106,6 +112,11 @@ impl WsServerContext { Ok(()) } + + pub fn request_gui_restart(&self) { + info!("GUI restart requested"); + self.gui_restart_requested.store(true, Ordering::SeqCst); + } } pub(crate) struct WsServer { @@ -118,11 +129,18 @@ impl WsServer { pub fn new( api_key: Vec, ws_req_tx: mpsc::Sender, + gui_restart_requested: Arc, vpn_state_rx: watch::Receiver, lock_file: Arc, redaction: Arc, ) -> Self { - let ctx = Arc::new(WsServerContext::new(api_key, ws_req_tx, vpn_state_rx, redaction)); + let ctx = Arc::new(WsServerContext::new( + api_key, + ws_req_tx, + gui_restart_requested, + vpn_state_rx, + redaction, + )); let cancel_token = CancellationToken::new(); Self { diff --git a/crates/gpapi/src/service/request.rs b/crates/gpapi/src/service/request.rs index ecdf3f7..c3f52ee 100644 --- a/crates/gpapi/src/service/request.rs +++ b/crates/gpapi/src/service/request.rs @@ -307,6 +307,9 @@ pub struct UpdateGuiRequest { pub checksum: String, } +#[derive(Debug, Deserialize, Serialize)] +pub struct RestartGuiRequest; + #[cfg(test)] mod tests { use serde_json::json;