mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2026-11-18 22:16:18 -05:00
feat: prepare 3.1.0 with connection details and improved authentication
Add tunnel statistics and an optional CLI dashboard, native sign-in controls and recovery, and broader client certificate support. Include platform integration fixes and the approved release notes.
This commit is contained in:
158 files changed
+13752
-1038
No files matched your search
+371
-18
@@ -9,6 +9,7 @@ use gpapi::{
|
||||
vpn_env::{HostInfo, VpnEnv},
|
||||
vpn_state::VpnState,
|
||||
},
|
||||
session::tunnel::{TunnelStatisticsSnapshot, TunnelUnavailableReason},
|
||||
utils::lock_file::LockFile,
|
||||
};
|
||||
use log::{info, warn};
|
||||
@@ -31,6 +32,28 @@ struct ServiceState {
|
||||
revision: u64,
|
||||
snapshot: VpnEnv,
|
||||
connections: HashMap<Uuid, ConnectionControl>,
|
||||
tunnel: crate::vpn_task::TunnelBinding,
|
||||
}
|
||||
|
||||
impl ServiceState {
|
||||
fn refresh_demand(&self) {
|
||||
if let Some(monitor) = &self.tunnel.monitor {
|
||||
monitor.set_statistics_enabled(
|
||||
self
|
||||
.connections
|
||||
.values()
|
||||
.any(|connection| connection.statistics_enabled() && !connection.is_closed()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn snapshot(&self) -> VpnEnv {
|
||||
let mut snapshot = self.snapshot.clone();
|
||||
if let Some(monitor) = &self.tunnel.monitor {
|
||||
snapshot.tunnel_state = Some(monitor.state());
|
||||
}
|
||||
snapshot
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct WsServerContext {
|
||||
@@ -57,6 +80,7 @@ impl WsServerContext {
|
||||
revision: 0,
|
||||
snapshot: build_snapshot(vpn_state_rx.borrow().clone()),
|
||||
connections: HashMap::new(),
|
||||
tunnel: Default::default(),
|
||||
}),
|
||||
handshake_slots: Arc::new(Semaphore::new(16)),
|
||||
hip_previews: crate::hip_preview::HipPreviews::new(),
|
||||
@@ -91,12 +115,14 @@ impl WsServerContext {
|
||||
let mut state = self.state.lock().await;
|
||||
let prepared = self.registry.prepare_attach(permit)?;
|
||||
|
||||
let mut attached = ServerMessage::Attached {
|
||||
connection_id: control.connection_id(),
|
||||
current_revision: state.revision,
|
||||
snapshot: state.snapshot(),
|
||||
};
|
||||
fit_snapshot_message(&mut attached).map_err(|_| SessionError::Unavailable)?;
|
||||
control
|
||||
.send(ConnectionCommand::Send(ServerMessage::Attached {
|
||||
connection_id: control.connection_id(),
|
||||
current_revision: state.revision,
|
||||
snapshot: state.snapshot.clone(),
|
||||
}))
|
||||
.send(ConnectionCommand::Send(attached))
|
||||
.map_err(|_| SessionError::Unavailable)?;
|
||||
state.connections.insert(control.connection_id(), control);
|
||||
Ok(prepared)
|
||||
@@ -117,42 +143,49 @@ impl WsServerContext {
|
||||
}
|
||||
match self.registry.commit_attach(permit, connection.clone()) {
|
||||
Ok(replaced) => {
|
||||
connection.mark_committed();
|
||||
if let Some(replaced) = &replaced {
|
||||
replaced.set_statistics_enabled(false);
|
||||
state.connections.remove(&replaced.connection_id());
|
||||
}
|
||||
state.refresh_demand();
|
||||
Ok(replaced)
|
||||
}
|
||||
Err(err) => {
|
||||
state.connections.remove(&connection.connection_id());
|
||||
state.refresh_demand();
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn abandon_connection(&self, connection_id: Uuid) {
|
||||
self.state.lock().await.connections.remove(&connection_id);
|
||||
let mut state = self.state.lock().await;
|
||||
state.connections.remove(&connection_id);
|
||||
state.refresh_demand();
|
||||
}
|
||||
|
||||
pub async fn remove_closed_connections(&self) {
|
||||
self
|
||||
.state
|
||||
.lock()
|
||||
.await
|
||||
.connections
|
||||
.retain(|_, connection| !connection.is_closed());
|
||||
let mut state = self.state.lock().await;
|
||||
state.connections.retain(|_, connection| !connection.is_closed());
|
||||
state.refresh_demand();
|
||||
}
|
||||
|
||||
pub async fn send_snapshot(&self, control: &ConnectionControl) -> Result<(), ()> {
|
||||
let state = self.state.lock().await;
|
||||
control.send(ConnectionCommand::Send(ServerMessage::Snapshot {
|
||||
let mut message = ServerMessage::Snapshot {
|
||||
current_revision: state.revision,
|
||||
snapshot: state.snapshot.clone(),
|
||||
}))
|
||||
snapshot: state.snapshot(),
|
||||
};
|
||||
fit_snapshot_message(&mut message)?;
|
||||
control.send(ConnectionCommand::Send(message))
|
||||
}
|
||||
|
||||
pub async fn remove_connection(&self, connection_id: Uuid, session_id: Uuid, generation: u64) {
|
||||
self.registry.disconnect_if_current(session_id, generation);
|
||||
self.state.lock().await.connections.remove(&connection_id);
|
||||
let mut state = self.state.lock().await;
|
||||
state.connections.remove(&connection_id);
|
||||
state.refresh_demand();
|
||||
}
|
||||
|
||||
pub async fn send_event(&self, event: WsEvent) {
|
||||
@@ -161,6 +194,8 @@ impl WsServerContext {
|
||||
state.snapshot = env.clone();
|
||||
} else if let WsEvent::VpnState(vpn_state) = &event {
|
||||
state.snapshot.vpn_state = vpn_state.clone();
|
||||
} else if let WsEvent::TunnelState(tunnel) = &event {
|
||||
state.snapshot.tunnel_state = tunnel.clone();
|
||||
}
|
||||
|
||||
let Some(revision) = state.revision.checked_add(1) else {
|
||||
@@ -168,6 +203,7 @@ impl WsServerContext {
|
||||
connection.close(CloseReason::InternalError);
|
||||
}
|
||||
state.connections.clear();
|
||||
state.refresh_demand();
|
||||
return;
|
||||
};
|
||||
state.revision = revision;
|
||||
@@ -186,6 +222,97 @@ impl WsServerContext {
|
||||
false
|
||||
}
|
||||
});
|
||||
state.refresh_demand();
|
||||
}
|
||||
|
||||
pub async fn set_statistics_enabled(
|
||||
&self,
|
||||
control: &ConnectionControl,
|
||||
session_id: Uuid,
|
||||
generation: u64,
|
||||
enabled: bool,
|
||||
) -> gpapi::service::transport::ServiceResult {
|
||||
use gpapi::service::transport::{ServiceErrorCode, ServiceResult};
|
||||
let state = self.state.lock().await;
|
||||
if !control.is_committed()
|
||||
|| control.is_closed()
|
||||
|| !state.connections.contains_key(&control.connection_id())
|
||||
|| !self.registry.is_current(session_id, generation)
|
||||
{
|
||||
return ServiceResult::rejected(
|
||||
ServiceErrorCode::InvalidRequest,
|
||||
"Service attachment is no longer active",
|
||||
);
|
||||
}
|
||||
control.set_statistics_enabled(enabled);
|
||||
state.refresh_demand();
|
||||
let snapshot = match &state.tunnel.monitor {
|
||||
Some(monitor) => monitor.statistics(),
|
||||
None => TunnelStatisticsSnapshot::Unavailable {
|
||||
reason: if state.tunnel.non_tunnel {
|
||||
TunnelUnavailableReason::NonTunnel
|
||||
} else {
|
||||
TunnelUnavailableReason::NoTunnel
|
||||
},
|
||||
},
|
||||
};
|
||||
ServiceResult::TunnelStatisticsSnapshot(if enabled { snapshot } else { snapshot.disabled() })
|
||||
}
|
||||
|
||||
async fn bind_tunnel(&self, binding: crate::vpn_task::TunnelBinding) {
|
||||
let mut state = self.state.lock().await;
|
||||
if let Some(previous) = &state.tunnel.monitor {
|
||||
previous.set_statistics_enabled(false);
|
||||
}
|
||||
state.tunnel = binding;
|
||||
for connection in state.connections.values() {
|
||||
connection.clear_statistics();
|
||||
}
|
||||
state.refresh_demand();
|
||||
let tunnel_state = state.tunnel.monitor.as_ref().map(|monitor| monitor.state());
|
||||
drop(state);
|
||||
self.send_event(WsEvent::TunnelState(tunnel_state)).await;
|
||||
let state = self.state.lock().await;
|
||||
let snapshot = match &state.tunnel.monitor {
|
||||
Some(monitor) => monitor.statistics(),
|
||||
None => TunnelStatisticsSnapshot::unavailable(if state.tunnel.non_tunnel {
|
||||
TunnelUnavailableReason::NonTunnel
|
||||
} else {
|
||||
TunnelUnavailableReason::NoTunnel
|
||||
}),
|
||||
};
|
||||
for connection in state.connections.values() {
|
||||
connection.publish_statistics(snapshot.clone());
|
||||
}
|
||||
}
|
||||
|
||||
async fn publish_tunnel(&self, attempt: u64) {
|
||||
let state = self.state.lock().await;
|
||||
if state.tunnel.attempt != attempt {
|
||||
return;
|
||||
}
|
||||
let Some(monitor) = &state.tunnel.monitor else {
|
||||
return;
|
||||
};
|
||||
let current = monitor.state();
|
||||
let changed = state
|
||||
.snapshot
|
||||
.tunnel_state
|
||||
.as_ref()
|
||||
.is_none_or(|previous| previous.tunnel_id != current.tunnel_id || previous.sequence != current.sequence);
|
||||
drop(state);
|
||||
if changed {
|
||||
self.send_event(WsEvent::TunnelState(Some(current))).await;
|
||||
}
|
||||
let state = self.state.lock().await;
|
||||
if state.tunnel.attempt != attempt {
|
||||
return;
|
||||
}
|
||||
if let Some(monitor) = &state.tunnel.monitor {
|
||||
for connection in state.connections.values() {
|
||||
connection.publish_statistics(monitor.statistics());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -240,6 +367,7 @@ impl WsServer {
|
||||
_ = watch_vpn_state(self.vpn_state_rx.clone(), Arc::clone(&self.ctx)) => {
|
||||
info!("VPN state watch task completed");
|
||||
}
|
||||
_ = watch_tunnel(self.ctx.dispatcher.subscribe_tunnel(), Arc::clone(&self.ctx)) => {}
|
||||
result = axum::serve(listener, routes::routes(Arc::clone(&self.ctx))) => {
|
||||
if let Err(err) = result {
|
||||
warn!("WS server stopped: {err}");
|
||||
@@ -264,6 +392,7 @@ impl WsServer {
|
||||
|
||||
fn build_snapshot(vpn_state: VpnState) -> VpnEnv {
|
||||
VpnEnv {
|
||||
tunnel_state: None,
|
||||
vpn_state,
|
||||
vpnc_script: find_vpnc_script(),
|
||||
auth_executable: binary_paths::gpauth().to_string_lossy().into_owned(),
|
||||
@@ -273,6 +402,36 @@ fn build_snapshot(vpn_state: VpnState) -> VpnEnv {
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply the observation's explicit truncation policy to the space remaining in
|
||||
/// a complete envelope. Existing host/connection facts are never shortened.
|
||||
fn fit_snapshot_message(message: &mut ServerMessage) -> Result<(), ()> {
|
||||
let encoded_size = serde_json::to_vec(&message).map_err(|_| ())?.len();
|
||||
let Some(excess) = encoded_size.checked_sub(gpapi::service::transport::MAX_PLAINTEXT) else {
|
||||
return Ok(());
|
||||
};
|
||||
if excess == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
let snapshot = match message {
|
||||
ServerMessage::Attached { snapshot, .. } | ServerMessage::Snapshot { snapshot, .. } => snapshot,
|
||||
_ => return Err(()),
|
||||
};
|
||||
let configuration = snapshot
|
||||
.tunnel_state
|
||||
.as_mut()
|
||||
.and_then(|state| state.configuration.as_mut())
|
||||
.ok_or(())?;
|
||||
let configuration_size = serde_json::to_vec(&configuration).map_err(|_| ())?.len();
|
||||
let remaining = configuration_size.checked_sub(excess).ok_or(())?;
|
||||
if !configuration.limit_encoded_size(remaining) {
|
||||
return Err(());
|
||||
}
|
||||
if serde_json::to_vec(message).map_err(|_| ())?.len() > gpapi::service::transport::MAX_PLAINTEXT {
|
||||
return Err(());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::sync::{Arc, atomic::AtomicBool};
|
||||
@@ -285,6 +444,171 @@ mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
fn context() -> (Arc<WsServerContext>, Arc<SessionRegistry>) {
|
||||
let registry = Arc::new(SessionRegistry::for_tests(Uuid::new_v4()));
|
||||
let dispatcher = Arc::new(RequestDispatcher::new(
|
||||
crate::vpn_task::LifecycleHandle::for_tests(),
|
||||
Arc::new(AtomicBool::new(false)),
|
||||
Arc::new(Redaction::new()),
|
||||
true,
|
||||
false,
|
||||
));
|
||||
let (_, state_rx) = watch::channel(VpnState::Disconnected);
|
||||
(
|
||||
Arc::new(WsServerContext::new(
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
registry.clone(),
|
||||
dispatcher,
|
||||
&state_rx,
|
||||
)),
|
||||
registry,
|
||||
)
|
||||
}
|
||||
|
||||
async fn attach(
|
||||
context: &WsServerContext,
|
||||
registry: &Arc<SessionRegistry>,
|
||||
) -> (ConnectionControl, Uuid, u64, mpsc::Receiver<ConnectionCommand>) {
|
||||
let credential = registry.issue(env!("CARGO_PKG_VERSION"), None).unwrap();
|
||||
let permit = registry
|
||||
.begin_handshake(
|
||||
credential.service_instance_id(),
|
||||
credential.session_id(),
|
||||
credential.product_version(),
|
||||
)
|
||||
.unwrap();
|
||||
let (tx, mut rx) = mpsc::channel(8);
|
||||
let (control, _) = ConnectionControl::new(Uuid::new_v4(), tx);
|
||||
let prepared = context.prepare_connection(&permit, control.clone()).await.unwrap();
|
||||
rx.recv().await.unwrap();
|
||||
context.commit_connection(&permit, &control).await.unwrap();
|
||||
(control, credential.session_id(), prepared.generation, rx)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn statistics_demand_is_per_attachment_and_does_not_consume_revisions() {
|
||||
use gpapi::service::transport::ServiceResult;
|
||||
use gpapi::session::tunnel::TunnelStatisticsState;
|
||||
let (context, registry) = context();
|
||||
let (a, a_session, a_generation, _a_rx) = attach(&context, ®istry).await;
|
||||
let (b, b_session, b_generation, _b_rx) = attach(&context, ®istry).await;
|
||||
let vpn = Arc::new(
|
||||
openconnect::Vpn::builder("vpn.example.com", "cookie")
|
||||
.network_configuration_handler(|_| Ok(()))
|
||||
.build()
|
||||
.unwrap(),
|
||||
);
|
||||
let monitor = gptunnel::TunnelMonitor::attach(vpn);
|
||||
context
|
||||
.bind_tunnel(crate::vpn_task::TunnelBinding {
|
||||
attempt: 1,
|
||||
monitor: Some(monitor.clone()),
|
||||
non_tunnel: false,
|
||||
})
|
||||
.await;
|
||||
let revision = context.state.lock().await.revision;
|
||||
for (connection, session, generation) in [(&a, a_session, a_generation), (&b, b_session, b_generation)] {
|
||||
assert!(matches!(
|
||||
context
|
||||
.set_statistics_enabled(connection, session, generation, true)
|
||||
.await,
|
||||
ServiceResult::TunnelStatisticsSnapshot(_)
|
||||
));
|
||||
}
|
||||
assert_eq!(
|
||||
monitor.statistics().view().unwrap().state,
|
||||
TunnelStatisticsState::Waiting
|
||||
);
|
||||
let ServiceResult::TunnelStatisticsSnapshot(disabled) =
|
||||
context.set_statistics_enabled(&b, b_session, b_generation, false).await
|
||||
else {
|
||||
panic!("expected snapshot");
|
||||
};
|
||||
assert_eq!(disabled.view().unwrap().state, TunnelStatisticsState::Disabled);
|
||||
assert_eq!(
|
||||
monitor.statistics().view().unwrap().state,
|
||||
TunnelStatisticsState::Waiting
|
||||
);
|
||||
context
|
||||
.remove_connection(a.connection_id(), a_session, a_generation)
|
||||
.await;
|
||||
assert_eq!(
|
||||
monitor.statistics().view().unwrap().state,
|
||||
TunnelStatisticsState::Disabled
|
||||
);
|
||||
assert_eq!(context.state.lock().await.revision, revision);
|
||||
monitor.stop().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn sample_slot_coalesces_without_consuming_control_queue_and_disable_clears_it() {
|
||||
let (context, registry) = context();
|
||||
let (control, session, generation, mut commands) = attach(&context, ®istry).await;
|
||||
context
|
||||
.set_statistics_enabled(&control, session, generation, true)
|
||||
.await;
|
||||
let mut samples = control.subscribe_statistics();
|
||||
let snapshot = TunnelStatisticsSnapshot::unavailable(TunnelUnavailableReason::NoTunnel);
|
||||
for _ in 0..1000 {
|
||||
control.publish_statistics(snapshot.clone());
|
||||
}
|
||||
assert_eq!(samples.borrow_and_update().as_ref(), Some(&snapshot));
|
||||
assert!(commands.try_recv().is_err());
|
||||
context
|
||||
.set_statistics_enabled(&control, session, generation, false)
|
||||
.await;
|
||||
assert!(samples.borrow().is_none());
|
||||
control.publish_statistics(snapshot);
|
||||
assert!(samples.borrow().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snapshot_budget_trims_observation_without_changing_existing_metadata() {
|
||||
use gpapi::session::tunnel::{TunnelConfiguration, TunnelPhase, TunnelState, TunnelTransport};
|
||||
let mut snapshot = build_snapshot(VpnState::Disconnected);
|
||||
let mut configuration = TunnelConfiguration::default();
|
||||
configuration.included_routes.items = vec!["r".repeat(1000); 15];
|
||||
configuration.included_routes.total_count = 15;
|
||||
snapshot.tunnel_state = Some(TunnelState {
|
||||
tunnel_id: Uuid::nil(),
|
||||
sequence: 4,
|
||||
phase: TunnelPhase::Connected,
|
||||
configuration_revision: 1,
|
||||
configuration: Some(configuration),
|
||||
transport: TunnelTransport::IpsecEsp,
|
||||
elapsed_ms: 600_000,
|
||||
});
|
||||
let original_metadata = "a".repeat(52_000);
|
||||
snapshot.auth_executable = original_metadata.clone();
|
||||
let mut message = ServerMessage::Snapshot {
|
||||
current_revision: 9,
|
||||
snapshot,
|
||||
};
|
||||
assert!(serde_json::to_vec(&message).unwrap().len() > gpapi::service::transport::MAX_PLAINTEXT);
|
||||
fit_snapshot_message(&mut message).unwrap();
|
||||
assert!(serde_json::to_vec(&message).unwrap().len() <= gpapi::service::transport::MAX_PLAINTEXT);
|
||||
let ServerMessage::Snapshot {
|
||||
mut snapshot,
|
||||
current_revision,
|
||||
} = message
|
||||
else {
|
||||
unreachable!()
|
||||
};
|
||||
assert_eq!(current_revision, 9);
|
||||
assert_eq!(snapshot.auth_executable, original_metadata);
|
||||
let configuration = snapshot.tunnel_state.as_ref().unwrap().configuration.as_ref().unwrap();
|
||||
assert!(configuration.included_routes.truncated);
|
||||
assert_eq!(configuration.included_routes.total_count, 15);
|
||||
snapshot.auth_executable = "a".repeat(gpapi::service::transport::MAX_PLAINTEXT);
|
||||
assert!(
|
||||
fit_snapshot_message(&mut ServerMessage::Snapshot {
|
||||
current_revision: 9,
|
||||
snapshot
|
||||
})
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn resync_snapshot_is_ordered_with_state_events() {
|
||||
let registry = Arc::new(SessionRegistry::for_tests(Uuid::new_v4()));
|
||||
@@ -301,7 +625,7 @@ mod tests {
|
||||
Arc::new(AtomicBool::new(false)),
|
||||
Arc::new(Redaction::new()),
|
||||
true,
|
||||
None,
|
||||
false,
|
||||
));
|
||||
let (_state_tx, state_rx) = watch::channel(VpnState::Disconnected);
|
||||
let context = Arc::new(WsServerContext::new(
|
||||
@@ -351,7 +675,7 @@ mod tests {
|
||||
Arc::new(AtomicBool::new(false)),
|
||||
Arc::new(Redaction::new()),
|
||||
true,
|
||||
None,
|
||||
false,
|
||||
));
|
||||
let (_state_tx, state_rx) = watch::channel(VpnState::Disconnected);
|
||||
let context = Arc::new(WsServerContext::new(
|
||||
@@ -397,3 +721,32 @@ async fn watch_vpn_state(mut vpn_state_rx: watch::Receiver<VpnState>, ctx: Arc<W
|
||||
ctx.send_event(WsEvent::VpnState(vpn_state)).await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn watch_tunnel(mut binding_rx: watch::Receiver<crate::vpn_task::TunnelBinding>, ctx: Arc<WsServerContext>) {
|
||||
loop {
|
||||
let binding = binding_rx.borrow_and_update().clone();
|
||||
let mut updates = binding.monitor.as_ref().map(|monitor| monitor.subscribe());
|
||||
let attempt = binding.attempt;
|
||||
ctx.bind_tunnel(binding).await;
|
||||
loop {
|
||||
tokio::select! {
|
||||
biased;
|
||||
changed = binding_rx.changed() => {
|
||||
if changed.is_err() { return; }
|
||||
break;
|
||||
}
|
||||
changed = async {
|
||||
match &mut updates {
|
||||
Some(receiver) => receiver.changed().await,
|
||||
None => std::future::pending().await,
|
||||
}
|
||||
} => {
|
||||
if changed.is_err() { break; }
|
||||
// Fence an update against a new lifecycle binding already published.
|
||||
if binding_rx.has_changed().unwrap_or(true) { break; }
|
||||
ctx.publish_tunnel(attempt).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user