diff options
author | Aaron Hill <aa1ronham@gmail.com> | 2018-10-13 20:16:47 -0400 |
---|---|---|
committer | Aaron Hill <aa1ronham@gmail.com> | 2018-10-17 21:19:08 -0400 |
commit | eb7698ad14432d9df8dab7183869b9802c77f172 (patch) | |
tree | ce7bb2766747707130e45b2c02834ffa5151221d | |
parent | ceea631423c9faeee3cb6ef23478cc28a14250bc (diff) | |
download | roughenough-eb7698ad14432d9df8dab7183869b9802c77f172.zip |
Move config and public key into server
-rw-r--r-- | src/bin/roughenough-server.rs | 44 | ||||
-rw-r--r-- | src/server.rs | 34 |
2 files changed, 45 insertions, 33 deletions
diff --git a/src/bin/roughenough-server.rs b/src/bin/roughenough-server.rs index d00709d..cf68126 100644 --- a/src/bin/roughenough-server.rs +++ b/src/bin/roughenough-server.rs @@ -63,11 +63,13 @@ use roughenough::{Error, RtMessage, Tag}; use roughenough::{MIN_REQUEST_LENGTH, VERSION}; use roughenough::server::Server; + + macro_rules! check_ctrlc { ($keep_running:expr) => { if !$keep_running.load(Ordering::Acquire) { warn!("Ctrl-C caught, exiting..."); - return true; + return; } } } @@ -76,9 +78,7 @@ macro_rules! check_ctrlc { - - -fn polling_loop(config: Box<ServerConfig>, online_key: OnlineKey, cert_bytes: Vec<u8>) -> bool { +fn polling_loop(config: Box<ServerConfig>) { /* let response_counter = AtomicUsize::new(0); let keep_running = Arc::new(AtomicBool::new(true)); let kr = keep_running.clone(); @@ -107,7 +107,15 @@ fn polling_loop(config: Box<ServerConfig>, online_key: OnlineKey, cert_bytes: Ve let mut requests = Vec::with_capacity(config.batch_size() as usize);*/ - let mut server = Server::new(config, online_key, cert_bytes); + let mut server = Server::new(config); + + info!("Long-term public key : {}", server.get_public_key()); + info!("Online public key : {}", server.get_online_key()); + info!("Max response batch size : {}", server.get_config().batch_size()); + info!("Status updates every : {} seconds", server.get_config().status_interval().as_secs()); + info!("Server listening on : {}:{}", server.get_config().interface(), server.get_config().port()); + + let kr = server.get_keep_running(); let kr_new = kr.clone(); @@ -118,7 +126,7 @@ fn polling_loop(config: Box<ServerConfig>, online_key: OnlineKey, cert_bytes: Ve loop { check_ctrlc!(kr_new); if server.process_events() { - return true; + return; } } @@ -157,30 +165,8 @@ pub fn main() { Ok(cfg) => cfg, }; - let mut online_key = OnlineKey::new(); - let public_key: String; - - let cert_bytes = { - let seed = match kms::load_seed(&config) { - Ok(seed) => seed, - Err(e) => { - error!("Failed to load seed: {:#?}", e); - process::exit(1); - } - }; - let mut long_term_key = LongTermKey::new(&seed); - public_key = hex::encode(long_term_key.public_key()); - - long_term_key.make_cert(&online_key).encode().unwrap() - }; - - info!("Long-term public key : {}", public_key); - info!("Online public key : {}", online_key); - info!("Max response batch size : {}", config.batch_size()); - info!("Status updates every : {} seconds", config.status_interval().as_secs()); - info!("Server listening on : {}:{}", config.interface(), config.port()); - polling_loop(config, online_key, cert_bytes); + polling_loop(config); info!("Done."); process::exit(0); diff --git a/src/server.rs b/src/server.rs index ff302d9..5f4d780 100644 --- a/src/server.rs +++ b/src/server.rs @@ -94,11 +94,23 @@ pub struct Server { requests: Vec<(Vec<u8>, SocketAddr)>, buf: [u8; 65_536], - + public_key: String, } impl Server { - pub fn new(config: Box<ServerConfig>, online_key: OnlineKey, cert_bytes: Vec<u8>) -> Server { + pub fn new(config: Box<ServerConfig>) -> Server { + + let mut online_key = OnlineKey::new(); + let public_key: String; + + let cert_bytes = { + let seed = kms::load_seed(&config).unwrap(); + let mut long_term_key = LongTermKey::new(&seed); + public_key = hex::encode(long_term_key.public_key()); + + long_term_key.make_cert(&online_key).encode().unwrap() + }; + let response_counter = AtomicUsize::new(0); let keep_running = Arc::new(AtomicBool::new(true)); @@ -134,7 +146,9 @@ impl Server { events: Events::with_capacity(32), merkle, requests, - buf: [0u8; 65_536] + buf: [0u8; 65_536], + + public_key } } @@ -239,7 +253,19 @@ impl Server { false } - fn send_to_self(&mut self, data: &[u8]) { + pub fn send_to_self(&mut self, data: &[u8]) { self.socket.send_to(data, &self.socket.local_addr().unwrap()); } + + pub fn get_public_key(&self) -> &str { + return &self.public_key + } + + pub fn get_online_key(&self) -> &OnlineKey { + return &self.online_key + } + + pub fn get_config(&self) -> &Box<ServerConfig> { + return &self.config + } } |