2024-10-20 18:52:05 +02:00
|
|
|
{
|
|
|
|
pkgs,
|
|
|
|
lib,
|
|
|
|
config,
|
|
|
|
nodes ? { },
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
let
|
2024-12-15 17:22:49 +01:00
|
|
|
inherit (lib)
|
|
|
|
mkOption
|
|
|
|
mkEnableOption
|
|
|
|
mkIf
|
|
|
|
mapAttrs
|
|
|
|
concatStringsSep
|
|
|
|
fold
|
|
|
|
;
|
|
|
|
inherit (lib.types)
|
|
|
|
port
|
|
|
|
listOf
|
|
|
|
submodule
|
|
|
|
coercedTo
|
|
|
|
str
|
|
|
|
raw
|
|
|
|
;
|
2024-10-20 18:52:05 +02:00
|
|
|
redirected-ports-mod.options = {
|
|
|
|
external = mkOption {
|
2024-12-15 17:22:49 +01:00
|
|
|
type = port;
|
2024-10-20 18:52:05 +02:00
|
|
|
};
|
|
|
|
internal = mkOption {
|
2024-12-15 17:22:49 +01:00
|
|
|
type = port;
|
2024-10-20 18:52:05 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-12-15 17:22:49 +01:00
|
|
|
redirection-port-type = coercedTo port (port: {
|
2024-10-20 18:52:05 +02:00
|
|
|
external = port;
|
|
|
|
internal = port;
|
2024-12-15 17:22:49 +01:00
|
|
|
}) (submodule redirected-ports-mod);
|
2024-10-20 18:52:05 +02:00
|
|
|
|
2024-11-14 15:30:17 +01:00
|
|
|
inherit (config.kat) fqdn;
|
2024-10-20 18:52:05 +02:00
|
|
|
hostname = config.networking.hostName;
|
|
|
|
cfg = config.kat.proxies;
|
|
|
|
|
|
|
|
error-webroot = pkgs.runCommand "${hostname}-error-webroot" { } ''
|
|
|
|
mkdir $out
|
|
|
|
cp ${./418.html} $out/418.html
|
|
|
|
cp ${./599.html} $out/599.html
|
|
|
|
substituteInPlace $out/* \
|
|
|
|
--replace-fail 'config.hostname' "${hostname}"
|
|
|
|
'';
|
|
|
|
|
|
|
|
redirections =
|
2024-12-15 17:22:49 +01:00
|
|
|
fold
|
2024-10-20 18:52:05 +02:00
|
|
|
(a: b: {
|
|
|
|
domains = a.domains ++ b.domains;
|
|
|
|
tcp = a.tcp ++ b.tcp;
|
|
|
|
udp = a.udp ++ b.udp;
|
|
|
|
vhosts = a.vhosts // b.vhosts;
|
|
|
|
})
|
|
|
|
{
|
|
|
|
domains = [ ];
|
|
|
|
tcp = [ ];
|
|
|
|
udp = [ ];
|
|
|
|
vhosts = { };
|
|
|
|
}
|
|
|
|
(
|
|
|
|
map (
|
|
|
|
host:
|
|
|
|
let
|
2024-11-14 15:30:17 +01:00
|
|
|
inherit (nodes.${host}.config.kat) fqdn;
|
2024-10-20 18:52:05 +02:00
|
|
|
host-cfg = nodes.${host}.config.kat.proxies;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
domains = [ fqdn ] ++ host-cfg.aliases;
|
|
|
|
tcp = map (
|
|
|
|
{ external, internal }:
|
|
|
|
{
|
|
|
|
input = external;
|
2024-11-14 15:30:17 +01:00
|
|
|
inherit (host-cfg) ip;
|
2024-10-20 18:52:05 +02:00
|
|
|
out = internal;
|
|
|
|
}
|
|
|
|
) host-cfg.open-tcp;
|
|
|
|
udp = map (
|
|
|
|
{ external, internal }:
|
|
|
|
{
|
|
|
|
input = external;
|
2024-11-14 15:30:17 +01:00
|
|
|
inherit (host-cfg) ip;
|
2024-10-20 18:52:05 +02:00
|
|
|
out = internal;
|
|
|
|
}
|
|
|
|
) host-cfg.open-udp;
|
|
|
|
vhosts.${fqdn} = {
|
2024-11-14 15:30:17 +01:00
|
|
|
inherit (host-cfg) ip aliases;
|
2024-10-20 18:52:05 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
) cfg.redirects
|
|
|
|
);
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.kat.proxies = {
|
|
|
|
enable = mkEnableOption "kat-proxies autoconfiguration" // {
|
|
|
|
default = cfg.redirects != [ ];
|
|
|
|
defaultText = ''config.kat.proxies.redirects != [ ]'';
|
|
|
|
};
|
|
|
|
|
|
|
|
ip = mkOption {
|
2024-12-15 17:22:49 +01:00
|
|
|
type = str;
|
2024-10-20 18:52:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
aliases = mkOption {
|
2024-12-15 17:22:49 +01:00
|
|
|
type = listOf str;
|
2024-10-20 18:52:05 +02:00
|
|
|
default = [ ];
|
|
|
|
};
|
|
|
|
open-tcp = mkOption {
|
2024-12-15 17:22:49 +01:00
|
|
|
type = listOf redirection-port-type;
|
2024-10-20 18:52:05 +02:00
|
|
|
default = [ ];
|
|
|
|
};
|
|
|
|
open-udp = mkOption {
|
2024-12-15 17:22:49 +01:00
|
|
|
type = listOf redirection-port-type;
|
2024-10-20 18:52:05 +02:00
|
|
|
default = [ ];
|
|
|
|
};
|
|
|
|
|
|
|
|
redirects = mkOption {
|
2024-12-15 17:22:49 +01:00
|
|
|
type = listOf str;
|
2024-10-20 18:52:05 +02:00
|
|
|
default = [ ];
|
|
|
|
};
|
|
|
|
|
|
|
|
test = mkOption {
|
2024-12-15 17:22:49 +01:00
|
|
|
type = raw;
|
2024-10-20 18:52:05 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
kat.proxies = {
|
|
|
|
test = redirections;
|
|
|
|
aliases = redirections.domains;
|
|
|
|
open-tcp = map ({ input, ... }: input) redirections.tcp;
|
|
|
|
open-udp = map ({ input, ... }: input) redirections.udp;
|
|
|
|
};
|
|
|
|
|
|
|
|
networking.firewall = {
|
|
|
|
allowedTCPPorts = [
|
|
|
|
80
|
|
|
|
443
|
|
|
|
] ++ map ({ internal, ... }: internal) cfg.open-tcp;
|
|
|
|
allowedUDPPorts = map ({ internal, ... }: internal) cfg.open-udp;
|
|
|
|
};
|
|
|
|
|
|
|
|
security.acme.certs.${fqdn}.extraDomainNames = cfg.aliases;
|
|
|
|
|
|
|
|
services.nginx = {
|
|
|
|
enable = true;
|
|
|
|
virtualHosts =
|
|
|
|
mapAttrs (
|
|
|
|
_:
|
|
|
|
{ aliases, ip }:
|
|
|
|
{
|
|
|
|
useACMEHost = fqdn;
|
|
|
|
forceSSL = true;
|
|
|
|
acmeFallbackHost = ip;
|
|
|
|
acmeFallbackRecommendedProxySettings = true;
|
|
|
|
serverAliases = aliases;
|
|
|
|
locations = {
|
|
|
|
"/.${hostname}/" = {
|
|
|
|
extraConfig = ''
|
|
|
|
internal;
|
|
|
|
error_page 404 =418 /.${hostname}/418.html;
|
|
|
|
'';
|
|
|
|
alias = "${error-webroot}/";
|
|
|
|
};
|
|
|
|
"/" = {
|
|
|
|
recommendedProxySettings = true;
|
|
|
|
proxyPass = "https://${ip}/";
|
|
|
|
extraConfig = ''
|
|
|
|
proxy_set_header Connection ''';
|
|
|
|
proxy_http_version 1.1;
|
|
|
|
chunked_transfer_encoding off;
|
|
|
|
proxy_buffering off;
|
|
|
|
proxy_cache off;
|
|
|
|
error_page 502 =599 "/.${hostname}/599.html";
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
) redirections.vhosts
|
|
|
|
// {
|
|
|
|
${fqdn} = {
|
|
|
|
default = true;
|
|
|
|
enableACME = true;
|
|
|
|
addSSL = true;
|
|
|
|
locations = {
|
|
|
|
"/.${hostname}/" = {
|
|
|
|
extraConfig = ''
|
|
|
|
internal;
|
|
|
|
error_page 404 =418 /.${hostname}/418.html;
|
|
|
|
'';
|
|
|
|
alias = "${error-webroot}/";
|
|
|
|
};
|
|
|
|
"/" = {
|
|
|
|
extraConfig = ''
|
|
|
|
return 418;
|
|
|
|
error_page 418 =418 /.${hostname}/418.html;
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
streamConfig = concatStringsSep "\n" (
|
|
|
|
map (
|
|
|
|
{
|
|
|
|
input,
|
|
|
|
ip,
|
|
|
|
out,
|
|
|
|
}:
|
|
|
|
''
|
|
|
|
server {
|
|
|
|
listen ${toString input};
|
|
|
|
listen [::]:${toString input};
|
|
|
|
proxy_pass ${ip}:${toString out};
|
|
|
|
}
|
|
|
|
''
|
|
|
|
) redirections.tcp
|
|
|
|
++ map (
|
|
|
|
{
|
|
|
|
input,
|
|
|
|
ip,
|
|
|
|
out,
|
|
|
|
}:
|
|
|
|
''
|
|
|
|
server {
|
|
|
|
listen ${toString input} udp;
|
|
|
|
listen [::]:${toString input} udp;
|
|
|
|
proxy_pass ${ip}:${toString out};
|
|
|
|
}
|
|
|
|
''
|
|
|
|
) redirections.udp
|
|
|
|
);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|