mirror of
https://github.com/rustdesk/rustdesk.git
synced 2024-11-24 20:59:37 +08:00
lan discovery almost done
Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
parent
5682b088de
commit
d00352f4e4
@ -4,9 +4,10 @@ package discovery;
|
||||
import "base_proto.proto";
|
||||
|
||||
message Discovery {
|
||||
base.PeerInfo peer = 1;
|
||||
string id = 1;
|
||||
base.PeerInfo peer = 2;
|
||||
/// response port for current listening port(udp for now)
|
||||
int32 port = 2;
|
||||
int32 port = 3;
|
||||
}
|
||||
|
||||
message DiscoveryBack {
|
||||
|
@ -14,6 +14,7 @@ async fn lan_discover(port: u16, port_back: u16) {
|
||||
..Default::default()
|
||||
};
|
||||
let client = DiscoveryClient::create(DiscoveryProto {
|
||||
id: "client id".to_owned(),
|
||||
peer: protobuf::MessageField::from_option(Some(peer)),
|
||||
port: port_back as i32,
|
||||
..Default::default()
|
||||
|
@ -32,7 +32,7 @@ impl DiscoveryClient {
|
||||
log::trace!("succeeded to bind {} for discovery client", addr);
|
||||
|
||||
socket.set_broadcast(true)?;
|
||||
log::info!("Broadcast mode set to ON");
|
||||
log::trace!("Broadcast mode set to ON");
|
||||
|
||||
let send_data = make_send_data(CMD_DISCOVERY, &info)?;
|
||||
Ok(Self { socket, send_data })
|
||||
@ -47,6 +47,7 @@ impl DiscoveryClient {
|
||||
|
||||
pub struct HandlerDiscovery {
|
||||
get_allow: Option<fn() -> bool>,
|
||||
id: String,
|
||||
send_data: Vec<u8>,
|
||||
}
|
||||
|
||||
@ -55,6 +56,7 @@ impl HandlerDiscovery {
|
||||
let send_data = make_send_data(CMD_DISCOVERY_BACK, &self_info).unwrap();
|
||||
Self {
|
||||
get_allow,
|
||||
id: self_info.id,
|
||||
send_data,
|
||||
}
|
||||
}
|
||||
@ -63,11 +65,13 @@ impl HandlerDiscovery {
|
||||
#[async_trait]
|
||||
impl crate::Handler<UdpRequest> for HandlerDiscovery {
|
||||
async fn call(&self, request: UdpRequest) -> ResultType<()> {
|
||||
log::trace!("received discover query from {}", request.addr);
|
||||
|
||||
let discovery = DiscoveryProto::parse_from_bytes(&request.data)?;
|
||||
if discovery.id == self.id {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let peer = discovery.peer.as_ref().take().unwrap();
|
||||
log::debug!(
|
||||
log::trace!(
|
||||
"received discovery query from {} {}",
|
||||
peer.username,
|
||||
peer.hostname
|
||||
@ -75,12 +79,12 @@ impl crate::Handler<UdpRequest> for HandlerDiscovery {
|
||||
|
||||
let allowed = self.get_allow.map_or(false, |f| f());
|
||||
if !allowed {
|
||||
log::info!(
|
||||
"received discovery query from {} {} {}, but discovery is not allowed",
|
||||
request.addr,
|
||||
peer.hostname,
|
||||
peer.username
|
||||
);
|
||||
// log::info!(
|
||||
// "received discovery query from {} {} {}, but discovery is not allowed",
|
||||
// request.addr,
|
||||
// peer.hostname,
|
||||
// peer.username
|
||||
// );
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@ -89,7 +93,7 @@ impl crate::Handler<UdpRequest> for HandlerDiscovery {
|
||||
|
||||
let mut peer_addr = request.addr;
|
||||
peer_addr.set_port(discovery.port as u16);
|
||||
log::debug!("send self peer info to {}", peer_addr);
|
||||
log::trace!("send self peer info to {}", peer_addr);
|
||||
|
||||
let send_len = self.send_data.len();
|
||||
let mut cur_len = 0usize;
|
||||
|
@ -64,7 +64,6 @@ impl Server {
|
||||
break;
|
||||
}
|
||||
n = server.next() => {
|
||||
log::info!("received message");
|
||||
let handlers = handlers.clone();
|
||||
let rt = rt.clone();
|
||||
match n {
|
||||
@ -76,24 +75,24 @@ impl Server {
|
||||
match handlers.get(&cmd) {
|
||||
Some(h) => {
|
||||
let request = UdpRequest {data: data[p+1..].to_vec(), addr};
|
||||
if let Err(e) = h.call(request).await {
|
||||
log::error!("handle {:?} failed, {}", cmd, e);
|
||||
if let Err(_e) = h.call(request).await {
|
||||
// log::error!("handle {:?} failed, {}", cmd, e);
|
||||
}
|
||||
}
|
||||
None => {
|
||||
log::warn!("no handler for {:?}", &cmd);
|
||||
// log::warn!("no handler for {:?}", &cmd);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
None => {
|
||||
log::error!("failed to parse command token");
|
||||
// log::error!("failed to parse command token");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Some(Err(e)) => {
|
||||
log::error!("recv error: {}", e)
|
||||
Some(Err(_e)) => {
|
||||
// log::error!("recv error: {}", e)
|
||||
}
|
||||
None => {
|
||||
log::error!("should never reach here");
|
||||
@ -139,11 +138,12 @@ impl UdpHandlers {
|
||||
/// ```rust
|
||||
/// extern crate socket_cs;
|
||||
/// use socket_cs::{ResultType, udp::{UdpHandlers, UdpRequest}};
|
||||
/// use async_trait::async_trait;
|
||||
///
|
||||
/// struct SimpleHandler;
|
||||
///
|
||||
/// #[async_trait]
|
||||
/// impl crate::Handler<UdpRequest> for SimpleHandler {
|
||||
/// impl socket_cs::Handler<UdpRequest> for SimpleHandler {
|
||||
/// async fn call(&self, _: UdpRequest) -> ResultType<()> {
|
||||
/// Ok(())
|
||||
/// }
|
||||
|
16
src/ipc.rs
16
src/ipc.rs
@ -389,6 +389,22 @@ pub fn get_id() -> String {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_id_async() -> String {
|
||||
if let Ok(Some(v)) = get_config_async("id", 1_000).await {
|
||||
// update salt also, so that next time reinstallation not causing first-time auto-login failure
|
||||
if let Ok(Some(v2)) = get_config_async("salt", 1_000).await {
|
||||
Config::set_salt(&v2);
|
||||
}
|
||||
if v != Config::get_id() {
|
||||
Config::set_key_confirmed(false);
|
||||
Config::set_id(&v);
|
||||
}
|
||||
v
|
||||
} else {
|
||||
Config::get_id()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_password() -> String {
|
||||
if let Ok(Some(v)) = get_config("password") {
|
||||
Config::set_password(&v);
|
||||
|
@ -28,7 +28,7 @@ mod connection;
|
||||
pub mod input_service;
|
||||
mod service;
|
||||
mod video_service;
|
||||
mod udp;
|
||||
pub mod udp;
|
||||
|
||||
use hbb_common::tcp::new_listener;
|
||||
|
||||
|
@ -25,23 +25,20 @@ fn get_peer_info() -> PeerInfo {
|
||||
}
|
||||
}
|
||||
|
||||
mod discovery {
|
||||
pub mod discovery {
|
||||
use super::get_peer_info;
|
||||
use crate::ipc;
|
||||
use hbb_common::{
|
||||
base_proto::PeerInfo,
|
||||
config::{PeerConfig, PeerInfoSerde},
|
||||
config::{Config, PeerConfig, PeerInfoSerde, SERVER_UDP_PORT},
|
||||
discovery_proto::{Discovery as DiscoveryProto, DiscoveryBack as DiscoveryBackProto},
|
||||
log, protobuf,
|
||||
tokio::runtime::Runtime,
|
||||
ResultType,
|
||||
log, protobuf, tokio, ResultType,
|
||||
};
|
||||
use socket_cs::{discovery::*, udp::UdpHandlers};
|
||||
|
||||
fn get_discovery_back_info() -> DiscoveryBackProto {
|
||||
async fn get_discovery_back_info() -> DiscoveryBackProto {
|
||||
let peer = get_peer_info();
|
||||
DiscoveryBackProto {
|
||||
id: ipc::get_id(),
|
||||
id: ipc::get_id_async().await,
|
||||
peer: protobuf::MessageField::from_option(Some(peer)),
|
||||
..Default::default()
|
||||
}
|
||||
@ -59,35 +56,52 @@ mod discovery {
|
||||
config.info = serde;
|
||||
config.store(info.id.as_str());
|
||||
|
||||
let rt = match Runtime::new() {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
log::error!("Failed to notify index window, {}", e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
#[tokio::main(flavor = "current_thread")]
|
||||
async fn notify_index_window() -> ResultType<()> {
|
||||
let ms_timeout = 1000;
|
||||
let ms_timeout = 300;
|
||||
let mut c = ipc::connect(ms_timeout, "_index").await?;
|
||||
c.send(&ipc::Data::SessionsUpdated).await?;
|
||||
Ok(())
|
||||
}
|
||||
rt.block_on(async move {
|
||||
if let Err(e) = notify_index_window().await {
|
||||
std::thread::spawn(move || {
|
||||
if let Err(e) = notify_index_window() {
|
||||
log::error!("Failed to notify index window, {}", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// pub(crate) fn lan_discover();
|
||||
pub fn launch_lan_discover() {
|
||||
std::thread::spawn(move || {
|
||||
if let Err(e) = lan_discover() {
|
||||
log::error!("Failed to lauch lan discover, {}", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub(super) fn handle_discovery(handlers: UdpHandlers) -> UdpHandlers {
|
||||
let info = get_discovery_back_info();
|
||||
#[tokio::main(flavor = "current_thread")]
|
||||
pub async fn lan_discover() -> ResultType<()> {
|
||||
let peer = get_peer_info();
|
||||
let client = DiscoveryClient::create(DiscoveryProto {
|
||||
id: ipc::get_id_async().await,
|
||||
peer: protobuf::MessageField::from_option(Some(peer)),
|
||||
port: SERVER_UDP_PORT as i32,
|
||||
..Default::default()
|
||||
})
|
||||
.await?;
|
||||
|
||||
client.lan_discover(SERVER_UDP_PORT).await
|
||||
}
|
||||
|
||||
pub(super) async fn handle_discovery(handlers: UdpHandlers) -> UdpHandlers {
|
||||
let info = get_discovery_back_info().await;
|
||||
handlers
|
||||
.handle(
|
||||
CMD_DISCOVERY.as_bytes().to_vec(),
|
||||
Box::new(HandlerDiscovery::new(Some(|| true), info)),
|
||||
Box::new(HandlerDiscovery::new(
|
||||
// Some(|| Config::get_option("enable-be-discovered") == "Y".to_owned()),
|
||||
Some(|| true),
|
||||
info,
|
||||
)),
|
||||
)
|
||||
.handle(
|
||||
CMD_DISCOVERY_BACK.as_bytes().to_vec(),
|
||||
@ -97,7 +111,7 @@ mod discovery {
|
||||
}
|
||||
|
||||
pub(super) async fn start_udp_server() -> ResultType<Server> {
|
||||
let handlers = discovery::handle_discovery(UdpHandlers::new());
|
||||
let handlers = discovery::handle_discovery(UdpHandlers::new()).await;
|
||||
|
||||
let mut server = Server::create(SERVER_UDP_PORT)?;
|
||||
server.start(handlers).await?;
|
||||
|
@ -87,7 +87,7 @@ pub fn start(args: &mut [String]) {
|
||||
let cloned = childs.clone();
|
||||
std::thread::spawn(move || check_zombie(cloned));
|
||||
let cloned = childs.clone();
|
||||
tokio::spawn(async move {start_ipc(cloned)});
|
||||
std::thread::spawn(move || start_ipc(cloned));
|
||||
crate::common::check_software_update();
|
||||
frame.event_handler(UI::new(childs));
|
||||
frame.sciter_handler(UIHostHandler {});
|
||||
@ -567,6 +567,10 @@ impl UI {
|
||||
fn is_xfce(&self) -> bool {
|
||||
crate::platform::is_xfce()
|
||||
}
|
||||
|
||||
fn lan_discover(&self) {
|
||||
crate::server::udp::discovery::launch_lan_discover();
|
||||
}
|
||||
}
|
||||
|
||||
impl sciter::EventHandler for UI {
|
||||
@ -615,6 +619,7 @@ impl sciter::EventHandler for UI {
|
||||
fn get_software_ext();
|
||||
fn open_url(String);
|
||||
fn create_shortcut(String);
|
||||
fn lan_discover();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -51,12 +51,17 @@ class RecentSessions: Reactor.Component {
|
||||
sessions = sessions.map(this.getSession);
|
||||
return <div style="width: *">
|
||||
<div .recent-sessions-title>RECENT SESSIONS</div>
|
||||
<button .button #discover>DISCOVER</button>
|
||||
<div .recent-sessions-content key={sessions.length}>
|
||||
{sessions}
|
||||
</div>
|
||||
</div>;
|
||||
}
|
||||
|
||||
event click $(button#discover) {
|
||||
handler.lan_discover();
|
||||
}
|
||||
|
||||
function getSession(s) {
|
||||
var id = s[0];
|
||||
var username = s[1];
|
||||
|
Loading…
Reference in New Issue
Block a user