From 49fe7447ff114ed754f96f5ac57e4716cfe2c8de Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Tue, 4 Nov 2025 23:09:15 +0800 Subject: [PATCH] feat: add VpnEnv struct and update WsEvent to include VPN environment details --- apps/gpservice/src/ws_server.rs | 16 ++++++++++++---- crates/gpapi/src/service/event.rs | 3 ++- crates/gpapi/src/service/mod.rs | 1 + crates/gpapi/src/service/vpn_env.rs | 22 ++++++++++++++++++++++ 4 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 crates/gpapi/src/service/vpn_env.rs diff --git a/apps/gpservice/src/ws_server.rs b/apps/gpservice/src/ws_server.rs index a2fe965..bcd7d97 100644 --- a/apps/gpservice/src/ws_server.rs +++ b/apps/gpservice/src/ws_server.rs @@ -1,11 +1,13 @@ use std::sync::Arc; use axum::extract::ws::Message; +use common::constants::GP_AUTH_BINARY; use gpapi::{ - service::{event::WsEvent, request::WsRequest, vpn_state::VpnState}, + service::{event::WsEvent, request::WsRequest, vpn_env::VpnEnv, vpn_state::VpnState}, utils::{crypto::Crypto, lock_file::LockFile, redact::Redaction}, }; use log::{info, warn}; +use openconnect::{find_csd_wrapper, find_vpnc_script}; use serde::de::DeserializeOwned; use tokio::{ net::TcpListener, @@ -56,9 +58,15 @@ impl WsServerContext { let conn = Arc::new(WsConnection::new(Arc::clone(&self.crypto), tx)); // Send current VPN state to new client - info!("Sending current VPN state to new client"); - let vpn_state = self.vpn_state_rx.borrow().clone(); - if let Err(err) = conn.send_event(&WsEvent::VpnState(vpn_state)).await { + info!("Sending current environment to new client"); + let vpn_env = VpnEnv { + vpn_state: self.vpn_state_rx.borrow().clone(), + vpnc_script: find_vpnc_script(), + csd_wrapper: find_csd_wrapper(), + auth_executable: GP_AUTH_BINARY.to_owned(), + }; + + if let Err(err) = conn.send_event(&WsEvent::VpnEnv(vpn_env)).await { warn!("Failed to send VPN state to new client: {}", err); } diff --git a/crates/gpapi/src/service/event.rs b/crates/gpapi/src/service/event.rs index 9f3339d..de7095c 100644 --- a/crates/gpapi/src/service/event.rs +++ b/crates/gpapi/src/service/event.rs @@ -1,10 +1,11 @@ use serde::{Deserialize, Serialize}; -use super::vpn_state::VpnState; +use crate::service::{vpn_env::VpnEnv, vpn_state::VpnState}; /// Events that can be emitted by the service #[derive(Debug, Serialize, Deserialize, Clone)] pub enum WsEvent { + VpnEnv(VpnEnv), VpnState(VpnState), ActiveGui, ResumeConnection, diff --git a/crates/gpapi/src/service/mod.rs b/crates/gpapi/src/service/mod.rs index 7fd4b55..aa22051 100644 --- a/crates/gpapi/src/service/mod.rs +++ b/crates/gpapi/src/service/mod.rs @@ -1,3 +1,4 @@ pub mod event; pub mod request; +pub mod vpn_env; pub mod vpn_state; diff --git a/crates/gpapi/src/service/vpn_env.rs b/crates/gpapi/src/service/vpn_env.rs new file mode 100644 index 0000000..2cc7121 --- /dev/null +++ b/crates/gpapi/src/service/vpn_env.rs @@ -0,0 +1,22 @@ +use serde::{Deserialize, Serialize}; + +use crate::service::vpn_state::VpnState; + +/// Represents the VPN environment configuration. +/// When a client connects, the gpservice sends the current VPN environment +/// to the client so that it can configure itself accordingly. +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct VpnEnv { + /// The VPN connection state + pub vpn_state: VpnState, + + /// The default VPN script path + pub vpnc_script: Option, + + /// The default CSD wrapper script path + pub csd_wrapper: Option, + + /// The gpauth executable path + /// Used by the client to launch gpauth for authentication + pub auth_executable: String, +}