diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:59:35 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:59:35 +0000 |
commit | d1b2d29528b7794b41e66fc2136e395a02f8529b (patch) | |
tree | a4a17504b260206dec3cf55b2dca82929a348ac2 /vendor/native-tls/examples | |
parent | Releasing progress-linux version 1.72.1+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-d1b2d29528b7794b41e66fc2136e395a02f8529b.tar.xz rustc-d1b2d29528b7794b41e66fc2136e395a02f8529b.zip |
Merging upstream version 1.73.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/native-tls/examples')
-rw-r--r-- | vendor/native-tls/examples/google-connect.rs | 17 | ||||
-rw-r--r-- | vendor/native-tls/examples/simple-server-pkcs8.rs | 45 | ||||
-rw-r--r-- | vendor/native-tls/examples/simple-server.rs | 37 |
3 files changed, 99 insertions, 0 deletions
diff --git a/vendor/native-tls/examples/google-connect.rs b/vendor/native-tls/examples/google-connect.rs new file mode 100644 index 000000000..bcf454245 --- /dev/null +++ b/vendor/native-tls/examples/google-connect.rs @@ -0,0 +1,17 @@ +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)); +} diff --git a/vendor/native-tls/examples/simple-server-pkcs8.rs b/vendor/native-tls/examples/simple-server-pkcs8.rs new file mode 100644 index 000000000..df9c95da0 --- /dev/null +++ b/vendor/native-tls/examples/simple-server-pkcs8.rs @@ -0,0 +1,45 @@ +extern crate native_tls; + +use native_tls::{Identity, TlsAcceptor, TlsStream}; +use std::fs::File; +use std::io::{Read, Write}; +use std::net::{TcpListener, TcpStream}; +use std::sync::Arc; +use std::thread; + +fn main() { + let mut cert_file = File::open("test/cert.pem").unwrap(); + let mut certs = vec![]; + cert_file.read_to_end(&mut certs).unwrap(); + let mut key_file = File::open("test/key.pem").unwrap(); + let mut key = vec![]; + key_file.read_to_end(&mut key).unwrap(); + let pkcs8 = Identity::from_pkcs8(&certs, &key).unwrap(); + + let acceptor = TlsAcceptor::new(pkcs8).unwrap(); + let acceptor = Arc::new(acceptor); + + let listener = TcpListener::bind("0.0.0.0:8443").unwrap(); + + fn handle_client(mut stream: TlsStream<TcpStream>) { + let mut buf = [0; 1024]; + let read = stream.read(&mut buf).unwrap(); + let received = std::str::from_utf8(&buf[0..read]).unwrap(); + stream + .write_all(format!("received '{}'", received).as_bytes()) + .unwrap(); + } + + 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 */ } + } + } +} diff --git a/vendor/native-tls/examples/simple-server.rs b/vendor/native-tls/examples/simple-server.rs new file mode 100644 index 000000000..483be948c --- /dev/null +++ b/vendor/native-tls/examples/simple-server.rs @@ -0,0 +1,37 @@ +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 pkcs12 = vec![]; + file.read_to_end(&mut pkcs12).unwrap(); + let pkcs12 = Identity::from_pkcs12(&pkcs12, "hunter2").unwrap(); + + let acceptor = TlsAcceptor::new(pkcs12).unwrap(); + let acceptor = Arc::new(acceptor); + + let listener = TcpListener::bind("0.0.0.0:8443").unwrap(); + + fn handle_client(_stream: TlsStream<TcpStream>) { + // ... + } + + 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 */ } + } + } +} |