2020-12-26 01:24:40 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.clerie.nginx-port-forward;
|
|
|
|
|
2020-12-26 22:11:22 +01:00
|
|
|
mkServerBlock = isUDP: port: forward: ''
|
2020-12-26 01:24:40 +01:00
|
|
|
server {
|
2020-12-26 22:11:22 +01:00
|
|
|
listen ${port}${optionalString isUDP " udp"};
|
|
|
|
listen [::]:${port}${optionalString isUDP " udp"};
|
2020-12-26 01:24:40 +01:00
|
|
|
proxy_pass ${forward.host}:${toString forward.port};
|
|
|
|
}
|
2020-12-26 22:11:22 +01:00
|
|
|
'';
|
|
|
|
|
|
|
|
portForwardConf = ''
|
|
|
|
stream {
|
|
|
|
${ concatStringsSep "\n" (mapAttrsToList (mkServerBlock false) cfg.tcpPorts) }
|
|
|
|
${ concatStringsSep "\n" (mapAttrsToList (mkServerBlock true) cfg.udpPorts) }
|
2020-12-26 01:24:40 +01:00
|
|
|
}
|
|
|
|
'';
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
}
|