refact: file copy&paste, cross platform (no macOS) (#10671)

* feat: unix, file copy&paste

Signed-off-by: fufesou <linlong1266@gmail.com>

* refact: unix file c&p, check peer version

Signed-off-by: fufesou <linlong1266@gmail.com>

* Update pubspec.yaml

---------

Signed-off-by: fufesou <linlong1266@gmail.com>
Co-authored-by: RustDesk <71636191+rustdesk@users.noreply.github.com>
This commit is contained in:
fufesou
2025-02-04 20:33:02 +08:00
committed by GitHub
parent a27fa43081
commit fbba8f0b34
42 changed files with 2026 additions and 1778 deletions

View File

@@ -3,8 +3,11 @@ use crate::ipc::ClipboardNonFile;
#[cfg(not(any(target_os = "android", target_os = "ios")))]
use crate::ipc::Connection;
#[cfg(not(any(target_os = "ios")))]
use crate::ipc::{self, Data};
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
use crate::{
clipboard::ClipboardSide,
ipc::{self, Data},
};
#[cfg(target_os = "windows")]
use clipboard::ContextSend;
#[cfg(not(any(target_os = "android", target_os = "ios")))]
use hbb_common::tokio::sync::mpsc::unbounded_channel;
@@ -71,9 +74,9 @@ struct IpcTaskRunner<T: InvokeUiCM> {
close: bool,
running: bool,
conn_id: i32,
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
#[cfg(target_os = "windows")]
file_transfer_enabled: bool,
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
#[cfg(target_os = "windows")]
file_transfer_enabled_peer: bool,
}
@@ -169,7 +172,7 @@ impl<T: InvokeUiCM> ConnectionManager<T> {
}
#[inline]
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
#[cfg(target_os = "windows")]
fn is_authorized(&self, id: i32) -> bool {
CLIENTS
.read()
@@ -190,12 +193,9 @@ impl<T: InvokeUiCM> ConnectionManager<T> {
.map(|c| c.disconnected = true);
}
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
#[cfg(target_os = "windows")]
{
let _ = ContextSend::proc(|context| -> ResultType<()> {
context.empty_clipboard(id)?;
Ok(())
});
crate::clipboard::try_empty_clipboard_files(ClipboardSide::Host, id);
}
#[cfg(any(target_os = "android"))]
@@ -345,31 +345,40 @@ impl<T: InvokeUiCM> IpcTaskRunner<T> {
// for tmp use, without real conn id
let mut write_jobs: Vec<fs::TransferJob> = Vec::new();
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
#[cfg(target_os = "windows")]
let is_authorized = self.cm.is_authorized(self.conn_id);
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
let rx_clip1;
#[cfg(target_os = "windows")]
let rx_clip_holder;
let mut rx_clip;
let _tx_clip;
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
#[cfg(target_os = "windows")]
if self.conn_id > 0 && is_authorized {
log::debug!("Clipboard is enabled from client peer: type 1");
rx_clip1 = clipboard::get_rx_cliprdr_server(self.conn_id);
rx_clip = rx_clip1.lock().await;
let conn_id = self.conn_id;
rx_clip_holder = (
clipboard::get_rx_cliprdr_server(conn_id),
Some(crate::SimpleCallOnReturn {
b: true,
f: Box::new(move || {
clipboard::remove_channel_by_conn_id(conn_id);
}),
}),
);
rx_clip = rx_clip_holder.0.lock().await;
} else {
log::debug!("Clipboard is enabled from client peer, actually useless: type 2");
let rx_clip2;
(_tx_clip, rx_clip2) = unbounded_channel::<clipboard::ClipboardFile>();
rx_clip1 = Arc::new(TokioMutex::new(rx_clip2));
rx_clip = rx_clip1.lock().await;
rx_clip_holder = (Arc::new(TokioMutex::new(rx_clip2)), None);
rx_clip = rx_clip_holder.0.lock().await;
}
#[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
#[cfg(not(target_os = "windows"))]
{
(_tx_clip, rx_clip) = unbounded_channel::<i32>();
}
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
#[cfg(target_os = "windows")]
{
if ContextSend::is_enabled() {
log::debug!("Clipboard is enabled");
@@ -397,7 +406,7 @@ impl<T: InvokeUiCM> IpcTaskRunner<T> {
log::debug!("conn_id: {}", id);
self.cm.add_connection(id, is_file_transfer, port_forward, peer_id, name, authorized, keyboard, clipboard, audio, file, restart, recording, block_input, from_switch, self.tx.clone());
self.conn_id = id;
#[cfg(any(target_os = "linux", target_os = "windows", target_os = "macos"))]
#[cfg(target_os = "windows")]
{
self.file_transfer_enabled = _file_transfer_enabled;
}
@@ -438,34 +447,31 @@ impl<T: InvokeUiCM> IpcTaskRunner<T> {
Data::FileTransferLog((action, log)) => {
self.cm.ui_handler.file_transfer_log(&action, &log);
}
#[cfg(not(any(target_os = "android", target_os = "ios")))]
#[cfg(target_os = "windows")]
Data::ClipboardFile(_clip) => {
#[cfg(any(target_os = "windows", target_os="linux", target_os = "macos"))]
{
let is_stopping_allowed = _clip.is_beginning_message();
let is_clipboard_enabled = ContextSend::is_enabled();
let file_transfer_enabled = self.file_transfer_enabled;
let stop = !is_stopping_allowed && !(is_clipboard_enabled && file_transfer_enabled);
log::debug!(
"Process clipboard message from client peer, stop: {}, is_stopping_allowed: {}, is_clipboard_enabled: {}, file_transfer_enabled: {}",
stop, is_stopping_allowed, is_clipboard_enabled, file_transfer_enabled);
if stop {
ContextSend::set_is_stopped();
} else {
if !is_authorized {
log::debug!("Clipboard message from client peer, but not authorized");
continue;
}
let conn_id = self.conn_id;
let _ = ContextSend::proc(|context| -> ResultType<()> {
context.server_clip_file(conn_id, _clip)
.map_err(|e| e.into())
});
let is_stopping_allowed = _clip.is_beginning_message();
let is_clipboard_enabled = ContextSend::is_enabled();
let file_transfer_enabled = self.file_transfer_enabled;
let stop = !is_stopping_allowed && !(is_clipboard_enabled && file_transfer_enabled);
log::debug!(
"Process clipboard message from client peer, stop: {}, is_stopping_allowed: {}, is_clipboard_enabled: {}, file_transfer_enabled: {}",
stop, is_stopping_allowed, is_clipboard_enabled, file_transfer_enabled);
if stop {
ContextSend::set_is_stopped();
} else {
if !is_authorized {
log::debug!("Clipboard message from client peer, but not authorized");
continue;
}
let conn_id = self.conn_id;
let _ = ContextSend::proc(|context| -> ResultType<()> {
context.server_clip_file(conn_id, _clip)
.map_err(|e| e.into())
});
}
}
Data::ClipboardFileEnabled(_enabled) => {
#[cfg(any(target_os= "windows",target_os ="linux", target_os = "macos"))]
#[cfg(target_os = "windows")]
{
self.file_transfer_enabled_peer = _enabled;
}
@@ -543,7 +549,7 @@ impl<T: InvokeUiCM> IpcTaskRunner<T> {
}
match &data {
Data::SwitchPermission{name: _name, enabled: _enabled} => {
#[cfg(any(target_os="linux", target_os="windows", target_os = "macos"))]
#[cfg(target_os = "windows")]
if _name == "file" {
self.file_transfer_enabled = *_enabled;
}
@@ -558,7 +564,7 @@ impl<T: InvokeUiCM> IpcTaskRunner<T> {
},
clip_file = rx_clip.recv() => match clip_file {
Some(_clip) => {
#[cfg(any(target_os = "windows", target_os ="linux", target_os = "macos"))]
#[cfg(target_os = "windows")]
{
let is_stopping_allowed = _clip.is_stopping_allowed();
let is_clipboard_enabled = ContextSend::is_enabled();
@@ -602,9 +608,9 @@ impl<T: InvokeUiCM> IpcTaskRunner<T> {
close: true,
running: true,
conn_id: 0,
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
#[cfg(target_os = "windows")]
file_transfer_enabled: false,
#[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
#[cfg(target_os = "windows")]
file_transfer_enabled_peer: false,
};
@@ -623,13 +629,7 @@ impl<T: InvokeUiCM> IpcTaskRunner<T> {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
#[tokio::main(flavor = "current_thread")]
pub async fn start_ipc<T: InvokeUiCM>(cm: ConnectionManager<T>) {
#[cfg(any(
target_os = "windows",
all(
any(target_os = "linux", target_os = "macos"),
feature = "unix-file-copy-paste"
),
))]
#[cfg(target_os = "windows")]
ContextSend::enable(option2bool(
OPTION_ENABLE_FILE_TRANSFER,
&Config::get_option(OPTION_ENABLE_FILE_TRANSFER),