feat: release GP Connect 3.3.0 with Native Shell support

This commit is contained in:
Kevin Yue committed 2026-11-05 23:05:14 +08:00
1 parent d12d677208
commit fd08cdbe66
226 files changed
+7700 -5225

No files matched your search

+107 -46
View File
@@ -14,7 +14,7 @@ use crate::{
gateway::{Gateway, parse_gateways},
gp_params::GpParams,
params,
session::{InternalSessionPolicy, network::InternalHostDetection},
session::{InternalHostDetection, InternalSessionPolicy},
utils::{normalize_server, parse_gp_response, redact::redact_form_params, remove_url_scheme, xml::ElementExt},
};
@@ -52,6 +52,37 @@ pub struct PortalConfig {
internal_session_policy: Option<InternalSessionPolicy>,
}
/// Parsed portal policy before a caller chooses network-location candidates.
pub struct PortalPolicy {
config: PortalConfig,
internal_gateways: Vec<Gateway>,
external_gateways: Vec<Gateway>,
}
impl PortalPolicy {
pub fn external_gateways(self) -> PortalConfig {
self.select_gateways(false, None)
}
pub(super) fn detection(&self) -> Option<&InternalHostDetection> {
self.config.internal_detection.as_ref()
}
pub(super) fn select_gateways(mut self, prefer_internal: bool, detected: Option<bool>) -> PortalConfig {
let mut gateways = if prefer_internal {
self.internal_gateways
} else {
self.external_gateways
};
if gateways.is_empty() {
gateways.push(Gateway::new(self.config.portal.clone(), self.config.portal.clone()));
}
self.config.gateways = gateways;
self.config.internal_host_detection = detected;
self.config
}
}
impl PortalConfig {
pub fn portal(&self) -> &str {
&self.portal
@@ -140,14 +171,13 @@ impl PortalConfig {
}
}
/// Cancellation is handled inside the request so a running native detection
/// helper is always cancelled and joined. Do not drop this future on disconnect.
/// Retrieve server policy without inspecting local network location.
pub async fn retrieve_config(
portal: &str,
cred: &Credential,
gp_params: &GpParams,
cancellation: &CancellationToken,
) -> anyhow::Result<PortalConfig> {
) -> anyhow::Result<PortalPolicy> {
let portal = normalize_server(portal)?;
let server_url = url::Url::parse(&portal)?;
let server = remove_url_scheme(&portal);
@@ -181,6 +211,9 @@ pub async fn retrieve_config(
if let Some(tls) = ServerTlsError::from_request(&server_url, &err) {
return Err(anyhow::anyhow!(tls));
}
if gp_params.os_profile().client_os() == crate::os_profile::ClientOs::Ios && err.is_authentication_rejection() {
return Err(anyhow::anyhow!(crate::error::AuthenticationRejected));
}
if err.status == StatusCode::NOT_FOUND {
bail!(PortalError::ConfigError("Config endpoint not found".to_string()));
}
@@ -216,11 +249,11 @@ pub async fn retrieve_config(
};
debug!("Portal CSC config response received: {} bytes", csc_xml.len());
let root = Element::parse(csc_xml.as_bytes()).map_err(|e| PortalError::ConfigError(e.to_string()))?;
return parse_portal_config(&server, cred, root, cancellation).await;
return parse_portal_policy(&server, cred, root, cancellation).await;
}
info!("Portal did not return CSC criteria");
parse_portal_config(&server, cred, root, cancellation).await
parse_portal_policy(&server, cred, root, cancellation).await
}
async fn retrieve_csc_config(
@@ -287,12 +320,12 @@ fn present(value: Option<&str>) -> &'static str {
}
}
async fn parse_portal_config(
async fn parse_portal_policy(
server: &str,
cred: &Credential,
root: Element,
cancellation: &CancellationToken,
) -> anyhow::Result<PortalConfig> {
) -> anyhow::Result<PortalPolicy> {
let internal_detection = root
.descendant("internal-host-detection")
.map(parse_internal_detection)
@@ -302,22 +335,11 @@ async fn parse_portal_config(
None
})
.flatten();
let prefer_internal = match &internal_detection {
Some(detection) => detection.detect(cancellation).await.unwrap_or_else(|error| {
warn!("Internal host detection failed: {error}");
false
}),
None => false,
};
if cancellation.is_cancelled() {
bail!("Portal configuration cancelled");
}
let mut gateways = parse_gateways(&root, prefer_internal).unwrap_or_else(|| {
info!("No gateways found in portal config");
vec![]
});
let internal_gateways = parse_gateways(&root, true).unwrap_or_default();
let external_gateways = parse_gateways(&root, false).unwrap_or_default();
let user_auth_cookie = root.descendant_text("portal-userauthcookie").unwrap_or_default();
let prelogon_user_auth_cookie = root
@@ -326,31 +348,29 @@ async fn parse_portal_config(
let config_digest = root.descendant_text("config-digest");
let connect_method = parse_connect_method(&root);
if gateways.is_empty() {
gateways.push(Gateway::new(server.to_string(), server.to_string()));
} else {
info!("Found {} gateways in portal config", gateways.len());
}
let version = root.descendant_text("version").map(|s| s.to_string());
info!("Detected portal version: {:?}", version);
let allow_extend_session = parse_allow_extend_session(&root);
let default_browser = parse_default_browser(&root);
let internal_session_policy = parse_internal_session_policy(&root).ok();
Ok(PortalConfig {
portal: server.to_string(),
auth_cookie: AuthCookieCredential::new(cred.username(), &user_auth_cookie, &prelogon_user_auth_cookie),
config_cred: cred.clone(),
gateways,
connect_method,
config_digest: config_digest.map(|s| s.to_string()),
internal_host_detection: internal_detection.as_ref().map(|_| prefer_internal),
internal_detection,
version,
allow_extend_session,
default_browser,
internal_session_policy,
Ok(PortalPolicy {
internal_gateways,
external_gateways,
config: PortalConfig {
portal: server.to_string(),
auth_cookie: AuthCookieCredential::new(cred.username(), &user_auth_cookie, &prelogon_user_auth_cookie),
config_cred: cred.clone(),
gateways: Vec::new(),
connect_method,
config_digest: config_digest.map(|s| s.to_string()),
internal_host_detection: None,
internal_detection,
version,
allow_extend_session,
default_browser,
internal_session_policy,
},
})
}
@@ -431,15 +451,54 @@ fn parse_internal_detection(element: &Element) -> anyhow::Result<Option<Internal
mod tests {
use super::*;
#[tokio::test]
async fn parsed_policy_keeps_location_selection_explicit() {
let xml = "<policy><internal-host-detection><ip-address>192.0.2.1</ip-address><host>inside.example</host></internal-host-detection><gateways><internal><list><entry name='internal.example'/></list></internal><external><list><entry name='external.example'/></list></external></gateways></policy>";
let credential = crate::credential::PasswordCredential::new("alice", "password").into();
let policy = parse_portal_policy("portal.example", &credential, parse_xml(xml), &CancellationToken::new())
.await
.unwrap();
assert!(policy.detection().is_some());
let external = policy.external_gateways();
assert_eq!(external.gateways()[0].server(), "external.example");
assert_eq!(external.internal_host_detection(), None);
let policy = parse_portal_policy("portal.example", &credential, parse_xml(xml), &CancellationToken::new())
.await
.unwrap();
let internal = policy.select_gateways(true, Some(true));
assert_eq!(internal.gateways()[0].server(), "internal.example");
assert_eq!(internal.internal_host_detection(), Some(true));
}
#[tokio::test]
async fn empty_candidates_preserve_portal_gateway_fallback() {
let credential = crate::credential::PasswordCredential::new("alice", "password").into();
for detected in [None, Some(false), Some(true)] {
let policy = parse_portal_policy(
"portal.example",
&credential,
parse_xml("<policy/>"),
&CancellationToken::new(),
)
.await
.unwrap();
let config = policy.select_gateways(detected.unwrap_or(false), detected);
assert_eq!(config.gateways().len(), 1);
assert_eq!(config.gateways()[0].server(), "portal.example");
assert_eq!(config.internal_host_detection(), detected);
}
}
#[tokio::test]
async fn invalid_non_tunnel_policy_does_not_reject_tunnel_candidates() {
let root = parse_xml(
"<policy><hip-collection><hip-report-interval>invalid</hip-report-interval></hip-collection><internal-host-detection><ip-address>invalid</ip-address><host>internal.example</host></internal-host-detection><gateways><external><list><entry name='vpn.example'/></list></external></gateways></policy>",
);
let credential = crate::credential::PasswordCredential::new("alice", "password").into();
let config = parse_portal_config("portal.example", &credential, root, &CancellationToken::new())
let config = parse_portal_policy("portal.example", &credential, root, &CancellationToken::new())
.await
.unwrap();
.unwrap()
.external_gateways();
assert!(config.internal_session_policy().is_none());
assert_eq!(config.gateways()[0].server(), "vpn.example");
assert_eq!(config.gateways()[0].kind(), crate::gateway::GatewayKind::External);
@@ -482,9 +541,10 @@ mod tests {
"fixture-user",
"fixture-password",
));
let config = parse_portal_config("vpn.example.com", &cred, root, &CancellationToken::new())
let config = parse_portal_policy("vpn.example.com", &cred, root, &CancellationToken::new())
.await
.unwrap();
.unwrap()
.external_gateways();
assert_eq!(
config.internal_session_policy().unwrap().hip_interval(),
std::time::Duration::from_secs(3600)
@@ -567,9 +627,10 @@ mod tests {
);
let cred = Credential::from(crate::credential::PasswordCredential::new("alice", "secret"));
let config = parse_portal_config("vpn.example.com", &cred, root, &CancellationToken::new())
let config = parse_portal_policy("vpn.example.com", &cred, root, &CancellationToken::new())
.await
.unwrap();
.unwrap()
.external_gateways();
assert_eq!(config.auth_cookie().user_auth_cookie(), "user-cookie");
assert_eq!(config.auth_cookie().prelogon_user_auth_cookie(), "prelogon-cookie");
+2 -2
View File
@@ -480,13 +480,13 @@ mod tests {
fn gp_params(client_os: ClientOs) -> GpParams {
let profile = OsProfile::builder(client_os)
.host_identity(HostIdentity::new(
.host_identity(HostIdentity::from_parts(
"test-computer".to_string(),
"host-id".to_string(),
"serial".to_string(),
"aa:bb:cc:dd:ee:ff".to_string(),
))
.build();
.build(crate::os_profile::test_snapshot());
GpParams::builder(profile).build()
}
+29
View File
@@ -0,0 +1,29 @@
//! Desktop portal discovery: protocol retrieval followed by native location detection.
use super::{PortalConfig, retrieve_config};
use crate::{credential::Credential, gp_params::GpParams};
use tokio_util::sync::CancellationToken;
/// Do not drop this future on disconnect: native detection must cancel and join.
pub async fn discover(
portal: &str,
credential: &Credential,
params: &GpParams,
cancellation: &CancellationToken,
) -> anyhow::Result<PortalConfig> {
let policy = retrieve_config(portal, credential, params, cancellation).await?;
let detected = match policy.detection() {
Some(detection) => Some(
crate::session::desktop::network::detect(detection, cancellation)
.await
.unwrap_or_else(|error| {
log::warn!("Internal host detection failed: {error}");
false
}),
),
None => None,
};
if cancellation.is_cancelled() {
anyhow::bail!("Portal configuration cancelled");
}
Ok(policy.select_gateways(detected.unwrap_or(false), detected))
}
+3
View File
@@ -4,3 +4,6 @@ mod prelogin;
pub use config::*;
pub use prelogin::*;
#[cfg(feature = "desktop")]
pub mod discovery;
+16 -4
View File
@@ -19,6 +19,7 @@ pub struct SamlPrelogin {
region: String,
is_gateway: bool,
saml_request: String,
saml_method: String,
support_default_browser: bool,
}
@@ -27,6 +28,10 @@ impl SamlPrelogin {
&self.region
}
pub fn saml_method(&self) -> &str {
&self.saml_method
}
pub fn saml_request(&self) -> &str {
&self.saml_request
}
@@ -171,7 +176,7 @@ pub async fn prelogin(portal: &str, gp_params: &GpParams, options: PreloginOptio
debug!("Prelogin response XML: {}", res_xml);
let prelogin = parse_res_xml(&res_xml, is_gateway).map_err(|err| {
let prelogin = parse_prelogin_response(&res_xml, is_gateway).map_err(|err| {
warn!("Parse response error, response: {}", res_xml);
PortalError::PreloginError(err.to_string())
})?;
@@ -187,7 +192,8 @@ fn request_param<'a>(params: &'a crate::params::RequestParams, name: &str) -> Op
.find_map(|(key, value)| (key == name).then_some(value.as_str()))
}
fn parse_res_xml(res_xml: &str, is_gateway: bool) -> anyhow::Result<Prelogin> {
/// Parse server authentication metadata without performing a network request.
pub fn parse_prelogin_response(res_xml: &str, is_gateway: bool) -> anyhow::Result<Prelogin> {
let root = Element::parse(res_xml.as_bytes())?;
let status = root
@@ -211,6 +217,11 @@ fn parse_res_xml(res_xml: &str, is_gateway: bool) -> anyhow::Result<Prelogin> {
let saml_default_browser = root.descendant_text("saml-default-browser");
// Check if the prelogin response is SAML
if saml_method.is_some() && saml_request.is_some() {
let saml_method = saml_method.unwrap();
anyhow::ensure!(
matches!(saml_method.as_str(), "REDIRECT" | "POST"),
"Unsupported SAML binding"
);
let saml_request = base64::decode_to_string(&saml_request.unwrap())?;
let support_default_browser = saml_default_browser.map(|s| s.to_lowercase() == "yes").unwrap_or(false);
@@ -218,6 +229,7 @@ fn parse_res_xml(res_xml: &str, is_gateway: bool) -> anyhow::Result<Prelogin> {
region,
is_gateway,
saml_request,
saml_method,
support_default_browser,
};
@@ -258,13 +270,13 @@ mod tests {
fn gp_params(client_os: ClientOs) -> GpParams {
let profile = OsProfile::builder(client_os)
.host_identity(HostIdentity::new(
.host_identity(HostIdentity::from_parts(
"test-computer".to_string(),
"host-id".to_string(),
"serial".to_string(),
"aa:bb:cc:dd:ee:ff".to_string(),
))
.build();
.build(crate::os_profile::test_snapshot());
GpParams::builder(profile).build()
}