revert back from lan discovery merge

This commit is contained in:
open-trade
2022-01-10 18:05:42 +08:00
parent b400e4305f
commit 9a92b6ac4a
19 changed files with 26 additions and 701 deletions

View File

@@ -10,7 +10,6 @@ use hbb_common::{
bail,
config::{Config, PeerConfig, PeerInfoSerde, CONNECT_TIMEOUT, RELAY_PORT, RENDEZVOUS_TIMEOUT},
log,
base_proto::*,
message_proto::*,
protobuf::Message as _,
rendezvous_proto::*,

View File

@@ -91,7 +91,6 @@ pub enum Data {
RawMessage(Vec<u8>),
Socks(Option<config::Socks5Server>),
FS(FS),
SessionsUpdated,
Test,
}
@@ -407,22 +406,6 @@ 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);

View File

@@ -7,7 +7,6 @@ use hbb_common::{
config::{Config, CONNECT_TIMEOUT, RELAY_PORT},
log,
message_proto::*,
base_proto::*,
protobuf::{Message as _, ProtobufEnum},
rendezvous_proto::*,
sleep,
@@ -28,7 +27,6 @@ mod connection;
pub mod input_service;
mod service;
mod video_service;
pub mod udp;
use hbb_common::tcp::new_listener;
@@ -265,7 +263,6 @@ pub fn check_zombie() {
#[tokio::main]
pub async fn start_server(is_server: bool, _tray: bool) {
// TODO: Add a graceful shutdown handler, and attach all servers to that handler.
#[cfg(target_os = "linux")]
{
log::info!("DISPLAY={:?}", std::env::var("DISPLAY"));
@@ -278,13 +275,6 @@ pub async fn start_server(is_server: bool, _tray: bool) {
std::process::exit(-1);
}
});
let _server_guard = match udp::start_udp_server().await {
Ok(s) => Some(s),
Err(e) => {
log::warn!("Failed to start udp server {}", e);
None
}
};
input_service::fix_key_down_timeout_loop();
crate::RendezvousMediator::start_all().await;
} else {

View File

@@ -1,125 +0,0 @@
/// udp server
///
/// eg. discovery
///
use hbb_common::{base_proto::PeerInfo, config::SERVER_UDP_PORT, ResultType};
use socket_cs::udp::{Server, UdpHandlers};
/// Simple copy from ../connections.rs#send_logon_response
/// Should be merged into one function.
fn get_peer_info() -> PeerInfo {
let username = crate::platform::get_active_username();
#[allow(unused_mut)]
let mut sas_enabled = false;
#[cfg(windows)]
if crate::platform::is_root() {
sas_enabled = true;
}
PeerInfo {
hostname: whoami::hostname(),
username,
platform: whoami::platform().to_string(),
version: crate::VERSION.to_owned(),
sas_enabled,
..Default::default()
}
}
pub mod discovery {
use super::get_peer_info;
use crate::ipc;
use hbb_common::{
config::{PeerConfig, PeerInfoSerde, SERVER_UDP_PORT},
discovery_proto::{Discovery as DiscoveryProto, DiscoveryBack as DiscoveryBackProto},
log, protobuf, tokio, ResultType,
};
use socket_cs::{discovery::*, udp::UdpHandlers};
async fn get_discovery_back_info() -> DiscoveryBackProto {
let peer = get_peer_info();
DiscoveryBackProto {
id: ipc::get_id_async().await,
peer: protobuf::MessageField::from_option(Some(peer)),
..Default::default()
}
}
/// process sicovery bakc(response)
/// 1. update current peers.
/// 2. notify index window to udpate recent sessions.
fn process_discovery_back(info: DiscoveryBackProto) {
let mut config = PeerConfig::load(info.id.as_str());
let peer = info.peer.as_ref().unwrap();
let serde = PeerInfoSerde {
username: peer.username.clone(),
hostname: peer.hostname.clone(),
platform: peer.platform.clone(),
};
config.info = serde;
config.store(info.id.as_str());
#[tokio::main(flavor = "current_thread")]
async fn notify_index_window() -> ResultType<()> {
let ms_timeout = 300;
let mut c = ipc::connect(ms_timeout, "_index").await?;
c.send(&ipc::Data::SessionsUpdated).await?;
Ok(())
}
std::thread::spawn(move || {
if let Err(e) = notify_index_window() {
log::error!("Failed to notify index window, {}", e);
}
});
}
/// launch lan discover when user click "discover" button.
pub fn launch_lan_discover() {
std::thread::spawn(move || {
if let Err(e) = lan_discover() {
log::error!("Failed to lauch lan discover, {}", e);
}
});
}
#[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 discover request
.handle(
CMD_DISCOVERY.as_bytes().to_vec(),
Box::new(HandlerDiscovery::new(
// Some(|| Config::get_option("enable-be-discovered") == "Y".to_owned()),
Some(|| true),
info,
)),
)
// handle discover back(response)
.handle(
CMD_DISCOVERY_BACK.as_bytes().to_vec(),
Box::new(HandlerDiscoveryBack::new(process_discovery_back)),
)
}
}
pub(super) async fn start_udp_server() -> ResultType<Server> {
let handlers = discovery::handle_discovery(UdpHandlers::new()).await;
let mut server = Server::create(SERVER_UDP_PORT)?;
server.start(handlers).await?;
Ok(server)
}

View File

@@ -86,8 +86,6 @@ pub fn start(args: &mut [String]) {
let childs: Childs = Default::default();
let cloned = childs.clone();
std::thread::spawn(move || check_zombie(cloned));
let cloned = childs.clone();
std::thread::spawn(move || start_ipc(cloned));
crate::common::check_software_update();
frame.event_handler(UI::new(childs));
frame.sciter_handler(UIHostHandler {});
@@ -628,10 +626,6 @@ 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 {
@@ -689,7 +683,6 @@ impl sciter::EventHandler for UI {
fn get_software_ext();
fn open_url(String);
fn create_shortcut(String);
fn lan_discover();
}
}
@@ -721,55 +714,6 @@ pub fn check_zombie(childs: Childs) {
}
}
// TODO: Duplicated code.
// Need more generic and better shutdown handler
#[tokio::main(flavor = "current_thread")]
async fn start_ipc(childs: Childs) {
match ipc::new_listener("_index").await {
Ok(mut incoming) => {
while let Some(result) = incoming.next().await {
match result {
Ok(stream) => {
let mut stream = ipc::Connection::new(stream);
let childs = childs.clone();
tokio::spawn(async move {
loop {
tokio::select! {
res = stream.next() => {
match res {
Err(err) => {
log::info!("cm ipc connection closed: {}", err);
break;
}
Ok(Some(data)) => {
match data {
ipc::Data::SessionsUpdated => {
childs.lock().unwrap().0 = true;
}
_ => {
}
}
}
_ => {}
}
}
}
}
});
}
Err(err) => {
log::error!("Couldn't get index client: {:?}", err);
}
}
}
}
Err(err) => {
log::error!("Failed to start index ipc server: {}", err);
}
}
std::process::exit(-1);
}
// notice: avoiding create ipc connection repeatedly,
// because windows named pipe has serious memory leak issue.
#[tokio::main(flavor = "current_thread")]

View File

@@ -45,10 +45,6 @@ class ConnectStatus: Reactor.Component {
return translate("Ready");
}
event click $(.connect-status .link) () {
handler.set_option("stop-service", "");
}
event click $(#start-service) () {
handler.set_option("stop-service", "");
}

View File

@@ -7,7 +7,6 @@ use hbb_common::{
allow_err,
config::{self, Config, PeerConfig},
fs, log,
base_proto::*,
message_proto::*,
protobuf::Message as _,
rendezvous_proto::ConnType,