diff --git a/README.md b/README.md index cf572e9..fdba30b 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ Or you can try the following command if the above command does not work: gpauth --browser default 2>/dev/null | sudo gpclient connect --cookie-on-stdin ``` -You can specify the browser with the `--browser ` option, e.g., `--browser firefox`, `--browser chrome`, etc. +You can specify the browser with the `--browser ` option, e.g., `--browser firefox`, `--browser chrome`, etc. Use `--browser remote` to use a remote browser for authentication, this will give you a URL you can access on a separate computer with a browser to complete authentication. Useful for headless servers. ### GUI diff --git a/apps/gpclient/src/connect.rs b/apps/gpclient/src/connect.rs index 551f0fd..036581f 100644 --- a/apps/gpclient/src/connect.rs +++ b/apps/gpclient/src/connect.rs @@ -113,7 +113,7 @@ pub(crate) struct ConnectArgs { #[arg( long, - help = "Use the specified browser to authenticate, e.g., `default`, `firefox`, `chrome`, `chromium`, or the path to the browser executable" + help = "Use the specified browser to authenticate, e.g., `default`, `firefox`, `chrome`, `chromium`, `remote`, or the path to the browser executable. Use 'remote' for headless servers." )] browser: Option, } diff --git a/crates/auth/src/browser/auth_server.rs b/crates/auth/src/browser/auth_server.rs index 48c402d..d1ae820 100644 --- a/crates/auth/src/browser/auth_server.rs +++ b/crates/auth/src/browser/auth_server.rs @@ -10,8 +10,8 @@ pub(super) struct AuthServer { } impl AuthServer { - pub fn new() -> anyhow::Result { - let server = Server::http("127.0.0.1:0").map_err(|err| anyhow::anyhow!(err))?; + pub fn new(addr: &str) -> anyhow::Result { + let server = Server::http(addr).map_err(|err| anyhow::anyhow!(err))?; let auth_id = Uuid::new_v4().to_string(); Ok(Self { server, auth_id }) diff --git a/crates/auth/src/browser/browser_auth.rs b/crates/auth/src/browser/browser_auth.rs index c68e8b9..30dc2d1 100644 --- a/crates/auth/src/browser/browser_auth.rs +++ b/crates/auth/src/browser/browser_auth.rs @@ -2,7 +2,10 @@ use std::{env::temp_dir, fs, os::unix::fs::PermissionsExt}; use gpapi::{auth::SamlAuthData, GP_CALLBACK_PORT_FILENAME}; use log::info; -use tokio::{io::AsyncReadExt, net::TcpListener}; +use tokio::{ + io::AsyncReadExt, + net::{TcpListener, UdpSocket}, +}; use crate::browser::auth_server::AuthServer; @@ -10,6 +13,7 @@ pub enum Browser<'a> { Default, Chrome, Firefox, + Remote, Other(&'a str), } @@ -19,6 +23,7 @@ impl<'a> Browser<'a> { "default" => Browser::Default, "chrome" => Browser::Chrome, "firefox" => Browser::Firefox, + "remote" => Browser::Remote, _ => Browser::Other(browser), } } @@ -28,6 +33,7 @@ impl<'a> Browser<'a> { Browser::Default => "default", Browser::Chrome => "chrome", Browser::Firefox => "firefox", + Browser::Remote => "remote", Browser::Other(browser) => browser, } } @@ -47,7 +53,8 @@ impl<'a> BrowserAuthenticator<'a> { } pub async fn authenticate(&self) -> anyhow::Result { - let auth_server = AuthServer::new()?; + let addr = self.determine_addr().await?; + let auth_server = AuthServer::new(&addr)?; let auth_url = auth_server.auth_url(); let auth_request = self.auth_request.to_string(); @@ -56,6 +63,25 @@ impl<'a> BrowserAuthenticator<'a> { }); match self.browser { + Browser::Remote => { + info!( + r#" + +==== Manual Authentication Required ==== + +Please open the following URL in your browser: + +{} + +After completing the authentication, please paste the authentication data back to this terminal. +(The data should start with "globalprotectcallback:...") + +Note that the URL is only valid for a single use. +"#, + auth_url + ); + return read_auth_data_from_stdin(); + } Browser::Default => { info!("Launching the default browser..."); webbrowser::open(&auth_url)?; @@ -71,6 +97,28 @@ impl<'a> BrowserAuthenticator<'a> { info!("Please continue the authentication process in the default browser"); wait_auth_data().await } + + async fn determine_addr(&self) -> anyhow::Result { + if matches!(self.browser, Browser::Remote) { + let local_ip = detect_local_ip().await?; + Ok(format!("{}:0", local_ip)) + } else { + Ok("127.0.0.1:0".to_string()) + } + } +} + +/// Detect the local IP address by creating a UDP socket and connecting to an external address +async fn detect_local_ip() -> anyhow::Result { + let socket = UdpSocket::bind("0.0.0.0:0").await?; + if let Err(err) = socket.connect("1.1.1.1:80").await { + anyhow::bail!("Failed to connect to external address to determine local IP: {}", err); + } + let local_addr = socket.local_addr()?; + let ip = local_addr.ip().to_string(); + info!("Determined local IP address: {}", ip); + + Ok(ip.to_string()) } fn find_browser_path(browser: &Browser) -> String { @@ -119,3 +167,11 @@ async fn wait_auth_data() -> anyhow::Result { let auth_data = SamlAuthData::from_gpcallback(&data)?; Ok(auth_data) } + +fn read_auth_data_from_stdin() -> anyhow::Result { + let mut data = String::new(); + std::io::stdin().read_line(&mut data)?; + + let auth_data = SamlAuthData::from_gpcallback(data.trim())?; + Ok(auth_data) +}