nix-config/hosts/public-access-proxy/proxy.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

134 lines
4.2 KiB
Nix
Raw Normal View History

2022-12-04 08:53:28 +01:00
{ config, lib, ... }:
2022-12-04 08:53:28 +01:00
let
cfg = config.services.proxy;
canonicalize = builtins.replaceStrings [ "*" "." ":" "[" "]" ] [ "all" "_" "_" "" "" ];
in
{
2022-06-20 20:17:13 +02:00
options.services.proxy = {
2022-12-04 08:53:28 +01:00
enable = lib.mkOption {
default = false;
description = "whether to enable proxy";
2022-12-04 08:53:28 +01:00
type = lib.types.bool;
};
2022-12-04 08:53:28 +01:00
proxyHosts = lib.mkOption {
type = lib.types.listOf (lib.types.submodule {
2021-02-22 11:45:12 +01:00
options = {
2022-12-04 08:53:28 +01:00
hostNames = lib.mkOption {
type = with lib.types; listOf str;
2021-02-22 11:45:12 +01:00
default = [ ];
description = ''
Proxy these hostNames.
'';
};
2023-01-18 01:52:47 +01:00
2022-12-04 08:53:28 +01:00
proxyTo = lib.mkOption {
type = lib.types.submodule {
2021-02-22 11:45:12 +01:00
options = {
2022-12-04 08:53:28 +01:00
host = lib.mkOption {
type = with lib.types; nullOr string;
2021-02-22 11:45:12 +01:00
default = null;
description = ''
Host to forward traffic to.
Any hostname may only be used once
'';
};
2022-12-04 08:53:28 +01:00
httpPort = lib.mkOption {
2023-03-10 20:25:47 +01:00
type = lib.types.port;
2021-02-22 11:45:12 +01:00
default = 80;
description = ''
Port to forward http to.
'';
};
2022-12-04 08:53:28 +01:00
httpsPort = lib.mkOption {
2023-03-10 20:25:47 +01:00
type = lib.types.port;
2021-02-22 11:45:12 +01:00
default = 443;
description = ''
Port to forward http to.
'';
};
};
2021-10-31 19:00:03 +01:00
};
2021-02-22 11:45:12 +01:00
description = ''
{ host = /* ip or fqdn */; httpPort = 80; httpsPort = 443; } to proxy to
'';
default = { };
};
2023-01-18 01:52:47 +01:00
proxyProtocol = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Whether to use proxy protocol to connect to the server.";
};
2022-12-04 08:53:28 +01:00
matchArg = lib.mkOption {
type = lib.types.str;
default = "";
description = "Optional argument to HAProxy `req.ssl_sni -i`";
};
2021-02-22 11:45:12 +01:00
};
2021-10-31 19:00:03 +01:00
});
2021-02-22 11:45:12 +01:00
default = [ ];
example = [{
hostNames = [ "test.hq.c3d2.de" "test.c3d2.de" ];
proxyTo = {
host = "172.22.99.99";
httpPort = 80;
httpsPort = 443;
};
}];
};
};
2022-12-04 08:53:28 +01:00
config = lib.mkIf cfg.enable {
services.haproxy = {
enable = true;
config = ''
2021-07-14 18:53:12 +02:00
defaults
timeout client 30000
timeout connect 5000
timeout check 5000
timeout server 30000
frontend http-in
bind :::80 v4v6
option http-keep-alive
default_backend proxy-backend-http
2021-02-22 11:45:12 +01:00
backend proxy-backend-http
mode http
option http-server-close
option forwardfor
http-request set-header X-Forwarded-Proto http
http-request set-header X-Forwarded-Port 80
2023-01-18 01:52:47 +01:00
${lib.concatMapStrings ({ proxyTo, proxyProtocol, hostNames, matchArg }:
2022-12-04 08:53:28 +01:00
lib.optionalString (hostNames != [ ] && proxyTo.host != null) (
lib.concatMapStrings (hostname: ''
use-server ${canonicalize hostname}-http if { req.hdr(host) -i ${matchArg} ${hostname} }
2023-01-18 01:52:47 +01:00
server ${canonicalize hostname}-http ${proxyTo.host}:${toString proxyTo.httpPort} weight 1 check ${lib.optionalString proxyProtocol "send-proxy"}
'') hostNames
)
) cfg.proxyHosts
}
frontend https-in
bind :::443 v4v6
2021-07-14 18:53:12 +02:00
tcp-request inspect-delay 5s
tcp-request content accept if { req.ssl_hello_type 1 }
2023-01-18 01:52:47 +01:00
${lib.concatMapStrings ({ proxyTo, hostNames, matchArg, ... }:
2022-12-04 08:53:28 +01:00
lib.concatMapStrings (hostname: ''
use_backend ${canonicalize proxyTo.host}-https if { req.ssl_sni -i ${matchArg} ${hostname} }
'') hostNames
) cfg.proxyHosts}
2023-01-18 01:52:47 +01:00
${lib.concatMapStrings ({ proxyTo, proxyProtocol, ... }: ''
backend ${canonicalize proxyTo.host}-https
2023-01-18 01:52:47 +01:00
server ${canonicalize proxyTo.host}-https ${proxyTo.host}:${toString proxyTo.httpsPort} weight 1 check ${lib.optionalString proxyProtocol "send-proxy"}
'') cfg.proxyHosts}
'';
};
};
}