This commit is contained in:
rustdesk
2022-12-28 13:52:13 +08:00
parent 75f57cf0fc
commit 08b8f40397
10 changed files with 167 additions and 208 deletions

115
src/ui.rs
View File

@@ -9,13 +9,9 @@ use sciter::Value;
use hbb_common::{
allow_err,
config::{self, Config, PeerConfig, RENDEZVOUS_PORT, RENDEZVOUS_TIMEOUT},
futures::future::join_all,
config::{self, PeerConfig},
log,
protobuf::Message as _,
rendezvous_proto::*,
tcp::FramedStream,
tokio,
};
use crate::common::get_app_name;
@@ -31,8 +27,6 @@ pub mod remote;
#[cfg(target_os = "windows")]
pub mod win_privacy;
type Message = RendezvousMessage;
pub type Children = Arc<Mutex<(bool, HashMap<(String, String), Child>)>>;
#[allow(dead_code)]
type Status = (i32, bool, i64, String);
@@ -124,15 +118,11 @@ pub fn start(args: &mut [String]) {
let args: Vec<String> = iter.map(|x| x.clone()).collect();
frame.set_title(&id);
frame.register_behavior("native-remote", move || {
let handler = remote::SciterSession::new(
cmd.clone(),
id.clone(),
pass.clone(),
args.clone(),
);
let handler =
remote::SciterSession::new(cmd.clone(), id.clone(), pass.clone(), args.clone());
#[cfg(not(feature = "flutter"))]
crate::keyboard::set_cur_session(handler.inner());
Box::new(handler)
});
page = "remote.html";
@@ -514,7 +504,7 @@ impl UI {
fn change_id(&self, id: String) {
let old_id = self.get_id();
change_id(id, old_id);
change_id_shared(id, old_id);
}
fn post_request(&self, url: String, body: String, header: String) {
@@ -694,101 +684,6 @@ fn get_sound_inputs() -> Vec<String> {
.collect()
}
const INVALID_FORMAT: &'static str = "Invalid format";
const UNKNOWN_ERROR: &'static str = "Unknown error";
#[tokio::main(flavor = "current_thread")]
async fn change_id(id: String, old_id: String) -> &'static str {
if !hbb_common::is_valid_custom_id(&id) {
return INVALID_FORMAT;
}
let uuid = machine_uid::get().unwrap_or("".to_owned());
if uuid.is_empty() {
return UNKNOWN_ERROR;
}
let rendezvous_servers = crate::ipc::get_rendezvous_servers(1_000).await;
let mut futs = Vec::new();
let err: Arc<Mutex<&str>> = Default::default();
for rendezvous_server in rendezvous_servers {
let err = err.clone();
let id = id.to_owned();
let uuid = uuid.clone();
let old_id = old_id.clone();
futs.push(tokio::spawn(async move {
let tmp = check_id(rendezvous_server, old_id, id, uuid).await;
if !tmp.is_empty() {
*err.lock().unwrap() = tmp;
}
}));
}
join_all(futs).await;
let err = *err.lock().unwrap();
if err.is_empty() {
crate::ipc::set_config_async("id", id.to_owned()).await.ok();
}
err
}
async fn check_id(
rendezvous_server: String,
old_id: String,
id: String,
uuid: String,
) -> &'static str {
let any_addr = Config::get_any_listen_addr();
if let Ok(mut socket) = FramedStream::new(
crate::check_port(rendezvous_server, RENDEZVOUS_PORT),
any_addr,
RENDEZVOUS_TIMEOUT,
)
.await
{
let mut msg_out = Message::new();
msg_out.set_register_pk(RegisterPk {
old_id,
id,
uuid: uuid.into(),
..Default::default()
});
let mut ok = false;
if socket.send(&msg_out).await.is_ok() {
if let Some(Ok(bytes)) = socket.next_timeout(3_000).await {
if let Ok(msg_in) = RendezvousMessage::parse_from_bytes(&bytes) {
match msg_in.union {
Some(rendezvous_message::Union::RegisterPkResponse(rpr)) => {
match rpr.result.enum_value_or_default() {
register_pk_response::Result::OK => {
ok = true;
}
register_pk_response::Result::ID_EXISTS => {
return "Not available";
}
register_pk_response::Result::TOO_FREQUENT => {
return "Too frequent";
}
register_pk_response::Result::NOT_SUPPORT => {
return "server_not_support";
}
register_pk_response::Result::INVALID_ID_FORMAT => {
return INVALID_FORMAT;
}
_ => {}
}
}
_ => {}
}
}
}
}
if !ok {
return UNKNOWN_ERROR;
}
} else {
return "Failed to connect to rendezvous server";
}
""
}
// sacrifice some memory
pub fn value_crash_workaround(values: &[Value]) -> Arc<Vec<Value>> {
let persist = Arc::new(values.to_vec());