mirror of
https://github.com/ntex-rs/ntex.git
synced 2025-04-04 13:27:39 +03:00
impl rustls acceptor; update filter trait
This commit is contained in:
parent
d7083c15d8
commit
1af728eb01
30 changed files with 1184 additions and 306 deletions
72
ntex-tls/examples/rustls-server.rs
Normal file
72
ntex-tls/examples/rustls-server.rs
Normal file
|
@ -0,0 +1,72 @@
|
|||
use std::{fs::File, io, io::BufReader, sync::Arc};
|
||||
|
||||
use ntex::service::{fn_service, pipeline_factory};
|
||||
use ntex::{codec, io::filter_factory, io::Io, server, util::Either};
|
||||
use ntex_tls::rustls::TlsAcceptor;
|
||||
use rustls_pemfile::{certs, rsa_private_keys};
|
||||
use tls_rust::{Certificate, PrivateKey, ServerConfig};
|
||||
// use tls_openssl::ssl::{self, SslFiletype, SslMethod};
|
||||
|
||||
#[ntex::main]
|
||||
async fn main() -> io::Result<()> {
|
||||
std::env::set_var("RUST_LOG", "trace");
|
||||
env_logger::init();
|
||||
|
||||
println!("Started openssl echp server: 127.0.0.1:8443");
|
||||
|
||||
// load ssl keys
|
||||
let cert_file =
|
||||
&mut BufReader::new(File::open("../ntex-tls/examples/cert.pem").unwrap());
|
||||
let key_file =
|
||||
&mut BufReader::new(File::open("../ntex-tls/examples/key.pem").unwrap());
|
||||
let keys = PrivateKey(rsa_private_keys(key_file).unwrap().remove(0));
|
||||
let cert_chain = certs(cert_file)
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|c| Certificate(c.to_vec()))
|
||||
.collect();
|
||||
let tls_config = Arc::new(
|
||||
ServerConfig::builder()
|
||||
.with_safe_defaults()
|
||||
.with_no_client_auth()
|
||||
.with_single_cert(cert_chain, keys)
|
||||
.unwrap(),
|
||||
);
|
||||
|
||||
// start server
|
||||
server::ServerBuilder::new()
|
||||
.bind("basic", "127.0.0.1:8443", move || {
|
||||
pipeline_factory(filter_factory(TlsAcceptor::new(tls_config.clone())))
|
||||
.and_then(fn_service(|io: Io<_>| async move {
|
||||
println!("New client is connected");
|
||||
|
||||
io.send(
|
||||
ntex_bytes::Bytes::from_static(b"Welcome!\n"),
|
||||
&codec::BytesCodec,
|
||||
)
|
||||
.await
|
||||
.map_err(Either::into_inner)?;
|
||||
|
||||
loop {
|
||||
match io.next(&codec::BytesCodec).await {
|
||||
Ok(Some(msg)) => {
|
||||
println!("Got message: {:?}", msg);
|
||||
io.send(msg.freeze(), &codec::BytesCodec)
|
||||
.await
|
||||
.map_err(Either::into_inner)?;
|
||||
}
|
||||
Ok(None) => break,
|
||||
Err(e) => {
|
||||
println!("Got error: {:?}", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("Client is disconnected");
|
||||
Ok(())
|
||||
}))
|
||||
})?
|
||||
.workers(1)
|
||||
.run()
|
||||
.await
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue