feat: implement GUI restart functionality and update related routes

This commit is contained in:
Kevin Yue committed 2026-07-04 18:37:16 +08:00
1 parent 6e01164a56
commit ca221e3488
5 files changed
+62 -10

No files matched your search

+23 -7
View File
@@ -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::<WsRequest>(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<HashMap<String, String>>, api_key: Vec<u8>, mut minimized: bool) {
async fn launch_gui(
envs: Option<HashMap<String, String>>,
api_key: Vec<u8>,
mut minimized: bool,
restart_requested: Arc<AtomicBool>,
) {
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<HashMap<String, String>>, api_key: Vec<u8>, 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) => {
+15 -1
View File
@@ -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<Arc<WsServerContext>>) -> impl
ctx.send_event(WsEvent::ActiveGui).await;
}
pub(crate) async fn restart_gui(State(ctx): State<Arc<WsServerContext>>, body: Bytes) -> Result<(), StatusCode> {
if let Err(err) = ctx.decrypt::<RestartGuiRequest>(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<Arc<WsServerContext>>, body: Bytes) -> Result<(), StatusCode> {
let payload = match ctx.decrypt::<UpdateGuiRequest>(body.to_vec()) {
Ok(payload) => payload,
+1
View File
@@ -11,6 +11,7 @@ pub(crate) fn routes(ctx: Arc<WsServerContext>) -> 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)
+20 -2
View File
@@ -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<Crypto>,
ws_req_tx: mpsc::Sender<WsRequest>,
gui_restart_requested: Arc<AtomicBool>,
vpn_state_rx: watch::Receiver<VpnState>,
redaction: Arc<Redaction>,
connections: RwLock<Vec<Arc<WsConnection>>>,
@@ -35,12 +39,14 @@ impl WsServerContext {
pub fn new(
api_key: Vec<u8>,
ws_req_tx: mpsc::Sender<WsRequest>,
gui_restart_requested: Arc<AtomicBool>,
vpn_state_rx: watch::Receiver<VpnState>,
redaction: Arc<Redaction>,
) -> 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<u8>,
ws_req_tx: mpsc::Sender<WsRequest>,
gui_restart_requested: Arc<AtomicBool>,
vpn_state_rx: watch::Receiver<VpnState>,
lock_file: Arc<LockFile>,
redaction: Arc<Redaction>,
) -> 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 {
+3
View File
@@ -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;