-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move and update server+tls examples (#190)
- Loading branch information
Showing
14 changed files
with
241 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
//! Simple composite-service TCP echo server. | ||
//! | ||
//! Using the following command: | ||
//! | ||
//! ```sh | ||
//! nc 127.0.0.1 8080 | ||
//! ``` | ||
//! | ||
//! Start typing. When you press enter the typed line will be echoed back. The server will log | ||
//! the length of each line it echos and the total size of data sent when the connection is closed. | ||
use std::sync::{ | ||
atomic::{AtomicUsize, Ordering}, | ||
Arc, | ||
}; | ||
use std::{env, io}; | ||
|
||
use actix_rt::net::TcpStream; | ||
use actix_server::Server; | ||
use actix_service::pipeline_factory; | ||
use bytes::BytesMut; | ||
use futures_util::future::ok; | ||
use log::{error, info}; | ||
use tokio::io::{AsyncReadExt, AsyncWriteExt}; | ||
|
||
#[actix_rt::main] | ||
async fn main() -> io::Result<()> { | ||
env::set_var("RUST_LOG", "actix=trace,basic=trace"); | ||
env_logger::init(); | ||
|
||
let count = Arc::new(AtomicUsize::new(0)); | ||
|
||
let addr = ("127.0.0.1", 8080); | ||
info!("starting server on port: {}", &addr.0); | ||
|
||
// Bind socket address and start worker(s). By default, the server uses the number of available | ||
// logical CPU cores as the worker count. For this reason, the closure passed to bind needs | ||
// to return a service *factory*; so it can be created once per worker. | ||
Server::build() | ||
.bind("echo", addr, move || { | ||
let count = Arc::clone(&count); | ||
let num2 = Arc::clone(&count); | ||
|
||
pipeline_factory(move |mut stream: TcpStream| { | ||
let count = Arc::clone(&count); | ||
|
||
async move { | ||
let num = count.fetch_add(1, Ordering::SeqCst); | ||
let num = num + 1; | ||
|
||
let mut size = 0; | ||
let mut buf = BytesMut::new(); | ||
|
||
loop { | ||
match stream.read_buf(&mut buf).await { | ||
// end of stream; bail from loop | ||
Ok(0) => break, | ||
|
||
// more bytes to process | ||
Ok(bytes_read) => { | ||
info!("[{}] read {} bytes", num, bytes_read); | ||
stream.write_all(&buf[size..]).await.unwrap(); | ||
size += bytes_read; | ||
} | ||
|
||
// stream error; bail from loop with error | ||
Err(err) => { | ||
error!("Stream Error: {:?}", err); | ||
return Err(()); | ||
} | ||
} | ||
} | ||
|
||
// send data down service pipeline | ||
Ok((buf.freeze(), size)) | ||
} | ||
}) | ||
.map_err(|err| error!("Service Error: {:?}", err)) | ||
.and_then(move |(_, size)| { | ||
let num = num2.load(Ordering::SeqCst); | ||
info!("[{}] total bytes read: {}", num, size); | ||
ok(size) | ||
}) | ||
})? | ||
.workers(1) | ||
.run() | ||
.await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
//! TLS Acceptor Server | ||
//! | ||
//! Using either HTTPie (`http`) or cURL: | ||
//! | ||
//! This commands will produce errors in the server log: | ||
//! ```sh | ||
//! curl 127.0.0.1:8443 | ||
//! http 127.0.0.1:8443 | ||
//! ``` | ||
//! | ||
//! These commands will show "empty reply" on the client but will debug print the TLS stream info | ||
//! in the server log, indicating a successful TLS handshake: | ||
//! ```sh | ||
//! curl -k https://127.0.0.1:8443 | ||
//! http --verify=false https://127.0.0.1:8443 | ||
//! ``` | ||
use std::{ | ||
env, | ||
fs::File, | ||
io::{self, BufReader}, | ||
sync::{ | ||
atomic::{AtomicUsize, Ordering}, | ||
Arc, | ||
}, | ||
}; | ||
|
||
use actix_server::Server; | ||
use actix_service::pipeline_factory; | ||
use actix_tls::rustls::Acceptor as RustlsAcceptor; | ||
use futures_util::future::ok; | ||
use log::info; | ||
use rust_tls::{ | ||
internal::pemfile::certs, internal::pemfile::rsa_private_keys, NoClientAuth, ServerConfig, | ||
}; | ||
|
||
#[derive(Debug)] | ||
struct ServiceState { | ||
num: Arc<AtomicUsize>, | ||
} | ||
|
||
#[actix_rt::main] | ||
async fn main() -> io::Result<()> { | ||
env::set_var("RUST_LOG", "actix=trace,basic=trace"); | ||
env_logger::init(); | ||
|
||
let mut tls_config = ServerConfig::new(NoClientAuth::new()); | ||
|
||
// Load TLS key and cert files | ||
let cert_file = &mut BufReader::new(File::open("./examples/cert.pem").unwrap()); | ||
let key_file = &mut BufReader::new(File::open("./examples/key.pem").unwrap()); | ||
|
||
let cert_chain = certs(cert_file).unwrap(); | ||
let mut keys = rsa_private_keys(key_file).unwrap(); | ||
tls_config | ||
.set_single_cert(cert_chain, keys.remove(0)) | ||
.unwrap(); | ||
|
||
let tls_acceptor = RustlsAcceptor::new(tls_config); | ||
|
||
let count = Arc::new(AtomicUsize::new(0)); | ||
|
||
let addr = ("127.0.0.1", 8443); | ||
info!("starting server on port: {}", &addr.0); | ||
|
||
Server::build() | ||
.bind("tls-example", addr, move || { | ||
let count = Arc::clone(&count); | ||
|
||
// Set up TLS service factory | ||
pipeline_factory(tls_acceptor.clone()) | ||
.map_err(|err| println!("Rustls error: {:?}", err)) | ||
.and_then(move |stream| { | ||
let num = count.fetch_add(1, Ordering::Relaxed); | ||
info!("[{}] Got TLS connection: {:?}", num, stream); | ||
ok(()) | ||
}) | ||
})? | ||
.workers(1) | ||
.run() | ||
.await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIENjCCAp6gAwIBAgIRANp+D9pBErdacw6KjrwJ+4swDQYJKoZIhvcNAQELBQAw | ||
bTEeMBwGA1UEChMVbWtjZXJ0IGRldmVsb3BtZW50IENBMSEwHwYDVQQLDBhyb2JA | ||
c29tYnJhLng1Mi5kZXYgKFJvYikxKDAmBgNVBAMMH21rY2VydCByb2JAc29tYnJh | ||
Lng1Mi5kZXYgKFJvYikwHhcNMTkwNjAxMDAwMDAwWhcNMzAwOTEzMDIzNDI0WjBM | ||
MScwJQYDVQQKEx5ta2NlcnQgZGV2ZWxvcG1lbnQgY2VydGlmaWNhdGUxITAfBgNV | ||
BAsMGHJvYkBzb21icmEueDUyLmRldiAoUm9iKTCCASIwDQYJKoZIhvcNAQEBBQAD | ||
ggEPADCCAQoCggEBALYAn8dsQUDTp8SptAtkiAySvQYLpAOct3/OjBn+dSYfbQcp | ||
Ph9w/Zo83Msl7Fb1DBvADHFtyBpESATZ2chS5fwCAwUFTlKrzMk3qauEoJ3cCQa8 | ||
ccqhTMLeT38jRlhXrMHWBfz0ipqy+yTLWeM32LX8s0jPbbsZ3gVJ/Ls4qm0CTaqb | ||
zRdcQ7GTVKYet5DR7ZvwvAaLtWk/iiHKwnOveuF27HNlxj0Rwd/lhJ/t9x8xJwyR | ||
MTdm852KQadI8xOSbWNK4j9419yzKjUEMKgn78wT/7DQfeKKCAreHa4MaEw4+koD | ||
2Bqb+V4fI6T84VvXkNG3CjSpmIiYGlIE1LVgBL8CAwEAAaNyMHAwDgYDVR0PAQH/ | ||
BAQDAgWgMBMGA1UdJQQMMAoGCCsGAQUFBwMBMAwGA1UdEwEB/wQCMAAwHwYDVR0j | ||
BBgwFoAUto/ox0MqZShmQpViV/gjfJKrMDkwGgYDVR0RBBMwEYIJbG9jYWxob3N0 | ||
hwR/AAABMA0GCSqGSIb3DQEBCwUAA4IBgQBUCMzqTY0sg+61gh8gKS5TCL6qs0R1 | ||
xys/EFFaD5JYUsfM/HyhHd0jq+x5Pd3mB2Jvhoq9xhjMwP11H8Uw5lLBHA8USdF9 | ||
EiLW1GvT3/gLfMqb0lPk0RMRBeX8c0QbDtqdiUCE7S6zJbZ5gjFeRuFNjdcGA1Ss | ||
8CPPts2mns5cwah6H7T/BFzj5aR9Qe14vo1Rpr5gD5CpHvk1t16q7YsczQfVMvt3 | ||
Ydk6p0rwA8Z5okQK7y3qKPZI+//ygWL6ZBjVjl1/Al8vybG2UYjYgfMBwaVvMiDJ | ||
j/vCdVmlvGb+MZlZID/p2veaNeEKgi1A1EOj3sNuQYXXFfSD9mdamX7JIfGi/U7v | ||
ivvUjJUbzGrUngldt5iCKqcCQum7nlzu9sT1Tm2t/n4tz/btrI+Wimg8riSzM+Nk | ||
dfuvv4NbWe6Th5460HH8mMvfPZSB8dCoxwm98tuqcMXLkR1RJX5Z8LYAaPTsUs/h | ||
HxQCY4EaY7feZ/qFal9FGwvpzVr3/XjgSCU= | ||
-----END CERTIFICATE----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
-----BEGIN RSA PRIVATE KEY----- | ||
MIIEpAIBAAKCAQEAtgCfx2xBQNOnxKm0C2SIDJK9BgukA5y3f86MGf51Jh9tByk+ | ||
H3D9mjzcy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BAituLN4mBgO | ||
IDBdj7GOYk3dkcc2J0HTlyIIeduvlinNM4Myel6NrDKY5rhbtgGhhGEUkY6W7NvG | ||
0SAh0L8tmB3JKH6upfr3023b4pKjGj2oZ+wij27DxnQEdqg5reOP+mHTPbDaKMki | ||
kml3TBMpj1XBbXaXsNJBaMUCgYEAnnNzEC4563QrU2pvUJ3HgT4Dotgqv/Sy6NuG | ||
W1e9jSPYgU0RDHndZWtygwdFTDpzNbJR5po8t2J7MxQOcsmcNE0y387sHpbdCYyy | ||
8Po2uxm7CoaJ/02BUVYL8/Aujob0dVGWrS5SYY3zAjO1S+VGKXA+EjW2cDRB3jKa | ||
45ucICcCgYBdMxB5Oj6GpdewWWaBss9dwHtDaD4oVGYIBbIc2qdyCYixWdW9NccV | ||
fRJs0ulGrpg9OtyWbwZASu2jz55+s3hi4rnrcaXKiIh9Rs25v1irF6Dmduvo7CaN | ||
Mf7zBg7LUttmqN6D3npIAxmBULl8KRfjnt6U2tJolF5X0qQ1uqnnTA== | ||
-----END RSA PRIVATE KEY----- |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.