use std::ffi::OsString;
use std::io::{self, Read, Write};
use std::path::PathBuf;
use std::process::{Child, Command, Stdio};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{mpsc, Arc};
use std::time::{Duration, Instant};
use anyhow::{bail, Context, Result};
use serde::{Deserialize, Serialize};
use crate::devsbd::bridge::{self, Bridge};
use crate::devsbd::forward::{Opened, Opener, ProcessLookup, Route};
use crate::serve::machines::ssh::{self, SkipError, Tail};
use crate::state::State;
const MAGIC: &str = "devsandbox-tunnel";
const VERSION: u32 = 1;
const HEADER_LIMIT: usize = 64 * 1024;
const HEADER_WAIT: Duration = Duration::from_secs(10);
const STOP_POLL: Duration = Duration::from_millis(50);
const STDERR_WAIT: Duration = Duration::from_millis(500);
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Header {
Error { error: String },
Route {
label: String,
host: String,
port: u16,
#[serde(default, skip_serializing_if = "Option::is_none")]
hash: Option<String>,
#[serde(default)]
helper: bool,
},
}
impl Header {
fn of_route(route: &Route) -> Header {
Header::Route {
label: route.label.clone(),
host: route.host.clone(),
port: route.port,
hash: route.arch.and_then(crate::devsbd::hash).map(Into::into),
helper: route.process_container == route.container,
}
}
pub fn line(&self) -> String {
let json = serde_json::to_string(self).expect("a header serializes");
format!("{MAGIC} {VERSION} {json}\n")
}
}
#[derive(Debug)]
pub enum HeaderError {
Eof,
TooLong,
Bad(String),
Io(io::Error),
}
impl std::fmt::Display for HeaderError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
HeaderError::Eof => write!(f, "the tunnel ended before its header"),
HeaderError::TooLong => write!(f, "no tunnel header in the first {HEADER_LIMIT} bytes"),
HeaderError::Bad(why) => write!(f, "{why}"),
HeaderError::Io(e) => write!(f, "reading the tunnel header: {e}"),
}
}
}
pub fn read_header(r: &mut impl Read) -> Result<Header, HeaderError> {
ssh::skip_lines(r, HEADER_LIMIT, &mut None, parse_line).map_err(|e| match e {
SkipError::Eof => HeaderError::Eof,
SkipError::TooLong => HeaderError::TooLong,
SkipError::Io(e) => HeaderError::Io(e),
SkipError::Refused(e) => e,
})
}
fn parse_line(line: &[u8]) -> Result<Option<Header>, HeaderError> {
let line = String::from_utf8_lossy(line);
let Some(at) = line.find(&format!("{MAGIC} ")) else { return Ok(None) };
let rest = line[at + MAGIC.len() + 1..].trim_end_matches('\r');
let (version, json) = rest.split_once(' ').unwrap_or((rest, ""));
if version != VERSION.to_string() {
return Err(HeaderError::Bad(format!(
"tunnel header version `{version}`, this devsandbox speaks {VERSION}: update devsandbox on the older side"
)));
}
serde_json::from_str(json).map(Some).map_err(|e| HeaderError::Bad(format!("bad tunnel header: {e}")))
}
pub fn tunnel(dir: PathBuf, instance: Option<String>, service: Option<String>, port: u16) -> Result<()> {
use std::os::unix::process::CommandExt;
let route = (|| -> Result<Route, String> {
if instance.is_none() && service.is_none() {
return Err("name an instance or a service".into());
}
let key = match &instance {
Some(name) => {
let state = State::load().map_err(|e| format!("{e:#}"))?;
Some(super::resolve_instance_noninteractive(&state, name).map_err(|e| format!("{e:#}"))?)
}
None => None,
};
super::port::resolver(dir, key, service, port)()
})();
let header = match &route {
Ok(route) => Header::of_route(route),
Err(error) => Header::Error { error: error.clone() },
};
let mut out = io::stdout().lock();
out.write_all(header.line().as_bytes()).and_then(|()| out.flush()).context("writing the tunnel header")?;
drop(out);
match route {
Err(error) => bail!("{error}"),
Ok(route) => Err(bridge::bridge_command(&route.container).exec())
.with_context(|| format!("cannot exec a bridge into {}", route.container)),
}
}
pub type TunnelArgv = Box<dyn Fn() -> Result<Vec<OsString>, String> + Send + Sync>;
pub fn remote_opener(machine: String, argv: TunnelArgv) -> Opener {
Box::new(move |stop| open_remote(&machine, &argv()?, stop))
}
fn open_remote(machine: &str, argv: &[OsString], stop: &AtomicBool) -> Result<(Opened, Bridge), String> {
let (program, args) = argv.split_first().ok_or("empty tunnel argv")?;
let mut cmd = Command::new(program);
if let Some(agent) = bridge::host_agent() {
cmd.env("SSH_AUTH_SOCK", agent);
}
let mut child = cmd
.args(args)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.map_err(|e| format!("cannot run {}: {e}", program.to_string_lossy()))?;
let (Some(stdin), Some(mut stdout), Some(stderr)) = (child.stdin.take(), child.stdout.take(), child.stderr.take())
else {
reap(&mut child);
return Err("the tunnel's pipes are missing".into());
};
let tail = match Tail::collect(stderr, "tunnel-stderr") {
Ok(tail) => tail,
Err(e) => {
reap(&mut child);
return Err(format!("cannot start the tunnel's reader: {e}"));
}
};
let (tx, rx) = mpsc::channel();
let reader = std::thread::Builder::new().name("tunnel-header".into()).spawn(move || {
let header = read_header(&mut stdout);
let _ = tx.send((header, stdout));
});
if let Err(e) = reader {
reap(&mut child);
return Err(format!("cannot start the tunnel's reader: {e}"));
}
let deadline = Instant::now() + HEADER_WAIT;
let (header, stdout) = loop {
if stop.load(Ordering::Relaxed) {
reap(&mut child);
return Err("stopped".into());
}
let left = deadline.saturating_duration_since(Instant::now());
if left.is_zero() {
reap(&mut child);
return Err(no_header(machine, &tail.read(Duration::ZERO)));
}
match rx.recv_timeout(STOP_POLL.min(left)) {
Ok(got) => break got,
Err(mpsc::RecvTimeoutError::Timeout) => {}
Err(mpsc::RecvTimeoutError::Disconnected) => {
reap(&mut child);
return Err("the tunnel's reader died".into());
}
}
};
match header {
Ok(Header::Route { label, host, port, hash, helper }) => {
let label = format!("{machine}/{label}");
let stderr = TailReader { tail, rest: None };
let bridge = bridge::attach(child, stdin, stdout, stderr, hash.as_deref().unwrap_or_default(), &label, Some(machine));
let lookup = if helper { ProcessLookup::Helper { container: String::new() } } else { ProcessLookup::None };
Ok((Opened { label, host, port, lookup }, bridge))
}
Ok(Header::Error { error }) => {
reap(&mut child);
Err(error)
}
Err(HeaderError::Eof) => {
reap(&mut child);
Err(no_header(machine, &tail.read(STDERR_WAIT)))
}
Err(e) => {
reap(&mut child);
Err(format!("{machine}: {e}"))
}
}
}
fn no_header(machine: &str, tail: &str) -> String {
let refused = ["unrecognized subcommand", "unknown subcommand", "wasn't expected"];
if tail.contains("'tunnel'") && refused.iter().any(|r| tail.contains(r)) {
return format!("devsandbox on {machine} can't forward ports yet: update it");
}
match tail.trim() {
"" => format!("no answer from {machine}"),
tail => tail.to_string(),
}
}
fn reap(child: &mut Child) {
let _ = child.kill();
let _ = child.wait();
}
struct TailReader {
tail: Arc<Tail>,
rest: Option<io::Cursor<Vec<u8>>>,
}
impl Read for TailReader {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let tail = &self.tail;
self.rest.get_or_insert_with(|| io::Cursor::new(tail.read(STDERR_WAIT).into_bytes())).read(buf)
}
}
#[cfg(test)]
mod tests {
use std::net::{IpAddr, Ipv4Addr, TcpStream};
use super::*;
use crate::devsbd::forward::{Forward, ForwardSpec, ForwardState, HostPort};
fn route_header() -> Header {
Header::Route { label: "web:3000".into(), host: "127.0.0.1".into(), port: 3000, hash: Some("abc".into()), helper: true }
}
#[test]
fn headers_round_trip() {
for header in [
route_header(),
Header::Route { label: "db:5432 (via instance web)".into(), host: "db".into(), port: 5432, hash: None, helper: false },
Header::Error { error: "no sandbox instance `web`".into() },
] {
let line = header.line();
assert!(line.starts_with("devsandbox-tunnel 1 {") && line.ends_with("}\n"), "{line}");
assert_eq!(read_header(&mut line.as_bytes()).unwrap(), header);
}
assert_eq!(
Header::Error { error: "x".into() }.line(),
"devsandbox-tunnel 1 {\"error\":\"x\"}\n"
);
}
#[test]
fn the_route_header_says_where_the_port_lives() {
let route = |process_container: &str| Route {
container: "c".into(),
arch: None,
host: "db".into(),
port: 5432,
label: "db:5432".into(),
process_container: process_container.into(),
};
let header = |helper| Header::Route { label: "db:5432".into(), host: "db".into(), port: 5432, hash: None, helper };
assert_eq!(Header::of_route(&route("c")), header(true));
assert_eq!(Header::of_route(&route("svc")), header(false));
}
#[test]
fn noise_before_the_header_is_skipped_and_nothing_after_it_is_read() {
let stream = format!("Welcome to box!\r\nLast login: today\n\nmotd without a newline{}BRIDGE", route_header().line());
let mut r = stream.as_bytes();
assert_eq!(read_header(&mut r).unwrap(), route_header());
assert_eq!(r, b"BRIDGE", "the bridge's bytes stay unread");
let crlf = route_header().line().replace('\n', "\r\n");
assert_eq!(read_header(&mut crlf.as_bytes()).unwrap(), route_header());
}
#[test]
fn a_reader_is_bounded_and_refuses_what_it_cant_read() {
let noise = "x".repeat(HEADER_LIMIT + 1);
assert!(matches!(read_header(&mut noise.as_bytes()), Err(HeaderError::TooLong)));
let lines = "noise\n".repeat(HEADER_LIMIT / 6 + 1) + &route_header().line();
assert!(matches!(read_header(&mut lines.as_bytes()), Err(HeaderError::TooLong)));
let e = read_header(&mut &b"devsandbox-tunnel 2 {}\n"[..]).unwrap_err();
assert!(matches!(&e, HeaderError::Bad(m) if m.contains("version `2`")), "{e}");
let e = read_header(&mut &b"devsandbox-tunnel 1 {\"label\":3}\n"[..]).unwrap_err();
assert!(matches!(&e, HeaderError::Bad(m) if m.starts_with("bad tunnel header")), "{e}");
assert!(matches!(read_header(&mut &b"devsandbox-tunnel 1\n"[..]), Err(HeaderError::Bad(_))));
assert!(matches!(read_header(&mut &b"motd\nno header yet"[..]), Err(HeaderError::Eof)));
assert!(matches!(read_header(&mut &b""[..]), Err(HeaderError::Eof)));
}
#[test]
fn no_header_errors() {
let update = "devsandbox on box can't forward ports yet: update it";
assert_eq!(no_header("box", "error: unrecognized subcommand 'tunnel'\n\nUsage: devsandbox"), update);
assert_eq!(no_header("box", "error: Found argument 'tunnel' which wasn't expected"), update);
assert_eq!(no_header("box", "me@box: Permission denied (publickey)."), "me@box: Permission denied (publickey).");
assert_eq!(no_header("box", " \n"), "no answer from box");
}
fn sh(script: &str) -> TunnelArgv {
let script = script.to_string();
Box::new(move || Ok(["sh", "-c", &script].into_iter().map(OsString::from).collect()))
}
fn open(script: &str) -> Result<(Opened, Bridge), String> {
remote_opener("box".into(), sh(script))(&AtomicBool::new(false))
}
#[test]
fn the_far_sides_error_header_is_the_error() {
let line = Header::Error { error: "no sandbox instance `web`".into() }.line();
let script = format!("echo motd; echo chatty >&2; printf '%s' '{line}'; exec cat");
assert_eq!(open(&script).err().as_deref(), Some("no sandbox instance `web`"));
}
#[test]
fn a_devsandbox_without_tunnel_says_update_it() {
let script = "echo \"error: unrecognized subcommand 'tunnel'\" >&2; exit 2";
assert_eq!(open(script).err().as_deref(), Some("devsandbox on box can't forward ports yet: update it"));
}
#[test]
fn without_a_header_ssh_says_why_or_nothing_does() {
let script = "echo 'me@box: Permission denied (publickey).' >&2; exit 255";
assert_eq!(open(script).err().as_deref(), Some("me@box: Permission denied (publickey)."));
assert_eq!(open("echo motd; exit 0").err().as_deref(), Some("no answer from box"));
let bad = remote_opener("box".into(), Box::new(|| Err("no machine `box`".into())));
assert_eq!(bad(&AtomicBool::new(false)).err().as_deref(), Some("no machine `box`"));
let gone = remote_opener("box".into(), Box::new(|| Ok(vec!["/nonexistent/ssh".into()])));
assert!(gone(&AtomicBool::new(false)).err().unwrap().starts_with("cannot run /nonexistent/ssh"));
}
#[test]
fn a_silent_tunnel_gives_up_when_stopped() {
let stop = Arc::new(AtomicBool::new(false));
let setter = {
let stop = Arc::clone(&stop);
std::thread::spawn(move || {
std::thread::sleep(Duration::from_millis(200));
stop.store(true, Ordering::Relaxed);
})
};
let started = Instant::now();
let got = remote_opener("box".into(), sh("exec sleep 30"))(&stop);
assert!(got.is_err());
assert!(started.elapsed() < Duration::from_secs(2), "took {:?}", started.elapsed());
setter.join().unwrap();
}
#[test_utils::docker_test(helper)]
fn forwards_through_a_tunnel_with_docker() -> Result<(), &'static str> {
let ok = |cmd: &mut Command| matches!(cmd.output(), Ok(o) if o.status.success());
let stamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos();
let name = format!("devsandbox-fwd-tunnel-{stamp}");
let serve = "busybox nc -lk -s 127.0.0.1 -p 8080 -e cat";
let cleanup = || {
let _ = Command::new("docker").args(["rm", "-f", &name]).output();
};
crate::test_support::with_cleanup(cleanup, || {
assert!(ok(Command::new("docker").args(["run", "-d", "--name", &name, "alpine:3.20", "sh", "-c", serve])));
let arch = crate::devsbd::install(&name, None).unwrap();
let header = Header::Route {
label: "web:8080".into(),
host: "127.0.0.1".into(),
port: 8080,
hash: crate::devsbd::hash(arch).map(Into::into),
helper: true,
};
let script = format!(
"echo motd; printf '%s' '{}'; exec docker exec -i -u root {name} {} bridge",
header.line(),
crate::devsbd::BIN
);
let forward = Forward::start(ForwardSpec {
bind: IpAddr::V4(Ipv4Addr::LOCALHOST),
host_port: HostPort::Prefer(0),
open: remote_opener("box".into(), sh(&script)),
probe: Box::new(|_, _| None),
})
.unwrap();
let deadline = Instant::now() + Duration::from_secs(15);
while forward.status().state != ForwardState::Active && Instant::now() < deadline {
std::thread::sleep(Duration::from_millis(50));
}
assert_eq!(forward.status().state, ForwardState::Active);
assert_eq!(forward.status().route_label, "box/web:8080");
let addr = forward.status().local_addr;
let ping = || -> io::Result<bool> {
let mut c = TcpStream::connect(addr)?;
c.set_read_timeout(Some(Duration::from_secs(5)))?;
c.write_all(b"ping\n")?;
let mut buf = [0u8; 5];
c.read_exact(&mut buf)?;
Ok(&buf == b"ping\n")
};
let mut round_tripped = false;
for _ in 0..40 {
if matches!(ping(), Ok(true)) {
round_tripped = true;
break;
}
std::thread::sleep(Duration::from_millis(200));
}
assert!(round_tripped, "echo did not round-trip");
let deadline = Instant::now() + Duration::from_secs(15);
let found = loop {
if let Some(p) = forward.status().process {
break p;
}
if Instant::now() >= deadline {
return Err("listening process never found");
}
std::thread::sleep(Duration::from_millis(100));
};
assert!(found.contains(" (pid "), "{found}");
drop(forward);
Ok(())
})
}
}