Compare commits

..

7 Commits

Author SHA1 Message Date
Kevin Yue
a0afabeb04 Release 2.1.4 2024-04-10 10:13:37 -04:00
Kevin Yue
1158ab9095 Add MFA support 2024-04-10 10:07:37 -04:00
Kevin Yue
54ccb761e5 Fix CI 2024-04-07 09:42:00 -04:00
Kevin Yue
f72dbd1dec Release 2.1.3 2024-04-07 20:46:23 +08:00
Kevin Yue
0814c3153a Merge branch 'feature/as_gateway' into release/2.1.3 2024-04-07 20:44:29 +08:00
Kevin Yue
9f085e8b8c Improve code style 2024-04-07 20:31:05 +08:00
Kevin Yue
0188752c0a Bump version 2.1.3 2024-04-06 20:07:57 +08:00
9 changed files with 128 additions and 35 deletions

View File

@@ -25,7 +25,7 @@ jobs:
id: set-matrix id: set-matrix
run: | run: |
if [[ "${{ github.ref }}" == "refs/tags/"* ]]; then if [[ "${{ github.ref }}" == "refs/tags/"* ]]; then
echo 'matrix=[{"runner": "ubuntu-latest", "arch": "amd64"}, {"runner": "arm64", "arch": "arm64"]' >> $GITHUB_OUTPUT echo 'matrix=[{"runner": "ubuntu-latest", "arch": "amd64"}, {"runner": "arm64", "arch": "arm64"}]' >> $GITHUB_OUTPUT
else else
echo 'matrix=[{"runner": "ubuntu-latest", "arch": "amd64"}]' >> $GITHUB_OUTPUT echo 'matrix=[{"runner": "ubuntu-latest", "arch": "amd64"}]' >> $GITHUB_OUTPUT
fi fi
@@ -182,7 +182,7 @@ jobs:
gh -R "$REPO" release create $RELEASE_TAG \ gh -R "$REPO" release create $RELEASE_TAG \
--title "$RELEASE_TAG" \ --title "$RELEASE_TAG" \
--notes "$NOTES" \ --notes "$NOTES" \
--target ${{ github.ref }} \ ${{ github.ref == 'refs/heads/dev' && '--target dev' || '' }} \
${{ github.ref == 'refs/heads/dev' && '--prerelease' || '' }} \ ${{ github.ref == 'refs/heads/dev' && '--prerelease' || '' }} \
gh-release/artifact-source/* \ gh-release/artifact-source/* \
gh-release/artifact-gpgui-*/* gh-release/artifact-gpgui-*/*

14
Cargo.lock generated
View File

@@ -564,7 +564,7 @@ dependencies = [
[[package]] [[package]]
name = "common" name = "common"
version = "2.1.2" version = "2.1.4"
dependencies = [ dependencies = [
"is_executable", "is_executable",
] ]
@@ -1430,7 +1430,7 @@ dependencies = [
[[package]] [[package]]
name = "gpapi" name = "gpapi"
version = "2.1.2" version = "2.1.4"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"base64 0.21.5", "base64 0.21.5",
@@ -1462,7 +1462,7 @@ dependencies = [
[[package]] [[package]]
name = "gpauth" name = "gpauth"
version = "2.1.2" version = "2.1.4"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
@@ -1483,7 +1483,7 @@ dependencies = [
[[package]] [[package]]
name = "gpclient" name = "gpclient"
version = "2.1.2" version = "2.1.4"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
@@ -1505,7 +1505,7 @@ dependencies = [
[[package]] [[package]]
name = "gpgui-helper" name = "gpgui-helper"
version = "2.1.2" version = "2.1.4"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
@@ -1523,7 +1523,7 @@ dependencies = [
[[package]] [[package]]
name = "gpservice" name = "gpservice"
version = "2.1.2" version = "2.1.4"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"axum", "axum",
@@ -2537,7 +2537,7 @@ dependencies = [
[[package]] [[package]]
name = "openconnect" name = "openconnect"
version = "2.1.2" version = "2.1.4"
dependencies = [ dependencies = [
"cc", "cc",
"common", "common",

View File

@@ -5,7 +5,7 @@ members = ["crates/*", "apps/gpclient", "apps/gpservice", "apps/gpauth", "apps/g
[workspace.package] [workspace.package]
rust-version = "1.70" rust-version = "1.70"
version = "2.1.2" version = "2.1.4"
authors = ["Kevin Yue <k3vinyue@gmail.com>"] authors = ["Kevin Yue <k3vinyue@gmail.com>"]
homepage = "https://github.com/yuezk/GlobalProtect-openconnect" homepage = "https://github.com/yuezk/GlobalProtect-openconnect"
edition = "2021" edition = "2021"

View File

@@ -366,17 +366,14 @@ fn read_auth_data_from_html(html: &str) -> Result<SamlAuthData, AuthDataParseErr
return Err(AuthDataParseError::Invalid); return Err(AuthDataParseError::Invalid);
} }
match SamlAuthData::from_html(html) { SamlAuthData::from_html(html).or_else(|err| {
Ok(auth_data) => Ok(auth_data), if let Some(gpcallback) = extract_gpcallback(html) {
Err(err) => { info!("Found gpcallback from html...");
if let Some(gpcallback) = extract_gpcallback(html) { SamlAuthData::from_gpcallback(&gpcallback)
info!("Found gpcallback from html..."); } else {
SamlAuthData::from_gpcallback(&gpcallback) Err(err)
} else {
Err(err)
}
} }
} })
} }
fn extract_gpcallback(html: &str) -> Option<String> { fn extract_gpcallback(html: &str) -> Option<String> {

View File

@@ -6,7 +6,7 @@ use gpapi::{
clap::args::Os, clap::args::Os,
credential::{Credential, PasswordCredential}, credential::{Credential, PasswordCredential},
error::PortalError, error::PortalError,
gateway::gateway_login, gateway::{gateway_login, GatewayLogin},
gp_params::{ClientOs, GpParams}, gp_params::{ClientOs, GpParams},
portal::{prelogin, retrieve_config, Prelogin}, portal::{prelogin, retrieve_config, Prelogin},
process::{ process::{
@@ -32,7 +32,7 @@ pub(crate) struct ConnectArgs {
user: Option<String>, user: Option<String>,
#[arg(long, short, help = "The VPNC script to use")] #[arg(long, short, help = "The VPNC script to use")]
script: Option<String>, script: Option<String>,
#[arg(long, help = "Treat the server as a gateway, instead of a portal")] #[arg(long, help = "Connect the server as a gateway, instead of a portal")]
as_gateway: bool, as_gateway: bool,
#[arg( #[arg(
@@ -154,7 +154,7 @@ impl<'a> ConnectHandler<'a> {
let gateway = selected_gateway.server(); let gateway = selected_gateway.server();
let cred = portal_config.auth_cookie().into(); let cred = portal_config.auth_cookie().into();
let cookie = match gateway_login(gateway, &cred, &gp_params).await { let cookie = match self.login_gateway(gateway, &cred, &gp_params).await {
Ok(cookie) => cookie, Ok(cookie) => cookie,
Err(err) => { Err(err) => {
info!("Gateway login failed: {}", err); info!("Gateway login failed: {}", err);
@@ -174,11 +174,28 @@ impl<'a> ConnectHandler<'a> {
let prelogin = prelogin(gateway, &gp_params).await?; let prelogin = prelogin(gateway, &gp_params).await?;
let cred = self.obtain_credential(&prelogin, gateway).await?; let cred = self.obtain_credential(&prelogin, gateway).await?;
let cookie = gateway_login(gateway, &cred, &gp_params).await?; let cookie = self.login_gateway(gateway, &cred, &gp_params).await?;
self.connect_gateway(gateway, &cookie).await self.connect_gateway(gateway, &cookie).await
} }
async fn login_gateway(&self, gateway: &str, cred: &Credential, gp_params: &GpParams) -> anyhow::Result<String> {
let mut gp_params = gp_params.clone();
loop {
match gateway_login(gateway, cred, &gp_params).await? {
GatewayLogin::Cookie(cookie) => return Ok(cookie),
GatewayLogin::Mfa(message, input_str) => {
let otp = Text::new(&message).prompt()?;
gp_params.set_input_str(&input_str);
gp_params.set_otp(&otp);
info!("Retrying gateway login with MFA...");
}
}
}
}
async fn connect_gateway(&self, gateway: &str, cookie: &str) -> anyhow::Result<()> { async fn connect_gateway(&self, gateway: &str, cookie: &str) -> anyhow::Result<()> {
let mtu = self.args.mtu.unwrap_or(0); let mtu = self.args.mtu.unwrap_or(0);
let csd_uid = get_csd_uid(&self.args.csd_user)?; let csd_uid = get_csd_uid(&self.args.csd_user)?;

View File

@@ -1,5 +1,17 @@
# Changelog # Changelog
## 2.1.4 - 2024-04-10
- Support MFA authentication (fix [#343](https://github.com/yuezk/GlobalProtect-openconnect/issues/343))
- Improve the Gateway switcher UI
## 2.1.3 - 2024-04-07
- Support CAS authentication (fix [#339](https://github.com/yuezk/GlobalProtect-openconnect/issues/339))
- CLI: Add `--as-gateway` option to connect as gateway directly (fix [#318](https://github.com/yuezk/GlobalProtect-openconnect/issues/318))
- GUI: Support connect the gateway directly (fix [#318](https://github.com/yuezk/GlobalProtect-openconnect/issues/318))
- GUI: Add an option to use symbolic tray icon (fix [#341](https://github.com/yuezk/GlobalProtect-openconnect/issues/341))
## 2.1.2 - 2024-03-29 ## 2.1.2 - 2024-03-29
- Treat portal as gateway when the gateway login is failed (fix #338) - Treat portal as gateway when the gateway login is failed (fix #338)

View File

@@ -162,7 +162,7 @@ pub enum Credential {
Password(PasswordCredential), Password(PasswordCredential),
Prelogin(PreloginCredential), Prelogin(PreloginCredential),
AuthCookie(AuthCookieCredential), AuthCookie(AuthCookieCredential),
CachedCredential(CachedCredential), Cached(CachedCredential),
} }
impl Credential { impl Credential {
@@ -179,7 +179,7 @@ impl Credential {
Credential::Password(cred) => cred.username(), Credential::Password(cred) => cred.username(),
Credential::Prelogin(cred) => cred.username(), Credential::Prelogin(cred) => cred.username(),
Credential::AuthCookie(cred) => cred.username(), Credential::AuthCookie(cred) => cred.username(),
Credential::CachedCredential(cred) => cred.username(), Credential::Cached(cred) => cred.username(),
} }
} }
@@ -197,7 +197,7 @@ impl Credential {
Some(cred.prelogon_user_auth_cookie()), Some(cred.prelogon_user_auth_cookie()),
None, None,
), ),
Credential::CachedCredential(cred) => ( Credential::Cached(cred) => (
cred.password(), cred.password(),
None, None,
Some(cred.auth_cookie.user_auth_cookie()), Some(cred.auth_cookie.user_auth_cookie()),
@@ -244,6 +244,6 @@ impl From<&AuthCookieCredential> for Credential {
impl From<&CachedCredential> for Credential { impl From<&CachedCredential> for Credential {
fn from(value: &CachedCredential) -> Self { fn from(value: &CachedCredential) -> Self {
Self::CachedCredential(value.clone()) Self::Cached(value.clone())
} }
} }

View File

@@ -11,7 +11,12 @@ use crate::{
utils::{normalize_server, parse_gp_error, remove_url_scheme}, utils::{normalize_server, parse_gp_error, remove_url_scheme},
}; };
pub async fn gateway_login(gateway: &str, cred: &Credential, gp_params: &GpParams) -> anyhow::Result<String> { pub enum GatewayLogin {
Cookie(String),
Mfa(String, String),
}
pub async fn gateway_login(gateway: &str, cred: &Credential, gp_params: &GpParams) -> anyhow::Result<GatewayLogin> {
let url = normalize_server(gateway)?; let url = normalize_server(gateway)?;
let gateway = remove_url_scheme(&url); let gateway = remove_url_scheme(&url);
@@ -49,10 +54,22 @@ pub async fn gateway_login(gateway: &str, cred: &Credential, gp_params: &GpParam
bail!("Gateway login error, reason: {}", reason); bail!("Gateway login error, reason: {}", reason);
} }
let res_xml = res.text().await?; let res = res.text().await?;
let doc = Document::parse(&res_xml)?;
build_gateway_token(&doc, gp_params.computer()) // MFA detected
if res.contains("Challenge") {
let Some((message, input_str)) = parse_mfa(&res) else {
bail!("Failed to parse MFA challenge: {res}");
};
return Ok(GatewayLogin::Mfa(message, input_str));
}
let doc = Document::parse(&res)?;
let cookie = build_gateway_token(&doc, gp_params.computer())?;
Ok(GatewayLogin::Cookie(cookie))
} }
fn build_gateway_token(doc: &Document, computer: &str) -> anyhow::Result<String> { fn build_gateway_token(doc: &Document, computer: &str) -> anyhow::Result<String> {
@@ -86,3 +103,33 @@ fn read_args<'a>(args: &'a [String], index: usize, key: &'a str) -> anyhow::Resu
.ok_or_else(|| anyhow::anyhow!("Failed to read {key} from args")) .ok_or_else(|| anyhow::anyhow!("Failed to read {key} from args"))
.map(|s| (key, s.as_ref())) .map(|s| (key, s.as_ref()))
} }
fn parse_mfa(res: &str) -> Option<(String, String)> {
let message = res
.lines()
.find(|l| l.contains("respMsg"))
.and_then(|l| l.split('"').nth(1).map(|s| s.to_string()))?;
let input_str = res
.lines()
.find(|l| l.contains("inputStr"))
.and_then(|l| l.split('"').nth(1).map(|s| s.to_string()))?;
Some((message, input_str))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn mfa() {
let res = r#"var respStatus = "Challenge";
var respMsg = "MFA message";
thisForm.inputStr.value = "5ef64e83000119ed";"#;
let (message, input_str) = parse_mfa(res).unwrap();
assert_eq!(message, "MFA message");
assert_eq!(input_str, "5ef64e83000119ed");
}
}

View File

@@ -42,7 +42,7 @@ impl ClientOs {
} }
} }
#[derive(Debug, Serialize, Deserialize, Type, Default)] #[derive(Debug, Serialize, Deserialize, Type, Default, Clone)]
pub struct GpParams { pub struct GpParams {
is_gateway: bool, is_gateway: bool,
user_agent: String, user_agent: String,
@@ -51,6 +51,9 @@ pub struct GpParams {
client_version: Option<String>, client_version: Option<String>,
computer: String, computer: String,
ignore_tls_errors: bool, ignore_tls_errors: bool,
// Used for MFA
input_str: Option<String>,
otp: Option<String>,
} }
impl GpParams { impl GpParams {
@@ -90,6 +93,14 @@ impl GpParams {
self.client_version.as_deref() self.client_version.as_deref()
} }
pub fn set_input_str(&mut self, input_str: &str) {
self.input_str = Some(input_str.to_string());
}
pub fn set_otp(&mut self, otp: &str) {
self.otp = Some(otp.to_string());
}
pub(crate) fn to_params(&self) -> HashMap<&str, &str> { pub(crate) fn to_params(&self) -> HashMap<&str, &str> {
let mut params: HashMap<&str, &str> = HashMap::new(); let mut params: HashMap<&str, &str> = HashMap::new();
let client_os = self.client_os.as_str(); let client_os = self.client_os.as_str();
@@ -100,11 +111,16 @@ impl GpParams {
params.insert("ok", "Login"); params.insert("ok", "Login");
params.insert("direct", "yes"); params.insert("direct", "yes");
params.insert("ipv6-support", "yes"); params.insert("ipv6-support", "yes");
params.insert("inputStr", "");
params.insert("clientVer", "4100"); params.insert("clientVer", "4100");
params.insert("clientos", client_os); params.insert("clientos", client_os);
params.insert("computer", &self.computer); params.insert("computer", &self.computer);
// MFA
params.insert("inputStr", self.input_str.as_deref().unwrap_or_default());
if let Some(otp) = &self.otp {
params.insert("passwd", otp);
}
if let Some(os_version) = &self.os_version { if let Some(os_version) = &self.os_version {
params.insert("os-version", os_version); params.insert("os-version", os_version);
} }
@@ -130,13 +146,15 @@ pub struct GpParamsBuilder {
impl GpParamsBuilder { impl GpParamsBuilder {
pub fn new() -> Self { pub fn new() -> Self {
let computer = whoami::fallible::hostname().unwrap_or_else(|_| String::from("localhost"));
Self { Self {
is_gateway: false, is_gateway: false,
user_agent: GP_USER_AGENT.to_string(), user_agent: GP_USER_AGENT.to_string(),
client_os: ClientOs::Linux, client_os: ClientOs::Linux,
os_version: Default::default(), os_version: Default::default(),
client_version: Default::default(), client_version: Default::default(),
computer: whoami::hostname(), computer,
ignore_tls_errors: false, ignore_tls_errors: false,
} }
} }
@@ -185,6 +203,8 @@ impl GpParamsBuilder {
client_version: self.client_version.clone(), client_version: self.client_version.clone(),
computer: self.computer.clone(), computer: self.computer.clone(),
ignore_tls_errors: self.ignore_tls_errors, ignore_tls_errors: self.ignore_tls_errors,
input_str: Default::default(),
otp: Default::default(),
} }
} }
} }