Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sfackler/rust-native-tls
https://github.com/sfackler/rust-native-tls
Last synced: 4 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/sfackler/rust-native-tls
- Owner: sfackler
- License: apache-2.0
- Created: 2016-04-17T02:40:11.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-10-14T19:24:58.000Z (20 days ago)
- Last Synced: 2024-10-15T07:03:47.373Z (20 days ago)
- Language: Rust
- Size: 407 KB
- Stars: 474
- Watchers: 16
- Forks: 198
- Open Issues: 59
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
Awesome Lists containing this project
- awesome-rust-cn - sfackler/rust-native-tls
- awesome-rust - sfackler/rust-native-tls
- awesome-rust - sfackler/rust-native-tls
- awesome-rust-cn - sfackler/rust-native-tls
- awesome-rust-zh - sfackler/rust-native-tls - 原生 TLS 库的绑定 (库 / 加密)
- awesome-rust - sfackler/rust-native-tls - Bindings for native TLS libraries (Libraries / Cryptography)
- awesome-rust - sfackler/rust-native-tls
- fucking-awesome-rust - sfackler/rust-native-tls - Bindings for native TLS libraries (Libraries / Cryptography)
- fucking-awesome-rust - sfackler/rust-native-tls - Bindings for native TLS libraries (Libraries / Cryptography)
README
# rust-native-tls
[Documentation](https://docs.rs/native-tls)
An abstraction over platform-specific TLS implementations.
Specifically, this crate uses SChannel on Windows (via the [`schannel`] crate),
Secure Transport on macOS (via the [`security-framework`] crate), and OpenSSL (via
the [`openssl`] crate) on all other platforms.[`schannel`]: https://crates.io/crates/schannel
[`security-framework`]: https://crates.io/crates/security-framework
[`openssl`]: https://crates.io/crates/openssl## Installation
```toml
# Cargo.toml
[dependencies]
native-tls = "0.2"
```## Usage
An example client looks like:
```rust,ignore
extern crate native_tls;use native_tls::TlsConnector;
use std::io::{Read, Write};
use std::net::TcpStream;fn main() {
let connector = TlsConnector::new().unwrap();let stream = TcpStream::connect("google.com:443").unwrap();
let mut stream = connector.connect("google.com", stream).unwrap();stream.write_all(b"GET / HTTP/1.0\r\n\r\n").unwrap();
let mut res = vec![];
stream.read_to_end(&mut res).unwrap();
println!("{}", String::from_utf8_lossy(&res));
}
```To accept connections as a server from remote clients:
```rust,ignore
extern crate native_tls;use native_tls::{Identity, TlsAcceptor, TlsStream};
use std::fs::File;
use std::io::{Read};
use std::net::{TcpListener, TcpStream};
use std::sync::Arc;
use std::thread;fn main() {
let mut file = File::open("identity.pfx").unwrap();
let mut identity = vec![];
file.read_to_end(&mut identity).unwrap();
let identity = Identity::from_pkcs12(&identity, "hunter2").unwrap();let acceptor = TlsAcceptor::new(identity).unwrap();
let acceptor = Arc::new(acceptor);let listener = TcpListener::bind("0.0.0.0:8443").unwrap();
fn handle_client(stream: TlsStream) {
// ...
}for stream in listener.incoming() {
match stream {
Ok(stream) => {
let acceptor = acceptor.clone();
thread::spawn(move || {
let stream = acceptor.accept(stream).unwrap();
handle_client(stream);
});
}
Err(e) => { /* connection failed */ }
}
}
}
```# License
`rust-native-tls` is primarily distributed under the terms of both the MIT
license and the Apache License (Version 2.0), with portions covered by various
BSD-like licenses.See LICENSE-APACHE, and LICENSE-MIT for details.