Skip to content

Commit

Permalink
fix: add basic error handling and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAlcibiades committed Sep 11, 2024
1 parent 116c27d commit 386e93a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ version = "1.0.0"
[dependencies]
async-stream = "0.3.5"
bytes = "1.7.1"
futures = "0.3.30"
http = "1.1.0"
http-body = "1.0.1"
http-body-util = "0.1.2"
Expand All @@ -27,7 +28,6 @@ tokio-rustls = "0.26.0"
tokio-stream = { version = "0.1.16", features = ["net"] }
tower = { version = "0.5.1", features = ["util"] }
tracing = "0.1.40"
futures = "0.3.30"

[dev-dependencies]
hyper = { version = "1.4.1", features = ["client"] }
Expand Down
59 changes: 44 additions & 15 deletions src/tls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{fs, io};
use rustls::pki_types::{CertificateDer, PrivateKeyDer};
use crate::Error;
use rustls::pki_types::{CertificateDer, PrivateKeyDer};
use std::{fs, io};
use tokio::io::{AsyncRead, AsyncWrite};
use tokio_rustls::TlsAcceptor;
use tokio_stream::{Stream, StreamExt};
Expand Down Expand Up @@ -41,35 +41,64 @@ where
// Transform each item in the TCP stream into a TLS stream
tcp_stream.then(move |result| {
// Clone the TLS acceptor for each connection
// This is necessary because the acceptor is moved into the async block
let tls = tls.clone();

Check warning on line 45 in src/tls.rs

View check run for this annotation

Codecov / codecov/patch

src/tls.rs#L34-L45

Added lines #L34 - L45 were not covered by tests

async move {
match result {

Check warning on line 48 in src/tls.rs

View check run for this annotation

Codecov / codecov/patch

src/tls.rs#L47-L48

Added lines #L47 - L48 were not covered by tests
// TODO(Can we get at the raw IO here so that it looks the same after the handshake?)
Ok(io) => tls.accept(io).await.map_err(Error::from),
// TODO(Unwrap into crate error and handle)
// If the TCP connection was successfully established
Ok(io) => {
// Attempt to perform the TLS handshake
// If successful, return the TLS stream; otherwise, wrap the error
tls.accept(io).await.map_err(Error::from)

Check warning on line 53 in src/tls.rs

View check run for this annotation

Codecov / codecov/patch

src/tls.rs#L50-L53

Added lines #L50 - L53 were not covered by tests
}
// If there was an error establishing the TCP connection, propagate it
Err(e) => Err(e),

Check warning on line 56 in src/tls.rs

View check run for this annotation

Codecov / codecov/patch

src/tls.rs#L56

Added line #L56 was not covered by tests
}
}
})
}

Check warning on line 60 in src/tls.rs

View check run for this annotation

Codecov / codecov/patch

src/tls.rs#L58-L60

Added lines #L58 - L60 were not covered by tests

// Load the public certificate from a file.
/// Load the public certificate from a file.
///
/// This function reads a PEM-encoded certificate file and returns a vector of
/// parsed certificates.
///
/// # Arguments
///
/// * `filename`: The path to the certificate file.
///
/// # Returns
///
/// A `Result` containing a vector of `CertificateDer` on success, or an `io::Error` on failure.
fn load_certs(filename: &str) -> io::Result<Vec<CertificateDer<'static>>> {

Check warning on line 74 in src/tls.rs

View check run for this annotation

Codecov / codecov/patch

src/tls.rs#L74

Added line #L74 was not covered by tests
// Open certificate file.
let certfile = fs::File::open(filename).unwrap();
// Open certificate file
let certfile = fs::File::open(filename)?;
let mut reader = io::BufReader::new(certfile);

// Load and return certificate.
// Load and return certificates
// The `collect()` method is used to gather all certificates into a vector
rustls_pemfile::certs(&mut reader).collect()
}

Check warning on line 82 in src/tls.rs

View check run for this annotation

Codecov / codecov/patch

src/tls.rs#L76-L82

Added lines #L76 - L82 were not covered by tests

// Load the private key from a file.
/// Load the private key from a file.
///
/// This function reads a PEM-encoded private key file and returns the parsed private key.
///
/// # Arguments
///
/// * `filename`: The path to the private key file.
///
/// # Returns
///
/// A `Result` containing a `PrivateKeyDer` on success, or an `io::Error` on failure.
fn load_private_key(filename: &str) -> io::Result<PrivateKeyDer<'static>> {

Check warning on line 95 in src/tls.rs

View check run for this annotation

Codecov / codecov/patch

src/tls.rs#L95

Added line #L95 was not covered by tests
// Open keyfile.
let keyfile = fs::File::open(filename).unwrap();
// Open keyfile
let keyfile = fs::File::open(filename)?;
let mut reader = io::BufReader::new(keyfile);

// Load and return a single private key.
rustls_pemfile::private_key(&mut reader).map(|key| key.unwrap())
}
// Load and return a single private key
// The `?` operator is used for error propagation
rustls_pemfile::private_key(&mut reader)?
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "No private key found in file"))
}

Check warning on line 104 in src/tls.rs

View check run for this annotation

Codecov / codecov/patch

src/tls.rs#L97-L104

Added lines #L97 - L104 were not covered by tests

0 comments on commit 386e93a

Please sign in to comment.