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.
This commit is contained in:
Wez Furlong committed 2019-06-22 10:13:02 -07:00
1 parent 9054343e51
commit 6c8d296c44
7 files changed
+133 -38

No files matched your search

+5
View File
@@ -371,6 +371,7 @@ pub struct UnixDomain {
pub socket_path: Option<PathBuf>,
/// 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<String>,
/// If true, connect to this domain automatically at startup
#[serde(default)]
pub connect_automatically: bool,
}
impl Default for Config {
+1 -1
View File
@@ -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,
+31 -17
View File
@@ -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<config::Config>, opts: &StartCommand) -> Result<
None
};
let domain: Arc<dyn Domain> = 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<dyn Domain> = 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<Mux>, client: ClientDomain) -> Fallible<()> {
let domain: Arc<dyn Domain> = 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
-2
View File
@@ -157,7 +157,6 @@ impl Mux {
self.domains.borrow().get(&id).cloned()
}
#[allow(dead_code)]
pub fn add_domain(&self, domain: &Arc<dyn Domain>) {
self.domains
.borrow_mut()
@@ -259,7 +258,6 @@ impl Mux {
self.tabs.borrow().is_empty()
}
#[allow(dead_code)]
pub fn iter_tabs(&self) -> Vec<Rc<dyn Tab>> {
self.tabs
.borrow()
+66 -15
View File
@@ -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<ReaderMessage>,
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::<ClientDomain>()
.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::<ClientTab>().unwrap();
.get_tab(local_tab_id)
.ok_or_else(|| format_err!("no such tab {}", local_tab_id))?;
let client_tab = tab.downcast_ref::<ClientTab>().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<dyn ReadAndWrite>,
local_domain_id: DomainId,
rx: PollableReceiver<ReaderMessage>,
) -> 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<dyn ReadAndWrite>) -> Self {
pub fn new(local_domain_id: DomainId, stream: Box<dyn ReadAndWrite>) -> 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<Config>) -> Fallible<Self> {
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<Config>, unix_dom: &UnixDomain) -> Fallible<Self> {
pub fn new_unix_domain(
local_domain_id: DomainId,
_config: &Arc<Config>,
unix_dom: &UnixDomain,
) -> Fallible<Self> {
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<Config>, tls_client: &TlsDomainClient) -> Fallible<Self> {
pub fn new_tls(
local_domain_id: DomainId,
_config: &Arc<Config>,
tls_client: &TlsDomainClient,
) -> Fallible<Self> {
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<Config>, tls_client: &TlsDomainClient) -> Fallible<Self> {
pub fn new_tls(
local_domain_id: DomainId,
_config: &Arc<Config>,
tls_client: &TlsDomainClient,
) -> Fallible<Self> {
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<Pdu> {
+26 -3
View File
@@ -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<HashMap<WindowId, WindowId>>,
remote_to_local_tab: Mutex<HashMap<TabId, TabId>>,
}
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<TabId> {
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::<ClientTab>() {
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 {
+4
View File
@@ -214,6 +214,10 @@ impl ClientTab {
};
Ok(())
}
pub fn remote_tab_id(&self) -> TabId {
self.remote_tab_id
}
}
impl Tab for ClientTab {