Skip to content

Commit

Permalink
feat: support server cert auto hot-reload
Browse files Browse the repository at this point in the history
  • Loading branch information
Itsusinn committed Dec 18, 2024
1 parent 01bfe6c commit e479918
Show file tree
Hide file tree
Showing 7 changed files with 287 additions and 58 deletions.
161 changes: 153 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Compared to origin, this fork's new features:
- More relaxed locks
- More CI targets via [cross-rs](https://github.com/cross-rs/cross)
- Self-signed cert and `skip_cert_verify` support
- ServerCert auto hot-reload
- And [more...](https://github.com/EAimTY/tuic/compare/dev...Itsusinn:tuic:dev)

## Introduction
Expand Down
2 changes: 2 additions & 0 deletions tuic-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ socket2 = { version = "0.5", default-features = false }
arc-swap = "1"
uuid = { version = "1", default-features = false, features = ["serde", "std", "v4"] }
chashmap = { package = "chashmap-async", version = "0.1" }
notify = "7"


# QUIC
quinn = { version = "0.11", default-features = false, features = ["runtime-tokio", "log"] }
Expand Down
3 changes: 2 additions & 1 deletion tuic-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod error;
mod old_config;
mod restful;
mod server;
mod tls;
mod utils;

#[cfg(all(not(target_env = "msvc"), feature = "jemallocator"))]
Expand Down Expand Up @@ -75,7 +76,7 @@ async fn main() -> eyre::Result<()> {
)
.try_init()?;
tokio::spawn(async move {
match Server::init(ctx.clone()) {
match Server::init(ctx.clone()).await {
Ok(server) => server.start().await,
Err(err) => {
eprintln!("{err}");
Expand Down
12 changes: 7 additions & 5 deletions tuic-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ use crate::{
AppContext,
connection::{Connection, INIT_CONCURRENT_STREAMS},
error::Error,
utils::{self, CongestionController},
tls::CertResolver,
utils::CongestionController,
};

pub struct Server {
Expand All @@ -29,7 +30,7 @@ pub struct Server {
}

impl Server {
pub fn init(ctx: Arc<AppContext>) -> Result<Self, Error> {
pub async fn init(ctx: Arc<AppContext>) -> Result<Self, Error> {
let mut crypto: RustlsServerConfig;
if ctx.cfg.tls.self_sign {
let cert = rcgen::generate_simple_self_signed(vec!["localhost".into()]).unwrap();
Expand All @@ -39,11 +40,12 @@ impl Server {
.with_no_client_auth()
.with_single_cert(vec![cert_der], PrivateKeyDer::Pkcs8(priv_key))?;
} else {
let certs = utils::load_cert_chain(&ctx.cfg.tls.certificate)?;
let priv_key = utils::load_priv_key(&ctx.cfg.tls.private_key)?;
let cert_resolver =
CertResolver::new(&ctx.cfg.tls.certificate, &ctx.cfg.tls.private_key).await?;

crypto = RustlsServerConfig::builder_with_protocol_versions(&[&rustls::version::TLS13])
.with_no_client_auth()
.with_single_cert(certs, priv_key)?;
.with_cert_resolver(cert_resolver);
}

crypto.alpn_protocols = ctx
Expand Down
Loading

0 comments on commit e479918

Please sign in to comment.