feat: add brokered macOS service mode

This commit is contained in:
Kevin Yue committed 2026-07-16 23:16:58 +08:00
1 parent ce97729984
commit 9c1428fb72
21 files changed
+642 -1426

No files matched your search

+90 -2
View File
@@ -1,7 +1,8 @@
use std::collections::HashMap;
use std::{collections::HashMap, fmt};
use serde::{Deserialize, Serialize};
use specta::Type;
use zeroize::Zeroize;
use crate::{
gateway::Gateway,
@@ -10,6 +11,8 @@ use crate::{
use super::vpn_state::ConnectInfo;
pub const MAX_CLIENT_IDENTITY_DATA: usize = 32 * 1024;
#[derive(Debug, Deserialize, Serialize)]
pub struct LaunchGuiRequest {
user: String,
@@ -30,7 +33,7 @@ impl LaunchGuiRequest {
}
}
#[derive(Debug, Deserialize, Serialize, Type, Clone)]
#[derive(Deserialize, Serialize, Type, Clone)]
pub struct ConnectArgs {
cookie: String,
vpnc_script: Option<String>,
@@ -43,6 +46,8 @@ pub struct ConnectArgs {
certificate: Option<String>,
sslkey: Option<String>,
certificate_data: Option<String>,
sslkey_data: Option<String>,
key_password: Option<String>,
hip: bool,
@@ -72,6 +77,8 @@ impl ConnectArgs {
host_id: None,
certificate: None,
sslkey: None,
certificate_data: None,
sslkey_data: None,
key_password: None,
hip: false,
csd_uid: 0,
@@ -127,6 +134,22 @@ impl ConnectArgs {
self.sslkey.clone()
}
pub fn certificate_data(&self) -> anyhow::Result<Option<Vec<u8>>> {
self
.certificate_data
.as_deref()
.map(crate::utils::base64::decode_to_vec)
.transpose()
}
pub fn sslkey_data(&self) -> anyhow::Result<Option<Vec<u8>>> {
self
.sslkey_data
.as_deref()
.map(crate::utils::base64::decode_to_vec)
.transpose()
}
pub fn key_password(&self) -> Option<String> {
self.key_password.clone()
}
@@ -176,6 +199,40 @@ impl ConnectArgs {
}
}
impl fmt::Debug for ConnectArgs {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ConnectArgs")
.field("cookie", &"<redacted>")
.field("vpnc_script", &self.vpnc_script)
.field(
"client_auth_path",
&(self.certificate.is_some() || self.sslkey.is_some()),
)
.field(
"client_auth_data",
&(self.certificate_data.is_some() || self.sslkey_data.is_some()),
)
.field("hip", &self.hip)
.field("csd_uid", &self.csd_uid)
.field("csd_wrapper", &self.csd_wrapper)
.field("reconnect_timeout", &self.reconnect_timeout)
.field("mtu", &self.mtu)
.field("disable_ipv6", &self.disable_ipv6)
.field("no_dtls", &self.no_dtls)
.field("allow_extend_session", &self.allow_extend_session)
.finish_non_exhaustive()
}
}
impl Drop for ConnectArgs {
fn drop(&mut self) {
self.cookie.zeroize();
self.key_password.zeroize();
self.certificate_data.zeroize();
self.sslkey_data.zeroize();
}
}
#[derive(Debug, Deserialize, Serialize, Type, Clone)]
pub struct ConnectRequest {
info: ConnectInfo,
@@ -229,6 +286,16 @@ impl ConnectRequest {
self
}
pub fn with_certificate_data(mut self, certificate: Option<Vec<u8>>) -> Self {
self.args.certificate_data = certificate.map(encode_secret);
self
}
pub fn with_sslkey_data(mut self, sslkey: Option<Vec<u8>>) -> Self {
self.args.sslkey_data = sslkey.map(encode_secret);
self
}
pub fn with_key_password<T: Into<Option<String>>>(mut self, key_password: T) -> Self {
self.args.key_password = key_password.into();
self
@@ -287,6 +354,12 @@ impl ConnectRequest {
}
}
fn encode_secret(mut data: Vec<u8>) -> String {
let encoded = crate::utils::base64::encode(&data);
data.zeroize();
encoded
}
#[derive(Debug, Deserialize, Serialize, Type)]
pub struct DisconnectRequest;
@@ -351,4 +424,19 @@ mod tests {
Some(profile.host_identity().host_id().to_string())
);
}
#[test]
fn client_identity_data_round_trips_without_debug_disclosure() {
let req = ConnectRequest::new(test_connect_info(), "secret-cookie".to_string())
.with_certificate_data(Some(b"certificate".to_vec()))
.with_sslkey_data(Some(b"private-key".to_vec()));
let encoded = serde_json::to_vec(&req).unwrap();
let decoded: ConnectRequest = serde_json::from_slice(&encoded).unwrap();
assert_eq!(decoded.args().certificate_data().unwrap().unwrap(), b"certificate");
assert_eq!(decoded.args().sslkey_data().unwrap().unwrap(), b"private-key");
let debug = format!("{decoded:?}");
assert!(!debug.contains("secret-cookie"));
assert!(!debug.contains("private-key"));
}
}
@@ -115,6 +115,14 @@ pub(crate) fn decode<T: for<'de> Deserialize<'de>>(encoded: &[u8]) -> Result<T,
#[cfg(test)]
mod tests {
use crate::{
gateway::Gateway,
service::{
request::{ConnectRequest, MAX_CLIENT_IDENTITY_DATA, WsRequest},
vpn_state::ConnectInfo,
},
};
use super::*;
#[test]
@@ -137,4 +145,21 @@ mod tests {
);
assert!(serde_json::from_str::<ServiceResult>(&oversized).is_err());
}
#[test]
fn maximum_client_identity_fits_one_transport_message() {
let gateway = Gateway::new("Gateway".to_string(), "vpn.example.com".to_string());
let info = ConnectInfo::new("portal.example.com".to_string(), gateway.clone(), vec![gateway]);
let certificate_size = MAX_CLIENT_IDENTITY_DATA / 2;
let request = ConnectRequest::new(info, "cookie".to_string())
.with_certificate_data(Some(vec![b'c'; certificate_size]))
.with_sslkey_data(Some(vec![b'k'; MAX_CLIENT_IDENTITY_DATA - certificate_size]));
let message = ClientMessage::Request {
id: Uuid::new_v4(),
request: WsRequest::Connect(Box::new(request)),
};
let encoded = encode(&message).unwrap();
assert!(encoded.len() <= MAX_PLAINTEXT);
}
}
+4
View File
@@ -88,6 +88,7 @@ mod tests {
}
#[test]
#[cfg(not(target_os = "macos"))]
fn create_identity_from_pem_with_passphrase() {
let cert = "tests/files/badssl.com-client.pem";
let passphrase = "badssl.com";
@@ -98,6 +99,7 @@ mod tests {
}
#[test]
#[cfg(not(target_os = "macos"))]
fn create_identity_from_pem_unencrypted_key() {
let cert = "tests/files/badssl.com-client-unencrypted.pem";
let identity = create_identity_from_pem(cert, None, None);
@@ -107,6 +109,7 @@ mod tests {
}
#[test]
#[cfg(not(target_os = "macos"))]
fn create_identity_from_pem_cert_and_encrypted_key() {
let cert = "tests/files/badssl.com-client.pem";
let key = "tests/files/badssl.com-client.pem";
@@ -129,6 +132,7 @@ mod tests {
}
#[test]
#[cfg(not(target_os = "macos"))]
fn create_identity_from_pem_cert_and_unencrypted_key() {
let cert = "tests/files/badssl.com-client.pem";
let key = "tests/files/badssl.com-client-unencrypted.pem";