[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
+72 -16
View File
@@ -37,7 +37,12 @@ impl Req {
}
pub fn new(method: &str, host: &str, path: &str) -> Req {
Req { method: method.into(), path: path.into(), headers: vec![("host".into(), host.into())], body: Vec::new() }
Req {
method: method.into(),
path: path.into(),
headers: vec![("host".into(), host.into())],
body: Vec::new(),
}
}
pub fn header(mut self, name: &str, value: &str) -> Req {
@@ -61,11 +66,18 @@ pub struct Resp {
impl Resp {
pub fn header(&self, name: &str) -> Option<&str> {
self.headers.iter().find(|(n, _)| n.eq_ignore_ascii_case(name)).map(|(_, v)| v.as_str())
self.headers
.iter()
.find(|(n, _)| n.eq_ignore_ascii_case(name))
.map(|(_, v)| v.as_str())
}
pub fn header_all(&self, name: &str) -> Vec<&str> {
self.headers.iter().filter(|(n, _)| n.eq_ignore_ascii_case(name)).map(|(_, v)| v.as_str()).collect()
self.headers
.iter()
.filter(|(n, _)| n.eq_ignore_ascii_case(name))
.map(|(_, v)| v.as_str())
.collect()
}
pub fn text(&self) -> String {
@@ -81,7 +93,9 @@ fn other(e: impl std::fmt::Display) -> io::Error {
pub fn tls_connector(roots: Arc<RootCertStore>) -> TlsConnector {
crate::tls::install_crypto();
let mut cfg = rustls::ClientConfig::builder().with_root_certificates(roots).with_no_client_auth();
let mut cfg = rustls::ClientConfig::builder()
.with_root_certificates(roots)
.with_no_client_auth();
cfg.alpn_protocols = vec![b"http/1.1".to_vec()];
TlsConnector::from(Arc::new(cfg))
}
@@ -96,29 +110,58 @@ async fn tls_stream(
tls_connector(roots).connect(name, tcp).await
}
async fn send<S: AsyncRead + AsyncWrite + Unpin + Send + 'static>(io: S, req: &Req) -> io::Result<Resp> {
let (mut sender, conn) = hyper::client::conn::http1::handshake(TokioIo::new(io)).await.map_err(other)?;
async fn send<S: AsyncRead + AsyncWrite + Unpin + Send + 'static>(
io: S,
req: &Req,
) -> io::Result<Resp> {
let (mut sender, conn) = hyper::client::conn::http1::handshake(TokioIo::new(io))
.await
.map_err(other)?;
tokio::spawn(async move {
let _ = conn.await;
});
let mut b = hyper::Request::builder().method(req.method.as_str()).uri(req.path.as_str());
let mut b = hyper::Request::builder()
.method(req.method.as_str())
.uri(req.path.as_str());
for (n, v) in &req.headers {
b = b.header(n.as_str(), v.as_str());
}
let request = b.body(Full::new(Bytes::from(req.body.clone()))).map_err(other)?;
let request = b
.body(Full::new(Bytes::from(req.body.clone())))
.map_err(other)?;
let resp = sender.send_request(request).await.map_err(other)?;
let status = resp.status().as_u16();
let headers = resp
.headers()
.iter()
.map(|(n, v)| (n.as_str().to_owned(), String::from_utf8_lossy(v.as_bytes()).into_owned()))
.map(|(n, v)| {
(
n.as_str().to_owned(),
String::from_utf8_lossy(v.as_bytes()).into_owned(),
)
})
.collect();
let body = resp.into_body().collect().await.map_err(other)?.to_bytes().to_vec();
Ok(Resp { status, headers, body })
let body = resp
.into_body()
.collect()
.await
.map_err(other)?
.to_bytes()
.to_vec();
Ok(Resp {
status,
headers,
body,
})
}
/// One request over a fresh TLS connection to `addr` with the given SNI.
pub async fn https(addr: SocketAddr, sni: &str, roots: Arc<RootCertStore>, req: &Req) -> io::Result<Resp> {
pub async fn https(
addr: SocketAddr,
sni: &str,
roots: Arc<RootCertStore>,
req: &Req,
) -> io::Result<Resp> {
tokio::time::timeout(TIMEOUT, async {
let s = tls_stream(addr, sni, roots).await?;
send(s, req).await
@@ -157,7 +200,9 @@ pub async fn websocket(
) -> io::Result<WsOutcome> {
tokio::time::timeout(TIMEOUT, async {
let s = tls_stream(addr, sni, roots).await?;
let mut request = format!("wss://{host}{path}").into_client_request().map_err(other)?;
let mut request = format!("wss://{host}{path}")
.into_client_request()
.map_err(other)?;
for (n, v) in extra {
request.headers_mut().insert(
http::HeaderName::from_bytes(n.as_bytes()).map_err(other)?,
@@ -167,7 +212,9 @@ pub async fn websocket(
match tokio_tungstenite::client_async(request, s).await {
Ok((mut ws, resp)) => {
let status = resp.status().as_u16();
ws.send(tungstenite::Message::text(message)).await.map_err(other)?;
ws.send(tungstenite::Message::text(message))
.await
.map_err(other)?;
let echo = match ws.next().await {
Some(Ok(m)) => m.into_text().map_err(other)?.to_string(),
other_msg => return Err(other(format!("no echo: {other_msg:?}"))),
@@ -180,10 +227,19 @@ pub async fn websocket(
let headers = resp
.headers()
.iter()
.map(|(n, v)| (n.as_str().to_owned(), String::from_utf8_lossy(v.as_bytes()).into_owned()))
.map(|(n, v)| {
(
n.as_str().to_owned(),
String::from_utf8_lossy(v.as_bytes()).into_owned(),
)
})
.collect();
let body = resp.body().clone().unwrap_or_default();
Ok(WsOutcome::Refused(Resp { status, headers, body }))
Ok(WsOutcome::Refused(Resp {
status,
headers,
body,
}))
}
Err(e) => Err(other(e)),
}