feat: add reverse ssh sandbox targets

A sandbox behind NAT could not reach the room. A reverse target dials a tunnel-only sshd on the server, forwards localhost:22 to a loopback port, and the worker connects there.

The server assigns the port and writes one authorized_keys line per tunnel target. The key carries permitlisten for that port; the sshd allows remote forwarding only, with no shell or local forwarding. The tunnel private key is shown once and never stored; rotating it rewrites the file. The target list and setup dialog carry the command, and the docs include the sshd config and sidecar requirements.
This commit is contained in:
vhaudiquet committed 2026-11-29 19:25:14 +01:00
1 parent fc684115b5
commit d85f68a176
20 files changed
+893 -38

No files matched your search

+99
View File
@@ -38,12 +38,51 @@ pub struct Config {
/// cannot turn the server into a probe of its own network.
#[serde(default)]
pub allow_private_targets: bool,
/// The host sandboxes dial for a reverse ssh tunnel. Empty uses the
/// host from `public_url`.
#[serde(default)]
pub tunnel_ssh_host: String,
/// The port the tunnel sshd listens on.
#[serde(default = "default_tunnel_ssh_port")]
pub tunnel_ssh_port: u16,
/// The account the tunnel sshd accepts.
#[serde(default = "default_tunnel_user")]
pub tunnel_user: String,
/// The loopback port range the server hands to tunnel targets.
#[serde(default = "default_tunnel_port_start")]
pub tunnel_port_start: u16,
#[serde(default = "default_tunnel_port_end")]
pub tunnel_port_end: u16,
/// The authorized_keys file the tunnel sshd reads. The server writes
/// one restricted key per tunnel target.
#[serde(default = "default_tunnel_authorized_keys")]
pub tunnel_authorized_keys: PathBuf,
}
fn default_steps() -> u32 {
48
}
fn default_tunnel_ssh_port() -> u16 {
2222
}
fn default_tunnel_user() -> String {
"tunnel".into()
}
fn default_tunnel_port_start() -> u16 {
22000
}
fn default_tunnel_port_end() -> u16 {
22999
}
fn default_tunnel_authorized_keys() -> PathBuf {
PathBuf::from("data/tunnel/authorized_keys")
}
impl Default for Config {
fn default() -> Self {
Self {
@@ -57,6 +96,12 @@ impl Default for Config {
allowed_frame_ancestors: Vec::new(),
allow_registration: false,
allow_private_targets: false,
tunnel_ssh_host: String::new(),
tunnel_ssh_port: default_tunnel_ssh_port(),
tunnel_user: default_tunnel_user(),
tunnel_port_start: default_tunnel_port_start(),
tunnel_port_end: default_tunnel_port_end(),
tunnel_authorized_keys: default_tunnel_authorized_keys(),
}
}
}
@@ -98,11 +143,65 @@ impl Config {
if let Ok(value) = std::env::var("TOGETHER_ALLOW_PRIVATE_TARGETS") {
config.allow_private_targets = matches!(value.as_str(), "1" | "true" | "yes");
}
if let Ok(value) = std::env::var("TOGETHER_TUNNEL_SSH_HOST") {
config.tunnel_ssh_host = value;
}
if let Ok(value) = std::env::var("TOGETHER_TUNNEL_SSH_PORT") {
config.tunnel_ssh_port = value.parse()?;
}
if let Ok(value) = std::env::var("TOGETHER_TUNNEL_USER") {
config.tunnel_user = value;
}
if let Ok(value) = std::env::var("TOGETHER_TUNNEL_PORT_START") {
config.tunnel_port_start = value.parse()?;
}
if let Ok(value) = std::env::var("TOGETHER_TUNNEL_PORT_END") {
config.tunnel_port_end = value.parse()?;
}
if let Ok(value) = std::env::var("TOGETHER_TUNNEL_AUTHORIZED_KEYS") {
config.tunnel_authorized_keys = value.into();
}
if config.tunnel_port_start == 0 || config.tunnel_port_start > config.tunnel_port_end {
anyhow::bail!("the tunnel port range is invalid");
}
if config.tunnel_ssh_port == 0 {
anyhow::bail!("the tunnel ssh port is invalid");
}
config.tunnel_ssh_host = config.tunnel_ssh_host.trim().to_owned();
config.tunnel_user = config.tunnel_user.trim().to_owned();
if config.tunnel_user.is_empty() || config.tunnel_user.contains(char::is_whitespace) {
anyhow::bail!("the tunnel user is invalid");
}
config.public_url = config.public_url.trim_end_matches('/').to_owned();
config.preview_origin = config.preview_origin.trim_end_matches('/').to_owned();
Ok(config)
}
/// The host a sandbox passes to `autossh -R`.
pub fn tunnel_host(&self) -> String {
if !self.tunnel_ssh_host.is_empty() {
return self.tunnel_ssh_host.clone();
}
let rest = self
.public_url
.split("://")
.nth(1)
.unwrap_or(&self.public_url);
let host = rest.split('/').next().unwrap_or(rest);
host.split(':').next().unwrap_or(host).to_owned()
}
/// The command that turns a sandbox's local sshd into a loopback
/// port on the server.
pub fn tunnel_command(&self, port: u16, target_id: &str) -> String {
format!(
"autossh -M 0 -N -R 127.0.0.1:{port}:localhost:22 -p {} {}@{} -i ./rhema-tunnel-{target_id} -o ExitOnForwardFailure=yes -o ServerAliveInterval=30",
self.tunnel_ssh_port,
self.tunnel_user,
self.tunnel_host(),
)
}
/// Whether cookies should carry `Secure`.
pub fn secure_cookies(&self) -> bool {
self.public_url.starts_with("https://")