mirror of
https://github.com/weyne85/rustdesk.git
synced 2025-10-29 17:00:05 +00:00
opt force_relay/relay_hint logic
Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
@@ -213,6 +213,8 @@ impl Client {
|
||||
conn_type: ConnType,
|
||||
interface: impl Interface,
|
||||
) -> ResultType<(Stream, bool, Option<Vec<u8>>)> {
|
||||
interface.update_direct(None);
|
||||
interface.update_received(false);
|
||||
match Self::_start(peer, key, token, conn_type, interface).await {
|
||||
Err(err) => {
|
||||
let err_str = err.to_string();
|
||||
@@ -353,15 +355,8 @@ impl Client {
|
||||
my_addr.is_ipv4(),
|
||||
)
|
||||
.await?;
|
||||
let pk = Self::secure_connection(
|
||||
peer,
|
||||
signed_id_pk,
|
||||
key,
|
||||
&mut conn,
|
||||
false,
|
||||
interface,
|
||||
)
|
||||
.await?;
|
||||
let pk =
|
||||
Self::secure_connection(peer, signed_id_pk, key, &mut conn).await?;
|
||||
return Ok((conn, false, pk));
|
||||
}
|
||||
_ => {
|
||||
@@ -459,6 +454,7 @@ impl Client {
|
||||
let mut conn =
|
||||
socket_client::connect_tcp_local(peer, Some(local_addr), connect_timeout).await;
|
||||
let mut direct = !conn.is_err();
|
||||
interface.update_direct(Some(direct));
|
||||
if interface.is_force_relay() || conn.is_err() {
|
||||
if !relay_server.is_empty() {
|
||||
conn = Self::request_relay(
|
||||
@@ -471,6 +467,7 @@ impl Client {
|
||||
conn_type,
|
||||
)
|
||||
.await;
|
||||
interface.update_direct(Some(false));
|
||||
if conn.is_err() {
|
||||
bail!(
|
||||
"Failed to connect via relay server: {}",
|
||||
@@ -490,8 +487,7 @@ impl Client {
|
||||
}
|
||||
let mut conn = conn?;
|
||||
log::info!("{:?} used to establish connection", start.elapsed());
|
||||
let pk = Self::secure_connection(peer_id, signed_id_pk, key, &mut conn, direct, interface)
|
||||
.await?;
|
||||
let pk = Self::secure_connection(peer_id, signed_id_pk, key, &mut conn).await?;
|
||||
Ok((conn, direct, pk))
|
||||
}
|
||||
|
||||
@@ -501,8 +497,6 @@ impl Client {
|
||||
signed_id_pk: Vec<u8>,
|
||||
key: &str,
|
||||
conn: &mut Stream,
|
||||
direct: bool,
|
||||
interface: impl Interface,
|
||||
) -> ResultType<Option<Vec<u8>>> {
|
||||
let rs_pk = get_rs_pk(if key.is_empty() {
|
||||
hbb_common::config::RS_PUB_KEY
|
||||
@@ -532,13 +526,7 @@ impl Client {
|
||||
};
|
||||
match timeout(READ_TIMEOUT, conn.next()).await? {
|
||||
Some(res) => {
|
||||
let bytes = match res {
|
||||
Ok(bytes) => bytes,
|
||||
Err(err) => {
|
||||
interface.set_force_relay(direct, false, err.to_string());
|
||||
bail!("{}", err);
|
||||
}
|
||||
};
|
||||
let bytes = res?;
|
||||
if let Ok(msg_in) = Message::parse_from_bytes(&bytes) {
|
||||
if let Some(message::Union::SignedId(si)) = msg_in.union {
|
||||
if let Ok((id, their_pk_b)) = decode_id_pk(&si.id, &sign_pk) {
|
||||
@@ -1082,8 +1070,6 @@ pub struct LoginConfigHandler {
|
||||
pub direct: Option<bool>,
|
||||
pub received: bool,
|
||||
switch_uuid: Option<String>,
|
||||
pub success_time: Option<hbb_common::tokio::time::Instant>,
|
||||
pub direct_error_counter: usize,
|
||||
}
|
||||
|
||||
impl Deref for LoginConfigHandler {
|
||||
@@ -1130,8 +1116,6 @@ impl LoginConfigHandler {
|
||||
self.direct = None;
|
||||
self.received = false;
|
||||
self.switch_uuid = switch_uuid;
|
||||
self.success_time = None;
|
||||
self.direct_error_counter = 0;
|
||||
}
|
||||
|
||||
/// Check if the client should auto login.
|
||||
@@ -1759,20 +1743,6 @@ impl LoginConfigHandler {
|
||||
msg_out.set_misc(misc);
|
||||
msg_out
|
||||
}
|
||||
|
||||
pub fn set_force_relay(&mut self, direct: bool, received: bool, err: String) {
|
||||
self.force_relay = false;
|
||||
if direct && !received {
|
||||
let errno = errno::errno().0;
|
||||
// TODO: check mac and ios
|
||||
if cfg!(windows) && (errno == 10054 || err.contains("10054"))
|
||||
|| !cfg!(windows) && (errno == 104 || err.contains("104"))
|
||||
{
|
||||
self.force_relay = true;
|
||||
self.set_option("force-always-relay".to_owned(), "Y".to_owned());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Media data.
|
||||
@@ -2317,16 +2287,48 @@ pub trait Interface: Send + Clone + 'static + Sized {
|
||||
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, err: String) {
|
||||
self.get_login_config_handler()
|
||||
.write()
|
||||
.unwrap()
|
||||
.set_force_relay(direct, received, err);
|
||||
}
|
||||
|
||||
fn is_force_relay(&self) -> bool {
|
||||
self.get_login_config_handler().read().unwrap().force_relay
|
||||
}
|
||||
fn swap_modifier_mouse(&self, _msg: &mut hbb_common::protos::message::MouseEvent) {}
|
||||
|
||||
fn update_direct(&self, direct: Option<bool>) {
|
||||
self.get_login_config_handler().write().unwrap().direct = direct;
|
||||
}
|
||||
|
||||
fn update_received(&self, received: bool) {
|
||||
self.get_login_config_handler().write().unwrap().received = received;
|
||||
}
|
||||
|
||||
fn on_establish_connection_error(&self, err: String) {
|
||||
log::error!("Connection closed: {}", err);
|
||||
let title = "Connection Error";
|
||||
let text = err.to_string();
|
||||
let lc = self.get_login_config_handler();
|
||||
let direct = lc.read().unwrap().direct;
|
||||
let received = lc.read().unwrap().received;
|
||||
let relay_condition = direct == Some(true) && !received;
|
||||
|
||||
// force relay
|
||||
let errno = errno::errno().0;
|
||||
if relay_condition
|
||||
&& (cfg!(windows) && (errno == 10054 || err.contains("10054"))
|
||||
|| !cfg!(windows) && (errno == 104 || err.contains("104")))
|
||||
{
|
||||
lc.write().unwrap().force_relay = true;
|
||||
lc.write()
|
||||
.unwrap()
|
||||
.set_option("force-always-relay".to_owned(), "Y".to_owned());
|
||||
}
|
||||
|
||||
// relay-hint
|
||||
if cfg!(feature = "flutter") && relay_condition {
|
||||
self.msgbox("relay-hint", title, &text, "");
|
||||
} else {
|
||||
self.msgbox("error", title, &text, "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Data used by the client interface.
|
||||
|
||||
Reference in New Issue
Block a user