50 个稳定版本

新版本 1.44.1 2024 年 8 月 13 日
1.43.0 2024 年 7 月 23 日
1.34.0 2024 年 3 月 13 日
1.32.0 2023 年 12 月 8 日
0.0.0 2022 年 2 月 16 日

35网络编程 中排名

Download history 1282/week @ 2024-04-30 1508/week @ 2024-05-07 1208/week @ 2024-05-14 1501/week @ 2024-05-21 1499/week @ 2024-05-28 2012/week @ 2024-06-04 1159/week @ 2024-06-11 1371/week @ 2024-06-18 1086/week @ 2024-06-25 651/week @ 2024-07-02 893/week @ 2024-07-09 887/week @ 2024-07-16 914/week @ 2024-07-23 1109/week @ 2024-07-30 1224/week @ 2024-08-06 1751/week @ 2024-08-13

5,209 每月下载量
用于 10 个 Crates (6 直接使用)

Apache-2.0

5MB
109K SLoC

s2n-quic

s2n-quic 是 IETF QUIC 协议的 Rust 实现,具有以下特点:

请参阅 API 文档示例,以开始使用 s2n-quic

Crates.io docs.rs Apache 2.0 Licensed Build Status Dependencies MSRV

安装

s2n-quic 可在 crates.io 上找到,并可以添加到项目中,如下所示:

[dependencies]
s2n-quic = "1"

注意:在类Unix系统中,默认使用 s2n-tls 作为 TLS 提供者。在 Linux 系统中,使用 aws-lc-rs 进行加密操作。在这些系统中,可能需要 C 编译器和 CMake 才能进行安装。

示例

以下实现了一个基本的回显服务器和客户端。客户端连接到服务器,并将其 stdin 在流上管道化。服务器监听新的流,并将接收到的任何数据回送给客户端。然后客户端将所有流数据管道化到 stdout

服务器

// src/bin/server.rs
use s2n_quic::Server;
use std::{error::Error, path::Path};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let mut server = Server::builder()
        .with_tls((Path::new("cert.pem"), Path::new("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 {
            while let Ok(Some(mut stream)) = connection.accept_bidirectional_stream().await {
                // spawn a new task for the stream
                tokio::spawn(async move {
                    // echo any data back to the stream
                    while let Ok(Some(data)) = stream.receive().await {
                        stream.send(data).await.expect("stream should be open");
                    }
                });
            }
        });
    }

    Ok(())
}

客户端

// src/bin/client.rs
use s2n_quic::{client::Connect, Client};
use std::{error::Error, path::Path, net::SocketAddr};

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let client = Client::builder()
        .with_tls(Path::new("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(())
}

最低支持 Rust 版本 (MSRV)

s2n-quic 将维持至少 6 个月的滚动 MSRV(最低支持 Rust 版本)策略。当前 s2n-quic 版本不保证在低于 MSRV 的 Rust 版本上构建。

当前的 MSRV 是 1.71.0

安全问题通知

如果您在 s2n-quic 中发现潜在的安全问题,我们请求您通过我们的 漏洞报告页面 通知 AWS 安全团队。请 不要 创建公共 GitHub 事件。

如果您打包或分发 s2n-quic,或将其作为大型多用户服务的一部分使用,您可能有资格提前通知未来的 s2n-quic 发布。请联系 [email protected]

许可证

本项目采用 Apache-2.0 许可证

依赖关系

~6–33MB
~672K SLoC