diff options
author | Stuart Stock <stuart@int08h.com> | 2018-09-30 20:50:24 -0500 |
---|---|---|
committer | Stuart Stock <stuart@int08h.com> | 2018-09-30 20:50:24 -0500 |
commit | a1af24e764250cc4abda5c1c5670ca243ee4ad7f (patch) | |
tree | eac04856bf24ce7748ae5e785eea98afca16ed11 | |
parent | d1a76e00fc92d1550e748fd1b393eaf0f10f7112 (diff) | |
download | roughenough-a1af24e764250cc4abda5c1c5670ca243ee4ad7f.zip |
Work-in-progress; still working to clean up config
-rw-r--r-- | src/bin/server.rs | 34 | ||||
-rw-r--r-- | src/config/environment.rs | 4 | ||||
-rw-r--r-- | src/config/file.rs | 2 | ||||
-rw-r--r-- | src/config/mod.rs | 36 | ||||
-rw-r--r-- | src/lib.rs | 4 |
5 files changed, 69 insertions, 11 deletions
diff --git a/src/bin/server.rs b/src/bin/server.rs index 9b2b01a..4e6ec45 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -53,6 +53,7 @@ use mio_extras::timer::Timer; use byteorder::{LittleEndian, WriteBytesExt}; +use roughenough::config; use roughenough::config::{EnvironmentConfig, FileConfig, ServerConfig}; use roughenough::keys::{LongTermKey, OnlineKey}; use roughenough::merkle::MerkleTree; @@ -102,8 +103,9 @@ fn nonce_from_request(buf: &[u8], num_bytes: usize) -> Result<&[u8], Error> { } } + fn polling_loop( - config: Box<ServerConfig>, + config: &Box<ServerConfig>, online_key: &mut OnlineKey, cert_bytes: &[u8], ) { @@ -254,13 +256,31 @@ pub fn main() { process::exit(1); } - let arg1 = args.nth(1).unwrap(); - - let config: Box<ServerConfig> = match arg1.as_ref() { - "ENV" => Box::new(EnvironmentConfig::new()), - _ => Box::new(FileConfig::from_file(&arg1).unwrap()), + let config: Box<ServerConfig> = { + let arg1 = args.nth(1).unwrap(); + if &arg1 == "ENV" { + match EnvironmentConfig::new() { + Ok(cfg) => Box::new(cfg), + Err(e) => { + error!("{:?}", e); + process::exit(1) + } + } + } else { + match FileConfig::new(&arg1) { + Ok(cfg) => Box::new(cfg), + Err(e) => { + error!("{:?}", e); + process::exit(1) + } + } + } }; + if !config::is_valid_config(&config) { + process::exit(2); + } + let mut online_key = OnlineKey::new(); let mut long_term_key = LongTermKey::new(config.seed()); let cert_bytes = long_term_key.make_cert(&online_key).encode().unwrap(); @@ -271,7 +291,7 @@ pub fn main() { info!("Status updates every : {} seconds", config.status_interval().as_secs()); info!("Server listening on : {}:{}", config.interface(), config.port()); - polling_loop(config, &mut online_key, &cert_bytes); + polling_loop(&config, &mut online_key, &cert_bytes); info!("Done."); process::exit(0); diff --git a/src/config/environment.rs b/src/config/environment.rs index c4d2c06..7ebe588 100644 --- a/src/config/environment.rs +++ b/src/config/environment.rs @@ -49,7 +49,7 @@ const ROUGHENOUGH_BATCH_SIZE: &str = "ROUGHENOUGH_BATCH_SIZE"; const ROUGHENOUGH_STATUS_INTERVAL: &str = "ROUGHENOUGH_STATUS_INTERVAL"; impl EnvironmentConfig { - pub fn new() -> Self { + pub fn new() -> Result<Self, Error> { let mut cfg = EnvironmentConfig { port: 0, interface: "".to_string(), @@ -91,7 +91,7 @@ impl EnvironmentConfig { cfg.status_interval = Duration::from_secs(val as u64); }; - cfg + Ok(cfg) } } diff --git a/src/config/file.rs b/src/config/file.rs index 24011bb..8aa8ca2 100644 --- a/src/config/file.rs +++ b/src/config/file.rs @@ -45,7 +45,7 @@ pub struct FileConfig { } impl FileConfig { - pub fn from_file(config_file: &str) -> Result<Self, Error> { + pub fn new(config_file: &str) -> Result<Self, Error> { let mut infile = File::open(config_file).expect("failed to open config file"); let mut contents = String::new(); diff --git a/src/config/mod.rs b/src/config/mod.rs index 0cd8742..02ab941 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -22,6 +22,7 @@ //! extern crate hex; +extern crate log; use std::net::SocketAddr; use std::time::Duration; @@ -88,3 +89,38 @@ pub trait ServerConfig { /// Convenience function to create a `SocketAddr` from the provided `interface` and `port` fn socket_addr(&self) -> Result<SocketAddr, Error>; } + +pub fn is_valid_config(cfg: &Box<ServerConfig>) -> bool { + let mut is_valid = true; + + if cfg.port() == 0 { + error!("unset port: {}", cfg.port()); + is_valid = false; + } + if cfg.interface().is_empty() { + error!("interface is missing"); + is_valid = false; + } + match cfg.socket_addr() { + Err(e) => { + error!("{}:{}: {:?}", cfg.interface(), cfg.port(), e); + is_valid = false; + } + _ => () + } + if cfg.seed().is_empty() { + error!("seed value is missing"); + is_valid = false; + } + if !cfg.seed().is_empty() && cfg.seed().len() != 32 { + error!("seed value must be 32 characters long"); + is_valid = false; + } + if cfg.batch_size() < 1 || cfg.batch_size() > 64 { + error!("batch_size {} is invalid; valid range 1-64", cfg.batch_size()); + is_valid = false; + } + + is_valid +} + @@ -53,12 +53,14 @@ extern crate core; extern crate time; extern crate yaml_rust; +#[macro_use] +extern crate log; + mod error; mod message; mod tag; pub mod config; - pub mod keys; pub mod merkle; pub mod sign; |