Load balancers

router.loadBalancers: services on several hosts (HAProxy) with HTTP
checks, TLS terminated on the router or passed through, the client in
X-Forwarded-For or the PROXY protocol, host names that choose a service
on a shared port and are published in the local DNS. A status page lists
the backends.
This commit is contained in:
2026-11-27 20:54:45 +01:00
parent 2aa70630d4
commit 553361f811
16 changed files with 1349 additions and 3 deletions
+94
View File
@@ -437,6 +437,99 @@ let
}
];
lbs = cfg.loadBalancers;
lbReserved = lib.foldl' (acc: l: acc // l.dhcp.reservations) { } lans;
lbBackendOk = b: validIp b || lib.hasAttr b lbReserved;
# A name the router's certificate covers (a wildcard covers one label).
acmeCovers =
h:
lib.any (
d:
d == h
|| (
lib.hasPrefix "*." d
&& lib.hasInfix "." h
&& lib.removePrefix "*" d == "." + lib.concatStringsSep "." (lib.tail (lib.splitString "." h))
)
) cfg.acme.domains;
lbPortsTaken = [ 22 ] ++ [ 53 ] ++ cfg.managementPorts;
lbDnatPorts = lib.concatMap (n: lib.filter lib.isInt n.ports) (
lib.filter (n: n.type == "dnat") cfg.firewall.nat
);
lbAssertions =
name: lb:
let
where = "router.loadBalancers.${name}";
sharing = lib.filter (n: n != name && lbs.${n}.listen.port == lb.listen.port) (lib.attrNames lbs);
in
[
{
assertion = lb.backends != [ ];
message = "${where}.backends: at least one backend.";
}
{
assertion = lib.all lbBackendOk lb.backends;
message = "${where}.backends: `${
lib.concatStringsSep ", " (lib.filter (b: !lbBackendOk b) lb.backends)
}` is neither a DHCP reservation nor an IPv4 address.";
}
{
assertion = lib.all (z: z == "wan" || lib.elem z zoneNames) lb.listen.zones && lb.listen.zones != [ ];
message = "${where}.listen.zones: unknown zone (zones: wan, ${lib.concatStringsSep ", " zoneNames}).";
}
{
assertion = lib.all dnsNameOk lb.hosts;
message = "${where}.hosts: a name is not a valid DNS name.";
}
{
assertion = lb.tls != "passthrough" || lb.protocol == "tcp";
message = "${where}: passthrough only forwards bytes: protocol must be \"tcp\".";
}
{
assertion = lb.hosts == [ ] || lb.tls != "none" || lb.protocol == "http";
message = "${where}.hosts: without TLS the host name is only visible in HTTP: use protocol \"http\", or leave hosts empty.";
}
{
assertion = lb.tls == "terminate" || lb.certificate == null;
message = "${where}.certificate: only a service that terminates TLS has one.";
}
{
assertion = lb.tls != "terminate" || lb.certificate != null || cfg.acme.enable;
message = "${where}: it terminates TLS but has no `certificate` and router.acme is not enabled.";
}
{
assertion =
lb.tls != "terminate" || lb.certificate != null || !cfg.acme.enable || lib.all acmeCovers lb.hosts;
message = "${where}.hosts: the router's certificate (router.acme.domains) does not cover ${
lib.concatStringsSep ", " (lib.filter (h: !acmeCovers h) lb.hosts)
}; add the name there or give the service its own `certificate`.";
}
{
assertion = lb.backendTls.enable || !lb.backendTls.verify;
message = "${where}.backendTls.verify needs backendTls.enable.";
}
{
assertion = !(lib.elem lb.listen.port lbPortsTaken);
message = "${where}.listen.port ${toString lb.listen.port} is used by the router itself (SSH, DNS, the web UI).";
}
{
assertion = !(lib.elem lb.listen.port lbDnatPorts);
message = "${where}.listen.port ${toString lb.listen.port} is also forwarded by router.firewall.nat: the forward would take the connections first.";
}
{
assertion = sharing == [ ] || (lb.hosts != [ ] && lib.all (n: lbs.${n}.hosts != [ ]) sharing);
message = "${where}: port ${toString lb.listen.port} is shared with ${lib.concatStringsSep ", " sharing}: every service on it needs `hosts`.";
}
{
assertion = lib.all (n: lbs.${n}.tls == lb.tls && lbs.${n}.protocol == lb.protocol) sharing;
message = "${where}: services that share port ${toString lb.listen.port} must agree on tls and protocol.";
}
{
assertion = lib.all (n: !(lib.any (h: lib.elem h lbs.${n}.hosts) lb.hosts)) sharing;
message = "${where}.hosts: another service on port ${toString lb.listen.port} has one of these names.";
}
];
overlapsLan = n: lib.any (l: ipv4.overlaps n l) lanNets;
wgAssertions =
@@ -597,6 +690,7 @@ in
]
++ lib.concatLists (lib.mapAttrsToList perLan lansByName)
++ dnsAssertions
++ lib.concatLists (lib.mapAttrsToList lbAssertions lbs)
++ lib.concatMap ruleAssertions cfg.firewall.rules
++ lib.concatMap natAssertions cfg.firewall.nat
++ [