mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2025-05-20 07:26:58 -04:00
@@ -30,6 +30,7 @@ uzers.workspace = true
|
||||
serde_urlencoded.workspace = true
|
||||
md5.workspace = true
|
||||
sha256.workspace = true
|
||||
which.workspace = true
|
||||
|
||||
tauri = { workspace = true, optional = true }
|
||||
clap = { workspace = true, optional = true }
|
||||
|
@@ -19,6 +19,7 @@ pub struct SamlAuthLauncher<'a> {
|
||||
ignore_tls_errors: bool,
|
||||
clean: bool,
|
||||
default_browser: bool,
|
||||
external_browser: Option<&'a str>,
|
||||
}
|
||||
|
||||
impl<'a> SamlAuthLauncher<'a> {
|
||||
@@ -35,6 +36,7 @@ impl<'a> SamlAuthLauncher<'a> {
|
||||
ignore_tls_errors: false,
|
||||
clean: false,
|
||||
default_browser: false,
|
||||
external_browser: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,6 +90,11 @@ impl<'a> SamlAuthLauncher<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn external_browser(mut self, external_browser: Option<&'a str>) -> Self {
|
||||
self.external_browser = external_browser;
|
||||
self
|
||||
}
|
||||
|
||||
/// Launch the authenticator binary as the current user or SUDO_USER if available.
|
||||
pub async fn launch(self) -> anyhow::Result<Option<Credential>> {
|
||||
let mut auth_cmd = Command::new(GP_AUTH_BINARY);
|
||||
@@ -133,6 +140,10 @@ impl<'a> SamlAuthLauncher<'a> {
|
||||
auth_cmd.arg("--default-browser");
|
||||
}
|
||||
|
||||
if let Some(external_browser) = self.external_browser {
|
||||
auth_cmd.arg("--external-browser").arg(external_browser);
|
||||
}
|
||||
|
||||
let mut non_root_cmd = auth_cmd.into_non_root()?;
|
||||
let output = non_root_cmd
|
||||
.kill_on_drop(true)
|
||||
|
@@ -1,20 +1,31 @@
|
||||
use std::{env::temp_dir, fs, io::Write, os::unix::fs::PermissionsExt};
|
||||
use std::{borrow::Cow, env::temp_dir, fs, io::Write, os::unix::fs::PermissionsExt};
|
||||
|
||||
use anyhow::bail;
|
||||
use log::warn;
|
||||
use log::{info, warn};
|
||||
|
||||
pub struct BrowserAuthenticator<'a> {
|
||||
auth_request: &'a str,
|
||||
browser: Option<&'a str>,
|
||||
}
|
||||
|
||||
impl BrowserAuthenticator<'_> {
|
||||
pub fn new(auth_request: &str) -> BrowserAuthenticator {
|
||||
BrowserAuthenticator { auth_request }
|
||||
BrowserAuthenticator {
|
||||
auth_request,
|
||||
browser: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_with_browser<'a>(auth_request: &'a str, browser: &'a str) -> BrowserAuthenticator<'a> {
|
||||
BrowserAuthenticator {
|
||||
auth_request,
|
||||
browser: Some(browser),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn authenticate(&self) -> anyhow::Result<()> {
|
||||
if self.auth_request.starts_with("http") {
|
||||
open::that_detached(self.auth_request)?;
|
||||
let path = if self.auth_request.starts_with("http") {
|
||||
Cow::Borrowed(self.auth_request)
|
||||
} else {
|
||||
let html_file = temp_dir().join("gpauth.html");
|
||||
|
||||
@@ -31,9 +42,31 @@ impl BrowserAuthenticator<'_> {
|
||||
file.set_permissions(fs::Permissions::from_mode(0o600))?;
|
||||
file.write_all(self.auth_request.as_bytes())?;
|
||||
|
||||
open::that_detached(html_file)?;
|
||||
Cow::Owned(html_file.to_string_lossy().to_string())
|
||||
};
|
||||
|
||||
if let Some(browser) = self.browser {
|
||||
let app = find_browser_path(browser);
|
||||
|
||||
info!("Launching browser: {}", app);
|
||||
open::with_detached(path.as_ref(), app)?;
|
||||
} else {
|
||||
info!("Launching the default browser...");
|
||||
open::that_detached(path.as_ref())?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn find_browser_path(browser: &str) -> String {
|
||||
if browser == "chrome" {
|
||||
which::which("google-chrome-stable")
|
||||
.or_else(|_| which::which("google-chrome"))
|
||||
.or_else(|_| which::which("chromium"))
|
||||
.map(|path| path.to_string_lossy().to_string())
|
||||
.unwrap_or_else(|_| browser.to_string())
|
||||
} else {
|
||||
browser.into()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user