feat: s2n quic test

This commit is contained in:
2022-04-15 01:05:16 +08:00
parent 72a8969453
commit fe2f615943
6 changed files with 1198 additions and 0 deletions

1096
__network/s2n_quic/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,11 @@
[package]
name = "s2n_quic"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
tokio = { version = "1", features = ["full"] }
s2n-quic = "1.0.2"

View File

@@ -0,0 +1,11 @@
-----BEGIN CERTIFICATE-----
MIIBeDCCAR6gAwIBAgIBKjAKBggqhkjOPQQDAjAwMRgwFgYDVQQKDA9DcmFiIHdp
ZGdpdHMgU0UxFDASBgNVBAMMC01hc3RlciBDZXJ0MCIYDzE5NzUwMTAxMDAwMDAw
WhgPNDA5NjAxMDEwMDAwMDBaMDAxGDAWBgNVBAoMD0NyYWIgd2lkZ2l0cyBTRTEU
MBIGA1UEAwwLTWFzdGVyIENlcnQwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAQb
bVPayLOdbKxXB4yB4Vx3Kf2Z89vsUvhmiICsjncRwBEKkP+GjTg1bSEloLvzuha9
3u78xp2/1ZaeqtVwYgJMoyUwIzAhBgNVHREEGjAYggtxbGF3cy5xbGF3c4IJbG9j
YWxob3N0MAoGCCqGSM49BAMCA0gAMEUCIDrxPoQBu9G/g54f3TKYXj8bO2fdkPD1
PMO712Y3e0eNAiEA9mt1NW6TDPVf+xmUA/swi8gnhlusV2Y1sB4qhDCPr9c=
-----END CERTIFICATE-----

View File

@@ -0,0 +1,5 @@
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgtZAp9paqkz1vzQSp
tw52t+ZiSKAuJRfB5JnvA6q7+CKhRANCAAQbbVPayLOdbKxXB4yB4Vx3Kf2Z89vs
UvhmiICsjncRwBEKkP+GjTg1bSEloLvzuha93u78xp2/1ZaeqtVwYgJM
-----END PRIVATE KEY-----

View File

@@ -0,0 +1,36 @@
use s2n_quic::{Client, client::Connect};
use std::{error::Error, net::SocketAddr};
/// NOTE: this certificate is to be used for demonstration purposes only!
pub static CERT_PEM: &str = include_str!("../../cert.pem");
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let client = Client::builder()
.with_tls(CERT_PEM)?
.with_io("0.0.0.0:0")?
.start()?;
let addr: SocketAddr = "127.0.0.1:4433".parse()?;
let connect = Connect::new(addr).with_server_name("localhost");
let mut connection = client.connect(connect).await?;
// ensure the connection doesn't time out with inactivity
connection.keep_alive(true)?;
// open a new stream and split the receiving and sending sides
let stream = connection.open_bidirectional_stream().await?;
let (mut receive_stream, mut send_stream) = stream.split();
// spawn a task that copies responses from the server to stdout
tokio::spawn(async move {
let mut stdout = tokio::io::stdout();
let _ = tokio::io::copy(&mut receive_stream, &mut stdout).await;
});
// copy data from stdin and send it to the server
let mut stdin = tokio::io::stdin();
tokio::io::copy(&mut stdin, &mut send_stream).await?;
Ok(())
}

View File

@@ -0,0 +1,39 @@
use s2n_quic::Server;
use std::error::Error;
/// NOTE: this certificate is to be used for demonstration purposes only!
pub static CERT_PEM: &str = include_str!("../../cert.pem");
/// NOTE: this certificate is to be used for demonstration purposes only!
pub static KEY_PEM: &str = include_str!("../../key.pem");
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let mut server = Server::builder()
.with_tls((CERT_PEM, KEY_PEM))?
.with_io("127.0.0.1:4433")?
.start()?;
while let Some(mut connection) = server.accept().await {
// spawn a new task for the connection
tokio::spawn(async move {
eprintln!("Connection accepted from {:?}", connection.remote_addr());
while let Ok(Some(mut stream)) = connection.accept_bidirectional_stream().await {
// spawn a new task for the stream
tokio::spawn(async move {
eprintln!("Stream opened from {:?}", stream.connection().remote_addr());
// echo any data back to the stream
while let Ok(Some(data)) = stream.receive().await {
stream.send(data).await.expect("stream should be open");
}
println!("Stream closed from {:?}", stream.connection().remote_addr());
});
}
println!("Connection closed from {:?}", connection.remote_addr());
});
}
Ok(())
}