3 个不稳定版本
0.3.0 | 2020 年 5 月 22 日 |
---|---|
0.2.1 | 2020 年 5 月 21 日 |
0.2.0 | 2020 年 5 月 20 日 |
在 #tls-support 中排名 16
28KB
479 行
带有双向认证的 Apache Thrift TLS
此软件包旨在为 Rust 的 Apache Thrift 提供完整的 TLS(1.2 和 1.3)支持。它通过尽可能减少额外代码的需要,以尽可能不显眼的方式提供这种支持。
TLS 支持通过 Rustls 提供,这是一个用 Rust 编写的现代、快速且强大的 TLS 库。
注意:
- 我学习 RUST 的方法是尝试用它做些有用的事情... 所以这可能(很可能)是使用不良习惯,而且非常接近垃圾。
- 我真的很应该在了解自己在做什么之后,稍后尝试向 Apache Thrift 代码库提交 PR
tls_*.rs
文件包含大量从官方 Apache Thrift 代码库 复制的代码
技术说明
- Thrift 库通过克隆 TCP(客户端或服务器)流来使用分离的输入和输出协议。Rustls(特别是其会话)目前不支持全双工,所以我提出的快速解决方案是将会话包装在
Arc<Mutex>
中,提供同步以供并发使用。这个解决方案应该适用于 Thrift,但可能从性能和行为角度存在边缘情况。如果您有更好的想法,请提出建议 :-)
我该如何使用它?
客户端和服务器示例
GitHub 仓库中有一个客户端-服务器示例: https://github.com/dguerri/rust-thrift-tls。您可以在 thrift-tls-example
下找到一个使用 TLS 双向认证的客户端-服务器示例。
- 运行
setup.sh
以创建 X509 证书和相关密钥,以及创建 Thrift 规范文件 - 运行服务器:
cargo run --bin server
- 运行客户端:
cargo run --bin client
使用 RUST_LOG=debug
来查看调试信息
代码示例
客户端(无客户端认证)
let mut c = TLSTTcpChannel::new();
// create a new TLS session with default (embedded) RootCertStore
c.open(
"localhost:9000",
None, // Do not perform client auth
None, // Default (embedded) RootCertStore
)?;
// build the input/output protocol as usual (see "plain" Thrift examples)
// [...]
服务器示例
// build transport factories and protocols as usual (see "plain" Thrift examples)
// [...]
// create a pre-threaded server
let mut server = TLSTServer::new(
i_tran_fact,
i_prot_fact,
o_tran_fact,
o_prot_fact,
processor,
10,
X509Credentials::new("x509/server.crt", "x509/server.key"),
None, // Default (embedded) RootCertStore
false, // Client authentication not required
None, // No connection hook
);
// set listen address
let listen_address = "127.0.0.1:9000";
log::info!("binding to {}", listen_address);
server.listen(&listen_address)
依赖项
~10MB
~271K SLoC