use super::*;
pub(super) fn url_is_safe_to_open(url: &str) -> bool {
let lower: String = url
.chars()
.take(16)
.collect::<String>()
.to_ascii_lowercase();
let scheme_ok = lower.starts_with("https://")
|| lower.starts_with("http://")
|| lower.starts_with("mailto:");
if !scheme_ok {
return false;
}
!url.chars()
.any(|c| c == '\0' || c == '\n' || c == '\r' || c == '\t')
}
pub(super) fn open_local_url(url: &str) -> std::io::Result<std::process::ExitStatus> {
let program = if cfg!(target_os = "linux") {
"xdg-open"
} else {
"open"
};
Command::new(program)
.arg(url)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
}
pub(super) fn handle_open_request(
host: &str,
socket: &Path,
inbox_dir: &str,
request_id: &str,
url: &str,
) {
if !url_is_safe_to_open(url) {
write_relay_err(
host,
socket,
inbox_dir,
request_id,
"rejected: URL scheme must be http/https/mailto and contain no control chars",
);
return;
}
let dest_dir = format!("{inbox_dir}/{request_id}");
if !ssh_exec(
host,
socket,
&format!("mkdir -p {}", shell_quote(&dest_dir)),
) {
write_relay_err(host, socket, inbox_dir, request_id, "remote mkdir failed");
return;
}
match open_local_url(url) {
Ok(s) if s.success() => {
let _ = ssh_exec(
host,
socket,
&format!("touch {}/.ok", shell_quote(&dest_dir)),
);
}
Ok(s) => write_relay_err(
host,
socket,
inbox_dir,
request_id,
&format!("local open(1) exited {s}"),
),
Err(e) => write_relay_err(
host,
socket,
inbox_dir,
request_id,
&format!("local open(1) spawn failed: {e}"),
),
}
}
pub(super) fn pick_local_port(remote: u16) -> u16 {
use std::net::TcpListener;
if TcpListener::bind(("127.0.0.1", remote)).is_ok() {
return remote;
}
TcpListener::bind(("127.0.0.1", 0))
.ok()
.and_then(|l| l.local_addr().ok())
.map(|a| a.port())
.unwrap_or(remote)
}
pub(super) fn handle_forward_request(
host: &str,
socket: &Path,
inbox_dir: &str,
request_id: &str,
port: &str,
open: bool,
remote_ipv6: bool,
) {
let Some(remote_port) = port.parse::<u16>().ok().filter(|p| *p >= 1) else {
write_relay_err(
host,
socket,
inbox_dir,
request_id,
"rejected: port must be 1..=65535",
);
return;
};
let dest_dir = format!("{inbox_dir}/{request_id}");
if !ssh_exec(
host,
socket,
&format!("mkdir -p {}", shell_quote(&dest_dir)),
) {
write_relay_err(host, socket, inbox_dir, request_id, "remote mkdir failed");
return;
}
let local_port = pick_local_port(remote_port);
let remote_host = if remote_ipv6 { "[::1]" } else { "127.0.0.1" };
let spec = format!("127.0.0.1:{local_port}:{remote_host}:{remote_port}");
let forwarded = Ssh::control(socket, "forward")
.arg("-L")
.arg(&spec)
.arg(host)
.quiet()
.into_command()
.status()
.map(|s| s.success())
.unwrap_or(false);
if !forwarded {
write_relay_err(host, socket, inbox_dir, request_id, "ssh -O forward failed");
return;
}
if open {
let _ = open_local_url(&format!("http://127.0.0.1:{local_port}/"));
}
let _ = ssh_exec(
host,
socket,
&format!("printf %s {local_port} > {}/.ok", shell_quote(&dest_dir)),
);
}
pub(super) fn handle_unforward_request(
host: &str,
socket: &Path,
local: &str,
remote: &str,
remote_ipv6: bool,
) {
let (Ok(local), Ok(remote)) = (local.parse::<u16>(), remote.parse::<u16>()) else {
return;
};
let remote_host = if remote_ipv6 { "[::1]" } else { "127.0.0.1" };
let spec = format!("127.0.0.1:{local}:{remote_host}:{remote}");
let _ = Ssh::control(socket, "cancel")
.arg("-L")
.arg(&spec)
.arg(host)
.quiet()
.into_command()
.status();
}