Comments (3)
@vasilakisfil This is how I've done it. The key is to not await on the accept() call but instead wrap that future in the tokio timeout.
let tls_config = rustls::ServerConfig::builder()
.with_safe_defaults()
.with_no_client_auth()
.with_single_cert(certs, private_key)
.unwrap();
let tls_acceptor = tokio_rustls::TlsAcceptor::from(tls_config);
// ...
// Inside the acceptor loop after you have a tcp connection already accepted
// ...
let tls_stream = tls_acceptor.accept(tcp_stream).map_err(|e| {
std::io::Error::new(std::io::ErrorKind::Other, format!("Client TLS connect fail: {:?}", e))
});
// Wrap the accept future with a timeout to put an upper limit on how long a client can take to complete
// the TLS handshake and get to a TLS "connected" state
let tls_handshake_timeout_seconds: u8 = 5;
let tls_stream = tokio::time::timeout(tokio::time::Duration::from_secs(tls_handshake_timeout_seconds.into()), tls_stream).map_err(move |_e| {
std::io::Error::new(std::io::ErrorKind::TimedOut, format!("Client TLS handshake timeout after {} seconds", tls_handshake_timeout_seconds))
});
let tls_stream = match tls_stream.await {
// Happy path
Ok(tls_stream) => tls_stream,
// Some kind of error
Err(e) => ...
// Timeout
Ok(Err(e)) => ...
};
from tokio-rustls.
Thanks, yes I have done the same thing. On event-driven architectures (aka channels), not having a timeout limit by default can lead to serious unexpected stalls in an application. Probably it's a good idea to add something on the documentation about it.
from tokio-rustls.
This seems like a serious issue to me, the lack of a TLS timeout means that applications built on tokio-rustls are vulnerable out of the box to a DoS attack to exhaust FDs, like a very basic Slowloris attack but with TLS.
I think by default there should be an aggressive configurable timeout for completion of the TLS handshake.
You can do a DoS on the default rustls server example by just spawning tons of nc <host> <port>
instances that will never, ever timeout.
from tokio-rustls.
Related Issues (20)
- README and examples use unavailable API HOT 7
- Version tags HOT 6
- tokio-rustls write only first IoSlice when use write_vectored HOT 1
- Override `poll_write_vectored`? HOT 5
- Allow non-static lifetime for ServerName? HOT 3
- I can't find OwnedTrustAnchor??????
- Error: tls handshake eof HOT 1
- How to get peer certificates as server? HOT 2
- Error `NotConnected` when serving a `TlsStream<TcpStream>` when using `hyper` HOT 4
- Error: badRecordMac HOT 2
- Sending plaintext response for non-TLS connection attempts HOT 8
- Large percentage of cpu time in memset when reading with a larger buffer HOT 9
- Proposal: Share code between tokio-rustls and futures-rustls HOT 1
- Use Unbuffered API
- 0.5-RTT support
- Remove TCP shutdown
- Secure WebSocket Implementation with tokio-tungstenite | tokio-rustls | rustls-platform-verifier HOT 2
- Detailed Error Messages HOT 1
- Test failure in common::test_stream::stream_handshake_regression_issues_77 HOT 2
- does TLSAcceptor read early data? HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tokio-rustls.