From 712089a5dec7b9cb7885214e3965539d7fe9fc52 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 22 Jun 2019 08:21:25 -0700 Subject: [PATCH] refactor unix domain configuration It is now possible to specify multiple domains with varying configuration. --- src/config.rs | 41 ++++++++++++++++++++++++++++++++--------- src/main.rs | 4 ++-- src/server/client.rs | 18 ++++++++++-------- src/server/listener.rs | 24 ++++++++++-------------- 4 files changed, 54 insertions(+), 33 deletions(-) diff --git a/src/config.rs b/src/config.rs index 04df9af16..da703233a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -78,9 +78,9 @@ pub struct Config { #[serde(default)] pub pty: PtySystemSelection, - /// When using the MuxServer, this specifies the path to the unix - /// domain socket to use to communicate with the mux server. - pub mux_server_unix_domain_socket_path: Option, + /// The set of unix domains + #[serde(default = "UnixDomain::default_unix_domains")] + pub unix_domains: Vec, /// When using the MuxServer with the NetListener, specifies /// the address and port combination on which it should listen @@ -403,6 +403,34 @@ fn default_dpi() -> f64 { 96.0 } +/// Configures an instance of a multiplexer that can be communicated +/// with via a unix domain socket +#[derive(Default, Debug, Clone, Deserialize)] +pub struct UnixDomain { + /// The path to the socket. If unspecified, a resonable default + /// value will be computed. + pub socket_path: Option, + + /// If true, connect to this domain automatically at startup + pub connect_automatically: bool, +} + +impl UnixDomain { + pub fn socket_path(&self) -> PathBuf { + self.socket_path + .as_ref() + .cloned() + .unwrap_or_else(|| RUNTIME_DIR.join("sock")) + } + + fn default_unix_domains() -> Vec { + vec![UnixDomain { + socket_path: None, + connect_automatically: false, + }] + } +} + impl Default for Config { fn default() -> Self { Self { @@ -418,7 +446,6 @@ impl Default for Config { hyperlink_rules: default_hyperlink_rules(), term: default_term(), default_prog: None, - mux_server_unix_domain_socket_path: None, mux_server_bind_address: None, mux_server_pem_private_key: None, mux_server_pem_cert: None, @@ -433,6 +460,7 @@ impl Default for Config { ratelimit_mux_output_pushes_per_second: None, ratelimit_mux_output_scans_per_second: None, mux_pem_root_certs: None, + unix_domains: UnixDomain::default_unix_domains(), keys: vec![], } } @@ -687,11 +715,6 @@ impl Config { fn compute_extra_defaults(&self) -> Self { let mut cfg = self.clone(); - if cfg.mux_server_unix_domain_socket_path.is_none() { - cfg.mux_server_unix_domain_socket_path = - RUNTIME_DIR.join("sock").to_str().map(str::to_owned); - } - if cfg.font_rules.is_empty() { // Expand out some reasonable default font rules let bold = self.font.make_bold(); diff --git a/src/main.rs b/src/main.rs index c291edf2a..c7d1daff1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -147,7 +147,7 @@ fn run_terminal_gui(config: Arc, opts: &StartCommand) -> Result< }; let domain: Arc = if opts.mux_client_as_default_domain { - let client = Client::new_unix_domain(&config)?; + let client = Client::new_default_unix_domain(&config)?; Arc::new(ClientDomain::new(client)) } else if opts.mux_tls_client_as_default_domain { let client = Client::new_tls(&config)?; @@ -211,7 +211,7 @@ fn main() -> Result<(), Error> { run_terminal_gui(config, &start) } SubCommand::Cli(cli) => { - let client = Client::new_unix_domain(&config)?; + let client = Client::new_default_unix_domain(&config)?; match cli.sub { CliSubCommand::List => { let cols = vec![ diff --git a/src/server/client.rs b/src/server/client.rs index 33b1c27e6..44b69829c 100644 --- a/src/server/client.rs +++ b/src/server/client.rs @@ -1,5 +1,5 @@ #![allow(dead_code)] -use crate::config::Config; +use crate::config::{Config, UnixDomain}; use crate::frontend::gui_executor; use crate::mux::Mux; use crate::server::codec::*; @@ -144,13 +144,15 @@ impl Client { Self { sender } } - pub fn new_unix_domain(config: &Arc) -> Fallible { - let sock_path = Path::new( - config - .mux_server_unix_domain_socket_path - .as_ref() - .ok_or_else(|| err_msg("no mux_server_unix_domain_socket_path"))?, - ); + pub fn new_default_unix_domain(config: &Arc) -> Fallible { + for unix_dom in &config.unix_domains { + return Self::new_unix_domain(config, unix_dom); + } + bail!("no default unix domain is configured"); + } + + pub fn new_unix_domain(_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)) diff --git a/src/server/listener.rs b/src/server/listener.rs index 112f0533f..bcc16056d 100644 --- a/src/server/listener.rs +++ b/src/server/listener.rs @@ -854,9 +854,7 @@ impl Drop for UmaskSaver { /// we need to be sure that the directory that we create it in /// is owned by the user and has appropriate file permissions /// that prevent other users from manipulating its contents. -fn safely_create_sock_path(sock_path: &str) -> Result { - let sock_path = Path::new(sock_path); - +fn safely_create_sock_path(sock_path: &Path) -> Result { debug!("setting up {}", sock_path.display()); let _saver = UmaskSaver::new(); @@ -939,17 +937,15 @@ fn spawn_tls_listener(config: &Arc, executor: Box) -> Fall } pub fn spawn_listener(config: &Arc, executor: Box) -> Fallible<()> { - let sock_path = config - .mux_server_unix_domain_socket_path - .as_ref() - .ok_or_else(|| err_msg("no mux_server_unix_domain_socket_path"))?; - let mut listener = LocalListener::new( - safely_create_sock_path(sock_path)?, - executor.clone_executor(), - ); - thread::spawn(move || { - listener.run(); - }); + for unix_dom in &config.unix_domains { + let mut listener = LocalListener::new( + safely_create_sock_path(&unix_dom.socket_path())?, + executor.clone_executor(), + ); + thread::spawn(move || { + listener.run(); + }); + } spawn_tls_listener(config, executor.clone_executor())?; Ok(())