mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2026-11-19 06:26:14 -05:00
feat: add portable GlobalProtect APIs and iOS tunnel support
This commit is contained in:
112 files changed
+3864
-3011
No files matched your search
@@ -44,6 +44,7 @@ pub(crate) struct ConnectOptions {
|
||||
pub interface: *const c_char,
|
||||
pub script_tun: u32,
|
||||
pub script_tun_launch: *const ScriptTunLaunchRaw,
|
||||
pub packet_fd: c_int,
|
||||
|
||||
pub certificate: *const c_char,
|
||||
pub sslkey: *const c_char,
|
||||
|
||||
@@ -52,9 +52,13 @@ static void observe_statistics(void *private_data, const struct oc_stats *stats)
|
||||
}
|
||||
|
||||
/* Validate the peer certificate */
|
||||
static int validate_peer_cert(__attribute__((unused)) void *_vpninfo,
|
||||
static int validate_peer_cert(void *private_data,
|
||||
const char *reason)
|
||||
{
|
||||
struct vpn_context *context = private_data;
|
||||
if (context->options->servercert)
|
||||
return openconnect_check_peer_cert_hash(context->vpninfo,
|
||||
context->options->servercert);
|
||||
INFO("Accepting the server certificate though %s", reason);
|
||||
return 0;
|
||||
}
|
||||
@@ -82,7 +86,10 @@ static void setup_tun_handler(void *private_data)
|
||||
struct openconnect_info *_vpninfo = context->vpninfo;
|
||||
const vpn_options *options = context->options;
|
||||
int ret;
|
||||
if (options->script_tun) {
|
||||
if (options->packet_fd >= 0) {
|
||||
int fd = dup(options->packet_fd);
|
||||
ret = fd < 0 ? -errno : openconnect_setup_tun_fd_with_config(_vpninfo, fd);
|
||||
} else if (options->script_tun) {
|
||||
const struct openconnect_script_tun_launch *launch = options->script_tun_launch;
|
||||
if (launch) {
|
||||
ret = openconnect_setup_tun_script_exec(_vpninfo, launch);
|
||||
@@ -156,6 +163,9 @@ int vpn_connect(const vpn_options *options, vpn_connected_callback callback)
|
||||
}
|
||||
|
||||
context.vpninfo = vpninfo;
|
||||
/* A supplied pin is authoritative even when the system would trust a CA. */
|
||||
if (options->servercert)
|
||||
openconnect_set_system_trust(vpninfo, 0);
|
||||
openconnect_set_tunnel_handler(vpninfo, observe_tunnel);
|
||||
openconnect_set_reconnected_handler(vpninfo, observe_reconnected);
|
||||
openconnect_set_stats_handler(vpninfo, observe_statistics);
|
||||
|
||||
@@ -48,6 +48,7 @@ typedef struct vpn_options {
|
||||
const char *interface;
|
||||
const int script_tun;
|
||||
const struct openconnect_script_tun_launch *script_tun_launch;
|
||||
const int packet_fd;
|
||||
|
||||
const char *certificate;
|
||||
const char *sslkey;
|
||||
|
||||
@@ -151,6 +151,7 @@ pub struct Vpn {
|
||||
interface: Option<CString>,
|
||||
script_tun: bool,
|
||||
script_tun_launch: Option<ScriptTunLaunch>,
|
||||
packet_endpoint: Option<OwnedFd>,
|
||||
|
||||
certificate: Option<CString>,
|
||||
sslkey: Option<CString>,
|
||||
@@ -314,6 +315,7 @@ impl Vpn {
|
||||
interface: Self::option_to_ptr(&self.interface),
|
||||
script_tun: self.script_tun as u32,
|
||||
script_tun_launch: std::ptr::null(),
|
||||
packet_fd: self.packet_endpoint.as_ref().map_or(-1, AsRawFd::as_raw_fd),
|
||||
|
||||
certificate: Self::option_to_ptr(&self.certificate),
|
||||
sslkey: Self::option_to_ptr(&self.sslkey),
|
||||
@@ -382,6 +384,7 @@ pub struct VpnBuilder {
|
||||
interface: Option<String>,
|
||||
script_tun: bool,
|
||||
script_tun_launch: Option<ScriptTunLaunch>,
|
||||
packet_endpoint: Option<OwnedFd>,
|
||||
|
||||
user_agent: Option<String>,
|
||||
os: Option<String>,
|
||||
@@ -393,6 +396,7 @@ pub struct VpnBuilder {
|
||||
certificate: Option<String>,
|
||||
sslkey: Option<String>,
|
||||
key_password: Option<String>,
|
||||
servercert: Option<String>,
|
||||
|
||||
hip_source: Arc<HipSource>,
|
||||
|
||||
@@ -417,6 +421,7 @@ impl VpnBuilder {
|
||||
interface: None,
|
||||
script_tun: false,
|
||||
script_tun_launch: None,
|
||||
packet_endpoint: None,
|
||||
|
||||
user_agent: None,
|
||||
os: None,
|
||||
@@ -428,6 +433,7 @@ impl VpnBuilder {
|
||||
certificate: None,
|
||||
sslkey: None,
|
||||
key_password: None,
|
||||
servercert: None,
|
||||
|
||||
hip_source: Arc::new(HipSource::Disabled),
|
||||
|
||||
@@ -441,6 +447,16 @@ impl VpnBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
/// Use an application-owned packet descriptor. OpenConnect borrows a duplicate
|
||||
/// during connect; this owner remains alive until the Vpn is dropped. Apple/BSD
|
||||
/// endpoints use the native four-byte, network-order address-family prefix.
|
||||
/// Requires callback configuration and a SHA-256 server public-key pin.
|
||||
pub fn packet_endpoint(mut self, endpoint: OwnedFd, server_pin: String) -> Self {
|
||||
self.packet_endpoint = Some(endpoint);
|
||||
self.servercert = Some(server_pin);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn script<T: Into<Option<String>>>(mut self, script: T) -> Self {
|
||||
self.script = script.into();
|
||||
self.script_is_path = false;
|
||||
@@ -618,6 +634,24 @@ impl VpnBuilder {
|
||||
}
|
||||
|
||||
pub fn build(self) -> Result<Vpn, VpnError> {
|
||||
if self.packet_endpoint.is_some() {
|
||||
let pin = self.servercert.as_deref().unwrap_or_default();
|
||||
if self.network_configuration_handler.is_none()
|
||||
|| self.interface.is_some()
|
||||
|| self.script.is_some()
|
||||
|| self.script_tun
|
||||
|| self.script_tun_launch.is_some()
|
||||
{
|
||||
return Err(VpnError::new(
|
||||
"Packet endpoint requires callback configuration and no device/script options".into(),
|
||||
));
|
||||
}
|
||||
if !pin.starts_with("pin-sha256:") || pin.len() != 55 || pin.contains('\0') {
|
||||
return Err(VpnError::new(
|
||||
"Packet endpoint requires a pin-sha256 server public-key fingerprint".into(),
|
||||
));
|
||||
}
|
||||
}
|
||||
if self.script_tun_launch.is_some() && !self.script_tun {
|
||||
return Err(VpnError::new("Child identity requires script-tun".into()));
|
||||
}
|
||||
@@ -643,11 +677,12 @@ impl VpnBuilder {
|
||||
interface: self.interface.as_deref().map(Self::to_cstring),
|
||||
script_tun: self.script_tun,
|
||||
script_tun_launch: self.script_tun_launch,
|
||||
packet_endpoint: self.packet_endpoint,
|
||||
|
||||
certificate: self.certificate.as_deref().map(Self::to_cstring),
|
||||
sslkey: self.sslkey.as_deref().map(Self::to_cstring),
|
||||
key_password: self.key_password.as_deref().map(Self::to_cstring),
|
||||
servercert: None,
|
||||
servercert: self.servercert.as_deref().map(Self::to_cstring),
|
||||
|
||||
hip_source: self.hip_source,
|
||||
|
||||
@@ -775,6 +810,7 @@ mod tests {
|
||||
interface: None,
|
||||
script_tun: false,
|
||||
script_tun_launch: None,
|
||||
packet_endpoint: None,
|
||||
certificate: None,
|
||||
sslkey: None,
|
||||
key_password: None,
|
||||
|
||||
Reference in new issue
Block a user