[edge] Proxy: Pingora data plane with SNI certs, forward-auth, header rules, health checks, ACME

This commit is contained in:
2026-11-07 14:57:36 +01:00
parent 4fe041082d
commit 17e1df8320
24 changed files with 2316 additions and 123 deletions
+76
View File
@@ -0,0 +1,76 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! Active health checks, like Caddy's `health_checks.active`: `GET <path>` with the upstream
//! address as Host, healthy on 2xx, 5 s timeout, one result flips the state. The first round
//! runs at startup; upstreams start healthy.
use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::time::Duration;
use async_trait::async_trait;
use pingora::connectors::http::Connector;
use pingora::http::RequestHeader;
use pingora::server::ShutdownWatch;
use pingora::services::background::BackgroundService;
use crate::fetch::fetch;
use crate::runtime::{HostRt, Runtime, UpstreamRt};
const TIMEOUT: Duration = Duration::from_secs(5);
pub struct HealthChecks {
pub rt: Arc<Runtime>,
}
async fn probe(connector: &Connector, u: &UpstreamRt, path: &str) -> bool {
let Ok(mut req) = RequestHeader::build("GET", path.as_bytes(), None) else {
return false;
};
if req
.insert_header(http::header::HOST, u.authority.as_str())
.is_err()
{
return false;
}
match tokio::time::timeout(TIMEOUT, fetch(connector, u, req, 64 * 1024, TIMEOUT)).await {
Ok(Ok(f)) => f.header.status.is_success(),
_ => false,
}
}
async fn watch(host: Arc<HostRt>, path: String, every: Duration) {
let connector = Connector::new(None);
loop {
for u in &host.upstreams {
let ok = probe(&connector, u, &path).await;
if u.healthy.swap(ok, Ordering::Relaxed) != ok {
if ok {
log::info!(target: "ianitor::health", "{}: upstream {} is healthy again", host.cfg.hostname, u.authority);
} else {
log::warn!(target: "ianitor::health", "{}: upstream {} failed its health check", host.cfg.hostname, u.authority);
}
}
}
tokio::time::sleep(every).await;
}
}
#[async_trait]
impl BackgroundService for HealthChecks {
async fn start(&self, mut shutdown: ShutdownWatch) {
let mut tasks = Vec::new();
for host in self.rt.hosts.values() {
if let Some(hc) = &host.cfg.health_check {
let every = Duration::from_secs(u64::from(hc.interval_secs.max(1)));
tasks.push(tokio::spawn(watch(host.clone(), hc.path.clone(), every)));
}
}
let _ = shutdown.changed().await;
for t in tasks {
t.abort();
}
}
}