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 = Ssh::bare_client(socket)
.arg(host)
.arg(&mkdir)
.quiet()
.into_command()
.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 Ssh::bare_client(socket)
.arg(host)
.arg(&remote_cmd)
.into_command()
.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 _ = Ssh::bare_client(socket)
.arg(host)
.arg(&touch)
.quiet()
.into_command()
.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 ran = crate::proc::Bounded {
stdout: crate::proc::Keep::All { cap: limit },
stderr: crate::proc::Keep::Nothing,
apart: crate::proc::Apart::No,
limit: COPY_READ_TIMEOUT,
retry_busy: false,
}
.run(
&mut Ssh::bare_client(socket)
.arg(host)
.arg(&remote_cmd)
.into_command(),
);
let payload = match ran {
Ok(ran) => ran.stdout.into_bytes(),
Err(crate::proc::RunError::TimedOut) => {
write_relay_err(
host,
socket,
inbox_dir,
request_id,
"copy payload timed out",
);
return;
}
Err(_) => 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'_')
}