[edge] Proxy: Pingora data plane with SNI certs, forward-auth, header rules, health checks, ACME
This commit is contained in:
@@ -41,18 +41,25 @@ pub struct MockAuthelia {
|
||||
impl MockAuthelia {
|
||||
pub async fn start() -> MockAuthelia {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let m = MockAuthelia { addr: listener.local_addr().unwrap(), requests: Arc::default() };
|
||||
let m = MockAuthelia {
|
||||
addr: listener.local_addr().unwrap(),
|
||||
requests: Arc::default(),
|
||||
};
|
||||
let mm = m.clone();
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
let Ok((stream, _)) = listener.accept().await else { continue };
|
||||
let Ok((stream, _)) = listener.accept().await else {
|
||||
continue;
|
||||
};
|
||||
let mm = mm.clone();
|
||||
tokio::spawn(async move {
|
||||
let svc = service_fn(move |req| {
|
||||
let mm = mm.clone();
|
||||
async move { Ok::<_, Infallible>(mm.handle(req).await) }
|
||||
});
|
||||
let _ = http1::Builder::new().serve_connection(TokioIo::new(stream), svc).await;
|
||||
let _ = http1::Builder::new()
|
||||
.serve_connection(TokioIo::new(stream), svc)
|
||||
.await;
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -70,14 +77,28 @@ impl MockAuthelia {
|
||||
|
||||
async fn handle(&self, req: Request<Incoming>) -> Response<Full<Bytes>> {
|
||||
let (parts, body) = req.into_parts();
|
||||
let body = body.collect().await.map(|b| b.to_bytes()).unwrap_or_default();
|
||||
let body = body
|
||||
.collect()
|
||||
.await
|
||||
.map(|b| b.to_bytes())
|
||||
.unwrap_or_default();
|
||||
self.requests.lock().unwrap().push(Recorded {
|
||||
method: parts.method.to_string(),
|
||||
path: parts.uri.path().to_owned(),
|
||||
headers: parts.headers.iter().map(|(n, v)| (n.as_str().to_owned(), v.as_bytes().to_vec())).collect(),
|
||||
headers: parts
|
||||
.headers
|
||||
.iter()
|
||||
.map(|(n, v)| (n.as_str().to_owned(), v.as_bytes().to_vec()))
|
||||
.collect(),
|
||||
body: body.to_vec(),
|
||||
});
|
||||
let h = |name: &str| parts.headers.get(name).and_then(|v| v.to_str().ok()).map(str::to_owned);
|
||||
let h = |name: &str| {
|
||||
parts
|
||||
.headers
|
||||
.get(name)
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.map(str::to_owned)
|
||||
};
|
||||
let text = |status: u16, body: &str| {
|
||||
Response::builder()
|
||||
.status(status)
|
||||
@@ -89,9 +110,12 @@ impl MockAuthelia {
|
||||
if parts.method != hyper::Method::GET || parts.uri.path() != PATH {
|
||||
return text(404, "404 page not found");
|
||||
}
|
||||
let (Some(method), Some(proto), Some(host), Some(uri)) =
|
||||
(h("x-forwarded-method"), h("x-forwarded-proto"), h("x-forwarded-host"), h("x-forwarded-uri"))
|
||||
else {
|
||||
let (Some(method), Some(proto), Some(host), Some(uri)) = (
|
||||
h("x-forwarded-method"),
|
||||
h("x-forwarded-proto"),
|
||||
h("x-forwarded-host"),
|
||||
h("x-forwarded-uri"),
|
||||
) else {
|
||||
return text(400, "missing X-Forwarded headers");
|
||||
};
|
||||
|
||||
@@ -136,7 +160,9 @@ impl MockAuthelia {
|
||||
.header("location", location.clone())
|
||||
.header("set-cookie", "authelia_flow=1; Path=/; HttpOnly")
|
||||
.header("content-type", "text/html; charset=utf-8")
|
||||
.body(Full::new(Bytes::from(format!("<a href=\"{location}\">302 Found</a>"))))
|
||||
.body(Full::new(Bytes::from(format!(
|
||||
"<a href=\"{location}\">302 Found</a>"
|
||||
))))
|
||||
.unwrap()
|
||||
}
|
||||
_ => text(401, "401 Unauthorized"),
|
||||
|
||||
Reference in New Issue
Block a user