From 6c8d296c44becb6d49b2503920867d214fef35ef Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 22 Jun 2019 10:13:02 -0700 Subject: [PATCH] respect the connect_automatically configuration For unix and tls clients, if connect_automatically is set and we're not running the server, then instantiate and connect to those domains when we start the app. As part of this I realized that we weren't mapping the remote tab to the local tab for unilateral pushes, so add some stuff to support that. --- src/config.rs | 5 +++ src/frontend/mod.rs | 2 +- src/main.rs | 48 ++++++++++++++++---------- src/mux/mod.rs | 2 -- src/server/client.rs | 81 ++++++++++++++++++++++++++++++++++++-------- src/server/domain.rs | 29 ++++++++++++++-- src/server/tab.rs | 4 +++ 7 files changed, 133 insertions(+), 38 deletions(-) diff --git a/src/config.rs b/src/config.rs index 206dc34cc..7198ad0a5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -371,6 +371,7 @@ pub struct UnixDomain { pub socket_path: Option, /// If true, connect to this domain automatically at startup + #[serde(default)] pub connect_automatically: bool, } @@ -449,6 +450,10 @@ pub struct TlsDomainClient { /// the hostname portion of the `remote_address` configuration and you /// should not normally need to override this value. pub expected_cn: Option, + + /// If true, connect to this domain automatically at startup + #[serde(default)] + pub connect_automatically: bool, } impl Default for Config { diff --git a/src/frontend/mod.rs b/src/frontend/mod.rs index 9e9abf834..e31944193 100644 --- a/src/frontend/mod.rs +++ b/src/frontend/mod.rs @@ -18,7 +18,7 @@ pub mod muxserver; #[cfg(all(unix, not(feature = "force-glutin"), not(target_os = "macos")))] pub mod xwindows; -#[derive(Debug, Deserialize, Clone, Copy)] +#[derive(Debug, Deserialize, Clone, Copy, PartialEq, Eq)] pub enum FrontEndSelection { Glutin, X11, diff --git a/src/main.rs b/src/main.rs index 836d4dc23..c0d263247 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ // Don't create a new standard console window when launched from the windows GUI. #![windows_subsystem = "windows"] -use failure::Error; +use failure::{Error, Fallible}; use log::error; use std::ffi::OsString; use structopt::StructOpt; @@ -17,7 +17,7 @@ mod opengl; mod ratelim; mod server; use crate::frontend::FrontEndSelection; -use crate::mux::domain::{Domain, LocalDomain}; +use crate::mux::domain::{alloc_domain_id, Domain, LocalDomain}; use crate::mux::Mux; use crate::server::client::Client; use crate::server::domain::ClientDomain; @@ -146,28 +146,42 @@ fn run_terminal_gui(config: Arc, opts: &StartCommand) -> Result< None }; - let domain: Arc = if opts.mux_client_as_default_domain { - let client = Client::new_default_unix_domain(&config)?; - Arc::new(ClientDomain::new(client)) - } else if opts.mux_tls_client_as_default_domain { - let tls_client = config - .tls_clients - .first() - .expect("tls clients to be configured"); - let client = Client::new_tls(&config, tls_client)?; - Arc::new(ClientDomain::new(client)) - } else { - Arc::new(LocalDomain::new(&config)?) - }; - + let domain: Arc = Arc::new(LocalDomain::new(&config)?); let mux = Rc::new(mux::Mux::new(&config, &domain)); Mux::set_mux(&mux); let front_end = opts.front_end.unwrap_or(config.front_end); let gui = front_end.try_new(&mux)?; - domain.attach()?; + fn attach_client(mux: &Rc, client: ClientDomain) -> Fallible<()> { + let domain: Arc = Arc::new(client); + mux.add_domain(&domain); + domain.attach() + } + + if front_end != FrontEndSelection::MuxServer { + for unix_dom in &config.unix_domains { + if unix_dom.connect_automatically { + let domain_id = alloc_domain_id(); + attach_client( + &mux, + ClientDomain::new(Client::new_unix_domain(domain_id, &config, unix_dom)?), + )?; + } + } + + for tls_client in &config.tls_clients { + if tls_client.connect_automatically { + let domain_id = alloc_domain_id(); + attach_client( + &mux, + ClientDomain::new(Client::new_tls(domain_id, &config, tls_client)?), + )?; + } + } + } + if mux.is_empty() { let window_id = mux.new_empty_window(); let tab = mux diff --git a/src/mux/mod.rs b/src/mux/mod.rs index a01528fc4..195432007 100644 --- a/src/mux/mod.rs +++ b/src/mux/mod.rs @@ -157,7 +157,6 @@ impl Mux { self.domains.borrow().get(&id).cloned() } - #[allow(dead_code)] pub fn add_domain(&self, domain: &Arc) { self.domains .borrow_mut() @@ -259,7 +258,6 @@ impl Mux { self.tabs.borrow().is_empty() } - #[allow(dead_code)] pub fn iter_tabs(&self) -> Vec> { self.tabs .borrow() diff --git a/src/server/client.rs b/src/server/client.rs index 33e77d8bc..58177c1f1 100644 --- a/src/server/client.rs +++ b/src/server/client.rs @@ -1,8 +1,11 @@ #![allow(dead_code)] use crate::config::{Config, TlsDomainClient, UnixDomain}; use crate::frontend::gui_executor; +use crate::mux::domain::alloc_domain_id; +use crate::mux::domain::DomainId; use crate::mux::Mux; use crate::server::codec::*; +use crate::server::domain::ClientDomain; use crate::server::pollable::*; use crate::server::tab::ClientTab; use crate::server::UnixStream; @@ -24,6 +27,7 @@ enum ReaderMessage { #[derive(Clone)] pub struct Client { sender: PollableSender, + local_domain_id: DomainId, } macro_rules! rpc { @@ -55,15 +59,42 @@ macro_rules! rpc { }; } -fn process_unilateral(decoded: DecodedPdu) -> Fallible<()> { +fn process_unilateral(local_domain_id: DomainId, decoded: DecodedPdu) -> Fallible<()> { if let Some(tab_id) = decoded.pdu.tab_id() { let pdu = decoded.pdu; Future::with_executor(gui_executor().unwrap(), move || { let mux = Mux::get().unwrap(); + let client_domain = mux + .get_domain(local_domain_id) + .ok_or_else(|| format_err!("no such domain {}", local_domain_id))?; + let client_domain = client_domain + .downcast_ref::() + .ok_or_else(|| { + format_err!("domain {} is not a ClientDomain instance", local_domain_id) + })?; + + let local_tab_id = client_domain + .remote_to_local_tab_id(tab_id) + .ok_or_else(|| { + format_err!("remote tab id {} does not have a local tab id", tab_id) + })?; let tab = mux - .get_tab(tab_id) - .ok_or_else(|| format_err!("no such tab {}", tab_id))?; - let client_tab = tab.downcast_ref::().unwrap(); + .get_tab(local_tab_id) + .ok_or_else(|| format_err!("no such tab {}", local_tab_id))?; + let client_tab = tab.downcast_ref::().ok_or_else(|| { + log::error!( + "received unilateral PDU for tab {} which is \ + not an instance of ClientTab: {:?}", + local_tab_id, + pdu + ); + format_err!( + "received unilateral PDU for tab {} which is \ + not an instance of ClientTab: {:?}", + local_tab_id, + pdu + ) + })?; client_tab.process_unilateral(pdu) }); } else { @@ -74,6 +105,7 @@ fn process_unilateral(decoded: DecodedPdu) -> Fallible<()> { fn client_thread( mut stream: Box, + local_domain_id: DomainId, rx: PollableReceiver, ) -> Fallible<()> { let mut next_serial = 1u64; @@ -114,7 +146,7 @@ fn client_thread( if let Some(decoded) = res? { log::trace!("decoded serial {}", decoded.serial); if decoded.serial == 0 { - process_unilateral(decoded)?; + process_unilateral(local_domain_id, decoded)?; } else if let Some(mut promise) = promises.remove(&decoded.serial) { promise.result(Ok(decoded.pdu)); } else { @@ -132,34 +164,49 @@ fn client_thread( } impl Client { - pub fn new(stream: Box) -> Self { + pub fn new(local_domain_id: DomainId, stream: Box) -> Self { let (sender, receiver) = pollable_channel().expect("failed to create pollable_channel"); thread::spawn(move || { - if let Err(e) = client_thread(stream, receiver) { + if let Err(e) = client_thread(stream, local_domain_id, receiver) { log::debug!("client thread ended: {}", e); } }); - Self { sender } + Self { + sender, + local_domain_id, + } + } + + pub fn local_domain_id(&self) -> DomainId { + self.local_domain_id } pub fn new_default_unix_domain(config: &Arc) -> Fallible { for unix_dom in &config.unix_domains { - return Self::new_unix_domain(config, unix_dom); + return Self::new_unix_domain(alloc_domain_id(), config, unix_dom); } bail!("no default unix domain is configured"); } - pub fn new_unix_domain(_config: &Arc, unix_dom: &UnixDomain) -> Fallible { + pub fn new_unix_domain( + local_domain_id: DomainId, + _config: &Arc, + unix_dom: &UnixDomain, + ) -> Fallible { let sock_path = unix_dom.socket_path(); info!("connect to {}", sock_path.display()); let stream = Box::new(UnixStream::connect(sock_path)?); - Ok(Self::new(stream)) + Ok(Self::new(local_domain_id, stream)) } #[cfg(any(feature = "openssl", unix))] - pub fn new_tls(_config: &Arc, tls_client: &TlsDomainClient) -> Fallible { + pub fn new_tls( + local_domain_id: DomainId, + _config: &Arc, + tls_client: &TlsDomainClient, + ) -> Fallible { use crate::server::listener::read_bytes; use openssl::ssl::{SslConnector, SslFiletype, SslMethod}; use openssl::x509::X509; @@ -232,11 +279,15 @@ impl Client { ) })?, ); - Ok(Self::new(stream)) + Ok(Self::new(local_domain_id, stream)) } #[cfg(not(any(feature = "openssl", unix)))] - pub fn new_tls(_config: &Arc, tls_client: &TlsDomainClient) -> Fallible { + pub fn new_tls( + local_domain_id: DomainId, + _config: &Arc, + tls_client: &TlsDomainClient, + ) -> Fallible { use crate::server::listener::IdentitySource; use native_tls::TlsConnector; use std::convert::TryInto; @@ -278,7 +329,7 @@ impl Client { e ) })?); - Ok(Self::new(stream)) + Ok(Self::new(local_domain_id, stream)) } pub fn send_pdu(&self, pdu: Pdu) -> Future { diff --git a/src/server/domain.rs b/src/server/domain.rs index ebab820db..1c7b4f90f 100644 --- a/src/server/domain.rs +++ b/src/server/domain.rs @@ -1,7 +1,7 @@ use crate::font::{FontConfiguration, FontSystemSelection}; use crate::frontend::front_end; -use crate::mux::domain::{alloc_domain_id, Domain, DomainId}; -use crate::mux::tab::Tab; +use crate::mux::domain::{Domain, DomainId}; +use crate::mux::tab::{Tab, TabId}; use crate::mux::window::WindowId; use crate::mux::Mux; use crate::server::client::Client; @@ -18,6 +18,7 @@ pub struct ClientInner { pub local_domain_id: DomainId, pub remote_domain_id: DomainId, remote_to_local_window: Mutex>, + remote_to_local_tab: Mutex>, } impl ClientInner { @@ -57,7 +58,7 @@ pub struct ClientDomain { impl ClientInner { pub fn new(client: Client) -> Self { - let local_domain_id = alloc_domain_id(); + let local_domain_id = client.local_domain_id(); // Assumption: that the domain id on the other end is // always the first created default domain. In the future // we'll add a way to discover/enumerate domains to populate @@ -68,6 +69,7 @@ impl ClientInner { local_domain_id, remote_domain_id, remote_to_local_window: Mutex::new(HashMap::new()), + remote_to_local_tab: Mutex::new(HashMap::new()), } } } @@ -77,6 +79,27 @@ impl ClientDomain { let inner = Arc::new(ClientInner::new(client)); Self { inner } } + + pub fn remote_to_local_tab_id(&self, remote_tab_id: TabId) -> Option { + let mut tab_map = self.inner.remote_to_local_tab.lock().unwrap(); + + if let Some(id) = tab_map.get(&remote_tab_id) { + return Some(*id); + } + + let mux = Mux::get().unwrap(); + + for tab in mux.iter_tabs() { + if let Some(tab) = tab.downcast_ref::() { + if tab.remote_tab_id() == remote_tab_id { + let local_tab_id = tab.tab_id(); + tab_map.insert(remote_tab_id, local_tab_id); + return Some(local_tab_id); + } + } + } + None + } } impl Domain for ClientDomain { diff --git a/src/server/tab.rs b/src/server/tab.rs index ab75ce92d..0d39a3429 100644 --- a/src/server/tab.rs +++ b/src/server/tab.rs @@ -214,6 +214,10 @@ impl ClientTab { }; Ok(()) } + + pub fn remote_tab_id(&self) -> TabId { + self.remote_tab_id + } } impl Tab for ClientTab {