mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2026-11-19 06:26:14 -05:00
refactor: update session extension flow
This commit is contained in:
19 files changed
+927
-419
No files matched your search
@@ -1,6 +1,6 @@
|
||||
use crate::Vpn;
|
||||
use log::{debug, info, trace, warn};
|
||||
use std::ffi::{c_char, c_int, c_void};
|
||||
use std::ffi::{c_char, c_int, c_long, c_void};
|
||||
|
||||
/// ConnectOptions struct for FFI, the field names and order must match the C definition.
|
||||
#[repr(C)]
|
||||
@@ -38,10 +38,24 @@ pub(crate) struct ConnectOptions {
|
||||
pub no_xmlpost: u32,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct VpnSessionInfoRaw {
|
||||
pub auth_expiration: c_long,
|
||||
pub lifetime_secs: c_int,
|
||||
pub user_expires: c_long,
|
||||
pub lifetime_warning_prior: c_int,
|
||||
pub lifetime_warning_message: *const c_char,
|
||||
pub allow_extend_session: c_int,
|
||||
}
|
||||
|
||||
#[link(name = "vpn")]
|
||||
unsafe extern "C" {
|
||||
#[link_name = "vpn_connect"]
|
||||
fn vpn_connect(options: *const ConnectOptions, callback: extern "C" fn(i32, *mut c_void)) -> c_int;
|
||||
fn vpn_connect(
|
||||
options: *const ConnectOptions,
|
||||
callback: extern "C" fn(i32, *const VpnSessionInfoRaw, *mut c_void),
|
||||
) -> c_int;
|
||||
|
||||
#[link_name = "vpn_disconnect"]
|
||||
fn vpn_disconnect();
|
||||
@@ -56,9 +70,9 @@ pub(crate) fn disconnect() {
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn on_vpn_connected(pipe_fd: i32, vpn: *mut c_void) {
|
||||
extern "C" fn on_vpn_connected(pipe_fd: i32, session_info: *const VpnSessionInfoRaw, vpn: *mut c_void) {
|
||||
let vpn = unsafe { &*(vpn as *const Vpn) };
|
||||
vpn.on_connected(pipe_fd);
|
||||
vpn.on_connected(pipe_fd, crate::vpn::session_info_from_raw(session_info));
|
||||
}
|
||||
|
||||
// Logger used in the C code.
|
||||
|
||||
@@ -51,7 +51,18 @@ static void setup_tun_handler(void *_vpninfo)
|
||||
}
|
||||
|
||||
if (!ret) {
|
||||
on_vpn_connected(g_cmd_pipe_fd, g_user_data);
|
||||
vpn_session_info session_info = {
|
||||
.auth_expiration = (long)openconnect_get_auth_expiration(_vpninfo),
|
||||
.lifetime_secs = openconnect_get_gp_session_lifetime(_vpninfo),
|
||||
.user_expires = (long)openconnect_get_gp_user_expires(_vpninfo),
|
||||
.lifetime_warning_prior =
|
||||
openconnect_get_gp_lifetime_notify_prior(_vpninfo),
|
||||
.lifetime_warning_message =
|
||||
openconnect_get_gp_lifetime_notify_message(_vpninfo),
|
||||
.allow_extend_session =
|
||||
openconnect_get_gp_allow_extend_session(_vpninfo),
|
||||
};
|
||||
on_vpn_connected(g_cmd_pipe_fd, &session_info, g_user_data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,18 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef void (*vpn_connected_callback)(int cmd_pipe_fd, void *user_data);
|
||||
typedef struct vpn_session_info {
|
||||
long auth_expiration;
|
||||
int lifetime_secs;
|
||||
long user_expires;
|
||||
int lifetime_warning_prior;
|
||||
const char *lifetime_warning_message;
|
||||
int allow_extend_session;
|
||||
} vpn_session_info;
|
||||
|
||||
typedef void (*vpn_connected_callback)(int cmd_pipe_fd,
|
||||
const vpn_session_info *session_info,
|
||||
void *user_data);
|
||||
|
||||
typedef struct vpn_options {
|
||||
void *user_data;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use std::{
|
||||
ffi::{CString, c_char},
|
||||
ffi::{CStr, CString, c_char},
|
||||
fmt,
|
||||
sync::{Arc, RwLock},
|
||||
};
|
||||
@@ -9,7 +9,54 @@ use log::info;
|
||||
use crate::ffi;
|
||||
use crate::vpn_utils::{check_executable, find_csd_wrapper, find_vpnc_script};
|
||||
|
||||
type OnConnectedCallback = Arc<RwLock<Option<Box<dyn FnOnce() + 'static + Send + Sync>>>>;
|
||||
type OnConnectedCallback = Arc<RwLock<Option<Box<dyn FnOnce(VpnSessionInfo) + 'static + Send + Sync>>>>;
|
||||
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct VpnSessionInfo {
|
||||
pub lifetime_secs: Option<u32>,
|
||||
pub user_expires: Option<u32>,
|
||||
pub lifetime_warning: Option<VpnSessionWarning>,
|
||||
pub allow_extend_session: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct VpnSessionWarning {
|
||||
pub prior_secs: u32,
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
pub(crate) fn session_info_from_raw(raw: *const ffi::VpnSessionInfoRaw) -> VpnSessionInfo {
|
||||
if raw.is_null() {
|
||||
return VpnSessionInfo::default();
|
||||
}
|
||||
|
||||
let raw = unsafe { &*raw };
|
||||
let warning_message =
|
||||
unsafe { optional_c_string(raw.lifetime_warning_message) }.filter(|message| !message.is_empty());
|
||||
let user_expires = positive_i64_to_u32(raw.user_expires).or_else(|| positive_i64_to_u32(raw.auth_expiration));
|
||||
|
||||
VpnSessionInfo {
|
||||
lifetime_secs: positive_i64_to_u32(raw.lifetime_secs as i64),
|
||||
user_expires,
|
||||
lifetime_warning: match (positive_i64_to_u32(raw.lifetime_warning_prior as i64), warning_message) {
|
||||
(Some(prior_secs), Some(message)) => Some(VpnSessionWarning { prior_secs, message }),
|
||||
_ => None,
|
||||
},
|
||||
allow_extend_session: raw.allow_extend_session != 0,
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn optional_c_string(value: *const c_char) -> Option<String> {
|
||||
if value.is_null() {
|
||||
return None;
|
||||
}
|
||||
|
||||
unsafe { CStr::from_ptr(value) }.to_str().ok().map(ToOwned::to_owned)
|
||||
}
|
||||
|
||||
fn positive_i64_to_u32(value: i64) -> Option<u32> {
|
||||
u32::try_from(value).ok().filter(|value| *value > 0)
|
||||
}
|
||||
|
||||
pub struct Vpn {
|
||||
server: CString,
|
||||
@@ -49,18 +96,18 @@ impl Vpn {
|
||||
VpnBuilder::new(server, cookie)
|
||||
}
|
||||
|
||||
pub fn connect(&self, on_connected: impl FnOnce() + 'static + Send + Sync) -> i32 {
|
||||
pub fn connect(&self, on_connected: impl FnOnce(VpnSessionInfo) + 'static + Send + Sync) -> i32 {
|
||||
self.callback.write().unwrap().replace(Box::new(on_connected));
|
||||
let options = self.build_connect_options();
|
||||
|
||||
ffi::connect(&options)
|
||||
}
|
||||
|
||||
pub(crate) fn on_connected(&self, pipe_fd: i32) {
|
||||
pub(crate) fn on_connected(&self, pipe_fd: i32, session_info: VpnSessionInfo) {
|
||||
info!("Connected to VPN, pipe_fd: {}", pipe_fd);
|
||||
|
||||
if let Some(callback) = self.callback.write().unwrap().take() {
|
||||
callback();
|
||||
callback(session_info);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -362,3 +409,54 @@ impl VpnBuilder {
|
||||
CString::new(value.to_string()).expect("Failed to convert to CString")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::ffi::CString;
|
||||
|
||||
#[test]
|
||||
fn maps_session_info_from_callback_payload() {
|
||||
let message = CString::new("Session expires soon").unwrap();
|
||||
let raw = ffi::VpnSessionInfoRaw {
|
||||
auth_expiration: 0,
|
||||
lifetime_secs: 43_200,
|
||||
user_expires: 1_776_828_409,
|
||||
lifetime_warning_prior: 1_800,
|
||||
lifetime_warning_message: message.as_ptr(),
|
||||
allow_extend_session: 1,
|
||||
};
|
||||
|
||||
let info = session_info_from_raw(&raw);
|
||||
|
||||
assert_eq!(info.lifetime_secs, Some(43_200));
|
||||
assert_eq!(info.user_expires, Some(1_776_828_409));
|
||||
assert_eq!(
|
||||
info.lifetime_warning,
|
||||
Some(VpnSessionWarning {
|
||||
prior_secs: 1_800,
|
||||
message: "Session expires soon".to_string(),
|
||||
})
|
||||
);
|
||||
assert!(info.allow_extend_session);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn falls_back_to_auth_expiration_when_user_expires_is_absent() {
|
||||
let raw = ffi::VpnSessionInfoRaw {
|
||||
auth_expiration: 1_776_828_409,
|
||||
lifetime_secs: 0,
|
||||
user_expires: 0,
|
||||
lifetime_warning_prior: 0,
|
||||
lifetime_warning_message: std::ptr::null(),
|
||||
allow_extend_session: 0,
|
||||
};
|
||||
|
||||
let info = session_info_from_raw(&raw);
|
||||
|
||||
assert_eq!(info.user_expires, Some(1_776_828_409));
|
||||
assert_eq!(info.lifetime_secs, None);
|
||||
assert_eq!(info.lifetime_warning, None);
|
||||
assert!(!info.allow_extend_session);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user