From 2576b08a89a7328467a2414fea24399ad097dc67 Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Sat, 18 Apr 2026 22:54:25 +0800 Subject: [PATCH] backport remote browser and arm64 runner updates --- .github/workflows/build.yaml | 2 +- .github/workflows/release.yaml | 4 +- apps/gpauth/src/cli.rs | 9 +- apps/gpclient/src/connect.rs | 2 +- .../src/process/browser_authenticator.rs | 141 +++++++++++++++++- crates/gpapi/src/utils/mod.rs | 19 +++ 6 files changed, 168 insertions(+), 9 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index b8e375d..d58e580 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -42,7 +42,7 @@ jobs: id: set-matrix run: | 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": "ubuntu-24.04-arm", "arch": "ubuntu-24.04-arm"}]' >> $GITHUB_OUTPUT else echo 'matrix=[{"runner": "ubuntu-latest", "arch": "amd64"}]' >> $GITHUB_OUTPUT fi diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 97763fa..a712c4b 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -73,9 +73,9 @@ jobs: const inputs = ${{ toJson(inputs) }} const { arch } = inputs const osMap = { - "all": ["ubuntu-latest", "arm64"], + "all": ["ubuntu-latest", "ubuntu-24.04-arm"], "x86_64": ["ubuntu-latest"], - "arm64": ["arm64"] + "arm64": ["ubuntu-24.04-arm"] } const package = Object.entries(inputs) diff --git a/apps/gpauth/src/cli.rs b/apps/gpauth/src/cli.rs index bf94362..2d2848d 100644 --- a/apps/gpauth/src/cli.rs +++ b/apps/gpauth/src/cli.rs @@ -73,7 +73,7 @@ struct Cli { #[arg( long, - help = "The browser to use for authentication, e.g., `default`, `firefox`, `chrome`, `chromium`, or the path to the browser executable" + help = "The browser to use for authentication, e.g., `default`, `firefox`, `chrome`, `chromium`, `remote`, or the path to the browser executable. Use `remote` for headless servers" )] browser: Option, } @@ -104,7 +104,12 @@ impl Cli { }; if let Some(browser_auth) = browser_auth { - browser_auth.authenticate()?; + if let Some(auth_data) = browser_auth.authenticate()? { + info!("Authentication completed"); + println!("{}", json!(SamlAuthResult::Success(auth_data))); + + return Ok(()); + } info!("Please continue the authentication process in the default browser"); diff --git a/apps/gpclient/src/connect.rs b/apps/gpclient/src/connect.rs index 3ae70ae..404513e 100644 --- a/apps/gpclient/src/connect.rs +++ b/apps/gpclient/src/connect.rs @@ -104,7 +104,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/gpapi/src/process/browser_authenticator.rs b/crates/gpapi/src/process/browser_authenticator.rs index 7657ca3..c1ade6f 100644 --- a/crates/gpapi/src/process/browser_authenticator.rs +++ b/crates/gpapi/src/process/browser_authenticator.rs @@ -1,8 +1,20 @@ -use std::{borrow::Cow, env::temp_dir, fs, io::Write, os::unix::fs::PermissionsExt}; +use std::{ + borrow::Cow, + env::temp_dir, + fs, + io::{Read, Write}, + net::{TcpListener, UdpSocket}, + os::unix::fs::PermissionsExt, + thread, +}; use anyhow::bail; use log::{info, warn}; +use crate::auth::SamlAuthData; + +const REMOTE_AUTH_PATH: &str = "/auth"; + pub struct BrowserAuthenticator<'a> { auth_request: &'a str, browser: Option<&'a str>, @@ -28,7 +40,14 @@ impl BrowserAuthenticator<'_> { } } - pub fn authenticate(&self) -> anyhow::Result<()> { + pub fn authenticate(&self) -> anyhow::Result> { + if self + .browser + .is_some_and(|browser| browser.eq_ignore_ascii_case("remote")) + { + return self.authenticate_remote(); + } + let path = if self.auth_request.starts_with("http") { Cow::Borrowed(self.auth_request) } else { @@ -60,7 +79,36 @@ impl BrowserAuthenticator<'_> { webbrowser::open(path.as_ref())?; } - Ok(()) + Ok(None) + } + + fn authenticate_remote(&self) -> anyhow::Result> { + let local_ip = detect_local_ip()?; + let listener = TcpListener::bind((local_ip.as_str(), 0))?; + let auth_url = format!("http://{}{}", listener.local_addr()?, REMOTE_AUTH_PATH); + let auth_request = self.auth_request.to_string(); + + thread::spawn(move || serve_auth_request(listener, auth_request)); + + 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:...") +"#, + auth_url + ); + + let mut data = String::new(); + std::io::stdin().read_line(&mut data)?; + + Ok(Some(SamlAuthData::from_gpcallback(data.trim())?)) } } @@ -75,3 +123,90 @@ fn find_browser_path(browser: &str) -> String { browser.into() } } + +fn detect_local_ip() -> anyhow::Result { + let socket = UdpSocket::bind("0.0.0.0:0")?; + socket.connect("1.1.1.1:80")?; + + let ip = socket.local_addr()?.ip().to_string(); + info!("Determined local IP address: {}", ip); + + Ok(ip) +} + +fn serve_auth_request(listener: TcpListener, auth_request: String) { + let Ok(addr) = listener.local_addr() else { + warn!("Failed to determine auth server address"); + return; + }; + + let auth_url = format!("http://{}{}", addr, REMOTE_AUTH_PATH); + info!("Auth server started at: {}", auth_url); + + for incoming in listener.incoming() { + let mut stream = match incoming { + Ok(stream) => stream, + Err(err) => { + warn!("Failed to accept auth request: {}", err); + continue; + } + }; + + let mut request = [0_u8; 4096]; + let size = match stream.read(&mut request) { + Ok(size) => size, + Err(err) => { + warn!("Failed to read auth request: {}", err); + continue; + } + }; + + let request = String::from_utf8_lossy(&request[..size]); + let path = request + .lines() + .next() + .and_then(|line| line.split_whitespace().nth(1)) + .unwrap_or("/"); + + if path != REMOTE_AUTH_PATH { + let _ = write_response(&mut stream, "404 Not Found", "text/plain", "not found", None); + continue; + } + + let response = if auth_request.starts_with("http") { + write_response( + &mut stream, + "302 Found", + "text/plain", + "redirect", + Some(("Location", auth_request.as_str())), + ) + } else { + write_response(&mut stream, "200 OK", "text/html; charset=utf-8", &auth_request, None) + }; + + if let Err(err) = response { + warn!("Failed to write auth response: {}", err); + } + + break; + } +} + +fn write_response( + stream: &mut std::net::TcpStream, + status: &str, + content_type: &str, + body: &str, + extra_header: Option<(&str, &str)>, +) -> std::io::Result<()> { + write!(stream, "HTTP/1.1 {}\r\n", status)?; + write!(stream, "Content-Type: {}\r\n", content_type)?; + if let Some((name, value)) = extra_header { + write!(stream, "{}: {}\r\n", name, value)?; + } + write!(stream, "Content-Length: {}\r\n", body.len())?; + write!(stream, "Connection: close\r\n\r\n")?; + stream.write_all(body.as_bytes())?; + stream.flush() +} diff --git a/crates/gpapi/src/utils/mod.rs b/crates/gpapi/src/utils/mod.rs index 08db212..bb863b2 100644 --- a/crates/gpapi/src/utils/mod.rs +++ b/crates/gpapi/src/utils/mod.rs @@ -94,3 +94,22 @@ async fn parse_gp_error(res: Response) -> (String, String) { (reason, res) } + +#[cfg(test)] +mod tests { + use super::normalize_server; + + #[test] + fn normalize_server_preserves_custom_port() { + let normalized = normalize_server("vpn.example.com:4443").unwrap(); + + assert_eq!(normalized, "https://vpn.example.com:4443"); + } + + #[test] + fn normalize_server_removes_path_but_keeps_custom_port() { + let normalized = normalize_server("https://vpn.example.com:4443/global-protect").unwrap(); + + assert_eq!(normalized, "https://vpn.example.com:4443"); + } +}