mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2026-11-18 22:16:18 -05:00
feat: add split-DNS support to VPN connection options
This commit is contained in:
5 files changed
+146
-1
No files matched your search
@@ -109,6 +109,9 @@ pub(crate) struct ConnectArgs {
|
||||
#[arg(long, help = "Disable DTLS and ESP")]
|
||||
no_dtls: bool,
|
||||
|
||||
#[arg(long, help = "Comma-separated split-DNS domains to pass to the VPNC script")]
|
||||
split_dns: Option<String>,
|
||||
|
||||
#[arg(
|
||||
long = "force-dpd",
|
||||
help = "Same as the '--force-dpd' option in the openconnect command"
|
||||
@@ -158,6 +161,24 @@ impl ConnectArgs {
|
||||
Os::Mac => host_utils::get_macos_os_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn normalized_split_dns(&self) -> anyhow::Result<Option<String>> {
|
||||
let Some(split_dns) = self.split_dns.as_deref() else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
let domains = split_dns
|
||||
.split(',')
|
||||
.map(str::trim)
|
||||
.map(str::to_owned)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
if domains.iter().any(|domain| domain.is_empty()) {
|
||||
bail!("The '--split-dns' option must contain a comma-separated list of non-empty domains");
|
||||
}
|
||||
|
||||
Ok(Some(domains.join(",")))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct ConnectHandler<'a> {
|
||||
@@ -256,6 +277,8 @@ impl<'a> ConnectHandler<'a> {
|
||||
}
|
||||
|
||||
pub(crate) async fn handle_impl(&self) -> anyhow::Result<()> {
|
||||
self.args.normalized_split_dns()?;
|
||||
|
||||
let server = self.args.server.as_str();
|
||||
let as_gateway = self.args.as_gateway;
|
||||
|
||||
@@ -365,6 +388,7 @@ impl<'a> ConnectHandler<'a> {
|
||||
|
||||
async fn connect_gateway(&self, gateway: &str, cookie: &str, client_version: Option<&str>) -> anyhow::Result<()> {
|
||||
let mtu = self.args.mtu.unwrap_or(0);
|
||||
let split_dns = self.args.normalized_split_dns()?;
|
||||
let (hip, csd_wrapper) = self.determine_hip_script();
|
||||
let hip_user = self.determine_hip_user();
|
||||
let csd_uid = get_uid(&hip_user)?;
|
||||
@@ -380,6 +404,7 @@ impl<'a> ConnectHandler<'a> {
|
||||
.os(Some(os))
|
||||
.os_version(Some(os_version))
|
||||
.client_version(client_version)
|
||||
.split_dns(split_dns)
|
||||
.certificate(self.args.certificate.clone())
|
||||
.sslkey(self.args.sslkey.clone())
|
||||
.key_password(self.latest_key_password.borrow().clone())
|
||||
@@ -562,3 +587,52 @@ fn get_uid(user: &Option<String>) -> anyhow::Result<u32> {
|
||||
get_non_root_user().map_or_else(|_| Ok(0), |user| Ok(user.uid()))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use clap::Parser;
|
||||
|
||||
use super::ConnectArgs;
|
||||
|
||||
#[derive(Parser)]
|
||||
struct TestCli {
|
||||
#[command(flatten)]
|
||||
args: ConnectArgs,
|
||||
}
|
||||
|
||||
fn parse_connect_args(args: &[&str]) -> ConnectArgs {
|
||||
TestCli::parse_from(args).args
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_split_dns_domains() {
|
||||
let args = parse_connect_args(&["gpclient", "example.com", "--split-dns", "corp.local,svc.local"]);
|
||||
|
||||
assert_eq!(
|
||||
args.normalized_split_dns().unwrap(),
|
||||
Some("corp.local,svc.local".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalizes_split_dns_whitespace() {
|
||||
let args = parse_connect_args(&["gpclient", "example.com", "--split-dns", " corp.local , svc.local "]);
|
||||
|
||||
assert_eq!(
|
||||
args.normalized_split_dns().unwrap(),
|
||||
Some("corp.local,svc.local".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_empty_split_dns_domains() {
|
||||
let args = parse_connect_args(&["gpclient", "example.com", "--split-dns", "corp.local,,svc.local"]);
|
||||
|
||||
let err = args.normalized_split_dns().unwrap_err();
|
||||
|
||||
assert_eq!(
|
||||
err.to_string(),
|
||||
"The '--split-dns' option must contain a comma-separated list of non-empty domains"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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 split_dns: *const c_char,
|
||||
|
||||
pub script: *const c_char,
|
||||
pub interface: *const c_char,
|
||||
|
||||
@@ -12,6 +12,7 @@ void *g_user_data;
|
||||
static int g_cmd_pipe_fd;
|
||||
static const char *g_vpnc_script;
|
||||
static const char *g_vpnc_interface;
|
||||
static const char *g_split_dns;
|
||||
static int g_script_tun;
|
||||
static vpn_connected_callback on_vpn_connected;
|
||||
|
||||
@@ -40,9 +41,19 @@ static void print_progress(__attribute__((unused)) void *_vpninfo, int level,
|
||||
}
|
||||
}
|
||||
|
||||
static void sync_split_dns_env()
|
||||
{
|
||||
if (g_split_dns && *g_split_dns) {
|
||||
setenv("CISCO_SPLIT_DNS", g_split_dns, 1);
|
||||
} else {
|
||||
unsetenv("CISCO_SPLIT_DNS");
|
||||
}
|
||||
}
|
||||
|
||||
static void setup_tun_handler(void *_vpninfo)
|
||||
{
|
||||
int ret;
|
||||
sync_split_dns_env();
|
||||
if (g_script_tun) {
|
||||
ret = openconnect_setup_tun_script(_vpninfo, g_vpnc_script);
|
||||
} else {
|
||||
@@ -64,6 +75,7 @@ int vpn_connect(const vpn_options *options, vpn_connected_callback callback)
|
||||
g_user_data = options->user_data;
|
||||
g_vpnc_script = options->script;
|
||||
g_vpnc_interface = options->interface;
|
||||
g_split_dns = options->split_dns;
|
||||
g_script_tun = options->script_tun;
|
||||
on_vpn_connected = callback;
|
||||
|
||||
@@ -166,6 +178,7 @@ int vpn_connect(const vpn_options *options, vpn_connected_callback callback)
|
||||
|
||||
if (ret) {
|
||||
INFO("openconnect_mainloop returned %d, exiting", ret);
|
||||
unsetenv("CISCO_SPLIT_DNS");
|
||||
openconnect_vpninfo_free(vpninfo);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ typedef struct vpn_options {
|
||||
const char *os;
|
||||
const char *os_version;
|
||||
const char *client_version;
|
||||
const char *split_dns;
|
||||
|
||||
const char *script;
|
||||
const char *interface;
|
||||
|
||||
@@ -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<CString>,
|
||||
client_version: Option<CString>,
|
||||
split_dns: Option<CString>,
|
||||
|
||||
script: CString,
|
||||
interface: Option<CString>,
|
||||
@@ -77,6 +78,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),
|
||||
split_dns: Self::option_to_ptr(&self.split_dns),
|
||||
|
||||
script: self.script.as_ptr(),
|
||||
interface: Self::option_to_ptr(&self.interface),
|
||||
@@ -136,6 +138,7 @@ pub struct VpnBuilder {
|
||||
os: Option<String>,
|
||||
os_version: Option<String>,
|
||||
client_version: Option<String>,
|
||||
split_dns: Option<String>,
|
||||
|
||||
certificate: Option<String>,
|
||||
sslkey: Option<String>,
|
||||
@@ -166,6 +169,7 @@ impl VpnBuilder {
|
||||
os: None,
|
||||
os_version: None,
|
||||
client_version: None,
|
||||
split_dns: None,
|
||||
|
||||
certificate: None,
|
||||
sslkey: None,
|
||||
@@ -218,6 +222,11 @@ impl VpnBuilder {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn split_dns<T: Into<Option<String>>>(mut self, split_dns: T) -> Self {
|
||||
self.split_dns = split_dns.into();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn certificate<T: Into<Option<String>>>(mut self, certificate: T) -> Self {
|
||||
self.certificate = certificate.into();
|
||||
self
|
||||
@@ -315,6 +324,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),
|
||||
split_dns: self.split_dns.as_deref().map(Self::to_cstring),
|
||||
|
||||
script: Self::to_cstring(&script),
|
||||
interface: self.interface.as_deref().map(Self::to_cstring),
|
||||
@@ -342,3 +352,49 @@ impl VpnBuilder {
|
||||
CString::new(value.to_string()).expect("Failed to convert to CString")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::{
|
||||
ffi::CStr,
|
||||
fs,
|
||||
time::{SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
|
||||
#[cfg(unix)]
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
|
||||
use super::Vpn;
|
||||
|
||||
#[cfg(unix)]
|
||||
fn create_executable_script() -> String {
|
||||
let nanos = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_nanos();
|
||||
let path = std::env::temp_dir().join(format!("gp-openconnect-test-{nanos}.sh"));
|
||||
|
||||
fs::write(&path, "#!/bin/sh\nexit 0\n").unwrap();
|
||||
|
||||
let mut permissions = fs::metadata(&path).unwrap().permissions();
|
||||
permissions.set_mode(0o755);
|
||||
fs::set_permissions(&path, permissions).unwrap();
|
||||
|
||||
path.to_string_lossy().into_owned()
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn split_dns_survives_into_connect_options() {
|
||||
let script = create_executable_script();
|
||||
let vpn = Vpn::builder("https://vpn.example.com", "cookie")
|
||||
.script(Some(script.clone()))
|
||||
.split_dns(Some("corp.local,svc.local".to_string()))
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let options = vpn.build_connect_options();
|
||||
let split_dns = unsafe { CStr::from_ptr(options.split_dns) }.to_str().unwrap();
|
||||
|
||||
assert_eq!(split_dns, "corp.local,svc.local");
|
||||
|
||||
fs::remove_file(script).unwrap();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user