use super::*;
pub(super) fn handle_clipboard_request(
host: &str,
socket: &Path,
inbox_dir: &str,
request_id: &str,
) {
let dest_dir = format!("{inbox_dir}/{request_id}");
let mkdir = format!("mkdir -p {}", shell_quote(&dest_dir));
let mkdir_ok = Command::new("ssh")
.arg("-S")
.arg(socket)
.arg("-o")
.arg("ControlMaster=no")
.arg(host)
.arg(&mkdir)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.map(|s| s.success())
.unwrap_or(false);
if !mkdir_ok {
write_relay_err(host, socket, inbox_dir, request_id, "remote mkdir failed");
return;
}
let payload = match crate::clipboard::read_string() {
Some(s) => s,
None => {
write_relay_err(
host,
socket,
inbox_dir,
request_id,
"local clipboard unavailable",
);
return;
}
};
let remote_cmd = format!(
"cat > {dst}",
dst = shell_quote(&format!("{dest_dir}/clipboard.txt")),
);
let mut ssh_recv = match Command::new("ssh")
.arg("-S")
.arg(socket)
.arg("-o")
.arg("ControlMaster=no")
.arg(host)
.arg(&remote_cmd)
.stdin(Stdio::piped())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
{
Ok(c) => c,
Err(e) => {
write_relay_err(
host,
socket,
inbox_dir,
request_id,
&format!("clipboard relay spawn failed: {e}"),
);
return;
}
};
if let Some(mut stdin) = ssh_recv.stdin.take() {
use std::io::Write as _;
let _ = stdin.write_all(payload.as_bytes());
}
let status = ssh_recv.wait();
match status {
Ok(s) if s.success() => {
let touch = format!("touch {}/.ok", shell_quote(&dest_dir));
let _ = Command::new("ssh")
.arg("-S")
.arg(socket)
.arg("-o")
.arg("ControlMaster=no")
.arg(host)
.arg(&touch)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status();
}
Ok(s) => write_relay_err(
host,
socket,
inbox_dir,
request_id,
&format!("clipboard relay ssh exited {s}"),
),
Err(e) => write_relay_err(
host,
socket,
inbox_dir,
request_id,
&format!("clipboard relay wait failed: {e}"),
),
}
}
pub(super) fn handle_copy_request(host: &str, socket: &Path, inbox_dir: &str, request_id: &str) {
if !copy_request_id_is_safe(request_id) {
return;
}
let quoted = shell_quote(&format!("{inbox_dir}/{request_id}.txt"));
let limit = MAX_COPY_PAYLOAD_BYTES + 1;
let remote_cmd = format!("head -c {limit} {quoted}; rm -f {quoted}");
let Ok(mut child) = Command::new("ssh")
.arg("-S")
.arg(socket)
.arg("-o")
.arg("ControlMaster=no")
.arg(host)
.arg(&remote_cmd)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn()
else {
return;
};
let Some(mut out) = child.stdout.take() else {
let _ = child.kill();
let _ = child.wait();
return;
};
let (tx, rx) = std::sync::mpsc::channel();
thread::spawn(move || {
use std::io::Read as _;
let mut buf = Vec::new();
let _ = out.by_ref().take(limit as u64).read_to_end(&mut buf);
let _ = tx.send(buf);
});
let payload = match rx.recv_timeout(COPY_READ_TIMEOUT) {
Ok(bytes) => {
let _ = child.wait();
bytes
}
Err(_) => {
let _ = child.kill();
let _ = child.wait();
write_relay_err(
host,
socket,
inbox_dir,
request_id,
"copy payload timed out",
);
return;
}
};
if payload.is_empty() || payload.len() > MAX_COPY_PAYLOAD_BYTES {
return;
}
if !crate::clipboard::write_string(&String::from_utf8_lossy(&payload)) {
write_relay_err(host, socket, inbox_dir, request_id, "no clipboard to write");
}
}
pub(crate) const MAX_COPY_PAYLOAD_BYTES: usize = 4 * 1024 * 1024;
pub(super) const COPY_READ_TIMEOUT: Duration = Duration::from_secs(15);
pub(super) fn copy_request_id_is_safe(id: &str) -> bool {
!id.is_empty()
&& id.len() <= 128
&& id
.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_')
}