diff --git a/apps/gpclient/src/connect.rs b/apps/gpclient/src/connect.rs index 39e32ed..1276b99 100644 --- a/apps/gpclient/src/connect.rs +++ b/apps/gpclient/src/connect.rs @@ -109,12 +109,24 @@ pub(crate) struct ConnectArgs { #[arg(long, help = "Disable DTLS and ESP")] no_dtls: bool, + #[arg( + long = "local-hostname", + help = "Same as the '--local-hostname' option in the openconnect command" + )] + local_hostname: Option, + #[arg( long = "force-dpd", help = "Same as the '--force-dpd' option in the openconnect command" )] dpd_interval: Option, + #[arg( + long = "no-xmlpost", + help = "Same as the '--no-xmlpost' option in the openconnect command" + )] + no_xmlpost: bool, + #[cfg(feature = "webview-auth")] #[arg(long, help = "The HiDPI mode, useful for high-resolution screens")] hidpi: bool, @@ -390,7 +402,9 @@ impl<'a> ConnectHandler<'a> { .mtu(mtu) .disable_ipv6(self.args.disable_ipv6) .no_dtls(self.args.no_dtls) + .local_hostname(self.args.local_hostname.clone()) .dpd_interval(self.args.dpd_interval.unwrap_or(0)) + .no_xmlpost(self.args.no_xmlpost) .build()?; let vpn = Arc::new(vpn); diff --git a/apps/gpservice/src/vpn_task.rs b/apps/gpservice/src/vpn_task.rs index 515a15c..b19af39 100644 --- a/apps/gpservice/src/vpn_task.rs +++ b/apps/gpservice/src/vpn_task.rs @@ -9,7 +9,7 @@ use gpapi::{ }; use log::{info, warn}; use openconnect::Vpn; -use tokio::sync::{mpsc, oneshot, watch, RwLock}; +use tokio::sync::{RwLock, mpsc, oneshot, watch}; use tokio_util::sync::CancellationToken; pub(crate) struct VpnTaskContext { @@ -54,6 +54,9 @@ impl VpnTaskContext { .mtu(args.mtu()) .disable_ipv6(args.disable_ipv6()) .no_dtls(args.no_dtls()) + .local_hostname(args.local_hostname()) + .dpd_interval(args.force_dpd()) + .no_xmlpost(args.no_xmlpost()) .build() { Ok(vpn) => vpn, diff --git a/crates/gpapi/src/service/request.rs b/crates/gpapi/src/service/request.rs index 49ef68f..e7f23ce 100644 --- a/crates/gpapi/src/service/request.rs +++ b/crates/gpapi/src/service/request.rs @@ -49,6 +49,9 @@ pub struct ConnectArgs { mtu: u32, disable_ipv6: bool, no_dtls: bool, + local_hostname: Option, + force_dpd: u32, + no_xmlpost: bool, } impl ConnectArgs { @@ -70,6 +73,9 @@ impl ConnectArgs { mtu: 0, disable_ipv6: false, no_dtls: false, + local_hostname: None, + force_dpd: 0, + no_xmlpost: false, } } @@ -136,6 +142,18 @@ impl ConnectArgs { pub fn no_dtls(&self) -> bool { self.no_dtls } + + pub fn local_hostname(&self) -> Option { + self.local_hostname.clone() + } + + pub fn force_dpd(&self) -> u32 { + self.force_dpd + } + + pub fn no_xmlpost(&self) -> bool { + self.no_xmlpost + } } #[derive(Debug, Deserialize, Serialize, Type)] @@ -227,6 +245,21 @@ impl ConnectRequest { self } + pub fn with_local_hostname>>(mut self, local_hostname: T) -> Self { + self.args.local_hostname = local_hostname.into(); + self + } + + pub fn with_force_dpd(mut self, force_dpd: u32) -> Self { + self.args.force_dpd = force_dpd; + self + } + + pub fn with_no_xmlpost(mut self, no_xmlpost: bool) -> Self { + self.args.no_xmlpost = no_xmlpost; + self + } + pub fn gateway(&self) -> &Gateway { self.info.gateway() } diff --git a/crates/openconnect/src/ffi/mod.rs b/crates/openconnect/src/ffi/mod.rs index c99b915..ac0259f 100644 --- a/crates/openconnect/src/ffi/mod.rs +++ b/crates/openconnect/src/ffi/mod.rs @@ -15,6 +15,7 @@ pub(crate) struct ConnectOptions { pub os: *const c_char, pub os_version: *const c_char, pub client_version: *const c_char, + pub local_hostname: *const c_char, pub script: *const c_char, pub interface: *const c_char, @@ -34,6 +35,7 @@ pub(crate) struct ConnectOptions { pub no_dtls: u32, pub dpd_interval: u32, + pub no_xmlpost: u32, } #[link(name = "vpn")] diff --git a/crates/openconnect/src/ffi/vpn.c b/crates/openconnect/src/ffi/vpn.c index 29c14ec..68dd15f 100644 --- a/crates/openconnect/src/ffi/vpn.c +++ b/crates/openconnect/src/ffi/vpn.c @@ -60,6 +60,7 @@ int vpn_connect(const vpn_options *options, vpn_connected_callback callback) { struct openconnect_info *vpninfo; struct utsname utsbuf; + const char *effective_local_hostname = NULL; g_user_data = options->user_data; g_vpnc_script = options->script; @@ -79,6 +80,7 @@ int vpn_connect(const vpn_options *options, vpn_connected_callback callback) INFO("DISABLE_IPV6: %d", options->disable_ipv6); INFO("NO_DTLS: %d", options->no_dtls); INFO("DPD_INTERVAL: %d", options->dpd_interval); + INFO("NO_XMLPOST: %d", options->no_xmlpost); vpninfo = openconnect_vpninfo_new(options->user_agent, validate_peer_cert, @@ -109,6 +111,17 @@ int vpn_connect(const vpn_options *options, vpn_connected_callback callback) options->client_version); } + effective_local_hostname = options->local_hostname; + if (!effective_local_hostname && !uname(&utsbuf)) { + effective_local_hostname = utsbuf.nodename; + } + if (effective_local_hostname) { + openconnect_set_localname(vpninfo, effective_local_hostname); + } + + INFO("LOCAL_HOSTNAME: %s", + effective_local_hostname ? effective_local_hostname : "(not set)"); + if (options->certificate) { INFO("Setting client certificate: %s", options->certificate); openconnect_set_client_cert(vpninfo, options->certificate, @@ -119,6 +132,10 @@ int vpn_connect(const vpn_options *options, vpn_connected_callback callback) openconnect_set_key_password(vpninfo, options->key_password); } + if (options->no_xmlpost) { + openconnect_set_xmlpost(vpninfo, 0); + } + if (options->csd_wrapper) { openconnect_setup_csd(vpninfo, options->csd_uid, 1, options->csd_wrapper); @@ -143,10 +160,6 @@ int vpn_connect(const vpn_options *options, vpn_connected_callback callback) return 1; } - if (!uname(&utsbuf)) { - openconnect_set_localname(vpninfo, utsbuf.nodename); - } - // Essential step if (openconnect_make_cstp_connection(vpninfo) != 0) { ERROR("openconnect_make_cstp_connection failed"); diff --git a/crates/openconnect/src/ffi/vpn.h b/crates/openconnect/src/ffi/vpn.h index 7483419..9743d2d 100644 --- a/crates/openconnect/src/ffi/vpn.h +++ b/crates/openconnect/src/ffi/vpn.h @@ -15,6 +15,7 @@ typedef struct vpn_options { const char *os; const char *os_version; const char *client_version; + const char *local_hostname; const char *script; const char *interface; @@ -34,6 +35,7 @@ typedef struct vpn_options { const int no_dtls; const int dpd_interval; + const int no_xmlpost; } vpn_options; int vpn_connect(const vpn_options *options, vpn_connected_callback callback); diff --git a/crates/openconnect/src/vpn.rs b/crates/openconnect/src/vpn.rs index 08fe939..5691567 100644 --- a/crates/openconnect/src/vpn.rs +++ b/crates/openconnect/src/vpn.rs @@ -1,5 +1,5 @@ use std::{ - ffi::{c_char, CString}, + ffi::{CString, c_char}, fmt, sync::{Arc, RwLock}, }; @@ -19,6 +19,7 @@ pub struct Vpn { os: CString, os_version: Option, client_version: Option, + local_hostname: Option, script: CString, interface: Option, @@ -38,6 +39,7 @@ pub struct Vpn { no_dtls: bool, dpd_interval: u32, + no_xmlpost: bool, callback: OnConnectedCallback, } @@ -77,6 +79,7 @@ impl Vpn { os: self.os.as_ptr(), os_version: Self::option_to_ptr(&self.os_version), client_version: Self::option_to_ptr(&self.client_version), + local_hostname: Self::option_to_ptr(&self.local_hostname), script: self.script.as_ptr(), interface: Self::option_to_ptr(&self.interface), @@ -95,6 +98,7 @@ impl Vpn { disable_ipv6: self.disable_ipv6 as u32, no_dtls: self.no_dtls as u32, dpd_interval: self.dpd_interval, + no_xmlpost: self.no_xmlpost as u32, } } @@ -136,6 +140,7 @@ pub struct VpnBuilder { os: Option, os_version: Option, client_version: Option, + local_hostname: Option, certificate: Option, sslkey: Option, @@ -151,6 +156,7 @@ pub struct VpnBuilder { no_dtls: bool, dpd_interval: u32, + no_xmlpost: bool, } impl VpnBuilder { @@ -166,6 +172,7 @@ impl VpnBuilder { os: None, os_version: None, client_version: None, + local_hostname: None, certificate: None, sslkey: None, @@ -180,6 +187,7 @@ impl VpnBuilder { disable_ipv6: false, no_dtls: false, dpd_interval: 0, + no_xmlpost: false, } } @@ -218,6 +226,11 @@ impl VpnBuilder { self } + pub fn local_hostname>>(mut self, local_hostname: T) -> Self { + self.local_hostname = local_hostname.into(); + self + } + pub fn certificate>>(mut self, certificate: T) -> Self { self.certificate = certificate.into(); self @@ -273,6 +286,11 @@ impl VpnBuilder { self } + pub fn no_xmlpost(mut self, no_xmlpost: bool) -> Self { + self.no_xmlpost = no_xmlpost; + self + } + fn determine_script(&self) -> Result<&str, VpnError> { match &self.script { Some(script) => { @@ -315,6 +333,7 @@ impl VpnBuilder { os: Self::to_cstring(&os), os_version: self.os_version.as_deref().map(Self::to_cstring), client_version: self.client_version.as_deref().map(Self::to_cstring), + local_hostname: self.local_hostname.as_deref().map(Self::to_cstring), script: Self::to_cstring(&script), interface: self.interface.as_deref().map(Self::to_cstring), @@ -333,6 +352,7 @@ impl VpnBuilder { disable_ipv6: self.disable_ipv6, no_dtls: self.no_dtls, dpd_interval: self.dpd_interval, + no_xmlpost: self.no_xmlpost, callback: Default::default(), })