[edge] Reload and share links: in-process config swap with self-reverting trials, admin socket with health and certificate status, share-link cookies, visitor pages and throttle

This commit is contained in:
2026-11-07 17:49:53 +01:00
parent c0ed9c65c5
commit 6ce1fe9d2b
18 changed files with 2137 additions and 186 deletions
+47 -9
View File
@@ -22,13 +22,17 @@ use crate::fetch;
use crate::forward_auth::{self, Outcome};
use crate::logging::{AccessRecord, now_secs, truncate_ip};
use crate::runtime::{HostRt, Op, Runtime};
use crate::share::{self, Decision};
use crate::shared::Shared;
pub struct EdgeProxy {
pub rt: Arc<Runtime>,
pub shared: Arc<Shared>,
pub connector: Connector,
}
pub struct Ctx {
/// The runtime current when the request started; used for the whole request.
rt: Option<Arc<Runtime>>,
host: Option<Arc<HostRt>>,
/// Chosen upstream and its resolved addresses; connect failures move to the next address.
upstream: Option<usize>,
@@ -113,6 +117,7 @@ impl ProxyHttp for EdgeProxy {
fn new_ctx(&self) -> Ctx {
Ctx {
rt: None,
host: None,
upstream: None,
addrs: Vec::new(),
@@ -123,15 +128,37 @@ impl ProxyHttp for EdgeProxy {
}
async fn request_filter(&self, session: &mut Session, ctx: &mut Ctx) -> Result<bool> {
let rt = self.shared.current();
ctx.rt = Some(rt.clone());
ctx.host_header = host_header(session.req_header());
let Some(host) = self.rt.host(&hostname(&ctx.host_header)).cloned() else {
let Some(host) = rt.host(&hostname(&ctx.host_header)).cloned() else {
respond_empty(session, 404).await?;
return Ok(true);
};
ctx.host = Some(host.clone());
let mut shared_access = false;
if host.cfg.share_links {
let ip = client_ip(session);
let revs = self.shared.revocations();
match share::handle(
session,
&host.cfg.hostname,
rt.share_key.as_deref(),
&revs,
&self.shared.throttle,
ip,
)
.await?
{
Decision::Answered => return Ok(true),
Decision::Allowed => shared_access = true,
Decision::Continue => {}
}
}
if host.cfg.forward_auth {
let fa = self.rt.forward_auth.as_ref().ok_or_else(|| {
let fa = rt.forward_auth.as_ref().ok_or_else(|| {
Error::explain(ErrorType::HTTPStatus(500), "forward-auth is not configured")
})?;
// Client-sent identity headers never survive, whatever copy_headers says.
@@ -139,6 +166,10 @@ impl ProxyHttp for EdgeProxy {
for name in &fa.strip_headers {
req.remove_header(name);
}
if shared_access {
// A valid share link stands in for the sign-in on its prefix.
return self.after_auth(session, &host).await;
}
let ip = client_ip(session).map(|i| i.to_string());
let outcome = match forward_auth::check(
&self.connector,
@@ -180,11 +211,7 @@ impl ProxyHttp for EdgeProxy {
}
}
if !host.cfg.websockets && is_websocket_upgrade(session.req_header()) {
respond_empty(session, 403).await?;
return Ok(true);
}
Ok(false)
self.after_auth(session, &host).await
}
async fn upstream_peer(&self, _session: &mut Session, ctx: &mut Ctx) -> Result<Box<HttpPeer>> {
@@ -350,7 +377,7 @@ impl ProxyHttp for EdgeProxy {
if !host.cfg.access_log {
return;
}
let Some(log) = &self.rt.access_log else {
let Some(log) = ctx.rt.as_ref().and_then(|rt| rt.access_log.as_ref()) else {
return;
};
let req = session.req_header();
@@ -372,6 +399,17 @@ impl ProxyHttp for EdgeProxy {
}
}
impl EdgeProxy {
/// The steps after the sign-in check: websocket refusal.
async fn after_auth(&self, session: &mut Session, host: &HostRt) -> Result<bool> {
if !host.cfg.websockets && is_websocket_upgrade(session.req_header()) {
respond_empty(session, 403).await?;
return Ok(true);
}
Ok(false)
}
}
pub fn contains(hay: &[u8], needle: &[u8]) -> bool {
!needle.is_empty() && hay.windows(needle.len()).any(|w| w == needle)
}