From 83a1948dfd82772c11650088e6c49e993859ab64 Mon Sep 17 00:00:00 2001 From: fiag Date: Thu, 20 May 2021 01:19:53 +0800 Subject: [PATCH 1/3] Add TCP_NODELAY and TTL options. --- src/tls_listener.rs | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/tls_listener.rs b/src/tls_listener.rs index 888e753..2a23591 100644 --- a/src/tls_listener.rs +++ b/src/tls_listener.rs @@ -27,6 +27,8 @@ pub struct TlsListener { connection: TcpConnection, config: TlsListenerConfig, server: Option>, + tcp_nodelay: Option, + tcp_ttl: Option, } impl Debug for TlsListener { @@ -52,6 +54,8 @@ impl TlsListener { connection, config, server: None, + tcp_nodelay: None, + tcp_ttl: None, } } /// The primary entrypoint to create a TlsListener. See @@ -125,6 +129,32 @@ impl TlsListener { } Ok(()) } + + /// Set TCP_NODELAY socket option. + pub fn set_nodelay(&mut self, nodelay: bool) { + self.tcp_nodelay = Some(nodelay); + } + + /// Get TCP_NODELAY socket option. + pub fn nodelay(&self) -> Option { + self.tcp_nodelay + } + + /// Set TCP_NODELAY socket option. + pub fn with_nodelay(mut self, nodelay: bool) -> Self { + self.set_nodelay(nodelay); + self + } + + /// Set TTL option. + pub fn set_ttl(&mut self, ttl: u32) { + self.tcp_ttl = Some(ttl); + } + + /// Get TTL option. + pub fn ttl(&self) -> Option { + self.tcp_ttl + } } fn handle_tls( @@ -203,7 +233,17 @@ impl Listener for TlsListener handle_tls(server.clone(), stream, acceptor.clone()), + Ok(stream) => { + if let Some(nodelay) = self.tcp_nodelay { + stream.set_nodelay(nodelay)?; + } + + if let Some(ttl) = self.tcp_ttl { + stream.set_ttl(ttl)?; + } + + handle_tls(server.clone(), stream, acceptor.clone()) + }, }; } Ok(()) From ae009b5d894dfa44d2aa26c089b7f7aa3f19fc51 Mon Sep 17 00:00:00 2001 From: fiag Date: Mon, 24 May 2021 11:59:37 +0800 Subject: [PATCH 2/3] Move TCP_NODELAY and TTL options to TlsListenerBuilder. --- src/tls_listener.rs | 34 ++++------------------------------ src/tls_listener_builder.rs | 20 +++++++++++++++++++- 2 files changed, 23 insertions(+), 31 deletions(-) diff --git a/src/tls_listener.rs b/src/tls_listener.rs index 2a23591..2cb8f26 100644 --- a/src/tls_listener.rs +++ b/src/tls_listener.rs @@ -49,13 +49,13 @@ impl Debug for TlsListener { } impl TlsListener { - pub(crate) fn new(connection: TcpConnection, config: TlsListenerConfig) -> Self { + pub(crate) fn new(connection: TcpConnection, config: TlsListenerConfig, tcp_nodelay: Option, tcp_ttl: Option) -> Self { Self { connection, config, server: None, - tcp_nodelay: None, - tcp_ttl: None, + tcp_nodelay, + tcp_ttl, } } /// The primary entrypoint to create a TlsListener. See @@ -129,32 +129,6 @@ impl TlsListener { } Ok(()) } - - /// Set TCP_NODELAY socket option. - pub fn set_nodelay(&mut self, nodelay: bool) { - self.tcp_nodelay = Some(nodelay); - } - - /// Get TCP_NODELAY socket option. - pub fn nodelay(&self) -> Option { - self.tcp_nodelay - } - - /// Set TCP_NODELAY socket option. - pub fn with_nodelay(mut self, nodelay: bool) -> Self { - self.set_nodelay(nodelay); - self - } - - /// Set TTL option. - pub fn set_ttl(&mut self, ttl: u32) { - self.tcp_ttl = Some(ttl); - } - - /// Get TTL option. - pub fn ttl(&self) -> Option { - self.tcp_ttl - } } fn handle_tls( @@ -243,7 +217,7 @@ impl Listener for TlsListener { tls_acceptor: Option>, tcp: Option, addrs: Option>, + tcp_nodelay: Option, + tcp_ttl: Option, _state: PhantomData, } @@ -54,6 +56,8 @@ impl Default for TlsListenerBuilder { tls_acceptor: None, tcp: None, addrs: None, + tcp_nodelay: None, + tcp_ttl: None, _state: PhantomData, } } @@ -148,6 +152,18 @@ impl TlsListenerBuilder { self } + /// Provides a TCP_NODELAY option for this tls listener. + pub fn nodelay(mut self, nodelay: bool) -> Self { + self.tcp_nodelay = Some(nodelay); + self + } + + /// Provides a TTL option for this tls listener. + pub fn ttl(mut self, ttl: u32) -> Self { + self.tcp_ttl = Some(ttl); + self + } + /// finishes building a TlsListener from this TlsListenerBuilder. /// /// # Errors @@ -168,6 +184,8 @@ impl TlsListenerBuilder { tls_acceptor, tcp, addrs, + tcp_nodelay, + tcp_ttl, .. } = self; @@ -194,6 +212,6 @@ impl TlsListenerBuilder { } }; - Ok(TlsListener::new(connection, config)) + Ok(TlsListener::new(connection, config, tcp_nodelay, tcp_ttl)) } } From bdfa6488d2facb052552956a371f3076f92f4e2b Mon Sep 17 00:00:00 2001 From: Jacob Rothstein Date: Mon, 24 May 2021 17:27:38 -0700 Subject: [PATCH 3/3] add tcp_{nodelay,ttl} to Debug, example, tcp_ prefix --- src/lib.rs | 7 +++---- src/tls_listener.rs | 9 ++++++++- src/tls_listener_builder.rs | 20 +++++++++++++++++--- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 64649e8..6c7907c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,10 @@ -//! tide tls listener built on async-tls and rustls +//! tide tls listener built on async-rustls and rustls //! //! //! # Example //! ```rust //! # use tide_rustls::TlsListener; -//! fn main() -> tide::Result<()> { async_std::task::block_on(async { +//! # fn main() -> tide::Result<()> { async_std::task::block_on(async { //! let mut app = tide::new(); //! app.at("/").get(|_| async { Ok("Hello tls") }); //! # if false { @@ -15,8 +15,7 @@ //! .key(std::env::var("TIDE_KEY_PATH").unwrap()), //! ) //! .await?; -//! # } -//! # Ok(()) }) } +//! # } Ok(()) }) } //! ``` #![forbid(unsafe_code, future_incompatible)] #![deny( diff --git a/src/tls_listener.rs b/src/tls_listener.rs index 2cb8f26..1fdac5c 100644 --- a/src/tls_listener.rs +++ b/src/tls_listener.rs @@ -44,12 +44,19 @@ impl Debug for TlsListener { &"None" }, ) + .field("tcp_ttl", &self.tcp_ttl) + .field("tcp_nodelay", &self.tcp_nodelay) .finish() } } impl TlsListener { - pub(crate) fn new(connection: TcpConnection, config: TlsListenerConfig, tcp_nodelay: Option, tcp_ttl: Option) -> Self { + pub(crate) fn new( + connection: TcpConnection, + config: TlsListenerConfig, + tcp_nodelay: Option, + tcp_ttl: Option, + ) -> Self { Self { connection, config, diff --git a/src/tls_listener_builder.rs b/src/tls_listener_builder.rs index 0abb26d..373e7e0 100644 --- a/src/tls_listener_builder.rs +++ b/src/tls_listener_builder.rs @@ -35,6 +35,18 @@ use std::sync::Arc; /// .config(rustls::ServerConfig::new(rustls::NoClientAuth::new())) /// .finish(); /// ``` +/// +/// ```rust +/// # use tide_rustls::TlsListener; +/// let listener = TlsListener::<()>::build() +/// .addrs("localhost:4433") +/// .cert("./tls/localhost-4433.cert") +/// .key("./tls/localhost-4433.key") +/// .tcp_ttl(60) +/// .tcp_nodelay(true) +/// .finish(); +/// ``` + pub struct TlsListenerBuilder { key: Option, cert: Option, @@ -86,6 +98,8 @@ impl std::fmt::Debug for TlsListenerBuilder { ) .field("tcp", &self.tcp) .field("addrs", &self.addrs) + .field("tcp_nodelay", &self.tcp_nodelay) + .field("tcp_ttl", &self.tcp_ttl) .finish() } } @@ -153,13 +167,13 @@ impl TlsListenerBuilder { } /// Provides a TCP_NODELAY option for this tls listener. - pub fn nodelay(mut self, nodelay: bool) -> Self { + pub fn tcp_nodelay(mut self, nodelay: bool) -> Self { self.tcp_nodelay = Some(nodelay); self } - /// Provides a TTL option for this tls listener. - pub fn ttl(mut self, ttl: u32) -> Self { + /// Provides a TTL option for this tls listener, in seconds. + pub fn tcp_ttl(mut self, ttl: u32) -> Self { self.tcp_ttl = Some(ttl); self }