feat: expose openconnect connection options

This commit is contained in:
Kevin Yue committed 2026-05-06 23:20:55 +08:00
1 parent a51f034036
commit 5ace087f92
7 files changed
+93 -6

No files matched your search

+14
View File
@@ -109,12 +109,24 @@ pub(crate) struct ConnectArgs {
#[arg(long, help = "Disable DTLS and ESP")] #[arg(long, help = "Disable DTLS and ESP")]
no_dtls: bool, no_dtls: bool,
#[arg(
long = "local-hostname",
help = "Same as the '--local-hostname' option in the openconnect command"
)]
local_hostname: Option<String>,
#[arg( #[arg(
long = "force-dpd", long = "force-dpd",
help = "Same as the '--force-dpd' option in the openconnect command" help = "Same as the '--force-dpd' option in the openconnect command"
)] )]
dpd_interval: Option<u32>, dpd_interval: Option<u32>,
#[arg(
long = "no-xmlpost",
help = "Same as the '--no-xmlpost' option in the openconnect command"
)]
no_xmlpost: bool,
#[cfg(feature = "webview-auth")] #[cfg(feature = "webview-auth")]
#[arg(long, help = "The HiDPI mode, useful for high-resolution screens")] #[arg(long, help = "The HiDPI mode, useful for high-resolution screens")]
hidpi: bool, hidpi: bool,
@@ -390,7 +402,9 @@ impl<'a> ConnectHandler<'a> {
.mtu(mtu) .mtu(mtu)
.disable_ipv6(self.args.disable_ipv6) .disable_ipv6(self.args.disable_ipv6)
.no_dtls(self.args.no_dtls) .no_dtls(self.args.no_dtls)
.local_hostname(self.args.local_hostname.clone())
.dpd_interval(self.args.dpd_interval.unwrap_or(0)) .dpd_interval(self.args.dpd_interval.unwrap_or(0))
.no_xmlpost(self.args.no_xmlpost)
.build()?; .build()?;
let vpn = Arc::new(vpn); let vpn = Arc::new(vpn);
+4 -1
View File
@@ -9,7 +9,7 @@ use gpapi::{
}; };
use log::{info, warn}; use log::{info, warn};
use openconnect::Vpn; use openconnect::Vpn;
use tokio::sync::{mpsc, oneshot, watch, RwLock}; use tokio::sync::{RwLock, mpsc, oneshot, watch};
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
pub(crate) struct VpnTaskContext { pub(crate) struct VpnTaskContext {
@@ -54,6 +54,9 @@ impl VpnTaskContext {
.mtu(args.mtu()) .mtu(args.mtu())
.disable_ipv6(args.disable_ipv6()) .disable_ipv6(args.disable_ipv6())
.no_dtls(args.no_dtls()) .no_dtls(args.no_dtls())
.local_hostname(args.local_hostname())
.dpd_interval(args.force_dpd())
.no_xmlpost(args.no_xmlpost())
.build() .build()
{ {
Ok(vpn) => vpn, Ok(vpn) => vpn,
+33
View File
@@ -49,6 +49,9 @@ pub struct ConnectArgs {
mtu: u32, mtu: u32,
disable_ipv6: bool, disable_ipv6: bool,
no_dtls: bool, no_dtls: bool,
local_hostname: Option<String>,
force_dpd: u32,
no_xmlpost: bool,
} }
impl ConnectArgs { impl ConnectArgs {
@@ -70,6 +73,9 @@ impl ConnectArgs {
mtu: 0, mtu: 0,
disable_ipv6: false, disable_ipv6: false,
no_dtls: 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 { pub fn no_dtls(&self) -> bool {
self.no_dtls self.no_dtls
} }
pub fn local_hostname(&self) -> Option<String> {
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)] #[derive(Debug, Deserialize, Serialize, Type)]
@@ -227,6 +245,21 @@ impl ConnectRequest {
self self
} }
pub fn with_local_hostname<T: Into<Option<String>>>(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 { pub fn gateway(&self) -> &Gateway {
self.info.gateway() self.info.gateway()
} }
+2
View File
@@ -15,6 +15,7 @@ pub(crate) struct ConnectOptions {
pub os: *const c_char, pub os: *const c_char,
pub os_version: *const c_char, pub os_version: *const c_char,
pub client_version: *const c_char, pub client_version: *const c_char,
pub local_hostname: *const c_char,
pub script: *const c_char, pub script: *const c_char,
pub interface: *const c_char, pub interface: *const c_char,
@@ -34,6 +35,7 @@ pub(crate) struct ConnectOptions {
pub no_dtls: u32, pub no_dtls: u32,
pub dpd_interval: u32, pub dpd_interval: u32,
pub no_xmlpost: u32,
} }
#[link(name = "vpn")] #[link(name = "vpn")]
+17 -4
View File
@@ -60,6 +60,7 @@ int vpn_connect(const vpn_options *options, vpn_connected_callback callback)
{ {
struct openconnect_info *vpninfo; struct openconnect_info *vpninfo;
struct utsname utsbuf; struct utsname utsbuf;
const char *effective_local_hostname = NULL;
g_user_data = options->user_data; g_user_data = options->user_data;
g_vpnc_script = options->script; 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("DISABLE_IPV6: %d", options->disable_ipv6);
INFO("NO_DTLS: %d", options->no_dtls); INFO("NO_DTLS: %d", options->no_dtls);
INFO("DPD_INTERVAL: %d", options->dpd_interval); INFO("DPD_INTERVAL: %d", options->dpd_interval);
INFO("NO_XMLPOST: %d", options->no_xmlpost);
vpninfo = vpninfo =
openconnect_vpninfo_new(options->user_agent, validate_peer_cert, 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); 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) { if (options->certificate) {
INFO("Setting client certificate: %s", options->certificate); INFO("Setting client certificate: %s", options->certificate);
openconnect_set_client_cert(vpninfo, 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); openconnect_set_key_password(vpninfo, options->key_password);
} }
if (options->no_xmlpost) {
openconnect_set_xmlpost(vpninfo, 0);
}
if (options->csd_wrapper) { if (options->csd_wrapper) {
openconnect_setup_csd(vpninfo, options->csd_uid, 1, openconnect_setup_csd(vpninfo, options->csd_uid, 1,
options->csd_wrapper); options->csd_wrapper);
@@ -143,10 +160,6 @@ int vpn_connect(const vpn_options *options, vpn_connected_callback callback)
return 1; return 1;
} }
if (!uname(&utsbuf)) {
openconnect_set_localname(vpninfo, utsbuf.nodename);
}
// Essential step // Essential step
if (openconnect_make_cstp_connection(vpninfo) != 0) { if (openconnect_make_cstp_connection(vpninfo) != 0) {
ERROR("openconnect_make_cstp_connection failed"); ERROR("openconnect_make_cstp_connection failed");
+2
View File
@@ -15,6 +15,7 @@ typedef struct vpn_options {
const char *os; const char *os;
const char *os_version; const char *os_version;
const char *client_version; const char *client_version;
const char *local_hostname;
const char *script; const char *script;
const char *interface; const char *interface;
@@ -34,6 +35,7 @@ typedef struct vpn_options {
const int no_dtls; const int no_dtls;
const int dpd_interval; const int dpd_interval;
const int no_xmlpost;
} vpn_options; } vpn_options;
int vpn_connect(const vpn_options *options, vpn_connected_callback callback); int vpn_connect(const vpn_options *options, vpn_connected_callback callback);
+21 -1
View File
@@ -1,5 +1,5 @@
use std::{ use std::{
ffi::{c_char, CString}, ffi::{CString, c_char},
fmt, fmt,
sync::{Arc, RwLock}, sync::{Arc, RwLock},
}; };
@@ -19,6 +19,7 @@ pub struct Vpn {
os: CString, os: CString,
os_version: Option<CString>, os_version: Option<CString>,
client_version: Option<CString>, client_version: Option<CString>,
local_hostname: Option<CString>,
script: CString, script: CString,
interface: Option<CString>, interface: Option<CString>,
@@ -38,6 +39,7 @@ pub struct Vpn {
no_dtls: bool, no_dtls: bool,
dpd_interval: u32, dpd_interval: u32,
no_xmlpost: bool,
callback: OnConnectedCallback, callback: OnConnectedCallback,
} }
@@ -77,6 +79,7 @@ impl Vpn {
os: self.os.as_ptr(), os: self.os.as_ptr(),
os_version: Self::option_to_ptr(&self.os_version), os_version: Self::option_to_ptr(&self.os_version),
client_version: Self::option_to_ptr(&self.client_version), client_version: Self::option_to_ptr(&self.client_version),
local_hostname: Self::option_to_ptr(&self.local_hostname),
script: self.script.as_ptr(), script: self.script.as_ptr(),
interface: Self::option_to_ptr(&self.interface), interface: Self::option_to_ptr(&self.interface),
@@ -95,6 +98,7 @@ impl Vpn {
disable_ipv6: self.disable_ipv6 as u32, disable_ipv6: self.disable_ipv6 as u32,
no_dtls: self.no_dtls as u32, no_dtls: self.no_dtls as u32,
dpd_interval: self.dpd_interval, dpd_interval: self.dpd_interval,
no_xmlpost: self.no_xmlpost as u32,
} }
} }
@@ -136,6 +140,7 @@ pub struct VpnBuilder {
os: Option<String>, os: Option<String>,
os_version: Option<String>, os_version: Option<String>,
client_version: Option<String>, client_version: Option<String>,
local_hostname: Option<String>,
certificate: Option<String>, certificate: Option<String>,
sslkey: Option<String>, sslkey: Option<String>,
@@ -151,6 +156,7 @@ pub struct VpnBuilder {
no_dtls: bool, no_dtls: bool,
dpd_interval: u32, dpd_interval: u32,
no_xmlpost: bool,
} }
impl VpnBuilder { impl VpnBuilder {
@@ -166,6 +172,7 @@ impl VpnBuilder {
os: None, os: None,
os_version: None, os_version: None,
client_version: None, client_version: None,
local_hostname: None,
certificate: None, certificate: None,
sslkey: None, sslkey: None,
@@ -180,6 +187,7 @@ impl VpnBuilder {
disable_ipv6: false, disable_ipv6: false,
no_dtls: false, no_dtls: false,
dpd_interval: 0, dpd_interval: 0,
no_xmlpost: false,
} }
} }
@@ -218,6 +226,11 @@ impl VpnBuilder {
self self
} }
pub fn local_hostname<T: Into<Option<String>>>(mut self, local_hostname: T) -> Self {
self.local_hostname = local_hostname.into();
self
}
pub fn certificate<T: Into<Option<String>>>(mut self, certificate: T) -> Self { pub fn certificate<T: Into<Option<String>>>(mut self, certificate: T) -> Self {
self.certificate = certificate.into(); self.certificate = certificate.into();
self self
@@ -273,6 +286,11 @@ impl VpnBuilder {
self self
} }
pub fn no_xmlpost(mut self, no_xmlpost: bool) -> Self {
self.no_xmlpost = no_xmlpost;
self
}
fn determine_script(&self) -> Result<&str, VpnError> { fn determine_script(&self) -> Result<&str, VpnError> {
match &self.script { match &self.script {
Some(script) => { Some(script) => {
@@ -315,6 +333,7 @@ impl VpnBuilder {
os: Self::to_cstring(&os), os: Self::to_cstring(&os),
os_version: self.os_version.as_deref().map(Self::to_cstring), os_version: self.os_version.as_deref().map(Self::to_cstring),
client_version: self.client_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), script: Self::to_cstring(&script),
interface: self.interface.as_deref().map(Self::to_cstring), interface: self.interface.as_deref().map(Self::to_cstring),
@@ -333,6 +352,7 @@ impl VpnBuilder {
disable_ipv6: self.disable_ipv6, disable_ipv6: self.disable_ipv6,
no_dtls: self.no_dtls, no_dtls: self.no_dtls,
dpd_interval: self.dpd_interval, dpd_interval: self.dpd_interval,
no_xmlpost: self.no_xmlpost,
callback: Default::default(), callback: Default::default(),
}) })