From 7e372cd113410dd3cc4923cdae8f2eb3c9fa61f6 Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Sun, 21 Jan 2024 18:31:39 +0800 Subject: [PATCH] Align with the old behavior of the portal config request (#293) --- .vscode/settings.json | 3 +- README.md | 7 +++- crates/gpapi/src/credential.rs | 53 ++++++++++++++++--------------- crates/gpapi/src/gateway/login.rs | 10 ++---- crates/gpapi/src/gp_params.rs | 21 ++++-------- crates/gpapi/src/portal/config.rs | 10 ++---- 6 files changed, 46 insertions(+), 58 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 02071b0..7a00839 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -48,6 +48,7 @@ "vpnc", "vpninfo", "wmctrl", - "XAUTHORITY" + "XAUTHORITY", + "yuezk" ] } diff --git a/README.md b/README.md index 003d5a2..2cc9a57 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,11 @@ The GUI version is also available after you installed it. You can launch it from > > This instruction is for the 2.x version. The 1.x version is still available on the [1.x](https://github.com/yuezk/GlobalProtect-openconnect/tree/1.x) branch, you can build it from the source code by following the instructions in the `README.md` file. +> [!Warning] +> +> The client requires `openconnect >= 8.20`, please make sure you have it installed, you can check it with `openconnect --version`. +> Installing the client from PPA will automatically install the required version of `openconnect`. + ### Debian/Ubuntu based distributions #### Install from PPA @@ -111,7 +116,7 @@ Download the latest RPM package from [releases](https://github.com/yuezk/GlobalP ### Other distributions -The project depends on `openconnect`, `webkit2gtk`, `libsecret`, `libayatana-appindicator` or `libappindicator-gtk3`. You can install them first and then download the latest binary release (i.e., `*.bin.tar.gz`) from [releases](https://github.com/yuezk/GlobalProtect-openconnect/releases) page. +The project depends on `openconnect >= 8.20`, `webkit2gtk`, `libsecret`, `libayatana-appindicator` or `libappindicator-gtk3`. You can install them first and then download the latest binary release (i.e., `*.bin.tar.gz`) from [releases](https://github.com/yuezk/GlobalProtect-openconnect/releases) page. ## [License](./LICENSE) diff --git a/crates/gpapi/src/credential.rs b/crates/gpapi/src/credential.rs index b3b736b..a99d3ee 100644 --- a/crates/gpapi/src/credential.rs +++ b/crates/gpapi/src/credential.rs @@ -164,31 +164,34 @@ impl Credential { let mut params = HashMap::new(); params.insert("user", self.username()); - match self { - Credential::Password(cred) => { - params.insert("passwd", cred.password()); - } - Credential::PreloginCookie(cred) => { - params.insert("prelogin-cookie", cred.prelogin_cookie()); - } - Credential::AuthCookie(cred) => { - params.insert("portal-userauthcookie", cred.user_auth_cookie()); - params.insert( - "portal-prelogonuserauthcookie", - cred.prelogon_user_auth_cookie(), - ); - } - Credential::CachedCredential(cred) => { - if let Some(password) = cred.password() { - params.insert("passwd", password); - } - params.insert("portal-userauthcookie", cred.auth_cookie.user_auth_cookie()); - params.insert( - "portal-prelogonuserauthcookie", - cred.auth_cookie.prelogon_user_auth_cookie(), - ); - } - } + let (passwd, prelogin_cookie, portal_userauthcookie, portal_prelogonuserauthcookie) = match self + { + Credential::Password(cred) => (Some(cred.password()), None, None, None), + Credential::PreloginCookie(cred) => (None, Some(cred.prelogin_cookie()), None, None), + Credential::AuthCookie(cred) => ( + None, + None, + Some(cred.user_auth_cookie()), + Some(cred.prelogon_user_auth_cookie()), + ), + Credential::CachedCredential(cred) => ( + cred.password(), + None, + Some(cred.auth_cookie.user_auth_cookie()), + Some(cred.auth_cookie.prelogon_user_auth_cookie()), + ), + }; + + params.insert("passwd", passwd.unwrap_or_default()); + params.insert("prelogin-cookie", prelogin_cookie.unwrap_or_default()); + params.insert( + "portal-userauthcookie", + portal_userauthcookie.unwrap_or_default(), + ); + params.insert( + "portal-prelogonuserauthcookie", + portal_prelogonuserauthcookie.unwrap_or_default(), + ); params } diff --git a/crates/gpapi/src/gateway/login.rs b/crates/gpapi/src/gateway/login.rs index 04a9b98..ac8ad25 100644 --- a/crates/gpapi/src/gateway/login.rs +++ b/crates/gpapi/src/gateway/login.rs @@ -23,14 +23,8 @@ pub async fn gateway_login( info!("Gateway login, user_agent: {}", gp_params.user_agent()); - let res_xml = client - .post(&login_url) - .form(¶ms) - .send() - .await? - .error_for_status()? - .text() - .await?; + let res = client.post(&login_url).form(¶ms).send().await?; + let res_xml = res.error_for_status()?.text().await?; let doc = Document::parse(&res_xml)?; diff --git a/crates/gpapi/src/gp_params.rs b/crates/gpapi/src/gp_params.rs index 039f6b1..8ac98c6 100644 --- a/crates/gpapi/src/gp_params.rs +++ b/crates/gpapi/src/gp_params.rs @@ -48,7 +48,7 @@ pub struct GpParams { client_os: ClientOs, os_version: Option, client_version: Option, - computer: Option, + computer: String, ignore_tls_errors: bool, } @@ -62,10 +62,7 @@ impl GpParams { } pub(crate) fn computer(&self) -> &str { - match self.computer { - Some(ref computer) => computer, - None => self.client_os.as_str(), - } + &self.computer } pub fn ignore_tls_errors(&self) -> bool { @@ -84,14 +81,8 @@ impl GpParams { params.insert("ipv6-support", "yes"); params.insert("inputStr", ""); params.insert("clientVer", "4100"); - params.insert("clientos", client_os); - - if let Some(computer) = &self.computer { - params.insert("computer", computer); - } else { - params.insert("computer", client_os); - } + params.insert("computer", &self.computer); if let Some(os_version) = &self.os_version { params.insert("os-version", os_version); @@ -110,7 +101,7 @@ pub struct GpParamsBuilder { client_os: ClientOs, os_version: Option, client_version: Option, - computer: Option, + computer: String, ignore_tls_errors: bool, } @@ -121,7 +112,7 @@ impl GpParamsBuilder { client_os: ClientOs::Linux, os_version: Default::default(), client_version: Default::default(), - computer: Default::default(), + computer: whoami::hostname(), ignore_tls_errors: false, } } @@ -147,7 +138,7 @@ impl GpParamsBuilder { } pub fn computer(&mut self, computer: &str) -> &mut Self { - self.computer = Some(computer.to_string()); + self.computer = computer.to_string(); self } diff --git a/crates/gpapi/src/portal/config.rs b/crates/gpapi/src/portal/config.rs index 4becfbb..5d4ab98 100644 --- a/crates/gpapi/src/portal/config.rs +++ b/crates/gpapi/src/portal/config.rs @@ -132,14 +132,8 @@ pub async fn retrieve_config( info!("Portal config, user_agent: {}", gp_params.user_agent()); - let res_xml = client - .post(&url) - .form(¶ms) - .send() - .await? - .error_for_status()? - .text() - .await?; + let res = client.post(&url).form(¶ms).send().await?; + let res_xml = res.error_for_status()?.text().await?; ensure!(!res_xml.is_empty(), PortalConfigError::EmptyResponse);