Merge branch 'master' into dev

This commit is contained in:
xxrl
2022-11-10 00:01:42 +08:00
74 changed files with 1987 additions and 433 deletions

View File

@@ -250,14 +250,7 @@ impl Connection {
}
}
ipc::Data::Close => {
conn.close_manually = true;
let mut misc = Misc::new();
misc.set_close_reason("Closed manually by the peer".into());
let mut msg_out = Message::new();
msg_out.set_misc(misc);
conn.send(msg_out).await;
conn.on_close("Close requested from connection manager", false).await;
SESSIONS.lock().unwrap().remove(&conn.lr.my_id);
conn.on_close_manually("connection manager").await;
break;
}
ipc::Data::ChatMessage{text} => {
@@ -404,6 +397,18 @@ impl Connection {
_ => {}
}
}
match &msg.union {
Some(message::Union::Misc(m)) => {
match &m.union {
Some(misc::Union::StopService(_)) => {
conn.on_close_manually("stop service").await;
break;
}
_ => {},
}
}
_ => {}
}
if let Err(err) = conn.stream.send(msg).await {
conn.on_close(&err.to_string(), false).await;
break;
@@ -1490,6 +1495,18 @@ impl Connection {
self.port_forward_socket.take();
}
async fn on_close_manually(&mut self, close_from: &str) {
self.close_manually = true;
let mut misc = Misc::new();
misc.set_close_reason("Closed manually by the peer".into());
let mut msg_out = Message::new();
msg_out.set_misc(misc);
self.send(msg_out).await;
self.on_close(&format!("Close requested from {}", close_from), false)
.await;
SESSIONS.lock().unwrap().remove(&self.lr.my_id);
}
fn read_dir(&mut self, dir: &str, include_hidden: bool) {
let dir = dir.to_string();
self.send_fs(ipc::FS::ReadDir {

View File

@@ -1,4 +1,5 @@
use super::*;
#[cfg(target_os = "linux")]
use crate::common::IS_X11;
#[cfg(target_os = "macos")]
use dispatch::Queue;
@@ -7,6 +8,7 @@ use hbb_common::{config::COMPRESS_LEVEL, get_time, protobuf::EnumOrUnknown};
use rdev::{simulate, EventType, Key as RdevKey};
use std::{
convert::TryFrom,
ops::Sub,
sync::atomic::{AtomicBool, Ordering},
time::Instant,
};
@@ -37,10 +39,12 @@ impl super::service::Reset for StatePos {
}
}
#[derive(Default)]
#[derive(Default, Clone, Copy)]
struct Input {
conn: i32,
time: i64,
x: i32,
y: i32,
}
const KEY_CHAR_START: u64 = 9999;
@@ -100,8 +104,16 @@ pub fn new_pos() -> GenericService {
sp
}
fn update_last_cursor_pos(x: i32, y: i32) {
let mut lock = LATEST_CURSOR_POS.lock().unwrap();
if lock.1 .0 != x || lock.1 .1 != y {
(lock.0, lock.1) = (Instant::now(), (x, y))
}
}
fn run_pos(sp: GenericService, state: &mut StatePos) -> ResultType<()> {
if let Some((x, y)) = crate::get_cursor_pos() {
update_last_cursor_pos(x, y);
if state.cursor_pos.0 != x || state.cursor_pos.1 != y {
state.cursor_pos = (x, y);
let mut msg_out = Message::new();
@@ -112,7 +124,7 @@ fn run_pos(sp: GenericService, state: &mut StatePos) -> ResultType<()> {
});
let exclude = {
let now = get_time();
let lock = LATEST_INPUT.lock().unwrap();
let lock = LATEST_INPUT_CURSOR.lock().unwrap();
if now - lock.time < 300 {
lock.conn
} else {
@@ -170,10 +182,14 @@ lazy_static::lazy_static! {
Arc::new(Mutex::new(Enigo::new()))
};
static ref KEYS_DOWN: Arc<Mutex<HashMap<u64, Instant>>> = Default::default();
static ref LATEST_INPUT: Arc<Mutex<Input>> = Default::default();
static ref LATEST_INPUT_CURSOR: Arc<Mutex<Input>> = Default::default();
static ref LATEST_CURSOR_POS: Arc<Mutex<(Instant, (i32, i32))>> = Arc::new(Mutex::new((Instant::now().sub(MOUSE_MOVE_PROTECTION_TIMEOUT), (0, 0))));
}
static EXITING: AtomicBool = AtomicBool::new(false);
const MOUSE_MOVE_PROTECTION_TIMEOUT: Duration = Duration::from_millis(1_000);
const MOUSE_ACTIVE_DISTANCE: i32 = 5;
// mac key input must be run in main thread, otherwise crash on >= osx 10.15
#[cfg(target_os = "macos")]
lazy_static::lazy_static! {
@@ -357,17 +373,54 @@ fn fix_modifiers(modifiers: &[EnumOrUnknown<ControlKey>], en: &mut Enigo, ck: i3
}
}
fn is_mouse_active_by_conn(conn: i32) -> bool {
// out of time protection
if LATEST_CURSOR_POS.lock().unwrap().0.elapsed() > MOUSE_MOVE_PROTECTION_TIMEOUT {
return true;
}
let mut last_input = LATEST_INPUT_CURSOR.lock().unwrap();
// last conn input may be protected
if last_input.conn != conn {
return false;
}
// check if input is in valid range
match crate::get_cursor_pos() {
Some((x, y)) => {
let is_same_input = (last_input.x - x).abs() < MOUSE_ACTIVE_DISTANCE
&& (last_input.y - y).abs() < MOUSE_ACTIVE_DISTANCE;
if !is_same_input {
last_input.x = -MOUSE_ACTIVE_DISTANCE * 2;
last_input.y = -MOUSE_ACTIVE_DISTANCE * 2;
}
is_same_input
}
None => true,
}
}
fn handle_mouse_(evt: &MouseEvent, conn: i32) {
if EXITING.load(Ordering::SeqCst) {
return;
}
if !is_mouse_active_by_conn(conn) {
return;
}
#[cfg(windows)]
crate::platform::windows::try_change_desktop();
let buttons = evt.mask >> 3;
let evt_type = evt.mask & 0x7;
if evt_type == 0 {
let time = get_time();
*LATEST_INPUT.lock().unwrap() = Input { time, conn };
*LATEST_INPUT_CURSOR.lock().unwrap() = Input {
time,
conn,
x: evt.x,
y: evt.y,
};
}
let mut en = ENIGO.lock().unwrap();
#[cfg(not(target_os = "macos"))]
@@ -674,19 +727,19 @@ fn sync_status(evt: &KeyEvent) -> (bool, bool) {
let code = evt.chr();
let key = rdev::get_win_key(code, 0);
match key {
RdevKey::Home |
RdevKey::UpArrow |
RdevKey::PageUp |
RdevKey::LeftArrow |
RdevKey::RightArrow |
RdevKey::End |
RdevKey::DownArrow |
RdevKey::PageDown |
RdevKey::Insert |
RdevKey::Delete => en.get_key_state(enigo::Key::NumLock),
RdevKey::Home
| RdevKey::UpArrow
| RdevKey::PageUp
| RdevKey::LeftArrow
| RdevKey::RightArrow
| RdevKey::End
| RdevKey::DownArrow
| RdevKey::PageDown
| RdevKey::Insert
| RdevKey::Delete => en.get_key_state(enigo::Key::NumLock),
_ => click_numlock,
}
};
};
return (click_capslock, click_numlock);
}