backport remote browser and arm64 runner updates

This commit is contained in:
Kevin Yue committed 2026-04-18 22:54:25 +08:00
1 parent 19106150dd
commit 2576b08a89
6 files changed
+168 -9

No files matched your search

+1 -1
View File
@@ -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
+2 -2
View File
@@ -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)
+7 -2
View File
@@ -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<String>,
}
@@ -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");
+1 -1
View File
@@ -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<String>,
}
@@ -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<Option<SamlAuthData>> {
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<Option<SamlAuthData>> {
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<String> {
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()
}
+19
View File
@@ -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");
}
}