diff --git a/Cargo.lock b/Cargo.lock index b6f7ec5..b447e46 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -326,6 +326,15 @@ dependencies = [ "generic-array", ] +[[package]] +name = "block2" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdeb9d870516001442e364c5220d3574d2da8dc765554b4a617230d33fa58ef5" +dependencies = [ + "objc2", +] + [[package]] name = "blocking" version = "1.6.2" @@ -495,6 +504,17 @@ dependencies = [ "typenum", ] +[[package]] +name = "ctrlc" +version = "3.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0b1fab2ae45819af2d0731d60f2afe17227ebb1a1538a236da84c93e9a60162" +dependencies = [ + "dispatch2", + "nix", + "windows-sys 0.61.2", +] + [[package]] name = "data-encoding" version = "2.11.1" @@ -553,6 +573,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "dispatch2" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0e367e4e7da84520dedcac1901e4da967309406d1e51017ae1abfb97adbd38" +dependencies = [ + "bitflags", + "block2", + "libc", + "objc2", +] + [[package]] name = "displaydoc" version = "0.2.7" @@ -1208,10 +1240,11 @@ checksum = "db13adb97ab515a3691f56e4dbab09283d0b86cb45abd991d8634a9d6f501760" [[package]] name = "lexmount-browser" -version = "1.2.4" +version = "1.3.0" dependencies = [ "base64 0.22.1", "clap", + "ctrlc", "dirs", "httparse", "httpmock", @@ -1302,6 +1335,33 @@ version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086" +[[package]] +name = "nix" +version = "0.31.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf20d2fde8ff38632c426f1165ed7436270b44f199fc55284c38276f9db47c3d" +dependencies = [ + "bitflags", + "cfg-if", + "cfg_aliases", + "libc", +] + +[[package]] +name = "objc2" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08849bbd4767dfae9457696856ae1c84fe4e0281bbe4a7abff2d0e06fb7981f8" +dependencies = [ + "objc2-encode", +] + +[[package]] +name = "objc2-encode" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef25abbcd74fb2609453eb695bd2f860d389e457f67dc17cafc8b8cbc89d0c33" + [[package]] name = "once_cell" version = "1.21.4" diff --git a/Cargo.toml b/Cargo.toml index 935dc7e..d748389 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "lexmount-browser" -version = "1.2.4" +version = "1.3.0" edition = "2024" license = "MIT" description = "Native Rust SDK and CLI for Lexmount cloud browsers" @@ -17,6 +17,7 @@ path = "src/main.rs" [dependencies] base64 = "0.22" clap = { version = "4.5", features = ["derive"] } +ctrlc = { version = "3", features = ["termination"] } dirs = "6" httparse = "1" hyper-util = { version = "0.1.20", features = ["client-proxy"] } diff --git a/README.md b/README.md index 195608a..ed99b9b 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ or omit it to use `Agent`. Credentials are stored at `~/.config/lexmount/browser-cli/credentials.json` with mode `0600` on Unix and are never printed. -All commands emit one JSON document. Run `browser-cli --help` for the complete +Commands emit one JSON document; `tunnel open` emits its ready result before staying alive. Run `browser-cli --help` for the complete surface. ### Errors and output pipes @@ -46,6 +46,70 @@ failed still exits with status 1 even if stderr is closed and cannot report the error. These source changes require a new binary; updating Skill instructions does not change an already installed CLI. +## Local network proxy tunnels + +Introduced in 1.3.0. This requires a newly built or published binary; updating the +Skill alone does not add the commands. `tunnel open` runs on the machine whose +network/VPN the cloud browser should use: + +```bash +# Terminal 1: keep running on the local network machine. +browser-cli tunnel open +# Prints once, only after the gateway is ready: +# {"ok":true,"data":{"tunnel_id":"lt1.…","region_id":"nanjing-1","ready":true}} + +# Terminal 2: use the same account/project and API environment. +browser-cli session create --local-proxy-tunnel-id TUNNEL_ID +browser-cli action open-url --session-id SESSION_ID --url https://oa.internal/ +browser-cli tunnel status --tunnel-id TUNNEL_ID +browser-cli session close --session-id SESSION_ID +# Ctrl+C in terminal 1, or: +browser-cli tunnel close --tunnel-id TUNNEL_ID +``` + +The tunnel command stays in the foreground after flushing its JSON result. +Keep that process alive while sessions need the tunnel. `--duration SECONDS` +can bound its lifetime (default 0 means until Ctrl+C, termination, or server +revocation). Ctrl+C/SIGTERM stops forwarding and revokes the tunnel; closing it +from another process also stops forwarding. Abrupt process loss is handled by +the gateway heartbeat/lease expiration. Closing a tunnel does not close browser +sessions or stop their runtime billing; close those sessions separately. The +tunnel also does not send browser-session keepalives. + +Session creation pins the region encoded in the tunnel ID. A conflicting +`LEXMOUNT_REGION`, `--official-proxy`, or a non-`normal` browser mode is rejected. +The project needs the server's `custom_proxy` capability. A closed/offline or +foreign-project tunnel is rejected by the server. No direct/cloud-network +fallback is attempted. Existing action/CDP commands do not need additional flags. + +Destination DNS uses the local OS resolver, including hosts/VPN configuration, +and TCP connects from this machine. HTTP, HTTPS, WebSocket and secure WebSocket +bytes pass through unchanged. It does not import local browser cookies, trust +stores or integrated login, and is not a general VPN/UDP tunnel. The selected +browser sessions can reach destinations accessible from the tunnel machine; +keep the connector running only for the sessions you intend to grant access. + +The reverse WSS uses short, single-use tickets and `lexmount.local-proxy.v1`; +API keys and tickets are never printed or placed in its URL. It requires WSS +except for loopback test fixtures, validates TLS, and rejects redirects. The +connector's WSS honors the same HTTP CONNECT environment proxy settings as CDP; +local destination TCP is direct through the OS network. Reconnect requests a new +ticket and waits for ready again. Existing TCP streams are closed and are never +replayed. The initial JSON is a startup readiness snapshot; query `tunnel status` +for server state after a later disconnect. + +The transport limits each connection to 128 streams, each direction to a 256 KiB +credit window, and data frames to 64 KiB. Credits are returned only after writing +bytes to local TCP. Half-close preserves the reverse direction. DNS/TCP connects +have a 15-second deadline; DNS uses the bounded OS resolver described below. +Ready waits and WebSocket connection attempts also have bounded deadlines. + +Rust callers use `Client::open_tunnel()`, retain the returned `LocalTunnel`, and +set `CreateSession.proxy` to `{"type":"local","tunnel_id":tunnel.id()}`. The handle +exposes `ready()`, `id()`, `region_id()`, and `close()`. Drop performs best-effort +revocation; call `close()` explicitly to observe cleanup errors. An owned handle +must outlive its sessions' network use. + ## Cloud runtime proxies Version 1.2.1 routes CDP WebSocket connections through the environment's HTTP @@ -86,8 +150,8 @@ automatic action retries. These changes require a new CLI release; published Explicit page selection is introduced in version 1.2.0. Check that the installed binary's `browser-cli action --help` lists `--target-id`; the published 1.1.15 binary does not have it. The package version and both bootstrap scripts target -1.2.4 together. Merging or building this source does not publish release assets: -bootstrap can install 1.2.4 only after its binaries and checksums are published +1.3.0 together. Merging or building this source does not publish release assets: +bootstrap can install 1.3.0 only after its binaries and checksums are published to COS. Until then, use a source build for local verification. Every `action` command accepts an optional `--target-id`. Obtain the page's CDP @@ -199,7 +263,7 @@ overwrite an existing release with changed binaries. `skills/lexmount-browser/scripts/bootstrap.ps1` and `bootstrap.sh`. 2. Run `.github/scripts/test-release-version.ps1` with Windows PowerShell 5.1 or PowerShell 7, then `.github/scripts/verify-release-version.ps1 -ReleaseTag - v1.2.4` (substitute the intended version). Complete CI and merge the PR. + v1.3.0` (substitute the intended version). Complete CI and merge the PR. 3. Create the matching tag **on that merged commit**. Typing a new tag or release title in GitHub does not update any source version. The release workflow rejects inconsistent versions before building, signing or uploading. diff --git a/skills/lexmount-browser/references/commands.md b/skills/lexmount-browser/references/commands.md index eb03d50..9fb706a 100644 --- a/skills/lexmount-browser/references/commands.md +++ b/skills/lexmount-browser/references/commands.md @@ -12,6 +12,11 @@ browser-cli auth login [--client-name "NAME"] browser-cli session create [--browser-mode normal|light] [--context-id ID --context-mode read_write|read_only] [--downloads] [--recording] [--window-size 1920,1080] + [--official-proxy | --local-proxy-tunnel-id TUNNEL_ID] +browser-cli tunnel open [--duration SECONDS] +browser-cli tunnel status --tunnel-id TUNNEL_ID +browser-cli tunnel close --tunnel-id TUNNEL_ID + browser-cli session list [--status active] browser-cli session get --session-id ID browser-cli session targets --session-id ID @@ -89,3 +94,27 @@ Use temporary sessions for public browsing. Use a dedicated persistent Context p `wait-text` uses case-insensitive normalized contains matching by default. Add `--exact` only when the entire normalized text must match. The obsolete `--match contains` form is not supported. + +## Local network proxy (1.3.0) + +Check that the installed binary lists `tunnel` and +`session create --local-proxy-tunnel-id`. Run `tunnel open` on the machine whose +local/VPN network is needed, keep that process running, and read its single +flushed JSON ready result. In a separate invocation create a normal session with +that tunnel ID, then use existing actions. The tunnel ID pins the region; use +the same project and API environment. It conflicts with `--official-proxy` and +a different `LEXMOUNT_REGION`. The server requires `custom_proxy` capability. + +This grants selected cloud browser sessions access through that machine's local +DNS and TCP connections. It does not copy cookies or OS login, or tunnel UDP. +Do not start a local connector on a different machine from the intended network. +There is no cloud/direct fallback on tunnel failure. Reconnection terminates old +TCP streams; it never replays requests or browser actions. `tunnel status` checks +current server state; the initial ready output is not a continuous status feed. + +Close browser sessions when done, then stop the foreground connector with Ctrl+C +(or `tunnel close --tunnel-id ID`). Closing the tunnel does not close sessions or +stop session billing. `--duration` limits tunnel lifetime and defaults to 0 +(until interrupted or revoked). This process must remain alive between CLI +invocations, and does not keep browser sessions active by itself. Closing stdin +is not the shutdown signal; use Ctrl+C/termination or the close command. diff --git a/skills/lexmount-browser/scripts/bootstrap.ps1 b/skills/lexmount-browser/scripts/bootstrap.ps1 index 192e2d5..a3385e9 100644 --- a/skills/lexmount-browser/scripts/bootstrap.ps1 +++ b/skills/lexmount-browser/scripts/bootstrap.ps1 @@ -12,7 +12,7 @@ function Invoke-Tls12Download { } } -$version = if ($env:LEXMOUNT_BROWSER_CLI_VERSION) { $env:LEXMOUNT_BROWSER_CLI_VERSION } else { "1.2.4" } +$version = if ($env:LEXMOUNT_BROWSER_CLI_VERSION) { $env:LEXMOUNT_BROWSER_CLI_VERSION } else { "1.3.0" } $downloadBaseUrl = if ($env:LEXMOUNT_BROWSER_CLI_DOWNLOAD_BASE_URL) { $env:LEXMOUNT_BROWSER_CLI_DOWNLOAD_BASE_URL.TrimEnd('/') } else { "https://cli-bin-1377899528.cos.ap-nanjing.myqcloud.com/releases/browser-cli" } $architecture = if ($env:PROCESSOR_ARCHITEW6432) { $env:PROCESSOR_ARCHITEW6432 } else { $env:PROCESSOR_ARCHITECTURE } if ($architecture -ne "AMD64") { throw "Only Windows x64 is supported" } diff --git a/skills/lexmount-browser/scripts/bootstrap.sh b/skills/lexmount-browser/scripts/bootstrap.sh index 2a1ea79..36beb4d 100755 --- a/skills/lexmount-browser/scripts/bootstrap.sh +++ b/skills/lexmount-browser/scripts/bootstrap.sh @@ -1,7 +1,7 @@ #!/bin/sh set -eu -version="${LEXMOUNT_BROWSER_CLI_VERSION:-1.2.4}" +version="${LEXMOUNT_BROWSER_CLI_VERSION:-1.3.0}" download_base_url="${LEXMOUNT_BROWSER_CLI_DOWNLOAD_BASE_URL:-https://cli-bin-1377899528.cos.ap-nanjing.myqcloud.com/releases/browser-cli}" repo="${download_base_url%/}/v${version}" case "$(uname -s)-$(uname -m)" in diff --git a/src/cdp.rs b/src/cdp.rs index 3ea1048..a731efb 100644 --- a/src/cdp.rs +++ b/src/cdp.rs @@ -12,7 +12,7 @@ use tungstenite::Message; use crate::{Error, Result}; mod click; -mod proxy; +pub(crate) mod proxy; pub struct Cdp { socket: proxy::Socket, diff --git a/src/cdp/proxy.rs b/src/cdp/proxy.rs index 71af004..82de0f8 100644 --- a/src/cdp/proxy.rs +++ b/src/cdp/proxy.rs @@ -21,7 +21,7 @@ use crate::{Error, Result}; mod resolver; -pub(super) type Socket = WebSocket>; +pub(crate) type Socket = WebSocket>; const TIMEOUT: Duration = Duration::from_secs(15); @@ -76,6 +76,49 @@ fn connect_with_deadline(url: &str, matcher: &Matcher, deadline: Deadline) -> Re } fn connect_once(url: &str, matcher: &Matcher, deadline: Deadline) -> Result { + connect_transport(url, matcher, deadline, None).map(|(socket, _)| socket) +} + +// Single attempt: tickets must never be forwarded across WebSocket redirects. +pub(crate) fn connect_tunnel(url: &str, ticket: &str) -> Result<(Socket, Arc)> { + connect_transport( + url, + &Matcher::from_env(), + Deadline::new(TIMEOUT), + Some(ticket), + ) +} + +pub(crate) fn connect_local_tcp( + host: &str, + port: u16, + timeout: Duration, + cancelled: &AtomicBool, +) -> Result { + let deadline = Deadline::new(timeout); + let addresses = resolver::resolve(host, port, deadline, "local_dns")?; + for address in addresses { + if cancelled.load(Ordering::Relaxed) { + break; + } + if let Ok(stream) = TcpStream::connect_timeout(&address, deadline.remaining("local_tcp")?) { + if cancelled.load(Ordering::Relaxed) { + break; + } + stream.set_nodelay(true)?; + stream.set_nonblocking(true)?; + return Ok(stream); + } + } + Err(Error::Tunnel("local connection failed".into())) +} + +fn connect_transport( + url: &str, + matcher: &Matcher, + deadline: Deadline, + ticket: Option<&str>, +) -> Result<(Socket, Arc)> { let destination = destination(url)?; let proxy = matcher.intercept(&destination); // ponytail: HTTP CONNECT only. Never silently retry a proxy failure direct. @@ -145,22 +188,57 @@ fn connect_once(url: &str, matcher: &Matcher, deadline: Deadline) -> Result Error::from(error), - HandshakeError::Interrupted(_) => { - Error::Cdp("WebSocket handshake interrupted".into()) - } - }, - stage, + use tungstenite::client::IntoClientRequest; + let mut request = url.into_client_request()?; + let config = if let Some(ticket) = ticket { + request.headers_mut().insert( + "Authorization", + format!("Bearer {ticket}") + .parse() + .map_err(|_| Error::Config("invalid tunnel ticket".into()))?, + ); + request.headers_mut().insert( + "Sec-WebSocket-Protocol", + "lexmount.local-proxy.v1".parse().unwrap(), + ); + Some( + tungstenite::protocol::WebSocketConfig::default() + .write_buffer_size(0) + .max_write_buffer_size(2 * 65536 + 4096) + .max_message_size(Some(65536 + 4)) + .max_frame_size(Some(65536 + 4)), ) - })?; + } else { + None + }; + let (socket, response) = tungstenite::client_tls_with_config(request, stream, config, None) + .map_err(|error| { + deadline.normalize( + match error { + HandshakeError::Failure(error) => Error::from(error), + HandshakeError::Interrupted(_) => { + Error::Cdp("WebSocket handshake interrupted".into()) + } + }, + stage, + ) + })?; deadline.remaining(stage)?; timeout_handle.set_read_timeout(None)?; timeout_handle.set_write_timeout(None)?; connected.store(true, Ordering::Relaxed); - Ok(socket) + if ticket.is_some() + && response + .headers() + .get("Sec-WebSocket-Protocol") + .and_then(|h| h.to_str().ok()) + != Some("lexmount.local-proxy.v1") + { + return Err(Error::Tunnel( + "gateway did not select the local proxy protocol".into(), + )); + } + Ok((socket, timeout_handle)) } #[derive(Clone, Copy)] @@ -222,7 +300,7 @@ fn remaining(deadline: Instant) -> io::Result { // Each underlying I/O uses the remaining budget, including TLS handshake // loops and peers that keep trickling bytes. Disabled after the WS upgrade. #[derive(Debug)] -pub(super) struct ConnectionStream { +pub(crate) struct ConnectionStream { stream: Arc, deadline: Instant, connected: Arc, diff --git a/src/client.rs b/src/client.rs index 3f0baf0..e460187 100644 --- a/src/client.rs +++ b/src/client.rs @@ -144,7 +144,7 @@ impl Client { Ok(request.send()?) } - fn json( + pub(crate) fn json( &self, method: Method, path: &str, @@ -202,12 +202,63 @@ impl Client { Ok(bytes.to_vec()) } + pub(crate) fn tunnel_client(&self) -> Result { + let mut client = self.clone(); + client.http = HttpClient::builder() + .timeout(Duration::from_secs(15)) + .build()?; + Ok(client) + } + + pub(crate) fn tunnel_region_body(&self, id: Option<&str>) -> Result { + let region = id.map(crate::tunnels::tunnel_region).transpose()?; + if let (Some(requested), Some(actual)) = (&self.region, ®ion) + && requested != actual + { + return Err(Error::Config( + "local tunnel and configured region differ".into(), + )); + } + Ok(match region.as_ref().or(self.region.as_ref()) { + Some(region) => json!({"region_id": region}), + None => json!({}), + }) + } + pub fn create_session( &self, request: CreateSession, poll_timeout: Duration, ) -> Result { - let body = self.body_with_region(serde_json::to_value(request)?); + let local_id = request + .proxy + .as_ref() + .filter(|p| p["type"] == "local") + .map(|p| { + p["tunnel_id"] + .as_str() + .ok_or_else(|| Error::Config("local proxy requires tunnel_id".into())) + }) + .transpose()?; + let region = if let Some(id) = local_id { + if request.official_proxy == Some(true) { + return Err(Error::Config( + "local and official proxies are mutually exclusive".into(), + )); + } + if request.browser_mode != "normal" { + return Err(Error::Config( + "local proxy requires normal browser mode".into(), + )); + } + Some(self.tunnel_region_body(Some(id))?) + } else { + None + }; + let mut body = self.body_with_region(serde_json::to_value(request)?); + if let Some(region) = region { + body["region_id"] = region["region_id"].clone(); + } let accepted: Value = self.json(Method::POST, "/instance/v2", Some(body))?; let session_id = accepted .get("session_id") diff --git a/src/error.rs b/src/error.rs index 3573603..847ae36 100644 --- a/src/error.rs +++ b/src/error.rs @@ -26,6 +26,8 @@ pub enum Error { Json(#[from] serde_json::Error), #[error("I/O error: {0}")] Io(#[from] std::io::Error), + #[error("local tunnel error: {0}")] + Tunnel(String), #[error("CDP command failed: {0}")] Cdp(String), } diff --git a/src/lib.rs b/src/lib.rs index 3ed5dcb..5123849 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ pub mod cdp; pub mod client; pub mod error; pub mod models; +pub mod tunnels; pub use client::{Client, ClientBuilder}; pub use error::{Error, Result}; diff --git a/src/main.rs b/src/main.rs index dfa17eb..c501857 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,6 +32,11 @@ enum Command { command: AuthCommand, }, Doctor, + /// Forward cloud browser traffic through this machine's network. + Tunnel { + #[command(subcommand)] + command: TunnelCommand, + }, Session { #[command(subcommand)] command: SessionCommand, @@ -49,6 +54,24 @@ enum Command { }, } +#[derive(Subcommand)] +enum TunnelCommand { + /// Print the ready tunnel ID, then stay running until Ctrl+C or revocation. + Open { + /// Stop and revoke after this many seconds; 0 waits until interrupted. + #[arg(long, default_value_t = 0)] + duration: u64, + }, + Status { + #[arg(long)] + tunnel_id: String, + }, + Close { + #[arg(long)] + tunnel_id: String, + }, +} + #[derive(Subcommand)] enum AuthCommand { Status, @@ -149,6 +172,9 @@ struct CreateSessionArgs { weak_lock: bool, #[arg(long)] official_proxy: bool, + /// ID from a running `tunnel open` process on the local network machine. + #[arg(long, conflicts_with = "official_proxy")] + local_proxy_tunnel_id: Option, #[arg(long)] custom_image_id: Option, #[arg(long)] @@ -286,10 +312,51 @@ enum ActionCommand { } fn main() -> ExitCode { - let result = run(Cli::parse()); + let cli = Cli::parse(); + if let Command::Tunnel { + command: TunnelCommand::Open { duration }, + } = cli.command + { + return match run_tunnel_open(duration) { + Ok(()) => ExitCode::SUCCESS, + Err(error) => { + print_error(&mut io::stderr().lock(), &error); + ExitCode::FAILURE + } + }; + } + let result = run(cli); write_result(result, &mut io::stdout().lock(), &mut io::stderr().lock()) } +fn run_tunnel_open(duration: u64) -> Result<()> { + use std::sync::{ + Arc, + atomic::{AtomicBool, Ordering}, + }; + let interrupted = Arc::new(AtomicBool::new(false)); + let flag = interrupted.clone(); + ctrlc::set_handler(move || flag.store(true, Ordering::Release)) + .map_err(|_| Error::Tunnel("cannot install shutdown handler".into()))?; + let mut tunnel = Client::from_env()?.open_tunnel()?; + if !interrupted.load(Ordering::Acquire) { + print_json( + &mut io::stdout().lock(), + &json!({"ok":true,"data":{ + "tunnel_id":tunnel.id(),"region_id":tunnel.region_id(),"ready":true + }}), + )?; + } + let started = std::time::Instant::now(); + while !interrupted.load(Ordering::Acquire) + && tunnel.is_running() + && (duration == 0 || started.elapsed() < Duration::from_secs(duration)) + { + std::thread::sleep(Duration::from_millis(50)); + } + tunnel.close() +} + fn write_result( result: Result, stdout: &mut impl Write, @@ -329,6 +396,17 @@ fn run(cli: Cli) -> Result { } Command::Auth { command } => run_auth(command), Command::Doctor => doctor(), + Command::Tunnel { command } => { + let client = Client::from_env()?; + match command { + TunnelCommand::Open { .. } => unreachable!("foreground tunnel handled by main"), + TunnelCommand::Status { tunnel_id } => client.tunnel_status(&tunnel_id), + TunnelCommand::Close { tunnel_id } => { + client.close_tunnel(&tunnel_id)?; + Ok(json!({"tunnel_id":tunnel_id,"closed":true})) + } + } + } Command::Session { command } => run_session(Client::from_env()?, command), Command::Context { command } => run_context(Client::from_env()?, command), Command::Action { command, target_id } => { @@ -398,6 +476,9 @@ fn run_session(client: Client, command: SessionCommand) -> Result { context_description: args.context_description, weak_lock: args.weak_lock.then_some(true), official_proxy: args.official_proxy.then_some(true), + proxy: args + .local_proxy_tunnel_id + .map(|id| json!({"type":"local","tunnel_id":id})), custom_image_id: args.custom_image_id, window_size: args.window_size, downloads: args.downloads.then_some(json!({"enabled":true})), @@ -596,6 +677,7 @@ fn error_kind(error: &Error) -> &'static str { Error::Json(_) => "json_error", Error::Io(_) => "io_error", Error::Cdp(_) => "cdp_error", + Error::Tunnel(_) => "tunnel_error", } } fn print_json(writer: &mut impl Write, value: &Value) -> io::Result<()> { diff --git a/src/tunnels.rs b/src/tunnels.rs new file mode 100644 index 0000000..a5c2584 --- /dev/null +++ b/src/tunnels.rs @@ -0,0 +1,621 @@ +//! Reverse WebSocket tunnel: destination DNS and TCP run on this machine. +use std::{ + collections::{BTreeMap, VecDeque}, + io::{self, Read, Write}, + net::{Shutdown, TcpStream}, + sync::{ + Arc, + atomic::{AtomicBool, AtomicUsize, Ordering}, + mpsc, + }, + thread::{self, JoinHandle}, + time::{Duration, Instant}, +}; + +use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; +use reqwest::Method; +use serde::Deserialize; +use serde_json::{Value, json}; +use tungstenite::Message; + +use crate::{Client, Error, Result, cdp::proxy}; + +const FRAME: usize = 65536; +const WINDOW: usize = 262144; +const MAX_STREAMS: usize = 128; +const CONNECT_TIMEOUT: Duration = Duration::from_secs(15); +static CONNECTING: AtomicUsize = AtomicUsize::new(0); + +pub fn tunnel_region(id: &str) -> Result { + let invalid = || Error::Config("invalid local tunnel id".into()); + let parts: Vec<_> = id.split('.').collect(); + if parts.len() != 3 || parts[0] != "lt1" { + return Err(invalid()); + } + let uuid = parts[2].as_bytes(); + if uuid.len() != 36 + || !uuid.iter().enumerate().all(|(i, b)| { + if [8, 13, 18, 23].contains(&i) { + *b == b'-' + } else { + b.is_ascii_digit() || (b'a'..=b'f').contains(b) + } + }) + { + return Err(invalid()); + } + let bytes = URL_SAFE_NO_PAD.decode(parts[1]).map_err(|_| invalid())?; + let region = String::from_utf8(bytes).map_err(|_| invalid())?; + if region.is_empty() + || region.len() > 64 + || region.starts_with('-') + || !region + .bytes() + .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-') + || URL_SAFE_NO_PAD.encode(®ion) != parts[1] + { + return Err(invalid()); + } + Ok(region) +} + +#[derive(Deserialize)] +struct Ticket { + id: String, + region_id: String, + ticket: String, + ws_url: String, +} + +impl Client { + /// Opens a tunnel and waits for the gateway's ready frame. Keep the handle alive. + pub fn open_tunnel(&self) -> Result { + LocalTunnel::open(self) + } + + pub fn tunnel_status(&self, id: &str) -> Result { + self.tunnel_request(Some(id), "status") + } + + pub fn close_tunnel(&self, id: &str) -> Result { + self.tunnel_request(Some(id), "close") + } + + fn tunnel_request(&self, id: Option<&str>, action: &str) -> Result { + let body = self.tunnel_region_body(id)?; + let path = match id { + Some(id) if action == "close" => format!("/instance/v1/tunnels/{id}"), + Some(id) => format!("/instance/v1/tunnels/{id}/{action}"), + None => "/instance/v1/tunnels".into(), + }; + self.json( + if action == "close" { + Method::DELETE + } else { + Method::POST + }, + &path, + Some(body), + ) + } +} + +/// Owned local network connector. Drop stops forwarding and best-effort revokes it. +/// Call close explicitly to observe revocation errors. Reconnection never replays TCP. +pub struct LocalTunnel { + id: String, + region: String, + client: Client, + stop: Arc, + ready: Arc, + worker: Option>>, + revoked: bool, +} + +impl LocalTunnel { + fn open(client: &Client) -> Result { + let client = client.tunnel_client()?; + let ticket: Ticket = serde_json::from_value(client.tunnel_request(None, "")?)?; + let mut handle = Self { + id: ticket.id.clone(), + region: ticket.region_id.clone(), + client, + stop: Arc::new(AtomicBool::new(false)), + ready: Arc::new(AtomicBool::new(false)), + worker: None, + revoked: false, + }; + if tunnel_region(&ticket.id)? != ticket.region_id { + return Err(Error::Tunnel("gateway returned a mismatched region".into())); + } + let connection = Connection::connect(&ticket)?; + handle.ready.store(true, Ordering::Release); + let client = handle.client.clone(); + let stop = handle.stop.clone(); + let ready = handle.ready.clone(); + handle.worker = Some(thread::Builder::new().name("local-tunnel".into()).spawn( + move || { + let mut connection = connection; + loop { + let _ = connection.run(&stop); + ready.store(false, Ordering::Release); + // Drop all TCP streams before acquiring a fresh owner epoch. + drop(connection); + let mut delay = 1; + loop { + if wait_stop(&stop, Duration::from_secs(delay)) { + return Ok(()); + } + let reconnect = client.tunnel_request(Some(&ticket.id), "connect-ticket"); + let value = match reconnect { + Ok(value) => value, + Err(Error::NotFound(_)) => return Ok(()), + Err(Error::Authentication(_)) => { + return Err(Error::Tunnel( + "tunnel authorization was revoked".into(), + )); + } + Err(_) => { + delay = (delay * 2).min(15); + continue; + } + }; + if stop.load(Ordering::Acquire) { + return Ok(()); + } + let Some(token) = value["ticket"].as_str() else { + return Err(Error::Tunnel("missing reconnect ticket".into())); + }; + let next = Ticket { + ticket: token.into(), + id: ticket.id.clone(), + region_id: ticket.region_id.clone(), + ws_url: ticket.ws_url.clone(), + }; + match Connection::connect(&next) { + Ok(new) => { + connection = new; + ready.store(true, Ordering::Release); + break; + } + Err(_) => delay = (delay * 2).min(15), + } + } + } + }, + )?); + Ok(handle) + } + + pub fn id(&self) -> &str { + &self.id + } + pub fn region_id(&self) -> &str { + &self.region + } + pub fn ready(&self) -> bool { + self.ready.load(Ordering::Acquire) + } + pub fn is_running(&self) -> bool { + self.worker.as_ref().is_some_and(|w| !w.is_finished()) + } + + pub fn close(&mut self) -> Result<()> { + self.stop.store(true, Ordering::Release); + self.ready.store(false, Ordering::Release); + let worker_result = self + .worker + .take() + .map(|w| { + w.join().unwrap_or_else(|_| { + Err(Error::Tunnel("tunnel worker stopped unexpectedly".into())) + }) + }) + .unwrap_or(Ok(())); + if !self.revoked { + match self.client.close_tunnel(&self.id) { + Ok(_) | Err(Error::NotFound(_)) => self.revoked = true, + Err(error) => return Err(error), + } + } + worker_result + } +} + +impl Drop for LocalTunnel { + fn drop(&mut self) { + let _ = self.close(); + } +} + +fn wait_stop(stop: &AtomicBool, duration: Duration) -> bool { + let deadline = Instant::now() + duration; + while Instant::now() < deadline { + if stop.load(Ordering::Acquire) { + return true; + } + thread::sleep(Duration::from_millis(10)); + } + stop.load(Ordering::Acquire) +} + +fn tunnel_error() -> Error { + Error::Tunnel("invalid gateway frame".into()) +} +fn would_block(error: &tungstenite::Error) -> bool { + matches!(error, tungstenite::Error::Io(e) if e.kind() == io::ErrorKind::WouldBlock) +} + +struct Stream { + socket: Option, + pending: Option>>, + cancelled: Arc, + deadline: Instant, + send_credit: usize, + receive_credit: usize, + incoming: VecDeque, + remote_fin: bool, + write_closed: bool, + local_fin: bool, +} +impl Drop for Stream { + fn drop(&mut self) { + self.cancelled.store(true, Ordering::Release); + if let Some(socket) = &self.socket { + let _ = socket.shutdown(Shutdown::Both); + } + } +} + +struct ConnectPermit; +impl Drop for ConnectPermit { + fn drop(&mut self) { + CONNECTING.fetch_sub(1, Ordering::AcqRel); + } +} + +impl Stream { + fn open(host: String, port: u16) -> Result { + // Keep the bounded admission compatible with both older toolchains and + // newer Rust versions that renamed AtomicUsize::fetch_update. + let mut count = CONNECTING.load(Ordering::Acquire); + loop { + if count >= MAX_STREAMS { + return Err(Error::Tunnel("local connect capacity exceeded".into())); + } + match CONNECTING.compare_exchange_weak( + count, + count + 1, + Ordering::AcqRel, + Ordering::Acquire, + ) { + Ok(_) => break, + Err(current) => count = current, + } + } + let permit = ConnectPermit; + let cancelled = Arc::new(AtomicBool::new(false)); + let cancel = cancelled.clone(); + let (send, receive) = mpsc::sync_channel(1); + thread::Builder::new() + .name("local-connect".into()) + .spawn(move || { + let _permit = permit; + let result = proxy::connect_local_tcp(&host, port, CONNECT_TIMEOUT, &cancel); + let _ = send.send(result); + })?; + Ok(Self { + socket: None, + pending: Some(receive), + cancelled, + deadline: Instant::now() + CONNECT_TIMEOUT, + send_credit: WINDOW, + receive_credit: WINDOW, + incoming: VecDeque::new(), + remote_fin: false, + write_closed: false, + local_fin: false, + }) + } +} + +struct Connection { + ws: proxy::Socket, + transport: Arc, + streams: BTreeMap, + outgoing: VecDeque, + last_id: u32, + last_serviced: u32, + last_ping: Instant, +} + +impl Drop for Connection { + fn drop(&mut self) { + let _ = self.transport.shutdown(Shutdown::Both); + } +} + +impl Connection { + fn connect(ticket: &Ticket) -> Result { + let url = url::Url::parse(&ticket.ws_url) + .map_err(|_| Error::Config("invalid tunnel endpoint".into()))?; + if !url.username().is_empty() + || url.password().is_some() + || url.query().is_some() + || url.fragment().is_some() + || !(url.scheme() == "wss" + || (url.scheme() == "ws" + && ["localhost", "127.0.0.1", "[::1]"].contains(&url.host_str().unwrap_or("")))) + { + return Err(Error::Config( + "tunnel endpoint must use wss (ws is allowed only on loopback)".into(), + )); + } + let (ws, transport) = proxy::connect_tunnel(&ticket.ws_url, &ticket.ticket) + .map_err(|_| Error::Tunnel("gateway WebSocket handshake failed".into()))?; + transport.set_nonblocking(true)?; + let mut this = Self { + ws, + transport, + streams: BTreeMap::new(), + outgoing: VecDeque::new(), + last_id: 0, + last_serviced: 0, + last_ping: Instant::now(), + }; + let deadline = Instant::now() + CONNECT_TIMEOUT; + loop { + if Instant::now() >= deadline { + return Err(Error::Timeout("local tunnel ready".into())); + } + match this.ws.read() { + Ok(Message::Text(text)) => { + if text.len() > 4096 { + return Err(tunnel_error()); + } + let value: Value = serde_json::from_str(&text).map_err(|_| tunnel_error())?; + if value["type"] != "ready" + || value["id"] != ticket.id + || value.get("epoch").is_none() + { + return Err(tunnel_error()); + } + return Ok(this); + } + Ok(Message::Ping(_)) => { + let _ = this.ws.flush(); + } + Err(e) if would_block(&e) => thread::sleep(Duration::from_millis(10)), + _ => return Err(Error::Tunnel("gateway closed before ready".into())), + } + } + } + + fn control(&mut self, value: Value) { + self.outgoing + .push_back(Message::Text(value.to_string().into())); + } + fn reset(&mut self, id: u32) { + self.streams.remove(&id); + self.control(json!({"type":"reset","id":id,"code":"stream_closed"})); + } + + fn receive(&mut self, message: Message) -> Result<()> { + match message { + Message::Ping(_) => self.last_ping = Instant::now(), + Message::Pong(_) => (), + Message::Close(_) => return Err(Error::Tunnel("gateway disconnected".into())), + Message::Binary(data) => { + if data.len() < 5 || data.len() > FRAME + 4 { + return Err(tunnel_error()); + } + let id = u32::from_be_bytes(data[..4].try_into().unwrap()); + if let Some(stream) = self.streams.get_mut(&id) { + let count = data.len() - 4; + if stream.socket.is_none() || stream.remote_fin || count > stream.receive_credit + { + return Err(tunnel_error()); + } + stream.receive_credit -= count; + stream.incoming.extend(&data[4..]); + } + } + Message::Text(text) => { + if text.len() > 4096 { + return Err(tunnel_error()); + } + let value: Value = serde_json::from_str(&text).map_err(|_| tunnel_error())?; + let id = value["id"] + .as_u64() + .and_then(|n| u32::try_from(n).ok()) + .filter(|n| *n > 0) + .ok_or_else(tunnel_error)?; + match value["type"].as_str() { + Some("open") => { + let host = value["host"] + .as_str() + .filter(|s| { + !s.is_empty() + && s.len() <= 253 + && !s.chars().any(|c| { + c.is_whitespace() || c.is_control() || c == '/' || c == '\\' + }) + }) + .ok_or_else(tunnel_error)?; + let port = value["port"] + .as_u64() + .and_then(|n| u16::try_from(n).ok()) + .filter(|n| *n > 0) + .ok_or_else(tunnel_error)?; + if id <= self.last_id { + return Err(tunnel_error()); + } + self.last_id = id; + if self.streams.len() >= MAX_STREAMS { + self.reset(id); + } else { + match Stream::open(host.into(), port) { + Ok(stream) => { + self.streams.insert(id, stream); + } + Err(_) => self.reset(id), + } + } + } + Some("reset") => { + self.streams.remove(&id); + } + Some("window") => { + if let Some(stream) = self.streams.get_mut(&id) { + let credit = value["credit"] + .as_u64() + .filter(|n| *n > 0 && *n <= WINDOW as u64) + .ok_or_else(tunnel_error)? + as usize; + if stream.send_credit + credit > WINDOW { + return Err(tunnel_error()); + } + stream.send_credit += credit; + } + } + Some("fin") => { + if let Some(stream) = self.streams.get_mut(&id) { + if stream.socket.is_none() || stream.remote_fin { + return Err(tunnel_error()); + } + stream.remote_fin = true; + } + } + _ => return Err(tunnel_error()), + } + } + _ => return Err(tunnel_error()), + } + Ok(()) + } + + // Each frame accepted by tungstenite is removed exactly once, including + // WouldBlock (already buffered). WriteBufferFull retains the original frame. + fn flush(&mut self) -> Result { + match self.ws.flush() { + Ok(()) => (), + Err(e) if would_block(&e) => return Ok(false), + Err(e) => return Err(e.into()), + } + while let Some(message) = self.outgoing.pop_front() { + match self.ws.send(message) { + Ok(()) => (), + Err(e) if would_block(&e) => return Ok(false), + Err(tungstenite::Error::WriteBufferFull(message)) => { + self.outgoing.push_front(message); + return Ok(false); + } + Err(e) => return Err(e.into()), + } + } + Ok(true) + } + + fn run(&mut self, stop: &AtomicBool) -> Result<()> { + let mut buffer = [0u8; FRAME]; + while !stop.load(Ordering::Acquire) { + if self.last_ping.elapsed() > Duration::from_secs(20) { + return Err(Error::Timeout("local tunnel heartbeat".into())); + } + for _ in 0..256 { + if self.outgoing.len() >= 256 { + break; + } + match self.ws.read() { + Ok(message) => self.receive(message)?, + Err(e) if would_block(&e) => break, + Err(e) => return Err(e.into()), + } + } + if !self.flush()? { + thread::sleep(Duration::from_millis(2)); + continue; + } + let mut ids: Vec<_> = self.streams.keys().copied().collect(); + let start = ids.partition_point(|id| *id <= self.last_serviced); + ids.rotate_left(start); + for id in ids { + self.last_serviced = id; + let Some(stream) = self.streams.get_mut(&id) else { + continue; + }; + if let Some(pending) = &stream.pending { + match pending.try_recv() { + Ok(Ok(socket)) => { + stream.socket = Some(socket); + stream.pending = None; + self.control(json!({"type":"opened","id":id})); + } + Err(mpsc::TryRecvError::Empty) if Instant::now() < stream.deadline => (), + _ => self.reset(id), + } + continue; + } + let socket = stream.socket.as_mut().unwrap(); + let mut controls = Vec::new(); + if !stream.incoming.is_empty() { + match socket.write(stream.incoming.make_contiguous()) { + Ok(0) => { + self.reset(id); + continue; + } + Ok(n) => { + stream.incoming.drain(..n); + stream.receive_credit += n; + controls.push(json!({"type":"window","id":id,"credit":n})); + } + Err(e) if e.kind() == io::ErrorKind::WouldBlock => (), + Err(_) => { + self.reset(id); + continue; + } + } + } + if stream.remote_fin && stream.incoming.is_empty() && !stream.write_closed { + if socket.shutdown(Shutdown::Write).is_err() { + self.reset(id); + continue; + } + stream.write_closed = true; + } + if !stream.local_fin && stream.send_credit > 0 { + match socket.read(&mut buffer[..FRAME.min(stream.send_credit)]) { + Ok(0) => { + stream.local_fin = true; + controls.push(json!({"type":"fin","id":id})); + } + Ok(n) => { + stream.send_credit -= n; + let mut data = id.to_be_bytes().to_vec(); + data.extend_from_slice(&buffer[..n]); + self.outgoing.push_back(Message::Binary(data.into())); + } + Err(e) if e.kind() == io::ErrorKind::WouldBlock => (), + Err(_) => { + self.reset(id); + continue; + } + } + } + let finished = stream.local_fin && stream.write_closed; + for control in controls { + self.control(control); + } + if finished { + self.streams.remove(&id); + } + if !self.flush()? { + break; + } + } + self.flush()?; + thread::sleep(Duration::from_millis(2)); + } + Ok(()) + } +} diff --git a/tests/local_tunnels.rs b/tests/local_tunnels.rs new file mode 100644 index 0000000..1d1978f --- /dev/null +++ b/tests/local_tunnels.rs @@ -0,0 +1,567 @@ +mod support; + +use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; +use httpmock::{ + Method::{DELETE, POST}, + Mock, MockServer, +}; +use lexmount_browser::{Client, Error, models::CreateSession}; +use serde_json::{Value, json}; +use std::{ + io::{BufRead, BufReader, Read, Write}, + net::{Shutdown, TcpListener, TcpStream}, + sync::mpsc, + thread, + time::{Duration, Instant}, +}; +use tungstenite::{ + Message, WebSocket, + handshake::server::{Request, Response}, +}; + +const FRAME: usize = 65536; +const WINDOW: usize = 262144; +fn id() -> String { + format!( + "lt1.{}.12345678-1234-1234-1234-123456789abc", + URL_SAFE_NO_PAD.encode("nanjing-1") + ) +} +fn client(api: &MockServer) -> Client { + Client::builder() + .api_key("local-test-key") + .project_id("local-test-project") + .base_url(api.base_url()) + .build() + .unwrap() +} +fn setup<'a>(api: &'a MockServer, ws: &str) -> (Mock<'a>, Mock<'a>) { + let create = api.mock(|when, then| { when.method(POST).path("/instance/v1/tunnels") + .header("x-api-key", "local-test-key").header("x-project-id", "local-test-project"); + then.status(201).json_body(json!({"id":id(),"region_id":"nanjing-1","ticket":"single-use-test-ticket","ws_url":ws})); }); + let close = api.mock(|when, then| { + when.method(DELETE) + .path(format!("/instance/v1/tunnels/{}", id())) + .json_body(json!({"region_id":"nanjing-1"})); + then.status(200).json_body(json!({"closed":true})); + }); + (create, close) +} +#[allow(clippy::result_large_err)] // tungstenite requires this callback error type. +fn ws_accept(listener: &TcpListener, token: &str) -> WebSocket { + let (stream, _) = listener.accept().unwrap(); + stream + .set_read_timeout(Some(Duration::from_secs(8))) + .unwrap(); + stream + .set_write_timeout(Some(Duration::from_secs(8))) + .unwrap(); + tungstenite::accept_hdr(stream, |req: &Request, mut res: Response| { + assert_eq!(req.headers()["authorization"], format!("Bearer {token}")); + assert_eq!( + req.headers()["sec-websocket-protocol"], + "lexmount.local-proxy.v1" + ); + assert!(!req.uri().to_string().contains("ticket")); + res.headers_mut().insert( + "sec-websocket-protocol", + "lexmount.local-proxy.v1".parse().unwrap(), + ); + Ok(res) + }) + .unwrap() +} +fn control(ws: &mut WebSocket, value: Value) { + ws.send(Message::Text(value.to_string().into())).unwrap(); +} +fn read_control(ws: &mut WebSocket) -> Value { + loop { + match ws.read().unwrap() { + Message::Text(s) => return serde_json::from_str(&s).unwrap(), + Message::Pong(_) => (), + other => panic!("unexpected frame: {other:?}"), + } + } +} +fn ready(ws: &mut WebSocket, epoch: &str) { + control(ws, json!({"type":"ready","id":id(),"epoch":epoch})); +} +fn wait_closed(ws: &mut WebSocket) { + loop { + match ws.read() { + Err(_) | Ok(Message::Close(_)) => break, + Ok(Message::Ping(_)) => { + let _ = ws.flush(); + } + _ => (), + } + } +} +fn wait_until(mut check: impl FnMut() -> bool) { + let deadline = Instant::now() + Duration::from_secs(6); + while !check() { + assert!(Instant::now() < deadline, "condition timed out"); + thread::sleep(Duration::from_millis(10)); + } +} + +#[test] +fn large_duplex_transfer_half_close_and_drop_revocation() { + if support::isolated_test( + "large_duplex_transfer_half_close_and_drop_revocation", + Duration::from_secs(30), + ) { + return; + } + let target = TcpListener::bind("127.0.0.1:0").unwrap(); + let port = target.local_addr().unwrap().port(); + let payload: Vec<_> = (0..2 * 1024 * 1024).map(|i| (i % 251) as u8).collect(); + let expected = payload.clone(); + let local = thread::spawn(move || { + let (mut stream, _) = target.accept().unwrap(); + stream + .set_read_timeout(Some(Duration::from_secs(8))) + .unwrap(); + let mut bytes = Vec::new(); + stream.read_to_end(&mut bytes).unwrap(); + assert_eq!(bytes, expected); + bytes.reverse(); + stream.write_all(&bytes).unwrap(); + stream.shutdown(Shutdown::Write).unwrap(); + }); + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let url = format!( + "ws://{}/local-proxy/{}", + listener.local_addr().unwrap(), + id() + ); + let (done, completed) = mpsc::channel(); + let gateway = thread::spawn(move || { + let mut ws = ws_accept(&listener, "single-use-test-ticket"); + ready(&mut ws, "one"); + control( + &mut ws, + json!({"type":"open","id":1,"host":"localhost","port":port}), + ); + assert_eq!(read_control(&mut ws), json!({"type":"opened","id":1})); + let mut sent = 0; + let mut credit = WINDOW; + while sent < payload.len() { + while credit > 0 && sent < payload.len() { + let n = FRAME.min(credit).min(payload.len() - sent); + let mut frame = 1u32.to_be_bytes().to_vec(); + frame.extend_from_slice(&payload[sent..sent + n]); + ws.send(Message::Binary(frame.into())).unwrap(); + sent += n; + credit -= n; + } + if sent < payload.len() { + let ack = read_control(&mut ws); + assert_eq!(ack["type"], "window"); + credit += ack["credit"].as_u64().unwrap() as usize; + } + } + control(&mut ws, json!({"type":"fin","id":1})); + let mut received = Vec::new(); + loop { + match ws.read().unwrap() { + Message::Binary(data) => { + assert_eq!(&data[..4], &1u32.to_be_bytes()); + received.extend_from_slice(&data[4..]); + control( + &mut ws, + json!({"type":"window","id":1,"credit":data.len()-4}), + ); + } + Message::Text(s) => { + let value: Value = serde_json::from_str(&s).unwrap(); + if value["type"] == "fin" { + break; + } + assert_eq!(value["type"], "window"); + } + other => panic!("unexpected {other:?}"), + } + } + assert_eq!(received, payload.into_iter().rev().collect::>()); + // Unknown/reset streams may have data already in flight. + ws.send(Message::Binary(vec![0, 0, 0, 1, 5].into())) + .unwrap(); + done.send(()).unwrap(); + wait_closed(&mut ws); + }); + let api = MockServer::start(); + let (create, close) = setup(&api, &url); + let tunnel = client(&api).open_tunnel().unwrap(); + assert!(tunnel.ready()); + assert_eq!(tunnel.region_id(), "nanjing-1"); + completed.recv_timeout(Duration::from_secs(15)).unwrap(); + drop(tunnel); + create.assert_hits(1); + close.assert_hits(1); + gateway.join().unwrap(); + local.join().unwrap(); +} + +#[test] +fn reconnect_uses_new_ticket_and_closes_old_tcp_streams() { + if support::isolated_test( + "reconnect_uses_new_ticket_and_closes_old_tcp_streams", + Duration::from_secs(20), + ) { + return; + } + let target = TcpListener::bind("127.0.0.1:0").unwrap(); + let port = target.local_addr().unwrap().port(); + let local = thread::spawn(move || { + let (mut stream, _) = target.accept().unwrap(); + stream + .set_read_timeout(Some(Duration::from_secs(5))) + .unwrap(); + let mut byte = [0]; + assert_eq!(stream.read(&mut byte).unwrap(), 0); + }); + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let url = format!("ws://{}/local-proxy", listener.local_addr().unwrap()); + let (done, completed) = mpsc::channel(); + let gateway = thread::spawn(move || { + let mut ws = ws_accept(&listener, "single-use-test-ticket"); + ready(&mut ws, "one"); + control( + &mut ws, + json!({"type":"open","id":1,"host":"localhost","port":port}), + ); + assert_eq!(read_control(&mut ws)["type"], "opened"); + drop(ws); + let mut ws = ws_accept(&listener, "replacement-ticket"); + ready(&mut ws, "two"); + // New epoch may reuse IDs, and ping/pong stays on the WebSocket. + ws.send(Message::Ping(vec![1, 2, 3].into())).unwrap(); + assert_eq!(ws.read().unwrap(), Message::Pong(vec![1, 2, 3].into())); + done.send(()).unwrap(); + wait_closed(&mut ws); + }); + let api = MockServer::start(); + let (_, close) = setup(&api, &url); + let ticket = api.mock(|when, then| { + when.method(POST) + .path(format!("/instance/v1/tunnels/{}/connect-ticket", id())) + .json_body(json!({"region_id":"nanjing-1"})); + then.status(200) + .json_body(json!({"ticket":"replacement-ticket"})); + }); + let mut tunnel = client(&api).open_tunnel().unwrap(); + completed.recv_timeout(Duration::from_secs(8)).unwrap(); + assert!(tunnel.ready()); + tunnel.close().unwrap(); + ticket.assert_hits(1); + close.assert_hits(1); + gateway.join().unwrap(); + local.join().unwrap(); +} + +#[test] +fn invalid_credit_disconnects_and_revoked_tunnel_stops_reconnecting() { + if support::isolated_test( + "invalid_credit_disconnects_and_revoked_tunnel_stops_reconnecting", + Duration::from_secs(15), + ) { + return; + } + let target = TcpListener::bind("127.0.0.1:0").unwrap(); + let port = target.local_addr().unwrap().port(); + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let url = format!("ws://{}/local-proxy", listener.local_addr().unwrap()); + let gateway = thread::spawn(move || { + let mut ws = ws_accept(&listener, "single-use-test-ticket"); + ready(&mut ws, "one"); + control( + &mut ws, + json!({"type":"open","id":1,"host":"127.0.0.1","port":port}), + ); + assert_eq!(read_control(&mut ws)["type"], "opened"); + control(&mut ws, json!({"type":"window","id":1,"credit":1})); + wait_closed(&mut ws); + }); + let api = MockServer::start(); + let (_, close) = setup(&api, &url); + let ticket = api.mock(|when, then| { + when.method(POST) + .path(format!("/instance/v1/tunnels/{}/connect-ticket", id())); + then.status(404).json_body(json!({"error":"closed"})); + }); + let mut tunnel = client(&api).open_tunnel().unwrap(); + wait_until(|| !tunnel.is_running()); + assert!(!tunnel.ready()); + tunnel.close().unwrap(); + ticket.assert_hits(1); + close.assert_hits(1); + gateway.join().unwrap(); +} + +#[test] +fn bad_ready_and_redirect_fail_closed_and_revoke() { + if support::isolated_test( + "bad_ready_and_redirect_fail_closed_and_revoke", + Duration::from_secs(15), + ) { + return; + } + for redirect in [false, true] { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let url = format!("ws://{}/local-proxy", listener.local_addr().unwrap()); + let destination = TcpListener::bind("127.0.0.1:0").unwrap(); + destination.set_nonblocking(true).unwrap(); + let address = destination.local_addr().unwrap(); + let gateway = thread::spawn(move || { + if redirect { + let (mut stream, _) = listener.accept().unwrap(); + let mut request = [0; 4096]; + assert!(stream.read(&mut request).unwrap() > 0); + write!(stream,"HTTP/1.1 302 Found\r\nLocation: ws://{address}/stolen\r\nContent-Length: 0\r\n\r\n").unwrap(); + } else { + let mut ws = ws_accept(&listener, "single-use-test-ticket"); + control(&mut ws, json!({"type":"ready","id":"wrong","epoch":"one"})); + wait_closed(&mut ws); + } + }); + let api = MockServer::start(); + let (create, close) = setup(&api, &url); + let error = client(&api).open_tunnel().err().unwrap().to_string(); + assert!(!error.contains("single-use-test-ticket")); + create.assert_hits(1); + close.assert_hits(1); + gateway.join().unwrap(); + assert_eq!( + destination.accept().unwrap_err().kind(), + std::io::ErrorKind::WouldBlock + ); + } +} + +#[test] +fn local_session_region_binding_and_cli_conflicts() { + if support::isolated_test( + "local_session_region_binding_and_cli_conflicts", + Duration::from_secs(15), + ) { + return; + } + let api = MockServer::start(); + let create = api.mock(|when, then| { + when.method(POST).path("/instance/v2").json_body_partial( + json!({"region_id":"nanjing-1","proxy":{"type":"local","tunnel_id":id()}}).to_string(), + ); + then.status(202) + .json_body(json!({"session_id":"session-test"})); + }); + let get=api.mock(|when,then|{when.method(POST).path("/instance/session");then.status(200).json_body(json!({"session_id":"session-test","status":"active","ws":"ws://127.0.0.1:1","region_id":"nanjing-1"}));}); + let temp = tempfile::tempdir().unwrap(); + let data = support::data(support::cli( + &api.base_url(), + temp.path(), + &["session", "create", "--local-proxy-tunnel-id", &id()], + )); + assert_eq!(data["session_id"], "session-test"); + create.assert_hits(1); + get.assert_hits(1); + let output = support::cli( + &api.base_url(), + temp.path(), + &[ + "session", + "create", + "--local-proxy-tunnel-id", + &id(), + "--official-proxy", + ], + ); + assert!(!output.status.success()); + create.assert_hits(1); + let client = client(&api); + let request = CreateSession { + proxy: Some(json!({"type":"local","tunnel_id":id()})), + official_proxy: Some(true), + ..Default::default() + }; + assert!(matches!( + client.create_session(request, Duration::from_secs(1)), + Err(Error::Config(_)) + )); + let other = Client::builder() + .api_key("x") + .project_id("x") + .base_url(api.base_url()) + .region("hk-1") + .build() + .unwrap(); + assert!(matches!(other.tunnel_status(&id()), Err(Error::Config(_)))); + create.assert_hits(1); +} + +#[test] +fn foreground_cli_prints_ready_once_then_revokes_at_duration() { + if support::isolated_test( + "foreground_cli_prints_ready_once_then_revokes_at_duration", + Duration::from_secs(15), + ) { + return; + } + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let url = format!("ws://{}/local-proxy", listener.local_addr().unwrap()); + let gateway = thread::spawn(move || { + let mut ws = ws_accept(&listener, "single-use-test-ticket"); + ready(&mut ws, "one"); + wait_closed(&mut ws); + }); + let api = MockServer::start(); + let (_, close) = setup(&api, &url); + let temp = tempfile::tempdir().unwrap(); + let mut child = support::command( + &api.base_url(), + temp.path(), + &["tunnel", "open", "--duration", "1"], + ) + .spawn() + .unwrap(); + let mut reader = BufReader::new(child.stdout.take().unwrap()); + let mut line = String::new(); + reader.read_line(&mut line).unwrap(); + let ready: Value = serde_json::from_str(&line).unwrap(); + assert_eq!(ready["data"]["ready"], true); + assert_eq!(ready["data"]["tunnel_id"], id()); + assert!(!line.contains("ticket")); + assert!(child.try_wait().unwrap().is_none()); + let output = child.wait_with_output().unwrap(); + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + assert!(output.stderr.is_empty()); + let mut rest = String::new(); + reader.read_to_string(&mut rest).unwrap(); + assert!(rest.is_empty()); + close.assert_hits(1); + gateway.join().unwrap(); +} + +#[test] +fn stream_failures_are_isolated_and_reset_closes_local_socket() { + if support::isolated_test( + "stream_failures_are_isolated_and_reset_closes_local_socket", + Duration::from_secs(20), + ) { + return; + } + let target = TcpListener::bind("127.0.0.1:0").unwrap(); + let port = target.local_addr().unwrap().port(); + let local = thread::spawn(move || { + let (mut stream, _) = target.accept().unwrap(); + stream + .set_read_timeout(Some(Duration::from_secs(5))) + .unwrap(); + stream.write_all(b"still-alive").unwrap(); + let mut b = [0]; + assert_eq!(stream.read(&mut b).unwrap(), 0); + }); + let unavailable = TcpListener::bind("127.0.0.1:0").unwrap(); + let bad_port = unavailable.local_addr().unwrap().port(); + drop(unavailable); + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let url = format!("ws://{}/local-proxy", listener.local_addr().unwrap()); + let (done, completed) = mpsc::channel(); + let gateway = thread::spawn(move || { + let mut ws = ws_accept(&listener, "single-use-test-ticket"); + ready(&mut ws, "one"); + control( + &mut ws, + json!({"type":"open","id":1,"host":"127.0.0.1","port":bad_port}), + ); + control( + &mut ws, + json!({"type":"open","id":2,"host":"127.0.0.1","port":port}), + ); + let mut failed = false; + let mut opened = false; + let mut data = Vec::new(); + while !failed || !opened || data.len() < 11 { + match ws.read().unwrap() { + Message::Text(text) => { + let value: Value = serde_json::from_str(&text).unwrap(); + match value["type"].as_str().unwrap() { + "reset" => { + assert_eq!(value["id"], 1); + failed = true; + } + "opened" => { + assert_eq!(value["id"], 2); + opened = true; + } + other => panic!("unexpected {other}"), + } + } + Message::Binary(bytes) => { + assert_eq!(&bytes[..4], &2u32.to_be_bytes()); + data.extend_from_slice(&bytes[4..]); + } + other => panic!("unexpected {other:?}"), + } + } + assert_eq!(data, b"still-alive"); + control(&mut ws, json!({"type":"reset","id":2,"code":"closed"})); + // Late data/FIN/window for a reset stream must not revive it. + ws.send(Message::Binary(vec![0, 0, 0, 2, 1].into())) + .unwrap(); + control(&mut ws, json!({"type":"fin","id":2})); + done.send(()).unwrap(); + wait_closed(&mut ws); + }); + let api = MockServer::start(); + let (_, close) = setup(&api, &url); + let mut tunnel = client(&api).open_tunnel().unwrap(); + completed.recv_timeout(Duration::from_secs(8)).unwrap(); + local.join().unwrap(); + assert!(tunnel.ready()); + tunnel.close().unwrap(); + gateway.join().unwrap(); + close.assert_hits(1); +} + +#[cfg(unix)] +#[test] +fn sigterm_revokes_foreground_tunnel() { + if support::isolated_test("sigterm_revokes_foreground_tunnel", Duration::from_secs(15)) { + return; + } + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let url = format!("ws://{}/local-proxy", listener.local_addr().unwrap()); + let gateway = thread::spawn(move || { + let mut ws = ws_accept(&listener, "single-use-test-ticket"); + ready(&mut ws, "one"); + wait_closed(&mut ws); + }); + let api = MockServer::start(); + let (_, close) = setup(&api, &url); + let temp = tempfile::tempdir().unwrap(); + let mut child = support::command(&api.base_url(), temp.path(), &["tunnel", "open"]) + .spawn() + .unwrap(); + let mut reader = BufReader::new(child.stdout.take().unwrap()); + let mut line = String::new(); + reader.read_line(&mut line).unwrap(); + assert!(line.contains("\"ready\":true")); + assert!( + std::process::Command::new("kill") + .args(["-TERM", &child.id().to_string()]) + .status() + .unwrap() + .success() + ); + wait_until(|| child.try_wait().unwrap().is_some()); + let output = child.wait_with_output().unwrap(); + assert!(output.status.success()); + assert!(output.stderr.is_empty()); + close.assert_hits(1); + gateway.join().unwrap(); +}