Skip to content

Commit

Permalink
Update cargo-hack check command and feature dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
sunmy2019 committed Feb 15, 2024
1 parent aaf8f5e commit 81b63d1
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 51 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ jobs:
- name: Setup cargo-hack
run: cargo install cargo-hack
- name: Check all features
run: cargo hack check --feature-powerset --no-dev-deps
run: "cargo hack check --feature-powerset --no-dev-deps \
--mutually-exclusive-features default,native-tls,websocket-native-tls,rustls,websocket-rustls \
--skip tls,websocket"

build:
name: Build for ${{ matrix.target }}
Expand Down
27 changes: 20 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ include = ["src/**/*", "LICENSE", "README.md", "build.rs"]
default = [
"server",
"client",
"native-tls-support",
"native-tls",
"noise",
"websocket",
"hot-reload",
Expand All @@ -26,23 +26,36 @@ server = []
client = []

# TLS support
tls-support = []
native-tls-support = ["tokio-native-tls", "tls-support"]
rustls-support = [
native-tls = ["tokio-native-tls"]
rustls = [
"tokio-rustls",
"rustls-pemfile",
"rustls-native-certs",
"p12",
"tls-support",
]
# original tls feature for backward compatibility
tls = ["native-tls-support"]
tls = ["native-tls"]

# Noise support
noise = ["snowstorm", "base64"]

# Websocket support
websocket = ["tokio-tungstenite", "tokio-util", "futures-core", "futures-sink"]
websocket-native-tls = [
"tokio-tungstenite",
"tokio-util",
"futures-core",
"futures-sink",
"native-tls",
]
websocket-rustls = [
"tokio-tungstenite",
"tokio-util",
"futures-core",
"futures-sink",
"rustls",
]
# original websocket feature for backward compatibility
websocket = ["websocket-native-tls"]

# Configuration hot-reload support
hot-reload = ["notify"]
Expand Down
12 changes: 6 additions & 6 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ use tracing::{debug, error, info, instrument, trace, warn, Instrument, Span};

#[cfg(feature = "noise")]
use crate::transport::NoiseTransport;
#[cfg(feature = "tls-support")]
#[cfg(any(feature = "native-tls", feature = "rustls"))]
use crate::transport::TlsTransport;
#[cfg(feature = "websocket")]
#[cfg(any(feature = "websocket-native-tls", feature = "websocket-rustls"))]
use crate::transport::WebsocketTransport;

use crate::constants::{run_control_chan_backoff, UDP_BUFFER_SIZE, UDP_SENDQ_SIZE, UDP_TIMEOUT};
Expand All @@ -48,12 +48,12 @@ pub async fn run_client(
client.run(shutdown_rx, update_rx).await
}
TransportType::Tls => {
#[cfg(feature = "tls-support")]
#[cfg(any(feature = "native-tls", feature = "rustls"))]
{
let mut client = Client::<TlsTransport>::from(config).await?;
client.run(shutdown_rx, update_rx).await
}
#[cfg(not(feature = "tls-support"))]
#[cfg(not(any(feature = "native-tls", feature = "rustls")))]
crate::helper::feature_not_compile("tls")
}
TransportType::Noise => {
Expand All @@ -66,12 +66,12 @@ pub async fn run_client(
crate::helper::feature_not_compile("noise")
}
TransportType::Websocket => {
#[cfg(feature = "websocket")]
#[cfg(any(feature = "websocket-native-tls", feature = "websocket-rustls"))]
{
let mut client = Client::<WebsocketTransport>::from(config).await?;
client.run(shutdown_rx, update_rx).await
}
#[cfg(not(feature = "websocket"))]
#[cfg(not(any(feature = "websocket-native-tls", feature = "websocket-rustls")))]
crate::helper::feature_not_compile("websocket")
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ use tracing::{debug, error, info, info_span, instrument, warn, Instrument, Span}

#[cfg(feature = "noise")]
use crate::transport::NoiseTransport;
#[cfg(feature = "tls-support")]
#[cfg(any(feature = "native-tls", feature = "rustls"))]
use crate::transport::TlsTransport;
#[cfg(feature = "websocket")]
#[cfg(any(feature = "websocket-native-tls", feature = "websocket-rustls"))]
use crate::transport::WebsocketTransport;

type ServiceDigest = protocol::Digest; // SHA256 of a service name
Expand Down Expand Up @@ -57,12 +57,12 @@ pub async fn run_server(
server.run(shutdown_rx, update_rx).await?;
}
TransportType::Tls => {
#[cfg(feature = "tls-support")]
#[cfg(any(feature = "native-tls", feature = "rustls"))]
{
let mut server = Server::<TlsTransport>::from(config).await?;
server.run(shutdown_rx, update_rx).await?;
}
#[cfg(not(feature = "tls-support"))]
#[cfg(not(any(feature = "native-tls", feature = "rustls")))]
crate::helper::feature_not_compile("tls")
}
TransportType::Noise => {
Expand All @@ -75,12 +75,12 @@ pub async fn run_server(
crate::helper::feature_not_compile("noise")
}
TransportType::Websocket => {
#[cfg(feature = "websocket")]
#[cfg(any(feature = "websocket-native-tls", feature = "websocket-rustls"))]
{
let mut server = Server::<WebsocketTransport>::from(config).await?;
server.run(shutdown_rx, update_rx).await?;
}
#[cfg(not(feature = "websocket"))]
#[cfg(not(any(feature = "websocket-native-tls", feature = "websocket-rustls")))]
crate::helper::feature_not_compile("websocket")
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,19 @@ pub trait Transport: Debug + Send + Sync {
mod tcp;
pub use tcp::TcpTransport;

#[cfg(feature = "tls-support")]
#[cfg(any(feature = "native-tls", feature = "rustls"))]
mod tls;

#[cfg(feature = "tls-support")]
#[cfg(any(feature = "native-tls", feature = "rustls"))]
pub(crate) use tls::TlsTransport;

#[cfg(feature = "noise")]
mod noise;
#[cfg(feature = "noise")]
pub use noise::NoiseTransport;

#[cfg(feature = "websocket")]
#[cfg(any(feature = "websocket-native-tls", feature = "websocket-rustls"))]
mod websocket;
#[cfg(feature = "websocket")]
#[cfg(any(feature = "websocket-native-tls", feature = "websocket-rustls"))]
pub use websocket::WebsocketTransport;

#[derive(Debug, Clone, Copy)]
Expand Down
46 changes: 31 additions & 15 deletions src/transport/tls.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#[cfg(all(feature = "native-tls-support", feature = "rustls-support"))]
compile_error!("Only one of `native-tls-support` and `rustls-support` can be enabled");
#[cfg(all(feature = "native-tls", feature = "rustls"))]
compile_error!("Only one of `native-tls` and `rustls` can be enabled");

#[cfg(all(not(feature = "native-tls-support"), not(feature = "rustls-support")))]
compile_error!("Either `native-tls-support` or `rustls-support` must be enabled");

#[cfg(feature = "native-tls-support")]
mod native_tls_support {
#[cfg(feature = "native-tls")]
mod native_tls {
use crate::config::{TlsConfig, TransportConfig};
use crate::helper::host_port_pair;
use crate::transport::{AddrMaybeCached, SocketOpts, TcpTransport, Transport};
Expand All @@ -15,7 +12,8 @@ mod native_tls_support {
use std::net::SocketAddr;
use tokio::net::{TcpListener, TcpStream, ToSocketAddrs};
use tokio_native_tls::native_tls::{self, Certificate, Identity};
use tokio_native_tls::{TlsAcceptor, TlsConnector, TlsStream};
pub(crate) use tokio_native_tls::TlsStream;
use tokio_native_tls::{TlsAcceptor, TlsConnector};

#[derive(Debug)]
pub struct TlsTransport {
Expand Down Expand Up @@ -116,13 +114,22 @@ mod native_tls_support {
.await?)
}
}

#[cfg(feature = "websocket-native-tls")]
pub(crate) fn get_tcpstream(s: &TlsStream<TcpStream>) -> &TcpStream {
s.get_ref().get_ref().get_ref()
}
}

#[cfg(feature = "native-tls-support")]
pub(crate) use native_tls_support::TlsTransport;
#[cfg(feature = "websocket-native-tls")]
pub(crate) use native_tls::get_tcpstream;
#[cfg(feature = "websocket-native-tls")]
pub(crate) use native_tls::TlsStream;
#[cfg(feature = "native-tls")]
pub(crate) use native_tls::TlsTransport;

#[cfg(feature = "rustls-support")]
pub(crate) mod ruslts_support {
#[cfg(feature = "rustls")]
pub(crate) mod rustls_support {
use crate::config::{TlsConfig, TransportConfig};
use crate::helper::host_port_pair;
use crate::transport::{AddrMaybeCached, SocketOpts, TcpTransport, Transport};
Expand All @@ -137,7 +144,8 @@ pub(crate) mod ruslts_support {
use async_trait::async_trait;
use p12::PFX;
use tokio_rustls::rustls::{ClientConfig, RootCertStore, ServerConfig};
use tokio_rustls::{TlsAcceptor, TlsConnector, TlsStream};
pub(crate) use tokio_rustls::TlsStream;
use tokio_rustls::{TlsAcceptor, TlsConnector};

pub struct TlsTransport {
tcp: TcpTransport,
Expand Down Expand Up @@ -274,7 +282,15 @@ pub(crate) mod ruslts_support {
))
}
}

pub(crate) fn get_tcpstream(s: &TlsStream<TcpStream>) -> &TcpStream {
&s.get_ref().0
}
}

#[cfg(feature = "rustls-support")]
pub(crate) use ruslts_support::TlsTransport;
#[cfg(feature = "websocket-rustls")]
pub(crate) use rustls_support::get_tcpstream;
#[cfg(feature = "websocket-rustls")]
pub(crate) use rustls_support::TlsStream;
#[cfg(feature = "rustls")]
pub(crate) use rustls_support::TlsTransport;
17 changes: 6 additions & 11 deletions src/transport/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@ use futures_sink::Sink;
use tokio::io::{AsyncBufRead, AsyncRead, AsyncWrite, ReadBuf};
use tokio::net::{TcpListener, TcpStream, ToSocketAddrs};

#[cfg(all(feature = "native-tls-support", feature = "rustls-support"))]
compile_error!("Only one of `native-tls-support` or `rustls-support` can be enabled");

#[cfg(feature = "native-tls-support")]
use tokio_native_tls::TlsStream;
#[cfg(feature = "rustls-support")]
use tokio_rustls::TlsStream;
#[cfg(any(feature = "native-tls", feature = "rustls"))]
use super::tls::get_tcpstream;
#[cfg(any(feature = "native-tls", feature = "rustls"))]
use super::tls::TlsStream;

use tokio_tungstenite::tungstenite::protocol::{Message, WebSocketConfig};
use tokio_tungstenite::{accept_async_with_config, client_async_with_config, WebSocketStream};
Expand All @@ -37,10 +34,8 @@ impl TransportStream {
fn get_tcpstream(&self) -> &TcpStream {
match self {
TransportStream::Insecure(s) => s,
#[cfg(feature = "native-tls-support")]
TransportStream::Secure(s) => s.get_ref().get_ref().get_ref(),
#[cfg(feature = "rustls-support")]
TransportStream::Secure(s) => s.get_ref().0,

TransportStream::Secure(s) => get_tcpstream(s),
}
}
}
Expand Down

0 comments on commit 81b63d1

Please sign in to comment.