Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tls): add ALPN support #299

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compio-tls/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "compio-tls"
version = "0.3.0"
version = "0.3.1"
description = "TLS adaptor with compio"
categories = ["asynchronous", "network-programming"]
keywords = ["async", "net", "tls"]
Expand All @@ -18,7 +18,7 @@ rustdoc-args = ["--cfg", "docsrs"]
compio-buf = { workspace = true }
compio-io = { workspace = true, features = ["compat"] }

native-tls = { version = "0.2.11", optional = true }
native-tls = { version = "0.2.11", optional = true, features = ["alpn"] }
rustls = { workspace = true, default-features = false, optional = true, features = [
"logging",
"std",
Expand Down
16 changes: 15 additions & 1 deletion compio-tls/src/stream/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{io, mem::MaybeUninit};
use std::{borrow::Cow, io, mem::MaybeUninit};

use compio_buf::{BufResult, IoBuf, IoBufMut};
use compio_io::{compat::SyncStream, AsyncRead, AsyncWrite};
Expand All @@ -24,6 +24,15 @@ impl<S> TlsStreamInner<S> {
Self::Rustls(s) => s.get_mut(),
}
}

pub fn negotiated_alpn(&self) -> Option<Cow<[u8]>> {
match self {
#[cfg(feature = "native-tls")]
Self::NativeTls(s) => s.negotiated_alpn().ok().flatten().map(Cow::from),
#[cfg(feature = "rustls")]
Self::Rustls(s) => s.negotiated_alpn().map(Cow::from),
}
}
}

impl<S> io::Read for TlsStreamInner<S> {
Expand Down Expand Up @@ -87,6 +96,11 @@ impl<S> TlsStream<S> {
pub(crate) fn new_rustls_server(s: SyncStream<S>, conn: rustls::ServerConnection) -> Self {
Self(TlsStreamInner::Rustls(rtls::TlsStream::new_server(s, conn)))
}

/// Returns the negotiated ALPN protocol.
pub fn negotiated_alpn(&self) -> Option<Cow<[u8]>> {
self.0.negotiated_alpn()
}
}

#[cfg(feature = "native-tls")]
Expand Down
7 changes: 7 additions & 0 deletions compio-tls/src/stream/rtls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ impl<S> TlsStream<S> {
pub fn get_mut(&mut self) -> &mut S {
&mut self.inner
}

pub fn negotiated_alpn(&self) -> Option<&[u8]> {
match &self.conn {
TlsConnection::Client(client) => client.alpn_protocol(),
TlsConnection::Server(server) => server.alpn_protocol(),
}
}
}

impl<S: io::Read> TlsStream<S> {
Expand Down