Skip to content

Commit fd26308

Browse files
committed
refactor: allow using either openssl/nativetls or rustls
1 parent fd7df21 commit fd26308

File tree

15 files changed

+363
-144
lines changed

15 files changed

+363
-144
lines changed

.github/workflows/ci.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ jobs:
6969
- name: Build | Check
7070
run: cargo check --all
7171

72+
- name: Setup | binstall
73+
run: |
74+
curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash
75+
76+
- name: Setup | check-all-features
77+
run: |
78+
cargo binstall -y cargo-all-features
79+
80+
- name: Build | Check all features
81+
run: |
82+
cargo check-all-features
83+
84+
7285
test:
7386
needs: check # Ensure check is run first.
7487
strategy:

Cargo.lock

Lines changed: 120 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ ansi_term = "0.12"
2424
anyhow = "1"
2525
async-recursion = "1.0.5"
2626
axum = { version = "0.6", features = ["ws"] }
27-
axum-server = { version = "0.5.1", features = ["tls-rustls"] }
27+
axum-server = "0.5.1"
2828
base64 = "0.21"
2929
bytes = "1"
3030
cargo-lock = "9"
3131
cargo_metadata = "0.18.1"
32-
crates_io_api = { version = "0.9", default-features = false, features = ["rustls"] }
32+
crates_io_api = { version = "0.9", default-features = false }
3333
clap = { version = "4", features = ["derive", "env"] }
3434
console = "0.15"
3535
directories = "5"
@@ -54,11 +54,7 @@ open = "5"
5454
oxipng = "9"
5555
parking_lot = "0.12"
5656
remove_dir_all = "0.8"
57-
reqwest = { version = "0.11", default-features = false, features = [
58-
"rustls-tls-native-roots",
59-
"stream",
60-
"trust-dns",
61-
] }
57+
reqwest = { version = "0.11", default-features = false, features = ["stream", "trust-dns"] }
6258
sha2 = "0.10"
6359
seahash = { version = "4", features = ["use_std"] }
6460
semver = "1"
@@ -70,7 +66,7 @@ time = { version = "0.3", features = ["serde-well-known"] }
7066
thiserror = "1"
7167
tokio = { version = "1", default-features = false, features = ["full"] }
7268
tokio-stream = { version = "0.1", default-features = false, features = ["fs", "sync"] }
73-
tokio-tungstenite = { version = "0.20", features = ["rustls", "rustls-tls-native-roots"] }
69+
tokio-tungstenite = "0.20"
7470
toml = "0.8"
7571
tower-http = { version = "0.4", features = ["fs", "trace", "set-header"] }
7672
tracing = "0.1"
@@ -85,7 +81,20 @@ lightningcss = "=1.0.0-alpha.54"
8581
tempfile = "3"
8682

8783
[features]
88-
default = ["update_check"]
84+
default = ["update_check", "rustls"]
85+
rustls = [
86+
"axum-server/tls-rustls",
87+
"crates_io_api/rustls",
88+
"reqwest/rustls",
89+
"reqwest/rustls-tls-native-roots",
90+
"tokio-tungstenite/rustls",
91+
"tokio-tungstenite/rustls-tls-native-roots",
92+
]
93+
native-tls = [
94+
"axum-server/tls-openssl",
95+
"reqwest/native-tls",
96+
"tokio-tungstenite/native-tls",
97+
]
8998

9099
# enable the update check on startup
91100
update_check = []

src/common.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub static SERVER: Emoji = Emoji("📡 ", "");
2020
pub static LOCAL: Emoji = Emoji("🏠 ", "");
2121
pub static NETWORK: Emoji = Emoji("💻 ", "");
2222
pub static STARTING: Emoji = Emoji("🚀 ", "");
23+
#[cfg(feature = "update_check")]
2324
pub static UPDATE: Emoji = Emoji("⏫ ", "");
2425

2526
static CWD: Lazy<PathBuf> =

src/config/rt/serve.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ use crate::config::{
22
models::AddressFamily, BaseUrl, ConfigOptsBuild, ConfigOptsCore, ConfigOptsHook,
33
ConfigOptsProxy, ConfigOptsServe, ConfigOptsTools, ConfigOptsWatch, WsProtocol,
44
};
5-
use anyhow::{anyhow, ensure, Context, Result};
5+
use anyhow::{anyhow, bail, ensure, Context, Result};
66
use axum::http::Uri;
7-
use axum_server::tls_rustls::RustlsConfig;
87
use local_ip_address::list_afinet_netifas;
98
use std::borrow::Cow;
109
use std::collections::HashMap;
@@ -13,6 +12,8 @@ use std::path::PathBuf;
1312
use std::sync::Arc;
1413
use tracing::log;
1514

15+
use crate::tls::TlsConfig;
16+
1617
/// Runtime config for the serve system.
1718
#[derive(Clone, Debug)]
1819
pub struct RtcServe {
@@ -46,8 +47,8 @@ pub struct RtcServe {
4647
pub ws_protocol: Option<WsProtocol>,
4748
/// Path used for autoreload WebSockets connection.
4849
pub ws_base: Option<String>,
49-
/// The tls config containing the certificate and private key. TLS is activated if both are set.
50-
pub tls: Option<RustlsConfig>,
50+
/// The TLS config containing the certificate and private key. TLS is activated if both are set.
51+
pub tls: Option<TlsConfig>,
5152
/// A base path to serve the application from
5253
pub serve_base: Option<String>,
5354
}
@@ -191,18 +192,32 @@ fn build_address_list(preference: Option<AddressFamily>, addresses: Vec<IpAddr>)
191192
}
192193
}
193194

195+
#[allow(unreachable_code)]
194196
async fn tls_config(
195197
tls_key_path: Option<PathBuf>,
196198
tls_cert_path: Option<PathBuf>,
197-
) -> anyhow::Result<Option<RustlsConfig>, anyhow::Error> {
199+
) -> Result<Option<TlsConfig>, anyhow::Error> {
198200
match (tls_key_path, tls_cert_path) {
199201
(Some(tls_key_path), Some(tls_cert_path)) => {
200202
tracing::info!("🔐 Private key {}", tls_key_path.display(),);
201203
tracing::info!("🔒 Public key {}", tls_cert_path.display());
202-
let tls_config = RustlsConfig::from_pem_file(tls_cert_path, tls_key_path)
203-
.await
204-
.with_context(|| "loading TLS cert/key failed")?;
205-
Ok(Some(tls_config))
204+
205+
#[cfg(feature = "rustls")]
206+
return Ok(Some(
207+
axum_server::tls_rustls::RustlsConfig::from_pem_file(tls_cert_path, tls_key_path)
208+
.await
209+
.with_context(|| "loading TLS cert/key failed")?
210+
.into(),
211+
));
212+
213+
#[cfg(feature = "native-tls")]
214+
return Ok(Some(
215+
axum_server::tls_openssl::OpenSSLConfig::from_pem_file(tls_cert_path, tls_key_path)
216+
.with_context(|| "loading TLS cert/key failed")?
217+
.into(),
218+
));
219+
220+
bail!("TLS configuration was requested, but no TLS provider was enabled during compilation")
206221
}
207222
(None, Some(_)) => Err(anyhow!("TLS cert path provided without key path")),
208223
(Some(_), None) => Err(anyhow!("TLS key path provided without cert path")),

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod pipelines;
99
mod processing;
1010
mod proxy;
1111
mod serve;
12+
mod tls;
1213
mod tools;
1314
mod version;
1415
mod watch;

0 commit comments

Comments
 (0)