Files
TrustTunnel/tools/setup_wizard/acme_http_server.rs
Andrey Yakushin 783908b315 Pull request 121: Clippy and github actions
Squashed commit of the following:

commit 6eae1e962a27b2c3bcb6362f53bb1d7d92a66983
Author: Andrey Yakushin <a.yakushin@adguard.com>
Date:   Fri Dec 26 11:46:12 2025 +0400

    Run lint on both macos and linux

commit 94254caec3ea166db80c6b3f4004b4126605a1b7
Author: Andrey Yakushin <a.yakushin@adguard.com>
Date:   Thu Dec 25 22:44:10 2025 +0400

    Fix note again by adding lint hint

commit 5a67ae358a5676a22e85798683674607d2788a51
Author: Andrey Yakushin <a.yakushin@adguard.com>
Date:   Thu Dec 25 22:42:02 2025 +0400

    Fix note

commit 937b178302244fe237d06b6f38ba0f29db6e0d7e
Author: Andrey Yakushin <a.yakushin@adguard.com>
Date:   Thu Dec 25 22:39:45 2025 +0400

    Fix README

commit 769c5d9ebdc03e8500f9fc00d7f2b6f316924557
Author: Andrey Yakushin <a.yakushin@adguard.com>
Date:   Thu Dec 25 22:39:15 2025 +0400

    Cargo update

commit 1e932e4037c2b9ffc4b12f398f1ef14c32b5481e
Merge: dcf6a53 2041edc
Author: Andrey Yakushin <a.yakushin@adguard.com>
Date:   Thu Dec 25 22:37:24 2025 +0400

    Merge remote-tracking branch 'origin/master' into feature/TRUST-235

commit dcf6a53410e59411a3e05f798ed4be7f7c9994ce
Author: Andrey Yakushin <a.yakushin@adguard.com>
Date:   Thu Dec 25 22:24:56 2025 +0400

    Get rid of rustls-pemfile and update sentry

commit cb2e26e47d4612d65ae990ec887875bb1ac94456
Author: Andrey Yakushin <a.yakushin@adguard.com>
Date:   Thu Dec 25 19:08:23 2025 +0400

    Fix tests

commit a3cde3fdf16edfe2e2a574b8d729c2b9d59daf84
Author: Andrey Yakushin <a.yakushin@adguard.com>
Date:   Thu Dec 25 18:33:46 2025 +0400

    Fix vulnerabilities

commit 35cb9c699a0ddf2eb344c7c475be3c36a26dbf83
Author: Andrey Yakushin <a.yakushin@adguard.com>
Date:   Thu Dec 25 17:13:18 2025 +0400

    Don't install cargo-audit manually

commit 71a5411ac4fe31fc08c3bacb83d327bf6b7ab8c3
Author: Andrey Yakushin <a.yakushin@adguard.com>
Date:   Thu Dec 25 17:07:30 2025 +0400

    Install stable rust for cargo-audit

commit b7f38a90054cda39d72760b0ebc3ce295fba95d2
Author: Andrey Yakushin <a.yakushin@adguard.com>
Date:   Thu Dec 25 16:54:13 2025 +0400

    Fix yaml

commit fbbe78f68b2987280874f23d4ed05ef75ed42f46
Author: Andrey Yakushin <a.yakushin@adguard.com>
Date:   Thu Dec 25 16:53:15 2025 +0400

    Try to lock cargo-audit version

commit 08f31734b49c70d9dc03c7977ac6182198d1cbde
Author: Andrey Yakushin <a.yakushin@adguard.com>
Date:   Thu Dec 25 16:46:06 2025 +0400

    Update audit workflow

commit c202f186cd1610439a13928fc1fabac88e83097b
Author: Andrey Yakushin <a.yakushin@adguard.com>
Date:   Thu Dec 25 16:30:55 2025 +0400

    Install rust tools and better rust cache

commit eccf2fa91efcc4c6e5684960e368892bc68e67cd
Author: Andrey Yakushin <a.yakushin@adguard.com>
Date:   Thu Dec 25 16:30:25 2025 +0400

    Name for job

commit dccc19f13180e767b8390c8ea32fde4285c0cab8
Author: Andrey Yakushin <a.yakushin@adguard.com>
Date:   Thu Dec 25 16:30:17 2025 +0400

    Update checkout step version

commit edbb4404bf6fc1927f0184433df9982767a9c762
Author: Andrey Yakushin <a.yakushin@adguard.com>
Date:   Thu Dec 25 16:30:02 2025 +0400

    Run lint only on linux

commit b59ed893fa55edf030f9ffee2e442c8b947fa28f
Author: Andrey Yakushin <a.yakushin@adguard.com>
Date:   Thu Dec 25 16:09:43 2025 +0400

    Lint in the same workflow as testing to avoid rebuilds

commit 8d8ecd51859c825d0437361f8c51bde6b46994bc
Author: Andrey Yakushin <a.yakushin@adguard.com>
Date:   Thu Dec 25 15:27:23 2025 +0400

    More clippy fixes

... and 6 more commits
2025-12-26 12:45:09 +03:00

121 lines
3.8 KiB
Rust

use crate::acme::AcmeError;
use http_body_util::Full;
use hyper::body::Bytes;
use hyper::server::conn::http1;
use hyper::service::service_fn;
use hyper::{Request, Response, StatusCode};
use hyper_util::rt::TokioIo;
use std::convert::Infallible;
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::net::TcpListener;
use tokio::sync::oneshot;
use tokio::task::JoinHandle;
const ACME_CHALLENGE_PATH_PREFIX: &str = "/.well-known/acme-challenge/";
const HTTP_PORT: u16 = 80;
struct ChallengeData {
token: String,
key_authorization: String,
}
async fn handle_request(
req: Request<hyper::body::Incoming>,
challenge_data: Arc<ChallengeData>,
) -> Result<Response<Full<Bytes>>, Infallible> {
let path = req.uri().path();
if let Some(request_token) = path.strip_prefix(ACME_CHALLENGE_PATH_PREFIX) {
if request_token == challenge_data.token {
println!(
" ✓ Serving ACME challenge response for token: {}",
request_token
);
return Ok(Response::builder()
.status(StatusCode::OK)
.header("Content-Type", "text/plain")
.body(Full::new(Bytes::from(
challenge_data.key_authorization.clone(),
)))
.unwrap());
} else {
println!(
" ✗ Token mismatch: expected {}, got {}",
challenge_data.token, request_token
);
}
}
Ok(Response::builder()
.status(StatusCode::NOT_FOUND)
.body(Full::new(Bytes::from("Not Found")))
.unwrap())
}
pub async fn run_http01_challenge_server(
token: String,
key_authorization: String,
) -> Result<(oneshot::Sender<()>, JoinHandle<()>), AcmeError> {
let addr = SocketAddr::from(([0, 0, 0, 0], HTTP_PORT));
// Check if port is available
let listener = TcpListener::bind(addr).await.map_err(|e| {
if e.kind() == std::io::ErrorKind::PermissionDenied {
AcmeError::PermissionDenied(HTTP_PORT)
} else {
AcmeError::PortInUse(HTTP_PORT)
}
})?;
println!(
" HTTP server listening on port {} for ACME challenge",
HTTP_PORT
);
let challenge_data = Arc::new(ChallengeData {
token,
key_authorization,
});
let (shutdown_tx, mut shutdown_rx) = oneshot::channel::<()>();
let handle = tokio::spawn(async move {
loop {
tokio::select! {
result = listener.accept() => {
match result {
Ok((stream, _addr)) => {
let io = TokioIo::new(stream);
let challenge_data = challenge_data.clone();
tokio::spawn(async move {
let service = service_fn(move |req| {
let challenge_data = challenge_data.clone();
async move { handle_request(req, challenge_data).await }
});
if let Err(e) = http1::Builder::new()
.serve_connection(io, service)
.await
{
eprintln!(" HTTP server error: {}", e);
}
});
}
Err(e) => {
eprintln!(" Failed to accept connection: {}", e);
}
}
}
_ = &mut shutdown_rx => {
println!(" HTTP server shutting down");
break;
}
}
}
});
Ok((shutdown_tx, handle))
}