feat: support the --script-tun option

This commit is contained in:
Kevin Yue committed 2025-12-02 19:54:09 +08:00
1 parent a917e16e62
commit a08ae655cf
5 files changed
+44 -18

No files matched your search

+19 -14
View File
@@ -5,12 +5,12 @@ use clap::Args;
use common::constants::GP_USER_AGENT;
use gpapi::{
auth::SamlAuthResult,
clap::{args::Os, ToVerboseArg},
clap::{ToVerboseArg, args::Os},
credential::{Credential, PasswordCredential},
error::PortalError,
gateway::{gateway_login, GatewayLogin},
gateway::{GatewayLogin, gateway_login},
gp_params::{ClientOs, GpParams},
portal::{prelogin, retrieve_config, Prelogin, StandardPrelogin},
portal::{Prelogin, StandardPrelogin, prelogin, retrieve_config},
process::{
auth_launcher::SamlAuthLauncher,
users::{get_non_root_user, get_user_by_name},
@@ -21,7 +21,7 @@ use inquire::{Password, PasswordDisplayMode, Select, Text};
use log::{info, warn};
use openconnect::Vpn;
use crate::{cli::SharedArgs, GP_CLIENT_LOCK_FILE};
use crate::{GP_CLIENT_LOCK_FILE, cli::SharedArgs};
#[derive(Args)]
pub(crate) struct ConnectArgs {
@@ -40,12 +40,15 @@ pub(crate) struct ConnectArgs {
#[arg(long, help = "Read the cookie from standard input")]
cookie_on_stdin: bool,
#[arg(long, short, help = "The VPNC script to use")]
#[arg(long, short, help = "The VPNC script to use", required_if_eq("script_tun", "true"))]
script: Option<String>,
#[arg(long, short, help = "The IFNAME for tunnel interface")]
interface: Option<String>,
#[arg(long, short = 'S', help = "Pass traffic to '--script' program, not tun")]
script_tun: bool,
#[arg(long, help = "Connect the server as a gateway, instead of a portal")]
as_gateway: bool,
@@ -115,7 +118,7 @@ pub(crate) struct ConnectArgs {
#[arg(
long,
help = "Use the specified browser to authenticate, e.g., `default`, `firefox`, `chrome`, `chromium`, `remote`, or the path to the browser executable. Use 'remote' for headless servers.",
help = "Use the specified browser to authenticate, e.g., `default`, `firefox`, `chrome`, `chromium`, `remote`.\nOr the path to the browser executable.\nUse 'remote' for headless servers.",
default_missing_value = "default",
num_args=0..=1
)]
@@ -124,11 +127,7 @@ pub(crate) struct ConnectArgs {
impl ConnectArgs {
fn default_os() -> Os {
if cfg!(target_os = "macos") {
Os::Mac
} else {
Os::Linux
}
if cfg!(target_os = "macos") { Os::Mac } else { Os::Linux }
}
fn os_version(&self) -> String {
@@ -326,6 +325,7 @@ impl<'a> ConnectHandler<'a> {
let vpn = Vpn::builder(gateway, cookie)
.script(self.args.script.clone())
.interface(self.args.interface.clone())
.script_tun(self.args.script_tun)
.user_agent(self.args.user_agent.clone())
.os(Some(os))
.certificate(self.args.certificate.clone())
@@ -373,7 +373,9 @@ impl<'a> ConnectHandler<'a> {
let browser = if prelogin.support_default_browser() {
self.args.browser.as_deref()
} else if !cfg!(feature = "webview-auth") {
bail!("The server does not support authentication via the default browser and the gpclient is not built with the `webview-auth` feature");
bail!(
"The server does not support authentication via the default browser and the gpclient is not built with the `webview-auth` feature"
);
} else {
None
};
@@ -465,8 +467,11 @@ fn read_cookie_from_stdin() -> anyhow::Result<Credential> {
fn write_pid_file() {
let pid = std::process::id();
fs::write(GP_CLIENT_LOCK_FILE, pid.to_string()).unwrap();
info!("Wrote PID {} to {}", pid, GP_CLIENT_LOCK_FILE);
if let Err(err) = fs::write(GP_CLIENT_LOCK_FILE, pid.to_string()) {
warn!("Failed to write PID file: {}", err);
} else {
info!("Wrote PID {} to {}", pid, GP_CLIENT_LOCK_FILE);
}
}
fn get_csd_uid(csd_user: &Option<String>) -> anyhow::Result<u32> {
+1
View File
@@ -13,6 +13,7 @@ pub(crate) struct ConnectOptions {
pub script: *const c_char,
pub interface: *const c_char,
pub script_tun: u32,
pub os: *const c_char,
pub certificate: *const c_char,
pub sslkey: *const c_char,
+13 -4
View File
@@ -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 int g_script_tun;
static vpn_connected_callback on_vpn_connected;
/* Validate the peer certificate */
@@ -41,8 +42,14 @@ static void print_progress(__attribute__((unused)) void *_vpninfo, int level,
static void setup_tun_handler(void *_vpninfo)
{
int ret = openconnect_setup_tun_device(_vpninfo, g_vpnc_script,
g_vpnc_interface);
int ret;
if (g_script_tun) {
ret = openconnect_setup_tun_script(_vpninfo, g_vpnc_script);
} else {
ret = openconnect_setup_tun_device(_vpninfo, g_vpnc_script,
g_vpnc_interface);
}
if (!ret) {
on_vpn_connected(g_cmd_pipe_fd, g_user_data);
}
@@ -58,11 +65,13 @@ 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_script_tun = options->script_tun;
on_vpn_connected = callback;
INFO("User agent: %s", options->user_agent);
INFO("VPNC script: %s", options->script);
INFO("USER_AGENT: %s", options->user_agent);
INFO("OS: %s", options->os);
INFO("VPNC_SCRIPT: %s", options->script);
INFO("SCRIPT_TUN: %d", g_script_tun);
INFO("CSD_USER: %d", options->csd_uid);
INFO("CSD_WRAPPER: %s", options->csd_wrapper);
INFO("RECONNECT_TIMEOUT: %d", options->reconnect_timeout);
+1
View File
@@ -13,6 +13,7 @@ typedef struct vpn_options {
const char *script;
const char *interface;
const int script_tun;
const char *os;
const char *certificate;
const char *sslkey;
+10
View File
@@ -17,6 +17,7 @@ pub struct Vpn {
user_agent: CString,
script: CString,
interface: Option<CString>,
script_tun: bool,
os: CString,
certificate: Option<CString>,
sslkey: Option<CString>,
@@ -69,6 +70,7 @@ impl Vpn {
user_agent: self.user_agent.as_ptr(),
script: self.script.as_ptr(),
interface: Self::option_to_ptr(&self.interface),
script_tun: self.script_tun as u32,
os: self.os.as_ptr(),
certificate: Self::option_to_ptr(&self.certificate),
@@ -119,6 +121,7 @@ pub struct VpnBuilder {
cookie: String,
script: Option<String>,
interface: Option<String>,
script_tun: bool,
user_agent: Option<String>,
os: Option<String>,
@@ -146,6 +149,7 @@ impl VpnBuilder {
cookie: cookie.to_string(),
script: None,
interface: None,
script_tun: false,
user_agent: None,
os: None,
@@ -176,6 +180,11 @@ impl VpnBuilder {
self
}
pub fn script_tun(mut self, script_tun: bool) -> Self {
self.script_tun = script_tun;
self
}
pub fn user_agent<T: Into<Option<String>>>(mut self, user_agent: T) -> Self {
self.user_agent = user_agent.into();
self
@@ -281,6 +290,7 @@ impl VpnBuilder {
user_agent: Self::to_cstring(&user_agent),
script: Self::to_cstring(&script),
interface: self.interface.as_deref().map(Self::to_cstring),
script_tun: self.script_tun,
os: Self::to_cstring(&os),
certificate: self.certificate.as_deref().map(Self::to_cstring),