make cli compilable

This commit is contained in:
rustdesk 2022-12-29 00:02:31 +08:00
parent 08b8f40397
commit 4d2e62981b
7 changed files with 115 additions and 103 deletions

View File

@ -6,6 +6,7 @@ use hbb_common::{
protobuf::Message as _,
tokio::{self, sync::mpsc},
Stream,
rendezvous_proto::ConnType,
};
use std::sync::{Arc, RwLock};
@ -33,14 +34,18 @@ impl Session {
.lc
.write()
.unwrap()
.initialize(id.to_owned(), false, true);
.initialize(id.to_owned(), ConnType::PORT_FORWARD);
session
}
}
#[async_trait]
impl Interface for Session {
fn msgbox(&self, msgtype: &str, title: &str, text: &str) {
fn get_login_config_handler(&self) -> Arc<RwLock<LoginConfigHandler>> {
return self.lc.clone();
}
fn msgbox(&self, msgtype: &str, title: &str, text: &str, link: &str) {
if msgtype == "input-password" {
self.sender
.send(Data::Login((self.password.clone(), true)))
@ -61,12 +66,11 @@ impl Interface for Session {
}
fn handle_peer_info(&mut self, pi: PeerInfo) {
let username = self.lc.read().unwrap().get_username(&pi);
self.lc.write().unwrap().handle_peer_info(username, pi);
self.lc.write().unwrap().handle_peer_info(&pi);
}
async fn handle_hash(&mut self, hash: Hash, peer: &mut Stream) {
handle_hash(self.lc.clone(), hash, self, peer).await;
async fn handle_hash(&mut self, pass: &str, hash: Hash, peer: &mut Stream) {
handle_hash(self.lc.clone(), &pass, hash, self, peer).await;
}
async fn handle_login_from_ui(&mut self, password: String, remember: bool, peer: &mut Stream) {
@ -95,9 +99,19 @@ pub async fn start_one_port_forward(
crate::common::test_nat_type();
let (sender, mut receiver) = mpsc::unbounded_channel::<Data>();
let handler = Session::new(&id, sender);
handler.lc.write().unwrap().port_forward = (remote_host, remote_port);
if let Err(err) =
crate::port_forward::listen(handler.id.clone(), port, handler.clone(), receiver, &key, &token).await
if let Err(err) = crate::port_forward::listen(
handler.id.clone(),
handler.password.clone(),
port,
handler.clone(),
receiver,
&key,
&token,
handler.lc.clone(),
remote_host,
remote_port,
)
.await
{
log::error!("Failed to listen on {}: {}", port, err);
}

View File

@ -1481,6 +1481,19 @@ impl LoginConfigHandler {
msg_out.set_misc(misc);
msg_out
}
pub fn set_force_relay(&mut self, direct: bool, received: bool) {
self.force_relay = false;
if direct && !received {
let errno = errno::errno().0;
log::info!("errno is {}", errno);
// TODO: check mac and ios
if cfg!(windows) && errno == 10054 || !cfg!(windows) && errno == 104 {
self.force_relay = true;
self.set_option("force-always-relay".to_owned(), "Y".to_owned());
}
}
}
}
/// Media data.
@ -1825,18 +1838,20 @@ pub trait Interface: Send + Clone + 'static + Sized {
fn msgbox(&self, msgtype: &str, title: &str, text: &str, link: &str);
fn handle_login_error(&mut self, err: &str) -> bool;
fn handle_peer_info(&mut self, pi: PeerInfo);
fn set_force_relay(&mut self, direct: bool, received: bool);
fn set_connection_info(&mut self, direct: bool, received: bool);
fn is_file_transfer(&self) -> bool;
fn is_port_forward(&self) -> bool;
fn is_rdp(&self) -> bool;
fn on_error(&self, err: &str) {
self.msgbox("error", "Error", err, "");
}
fn is_force_relay(&self) -> bool;
async fn handle_hash(&mut self, pass: &str, hash: Hash, peer: &mut Stream);
async fn handle_login_from_ui(&mut self, password: String, remember: bool, peer: &mut Stream);
async fn handle_test_delay(&mut self, t: TestDelay, peer: &mut Stream);
fn get_login_config_handler(&self) -> Arc<RwLock<LoginConfigHandler>>;
fn set_force_relay(&self, direct: bool, received: bool) {
self.get_login_config_handler().write().unwrap().set_force_relay(direct, received);
}
fn is_force_relay(&self) -> bool {
self.get_login_config_handler().read().unwrap().force_relay
}
}
/// Data used by the client interface.

View File

@ -22,7 +22,7 @@ pub trait FileManager: Interface {
#[cfg(any(target_os = "android", target_os = "ios", feature = "cli"))]
fn read_dir(&self, path: &str, include_hidden: bool) -> String {
use crate::flutter::make_fd_to_json;
use crate::common::make_fd_to_json;
match fs::read_dir(&fs::get_path(path), include_hidden) {
Ok(fd) => make_fd_to_json(fd.id, fd.path, &fd.entries),
Err(_) => "".into(),

View File

@ -693,3 +693,22 @@ lazy_static::lazy_static! {
lazy_static::lazy_static! {
pub static ref IS_X11: Mutex<bool> = Mutex::new("x11" == hbb_common::platform::linux::get_display_server());
}
pub fn make_fd_to_json(id: i32, path: String, entries: &Vec<FileEntry>) -> String {
use serde_json::json;
let mut fd_json = serde_json::Map::new();
fd_json.insert("id".into(), json!(id));
fd_json.insert("path".into(), json!(path));
let mut entries_out = vec![];
for entry in entries {
let mut entry_map = serde_json::Map::new();
entry_map.insert("entry_type".into(), json!(entry.entry_type.value()));
entry_map.insert("name".into(), json!(entry.name));
entry_map.insert("size".into(), json!(entry.size));
entry_map.insert("modified_time".into(), json!(entry.modified_time));
entries_out.push(entry_map);
}
fd_json.insert("entries".into(), json!(entries_out));
serde_json::to_string(&fd_json).unwrap_or("".into())
}

View File

@ -242,7 +242,7 @@ impl InvokeUiSession for FlutterHandler {
self.push_event(
"file_dir",
vec![
("value", &make_fd_to_json(id, path, entries)),
("value", &crate::common::make_fd_to_json(id, path, entries)),
("is_local", "false"),
],
);
@ -545,24 +545,6 @@ pub fn get_session_id(id: String) -> String {
};
}
pub fn make_fd_to_json(id: i32, path: String, entries: &Vec<FileEntry>) -> String {
let mut fd_json = serde_json::Map::new();
fd_json.insert("id".into(), json!(id));
fd_json.insert("path".into(), json!(path));
let mut entries_out = vec![];
for entry in entries {
let mut entry_map = serde_json::Map::new();
entry_map.insert("entry_type".into(), json!(entry.entry_type.value()));
entry_map.insert("name".into(), json!(entry.name));
entry_map.insert("size".into(), json!(entry.size));
entry_map.insert("modified_time".into(), json!(entry.modified_time));
entries_out.push(entry_map);
}
fd_json.insert("entries".into(), json!(entries_out));
serde_json::to_string(&fd_json).unwrap_or("".into())
}
pub fn make_fd_flutter(id: i32, entries: &Vec<FileEntry>, only_count: bool) -> String {
let mut m = serde_json::Map::new();
m.insert("id".into(), json!(id));

View File

@ -3,10 +3,10 @@ use crate::client::get_key_state;
use crate::common::GrabState;
#[cfg(feature = "flutter")]
use crate::flutter::FlutterHandler;
#[cfg(not(feature = "flutter"))]
#[cfg(not(any(feature = "flutter", feature = "cli")))]
use crate::ui::remote::SciterHandler;
use crate::ui_session_interface::Session;
use hbb_common::{log, message_proto::*, config::LocalConfig};
use hbb_common::{log, message_proto::*};
use rdev::{Event, EventType, Key};
#[cfg(any(target_os = "windows", target_os = "macos"))]
use std::sync::atomic::{AtomicBool, Ordering};
@ -27,7 +27,7 @@ lazy_static::lazy_static! {
static ref CUR_SESSION: Arc<Mutex<Option<Session<FlutterHandler>>>> = Default::default();
}
#[cfg(not(feature = "flutter"))]
#[cfg(not(any(feature = "flutter", feature = "cli")))]
lazy_static::lazy_static! {
static ref CUR_SESSION: Arc<Mutex<Option<Session<SciterHandler>>>> = Default::default();
}
@ -53,7 +53,7 @@ pub fn set_cur_session(session: Session<FlutterHandler>) {
*CUR_SESSION.lock().unwrap() = Some(session);
}
#[cfg(not(feature = "flutter"))]
#[cfg(not(any(feature = "flutter", feature = "cli")))]
pub fn set_cur_session(session: Session<SciterHandler>) {
*CUR_SESSION.lock().unwrap() = Some(session);
}
@ -62,11 +62,11 @@ pub mod client {
use super::*;
pub fn get_keyboard_mode() -> String {
#[cfg(not(feature = "cli"))]
if let Some(handler) = CUR_SESSION.lock().unwrap().as_ref() {
handler.get_keyboard_mode()
} else {
"legacy".to_string()
}
return handler.get_keyboard_mode();
}
"legacy".to_string()
}
pub fn start_grab_loop() {
@ -332,12 +332,8 @@ pub fn event_to_key_event(event: &Event) -> Option<KeyEvent> {
let keyboard_mode = get_keyboard_mode_enum();
key_event.mode = keyboard_mode.into();
let mut key_event = match keyboard_mode {
KeyboardMode::Map => {
map_keyboard_mode(event, key_event)?
}
KeyboardMode::Translate => {
translate_keyboard_mode(event, key_event)?
}
KeyboardMode::Map => map_keyboard_mode(event, key_event)?,
KeyboardMode::Translate => translate_keyboard_mode(event, key_event)?,
_ => {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
@ -366,18 +362,18 @@ pub fn event_type_to_event(event_type: EventType) -> Event {
}
pub fn send_key_event(key_event: &KeyEvent) {
#[cfg(not(feature = "cli"))]
if let Some(handler) = CUR_SESSION.lock().unwrap().as_ref() {
handler.send_key_event(key_event);
}
}
pub fn get_peer_platform() -> String {
#[cfg(not(feature = "cli"))]
if let Some(handler) = CUR_SESSION.lock().unwrap().as_ref() {
handler.peer_platform()
} else {
log::error!("get peer platform error");
"Windows".to_string()
}
return handler.peer_platform();
}
"Windows".to_string()
}
#[cfg(not(any(target_os = "android", target_os = "ios")))]
@ -389,7 +385,7 @@ pub fn legacy_keyboard_mode(event: &Event, mut key_event: KeyEvent) -> Option<Ke
_ => {
return None;
}
};
};
let peer = get_peer_platform();
let is_win = peer == "Windows";
@ -621,12 +617,12 @@ pub fn map_keyboard_mode(event: &Event, mut key_event: KeyEvent) -> Option<KeyEv
let keycode = match peer.as_str() {
"windows" => event.scan_code,
"macos" => {
if LocalConfig::get_kb_layout_type() == "ISO" {
if hbb_common::config::LocalConfig::get_kb_layout_type() == "ISO" {
rdev::win_scancode_to_macos_iso_code(event.scan_code)?
} else {
rdev::win_scancode_to_macos_code(event.scan_code)?
}
},
}
_ => rdev::win_scancode_to_linux_code(event.scan_code)?,
};
#[cfg(target_os = "macos")]
@ -639,12 +635,12 @@ pub fn map_keyboard_mode(event: &Event, mut key_event: KeyEvent) -> Option<KeyEv
let keycode = match peer.as_str() {
"windows" => rdev::linux_code_to_win_scancode(event.code as _)?,
"macos" => {
if LocalConfig::get_kb_layout_type() == "ISO" {
if hbb_common::config::LocalConfig::get_kb_layout_type() == "ISO" {
rdev::linux_code_to_macos_iso_code(event.scan_code)?
} else {
rdev::linux_code_to_macos_code(event.code as _)?
}
},
}
_ => event.code as _,
};
#[cfg(any(target_os = "android", target_os = "ios"))]

View File

@ -32,6 +32,32 @@ pub struct Session<T: InvokeUiSession> {
}
impl<T: InvokeUiSession> Session<T> {
pub fn is_file_transfer(&self) -> bool {
self.lc
.read()
.unwrap()
.conn_type
.eq(&ConnType::FILE_TRANSFER)
}
pub fn is_port_forward(&self) -> bool {
self.lc
.read()
.unwrap()
.conn_type
.eq(&ConnType::PORT_FORWARD)
}
pub fn is_rdp(&self) -> bool {
self.lc.read().unwrap().conn_type.eq(&ConnType::RDP)
}
pub fn set_connection_info(&mut self, direct: bool, received: bool) {
let mut lc = self.lc.write().unwrap();
lc.direct = Some(direct);
lc.received = received;
}
pub fn get_view_style(&self) -> String {
self.lc.read().unwrap().view_style.clone()
}
@ -631,32 +657,16 @@ impl<T: InvokeUiSession> FileManager for Session<T> {}
#[async_trait]
impl<T: InvokeUiSession> Interface for Session<T> {
fn get_login_config_handler(&self) -> Arc<RwLock<LoginConfigHandler>> {
return self.lc.clone();
}
fn send(&self, data: Data) {
if let Some(sender) = self.sender.read().unwrap().as_ref() {
sender.send(data).ok();
}
}
fn is_file_transfer(&self) -> bool {
self.lc
.read()
.unwrap()
.conn_type
.eq(&ConnType::FILE_TRANSFER)
}
fn is_port_forward(&self) -> bool {
self.lc
.read()
.unwrap()
.conn_type
.eq(&ConnType::PORT_FORWARD)
}
fn is_rdp(&self) -> bool {
self.lc.read().unwrap().conn_type.eq(&ConnType::RDP)
}
fn msgbox(&self, msgtype: &str, title: &str, text: &str, link: &str) {
let direct = self.lc.read().unwrap().direct.unwrap_or_default();
let received = self.lc.read().unwrap().received;
@ -748,30 +758,6 @@ impl<T: InvokeUiSession> Interface for Session<T> {
handle_test_delay(t, peer).await;
}
}
fn set_connection_info(&mut self, direct: bool, received: bool) {
let mut lc = self.lc.write().unwrap();
lc.direct = Some(direct);
lc.received = received;
}
fn set_force_relay(&mut self, direct: bool, received: bool) {
let mut lc = self.lc.write().unwrap();
lc.force_relay = false;
if direct && !received {
let errno = errno::errno().0;
log::info!("errno is {}", errno);
// TODO: check mac and ios
if cfg!(windows) && errno == 10054 || !cfg!(windows) && errno == 104 {
lc.force_relay = true;
lc.set_option("force-always-relay".to_owned(), "Y".to_owned());
}
}
}
fn is_force_relay(&self) -> bool {
self.lc.read().unwrap().force_relay
}
}
impl<T: InvokeUiSession> Session<T> {