Make timeouts configurable

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/1/head
trivernis 4 years ago
parent 392355e151
commit 984c1308f9
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -1,7 +1,7 @@
[package] [package]
name = "vented" name = "vented"
description = "Event driven encrypted tcp communicaton" description = "Event driven encrypted tcp communicaton"
version = "0.10.4" version = "0.10.5"
authors = ["trivernis <trivernis@protonmail.com>"] authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018" edition = "2018"
readme = "README.md" readme = "README.md"

@ -8,7 +8,7 @@ use x25519_dalek::PublicKey;
use crate::event_handler::EventHandler; use crate::event_handler::EventHandler;
use crate::stream::cryptostream::CryptoStream; use crate::stream::cryptostream::CryptoStream;
use crate::stream::manager::ConcurrentStreamManager; use crate::stream::manager::{ConcurrentStreamManager, CONNECTION_TIMEOUT_SECONDS};
use crate::utils::result::VentedError; use crate::utils::result::VentedError;
use crate::utils::sync::AsyncValue; use crate::utils::sync::AsyncValue;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
@ -46,6 +46,22 @@ pub(crate) struct ServerConnectionContext {
pub recv_pool: Arc<Mutex<ScheduledThreadPool>>, pub recv_pool: Arc<Mutex<ScheduledThreadPool>>,
pub redirect_handles: Arc<Mutex<HashMap<[u8; 16], AsyncValue<(), VentedError>>>>, pub redirect_handles: Arc<Mutex<HashMap<[u8; 16], AsyncValue<(), VentedError>>>>,
pub manager: ConcurrentStreamManager, pub manager: ConcurrentStreamManager,
pub timeouts: ServerTimeouts,
}
#[derive(Clone, Debug)]
pub struct ServerTimeouts {
pub send_timeout: Duration,
pub redirect_timeout: Duration,
}
impl Default for ServerTimeouts {
fn default() -> Self {
Self {
send_timeout: Duration::from_secs(CONNECTION_TIMEOUT_SECONDS),
redirect_timeout: Duration::from_secs(CONNECTION_TIMEOUT_SECONDS * 2),
}
}
} }
impl From<Node> for NodeData { impl From<Node> for NodeData {

@ -5,7 +5,7 @@ use std::mem;
use std::net::{TcpListener, TcpStream}; use std::net::{TcpListener, TcpStream};
use std::sync::Arc; use std::sync::Arc;
use std::thread; use std::thread;
use std::time::{Duration, Instant}; use std::time::Instant;
use crossbeam_utils::sync::WaitGroup; use crossbeam_utils::sync::WaitGroup;
use crypto_box::{PublicKey, SecretKey}; use crypto_box::{PublicKey, SecretKey};
@ -16,14 +16,14 @@ use x25519_dalek::StaticSecret;
use crate::event::Event; use crate::event::Event;
use crate::event_handler::EventHandler; use crate::event_handler::EventHandler;
use crate::server::data::{Node, NodeData, NodeState, ServerConnectionContext}; use crate::server::data::{Node, NodeData, NodeState, ServerConnectionContext, ServerTimeouts};
use crate::server::server_events::{ use crate::server::server_events::{
AuthPayload, ChallengePayload, NodeInformationPayload, RedirectPayload, VersionMismatchPayload, AuthPayload, ChallengePayload, NodeInformationPayload, RedirectPayload, VersionMismatchPayload,
ACCEPT_EVENT, AUTH_EVENT, CHALLENGE_EVENT, CONNECT_EVENT, MISMATCH_EVENT, READY_EVENT, ACCEPT_EVENT, AUTH_EVENT, CHALLENGE_EVENT, CONNECT_EVENT, MISMATCH_EVENT, READY_EVENT,
REDIRECT_EVENT, REJECT_EVENT, REDIRECT_EVENT, REJECT_EVENT,
}; };
use crate::stream::cryptostream::CryptoStream; use crate::stream::cryptostream::CryptoStream;
use crate::stream::manager::{ConcurrentStreamManager, CONNECTION_TIMEOUT_SECONDS}; use crate::stream::manager::ConcurrentStreamManager;
use crate::utils::result::{VentedError, VentedResult}; use crate::utils::result::{VentedError, VentedResult};
use crate::utils::sync::AsyncValue; use crate::utils::sync::AsyncValue;
use std::cmp::max; use std::cmp::max;
@ -40,7 +40,7 @@ type ForwardFutureVector = Arc<Mutex<HashMap<(String, String), AsyncValue<Crypto
/// Usage: /// Usage:
/// ```rust /// ```rust
/// use vented::server::VentedServer; /// use vented::server::VentedServer;
/// use vented::server::data::Node; /// use vented::server::data::{Node, ServerTimeouts};
/// use vented::stream::SecretKey; /// use vented::stream::SecretKey;
/// use rand::thread_rng; /// use rand::thread_rng;
/// use vented::event::Event; /// use vented::event::Event;
@ -57,7 +57,7 @@ type ForwardFutureVector = Arc<Mutex<HashMap<(String, String), AsyncValue<Crypto
/// // in a real world example the secret key needs to be loaded from somewhere because connections /// // in a real world example the secret key needs to be loaded from somewhere because connections
/// // with unknown keys are not accepted. /// // with unknown keys are not accepted.
/// let global_secret = SecretKey::generate(&mut thread_rng()); /// let global_secret = SecretKey::generate(&mut thread_rng());
/// let mut server = VentedServer::new("A".to_string(), global_secret, nodes.clone(), 4, 100); /// let mut server = VentedServer::new("A".to_string(), global_secret, nodes.clone(), ServerTimeouts::default(), 4, 100);
/// ///
/// ///
/// server.listen("localhost:20000".to_string()); /// server.listen("localhost:20000".to_string());
@ -78,6 +78,7 @@ pub struct VentedServer {
manager: ConcurrentStreamManager, manager: ConcurrentStreamManager,
sender_pool: Arc<Mutex<ScheduledThreadPool>>, sender_pool: Arc<Mutex<ScheduledThreadPool>>,
receiver_pool: Arc<Mutex<ScheduledThreadPool>>, receiver_pool: Arc<Mutex<ScheduledThreadPool>>,
timeouts: ServerTimeouts,
} }
impl VentedServer { impl VentedServer {
@ -88,6 +89,7 @@ impl VentedServer {
node_id: String, node_id: String,
secret_key: SecretKey, secret_key: SecretKey,
nodes: Vec<Node>, nodes: Vec<Node>,
timeouts: ServerTimeouts,
num_threads: usize, num_threads: usize,
max_threads: usize, max_threads: usize,
) -> Self { ) -> Self {
@ -112,6 +114,7 @@ impl VentedServer {
num_threads / 2, num_threads / 2,
1, 1,
)))), )))),
timeouts,
}; };
server.register_events(); server.register_events();
server.start_event_listener(); server.start_event_listener();
@ -203,6 +206,7 @@ impl VentedServer {
redirect_handles: Arc::clone(&self.redirect_handles), redirect_handles: Arc::clone(&self.redirect_handles),
manager: self.manager.clone(), manager: self.manager.clone(),
recv_pool: Arc::clone(&self.receiver_pool), recv_pool: Arc::clone(&self.receiver_pool),
timeouts: self.timeouts.clone(),
} }
} }
@ -347,7 +351,7 @@ impl VentedServer {
} }
if let Some(Ok(_)) = if let Some(Ok(_)) =
future.get_value_with_timeout(Duration::from_secs(CONNECTION_TIMEOUT_SECONDS)) future.get_value_with_timeout(context.timeouts.redirect_timeout.clone())
{ {
return Ok(()); return Ok(());
} else { } else {
@ -362,7 +366,7 @@ impl VentedServer {
/// then establishing an encrypted connection /// then establishing an encrypted connection
fn handle_connection(context: ServerConnectionContext, stream: TcpStream) -> VentedResult<()> { fn handle_connection(context: ServerConnectionContext, stream: TcpStream) -> VentedResult<()> {
let event_handler = Arc::clone(&context.event_handler); let event_handler = Arc::clone(&context.event_handler);
stream.set_write_timeout(Some(Duration::from_secs(CONNECTION_TIMEOUT_SECONDS)))?; stream.set_write_timeout(Some(context.timeouts.send_timeout))?;
log::trace!( log::trace!(
"Received connection from {}", "Received connection from {}",
stream.peer_addr().expect("Failed to get peer address") stream.peer_addr().expect("Failed to get peer address")
@ -451,7 +455,7 @@ impl VentedServer {
address: String, address: String,
) -> VentedResult<CryptoStream> { ) -> VentedResult<CryptoStream> {
let stream = TcpStream::connect(address)?; let stream = TcpStream::connect(address)?;
stream.set_write_timeout(Some(Duration::from_secs(CONNECTION_TIMEOUT_SECONDS)))?; stream.set_write_timeout(Some(context.timeouts.send_timeout))?;
context.is_server = false; context.is_server = false;
let stream = Self::get_crypto_stream(context, stream)?; let stream = Self::get_crypto_stream(context, stream)?;

@ -4,7 +4,7 @@ use std::sync::Arc;
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
use vented::event::Event; use vented::event::Event;
use vented::server::data::Node; use vented::server::data::{Node, ServerTimeouts};
use vented::server::server_events::NODE_LIST_REQUEST_EVENT; use vented::server::server_events::NODE_LIST_REQUEST_EVENT;
use vented::server::VentedServer; use vented::server::VentedServer;
@ -51,9 +51,30 @@ fn test_server_communication() {
trusted: false, trusted: false,
}) })
} }
let mut server_a = VentedServer::new("A".to_string(), global_secret_a, nodes_a, 20, 100); let mut server_a = VentedServer::new(
let mut server_b = VentedServer::new("B".to_string(), global_secret_b, nodes.clone(), 3, 100); "A".to_string(),
let server_c = VentedServer::new("C".to_string(), global_secret_c, nodes, 3, 100); global_secret_a,
nodes_a,
ServerTimeouts::default(),
20,
100,
);
let mut server_b = VentedServer::new(
"B".to_string(),
global_secret_b,
nodes.clone(),
ServerTimeouts::default(),
3,
100,
);
let server_c = VentedServer::new(
"C".to_string(),
global_secret_c,
nodes,
ServerTimeouts::default(),
3,
100,
);
let wg = server_a.listen("localhost:22222".to_string()); let wg = server_a.listen("localhost:22222".to_string());
wg.wait(); wg.wait();

Loading…
Cancel
Save