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
@@ -0,0 +1,137 @@
|
||||
diff --git a/gpst.c b/gpst.c
|
||||
index 244aaf04..78c16d1c 100644
|
||||
--- a/gpst.c
|
||||
+++ b/gpst.c
|
||||
@@ -438,8 +438,23 @@ static int gpst_parse_config_xml(struct openconnect_info *vpninfo, xmlNode *xml_
|
||||
new_ip_info.netmask = add_option_steal(&new_opts, "netmask", &s);
|
||||
} else if (!xmlnode_get_val(xml_node, "mtu", &s))
|
||||
new_ip_info.mtu = atoi(s);
|
||||
- else if (!xmlnode_get_val(xml_node, "lifetime", &s))
|
||||
+ else if (!xmlnode_get_val(xml_node, "lifetime", &s)) {
|
||||
+ vpninfo->gp_session_lifetime = atoi(s);
|
||||
vpninfo->auth_expiration = time(NULL) + atol(s);
|
||||
+ } else if (!xmlnode_get_val(xml_node, "user-expires", &s) ||
|
||||
+ !xmlnode_get_val(xml_node, "user_expires", &s)) {
|
||||
+ vpninfo->gp_user_expires = atol(s);
|
||||
+ } else if (!xmlnode_get_val(xml_node, "lifetime-notify-prior", &s)) {
|
||||
+ vpninfo->gp_lifetime_notify_prior = atoi(s);
|
||||
+ } else if (!xmlnode_get_val(xml_node, "lifetime-notify-message", &s)) {
|
||||
+ free(vpninfo->gp_lifetime_notify_message);
|
||||
+ vpninfo->gp_lifetime_notify_message = s;
|
||||
+ s = NULL;
|
||||
+ } else if (!xmlnode_get_val(xml_node, "allow-extend-session", &s)) {
|
||||
+ vpninfo->gp_allow_extend_session = !strcasecmp(s, "yes") ||
|
||||
+ !strcasecmp(s, "true") ||
|
||||
+ !strcmp(s, "1");
|
||||
+ }
|
||||
else if (!xmlnode_get_val(xml_node, "quarantine", &s)) {
|
||||
if (strcmp(s, "no"))
|
||||
vpn_progress(vpninfo, PRG_DEBUG,
|
||||
diff --git a/libopenconnect.map.in b/libopenconnect.map.in
|
||||
index f036c071..44110486 100644
|
||||
--- a/libopenconnect.map.in
|
||||
+++ b/libopenconnect.map.in
|
||||
@@ -113,6 +113,11 @@ OPENCONNECT_5_7 {
|
||||
openconnect_set_cookie;
|
||||
openconnect_set_allow_insecure_crypto;
|
||||
openconnect_get_auth_expiration;
|
||||
+ openconnect_get_gp_session_lifetime;
|
||||
+ openconnect_get_gp_user_expires;
|
||||
+ openconnect_get_gp_lifetime_notify_prior;
|
||||
+ openconnect_get_gp_lifetime_notify_message;
|
||||
+ openconnect_get_gp_allow_extend_session;
|
||||
openconnect_disable_dtls;
|
||||
openconnect_get_connect_url;
|
||||
openconnect_set_webview_callback;
|
||||
diff --git a/libopenconnect5.symbols b/libopenconnect5.symbols
|
||||
index 7cb04d05..2c7af781 100644
|
||||
--- a/libopenconnect5.symbols
|
||||
+++ b/libopenconnect5.symbols
|
||||
@@ -94,6 +94,11 @@ libopenconnect.so.5 libopenconnect5 #MINVER#
|
||||
openconnect_set_cookie@OPENCONNECT_5_7 8.20
|
||||
openconnect_set_allow_insecure_crypto@OPENCONNECT_5_7 8.20
|
||||
openconnect_get_auth_expiration@OPENCONNECT_5_7 8.20
|
||||
+ openconnect_get_gp_session_lifetime@OPENCONNECT_5_7 8.20
|
||||
+ openconnect_get_gp_user_expires@OPENCONNECT_5_7 8.20
|
||||
+ openconnect_get_gp_lifetime_notify_prior@OPENCONNECT_5_7 8.20
|
||||
+ openconnect_get_gp_lifetime_notify_message@OPENCONNECT_5_7 8.20
|
||||
+ openconnect_get_gp_allow_extend_session@OPENCONNECT_5_7 8.20
|
||||
openconnect_disable_dtls@OPENCONNECT_5_7 8.20
|
||||
openconnect_get_connect_url@OPENCONNECT_5_7 8.20
|
||||
openconnect_set_webview_callback@OPENCONNECT_5_7 8.20
|
||||
diff --git a/library.c b/library.c
|
||||
index b5e74e9b..03f47004 100644
|
||||
--- a/library.c
|
||||
+++ b/library.c
|
||||
@@ -838,6 +838,7 @@ void openconnect_vpninfo_free(struct openconnect_info *vpninfo)
|
||||
free(vpninfo->dtls_cipher);
|
||||
free(vpninfo->peer_cert_hash);
|
||||
free(vpninfo->ciphersuite_config);
|
||||
+ free(vpninfo->gp_lifetime_notify_message);
|
||||
#if defined(OPENCONNECT_OPENSSL)
|
||||
free(vpninfo->cstp_cipher);
|
||||
#if defined(HAVE_BIO_METH_FREE)
|
||||
@@ -1137,6 +1138,31 @@ time_t openconnect_get_auth_expiration(struct openconnect_info *vpninfo)
|
||||
return vpninfo->auth_expiration;
|
||||
}
|
||||
|
||||
+int openconnect_get_gp_session_lifetime(struct openconnect_info *vpninfo)
|
||||
+{
|
||||
+ return vpninfo->gp_session_lifetime;
|
||||
+}
|
||||
+
|
||||
+time_t openconnect_get_gp_user_expires(struct openconnect_info *vpninfo)
|
||||
+{
|
||||
+ return vpninfo->gp_user_expires;
|
||||
+}
|
||||
+
|
||||
+int openconnect_get_gp_lifetime_notify_prior(struct openconnect_info *vpninfo)
|
||||
+{
|
||||
+ return vpninfo->gp_lifetime_notify_prior;
|
||||
+}
|
||||
+
|
||||
+const char *openconnect_get_gp_lifetime_notify_message(struct openconnect_info *vpninfo)
|
||||
+{
|
||||
+ return vpninfo->gp_lifetime_notify_message;
|
||||
+}
|
||||
+
|
||||
+int openconnect_get_gp_allow_extend_session(struct openconnect_info *vpninfo)
|
||||
+{
|
||||
+ return vpninfo->gp_allow_extend_session;
|
||||
+}
|
||||
+
|
||||
int openconnect_get_ip_info(struct openconnect_info *vpninfo,
|
||||
const struct oc_ip_info **info,
|
||||
const struct oc_vpn_option **cstp_options,
|
||||
diff --git a/openconnect-internal.h b/openconnect-internal.h
|
||||
index abf45bbd..dd5507ca 100644
|
||||
--- a/openconnect-internal.h
|
||||
+++ b/openconnect-internal.h
|
||||
@@ -647,6 +647,11 @@ struct openconnect_info {
|
||||
int dtls_attempt_period;
|
||||
int udp_probes_sent;
|
||||
time_t auth_expiration;
|
||||
+ int gp_session_lifetime;
|
||||
+ time_t gp_user_expires;
|
||||
+ int gp_lifetime_notify_prior;
|
||||
+ char *gp_lifetime_notify_message;
|
||||
+ int gp_allow_extend_session;
|
||||
time_t new_dtls_started;
|
||||
#if defined(OPENCONNECT_OPENSSL)
|
||||
SSL_CTX *dtls_ctx;
|
||||
diff --git a/openconnect.h b/openconnect.h
|
||||
index 136f181a..febf15cc 100644
|
||||
--- a/openconnect.h
|
||||
+++ b/openconnect.h
|
||||
@@ -655,6 +655,11 @@ void openconnect_set_dpd(struct openconnect_info *, int min_seconds);
|
||||
void openconnect_set_trojan_interval(struct openconnect_info *, int seconds);
|
||||
int openconnect_get_idle_timeout(struct openconnect_info *);
|
||||
time_t openconnect_get_auth_expiration(struct openconnect_info *);
|
||||
+int openconnect_get_gp_session_lifetime(struct openconnect_info *);
|
||||
+time_t openconnect_get_gp_user_expires(struct openconnect_info *);
|
||||
+int openconnect_get_gp_lifetime_notify_prior(struct openconnect_info *);
|
||||
+const char *openconnect_get_gp_lifetime_notify_message(struct openconnect_info *);
|
||||
+int openconnect_get_gp_allow_extend_session(struct openconnect_info *);
|
||||
|
||||
/* The returned structures are owned by the library and may be freed/replaced
|
||||
due to rekey or reconnect. Assume that once the mainloop starts, the
|
||||
@@ -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