[core] Proxy socket: compile option proxy_socket gives both engines a 0660 Unix socket for the front proxy (edge in its config, Caddy a third server with trusted_proxies_unix); listeners change only with a restart
This commit is contained in:
@@ -19,6 +19,7 @@ use crate::{
|
||||
const ACCESS_LOGGER: &str = "ianitor_access";
|
||||
const HTTPS_SERVER: &str = "ianitor_https";
|
||||
const HTTP_SERVER: &str = "ianitor_http";
|
||||
const PROXY_SERVER: &str = "ianitor_proxy";
|
||||
|
||||
pub(super) fn compile(model: &Model, o: &CompileOptions) -> Value {
|
||||
let hosts = model.hosts();
|
||||
@@ -77,6 +78,23 @@ pub(super) fn compile(model: &Model, o: &CompileOptions) -> Value {
|
||||
.collect();
|
||||
policies.push(json!({}));
|
||||
|
||||
// ---- The front proxy's socket: the same routes in plain HTTP, its peer trusted (edge:
|
||||
// `client_ip` on the socket). No TLS here, so no certificates are managed for it.
|
||||
let mut proxy = o.proxy_socket.as_ref().map(|sock| {
|
||||
let mut p = Map::new();
|
||||
p.insert(
|
||||
"listen".into(),
|
||||
json!([format!("unix//{}|0660", sock.trim_start_matches('/'))]),
|
||||
);
|
||||
p.insert("routes".into(), Value::Array(routes.clone()));
|
||||
p.insert("automatic_https".into(), json!({ "disable": true }));
|
||||
p.insert("protocols".into(), json!(["h1"]));
|
||||
p.insert("trusted_proxies_unix".into(), json!(true));
|
||||
p.insert("client_ip_headers".into(), json!(["X-Forwarded-For"]));
|
||||
p.insert("trusted_proxies_strict".into(), json!(1));
|
||||
p
|
||||
});
|
||||
|
||||
let mut https = Map::new();
|
||||
https.insert("listen".into(), json!([o.https_listen.to_string()]));
|
||||
https.insert("routes".into(), Value::Array(routes));
|
||||
@@ -106,10 +124,11 @@ pub(super) fn compile(model: &Model, o: &CompileOptions) -> Value {
|
||||
.iter()
|
||||
.map(|h| (h.hostname.to_string(), json!([ACCESS_LOGGER])))
|
||||
.collect();
|
||||
https.insert(
|
||||
"logs".into(),
|
||||
json!({ "logger_names": names, "skip_unmapped_hosts": true }),
|
||||
);
|
||||
let logs = json!({ "logger_names": names, "skip_unmapped_hosts": true });
|
||||
if let Some(p) = proxy.as_mut() {
|
||||
p.insert("logs".into(), logs.clone());
|
||||
}
|
||||
https.insert("logs".into(), logs);
|
||||
}
|
||||
|
||||
// ---- HTTP server: redirect known hosts to HTTPS (ACME HTTP-01 is answered by Caddy itself).
|
||||
@@ -142,6 +161,9 @@ pub(super) fn compile(model: &Model, o: &CompileOptions) -> Value {
|
||||
let mut servers = Map::new();
|
||||
servers.insert(HTTPS_SERVER.into(), Value::Object(https));
|
||||
servers.insert(HTTP_SERVER.into(), http);
|
||||
if let Some(p) = proxy {
|
||||
servers.insert(PROXY_SERVER.into(), Value::Object(p));
|
||||
}
|
||||
|
||||
let mut tls = Map::new();
|
||||
if !domi.is_empty() {
|
||||
|
||||
@@ -35,6 +35,7 @@ pub(super) fn compile(model: &Model, o: &CompileOptions) -> EdgeConfig {
|
||||
allow: join(&o.cert_dir, DEVICE_ALLOW_FILE),
|
||||
}),
|
||||
trusted_proxies: o.trusted_proxies.iter().map(ToString::to_string).collect(),
|
||||
proxy_socket: o.proxy_socket.clone(),
|
||||
forward_auth: site.forward_auth.as_ref().map(|fa| EdgeForwardAuth {
|
||||
upstream: upstream(&fa.upstream),
|
||||
path_and_query: fa.path_and_query.clone(),
|
||||
|
||||
@@ -48,6 +48,10 @@ pub struct CompileOptions {
|
||||
/// Ianitor on loopback): the share-link throttle and the access log then see the real
|
||||
/// client. Empty (the default): only the connection's own address counts.
|
||||
pub trusted_proxies: Vec<std::net::IpAddr>,
|
||||
/// A Unix socket (mode 0660) on which the engine also serves the HTTPS hosts, in plain HTTP,
|
||||
/// to a front proxy that terminated TLS (Caddy beside Ianitor). Only the socket's owner and
|
||||
/// group can connect, so its peer is trusted and its `X-Forwarded-For` names the client.
|
||||
pub proxy_socket: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
||||
@@ -80,6 +84,19 @@ pub fn caddy(model: &Model, options: &CompileOptions) -> Result<String, CompileE
|
||||
}
|
||||
|
||||
fn check_options(o: &CompileOptions) -> Result<(), CompileError> {
|
||||
if let Some(s) = &o.proxy_socket {
|
||||
check_path(s).map_err(|reason| CompileError::BadOption {
|
||||
option: "proxy_socket",
|
||||
reason,
|
||||
})?;
|
||||
// sun_path holds 108 bytes on Linux, the terminating NUL included.
|
||||
if s.len() > 100 {
|
||||
return Err(CompileError::BadOption {
|
||||
option: "proxy_socket",
|
||||
reason: "a socket path is at most 100 characters",
|
||||
});
|
||||
}
|
||||
}
|
||||
if let Some(s) = &o.caddy_admin_socket {
|
||||
check_path(s).map_err(|reason| CompileError::BadOption {
|
||||
option: "caddy_admin_socket",
|
||||
|
||||
Reference in New Issue
Block a user