switch sides

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages
2023-01-17 13:28:33 +08:00
parent 45c0e10102
commit 333092f983
49 changed files with 373 additions and 61 deletions

View File

@@ -1,4 +1,5 @@
pub use async_trait::async_trait;
use bytes::Bytes;
#[cfg(not(any(target_os = "android", target_os = "linux")))]
use cpal::{
traits::{DeviceTrait, HostTrait, StreamTrait},
@@ -909,6 +910,7 @@ pub struct LoginConfigHandler {
pub force_relay: bool,
pub direct: Option<bool>,
pub received: bool,
switch_uuid: Option<String>,
}
impl Deref for LoginConfigHandler {
@@ -936,7 +938,7 @@ impl LoginConfigHandler {
///
/// * `id` - id of peer
/// * `conn_type` - Connection type enum.
pub fn initialize(&mut self, id: String, conn_type: ConnType) {
pub fn initialize(&mut self, id: String, conn_type: ConnType, switch_uuid: Option<String>) {
self.id = id;
self.conn_type = conn_type;
let config = self.load_config();
@@ -948,6 +950,7 @@ impl LoginConfigHandler {
self.force_relay = !self.get_option("force-always-relay").is_empty();
self.direct = None;
self.received = false;
self.switch_uuid = switch_uuid;
}
/// Check if the client should auto login.
@@ -1784,6 +1787,14 @@ pub async fn handle_hash(
interface: &impl Interface,
peer: &mut Stream,
) {
lc.write().unwrap().hash = hash.clone();
let uuid = lc.read().unwrap().switch_uuid.clone();
if let Some(uuid) = uuid {
if let Ok(uuid) = uuid::Uuid::from_str(&uuid) {
send_switch_login_request(lc.clone(), peer, uuid).await;
return;
}
}
let mut password = lc.read().unwrap().password.clone();
if password.is_empty() {
if !password_preset.is_empty() {
@@ -1848,6 +1859,26 @@ pub async fn handle_login_from_ui(
send_login(lc.clone(), hasher2.finalize()[..].into(), peer).await;
}
async fn send_switch_login_request(
lc: Arc<RwLock<LoginConfigHandler>>,
peer: &mut Stream,
uuid: Uuid,
) {
let mut msg_out = Message::new();
msg_out.set_switch_sides_response(SwitchSidesResponse {
uuid: Bytes::from(uuid.as_bytes().to_vec()),
lr: hbb_common::protobuf::MessageField::some(
lc.read()
.unwrap()
.create_login_msg(vec![])
.login_request()
.to_owned(),
),
..Default::default()
});
allow_err!(peer.send(&msg_out).await);
}
/// Interface for client to send data and commands.
#[async_trait]
pub trait Interface: Send + Clone + 'static + Sized {