1
0

Add nginx port forward module

This commit is contained in:
clerie 2020-12-26 01:24:40 +01:00
parent 9a7939dcc7
commit 2bf3623eae
2 changed files with 62 additions and 0 deletions

View File

@ -4,5 +4,6 @@
imports = [ imports = [
./policyrouting ./policyrouting
./anycast_healthchecker ./anycast_healthchecker
./nginx-port-forward
]; ];
} }

View File

@ -0,0 +1,61 @@
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.clerie.nginx-port-forward;
portForwardConf = ''
stream {
${ concatStringsSep "\n" ( mapAttrsToList ( port: forward: ''
server {
listen ${port};
listen [::]:${port};
proxy_pass ${forward.host}:${toString forward.port};
}
'' ) cfg.tcpPorts ) }
${ concatStringsSep "\n" ( mapAttrsToList ( port: forward: ''
server {
listen ${port} udp;
listen [::]:${port} udp;
proxy_pass ${forward.host}:${toString forward.port};
}
'' ) cfg.udpPorts ) }
}
'';
portOpts = { config, ... }@moduleAttrs: {
options = {
host = mkOption {
type = types.str;
};
port = mkOption {
type = types.int;
};
};
};
in
{
options = {
clerie.nginx-port-forward = {
enable = mkEnableOption "Nginx Port Forward";
tcpPorts = mkOption {
type = with types; attrsOf (submodule portOpts);
default = {};
};
udpPorts = mkOption {
type = with types; attrsOf (submodule portOpts);
default = {};
};
};
};
config = mkIf cfg.enable {
services.nginx.enable = true;
services.nginx.appendConfig = portForwardConf;
networking.firewall.allowedTCPPorts = mapAttrsToList ( port: dontcare: toInt port ) cfg.tcpPorts;
networking.firewall.allowedUDPPorts = mapAttrsToList ( port: dontcare: toInt port ) cfg.udpPorts;
};
}