2024-12-10 20:26:33 +01:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
2015-12-08 10:40:43 +01:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.miniupnpd;
|
|
|
|
configFile = pkgs.writeText "miniupnpd.conf" ''
|
|
|
|
ext_ifname=${cfg.externalInterface}
|
|
|
|
enable_natpmp=${if cfg.natpmp then "yes" else "no"}
|
|
|
|
enable_upnp=${if cfg.upnp then "yes" else "no"}
|
|
|
|
|
|
|
|
${concatMapStrings (range: ''
|
|
|
|
listening_ip=${range}
|
|
|
|
'') cfg.internalIPs}
|
|
|
|
|
2023-06-22 00:08:31 +07:00
|
|
|
${lib.optionalString (firewall == "nftables") ''
|
|
|
|
upnp_table_name=miniupnpd
|
|
|
|
upnp_nat_table_name=miniupnpd
|
|
|
|
''}
|
|
|
|
|
2015-12-08 10:40:43 +01:00
|
|
|
${cfg.appendConfig}
|
|
|
|
'';
|
2023-06-22 00:08:31 +07:00
|
|
|
firewall = if config.networking.nftables.enable then "nftables" else "iptables";
|
|
|
|
miniupnpd = pkgs.miniupnpd.override { inherit firewall; };
|
2024-12-10 20:26:33 +01:00
|
|
|
firewallScripts = lib.optionals (firewall == "iptables") (
|
|
|
|
[ "iptables" ] ++ lib.optional (config.networking.enableIPv6) "ip6tables"
|
|
|
|
);
|
2015-12-08 10:40:43 +01:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.miniupnpd = {
|
2024-04-13 14:54:15 +02:00
|
|
|
enable = mkEnableOption "MiniUPnP daemon";
|
2015-12-08 10:40:43 +01:00
|
|
|
|
|
|
|
externalInterface = mkOption {
|
|
|
|
type = types.str;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2015-12-08 10:40:43 +01:00
|
|
|
Name of the external interface.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
internalIPs = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2024-12-10 20:26:33 +01:00
|
|
|
example = [
|
|
|
|
"192.168.1.1/24"
|
|
|
|
"enp1s0"
|
|
|
|
];
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2015-12-08 10:40:43 +01:00
|
|
|
The IP address ranges to listen on.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-04-13 14:54:15 +02:00
|
|
|
natpmp = mkEnableOption "NAT-PMP support";
|
2015-12-08 10:40:43 +01:00
|
|
|
|
|
|
|
upnp = mkOption {
|
|
|
|
default = true;
|
|
|
|
type = types.bool;
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2015-12-08 10:40:43 +01:00
|
|
|
Whether to enable UPNP support.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
appendConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
2024-04-13 14:54:15 +02:00
|
|
|
description = ''
|
2015-12-08 10:40:43 +01:00
|
|
|
Configuration lines appended to the MiniUPnP config.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2024-12-10 20:26:33 +01:00
|
|
|
networking.firewall.extraCommands = lib.mkIf (firewallScripts != [ ]) (
|
|
|
|
builtins.concatStringsSep "\n" (
|
|
|
|
map (fw: ''
|
|
|
|
EXTIF=${cfg.externalInterface} ${pkgs.bash}/bin/bash -x ${miniupnpd}/etc/miniupnpd/${fw}_init.sh
|
|
|
|
'') firewallScripts
|
|
|
|
)
|
|
|
|
);
|
2023-06-22 00:08:31 +07:00
|
|
|
|
2024-12-10 20:26:33 +01:00
|
|
|
networking.firewall.extraStopCommands = lib.mkIf (firewallScripts != [ ]) (
|
|
|
|
builtins.concatStringsSep "\n" (
|
|
|
|
map (fw: ''
|
|
|
|
EXTIF=${cfg.externalInterface} ${pkgs.bash}/bin/bash -x ${miniupnpd}/etc/miniupnpd/${fw}_removeall.sh
|
|
|
|
'') firewallScripts
|
|
|
|
)
|
|
|
|
);
|
2015-12-13 16:43:31 +01:00
|
|
|
|
2023-06-22 00:08:31 +07:00
|
|
|
networking.nftables = lib.mkIf (firewall == "nftables") {
|
|
|
|
# see nft_init in ${miniupnpd-nftables}/etc/miniupnpd
|
|
|
|
tables.miniupnpd = {
|
|
|
|
family = "inet";
|
|
|
|
# The following is omitted because it's expected that the firewall is to be responsible for it.
|
|
|
|
#
|
|
|
|
# chain forward {
|
|
|
|
# type filter hook forward priority filter; policy drop;
|
|
|
|
# jump miniupnpd
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# Otherwise, it quickly gets ugly with (potentially) two forward chains with "policy drop".
|
|
|
|
# This means the chain "miniupnpd" never actually gets triggered and is simply there to satisfy
|
|
|
|
# miniupnpd. If you're doing it yourself (without networking.firewall), the easiest way to get
|
|
|
|
# it to work is adding a rule "ct status dnat accept" - this is what networking.firewall does.
|
|
|
|
# If you don't want to simply accept forwarding for all "ct status dnat" packets, override
|
|
|
|
# upnp_table_name with whatever your table is, create a chain "miniupnpd" in your table and
|
|
|
|
# jump into it from your forward chain.
|
|
|
|
content = ''
|
|
|
|
chain miniupnpd {}
|
|
|
|
chain prerouting_miniupnpd {
|
|
|
|
type nat hook prerouting priority dstnat; policy accept;
|
|
|
|
}
|
|
|
|
chain postrouting_miniupnpd {
|
|
|
|
type nat hook postrouting priority srcnat; policy accept;
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2015-12-13 16:43:31 +01:00
|
|
|
|
2015-12-08 10:40:43 +01:00
|
|
|
systemd.services.miniupnpd = {
|
|
|
|
description = "MiniUPnP daemon";
|
|
|
|
after = [ "network.target" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig = {
|
2023-06-22 00:08:31 +07:00
|
|
|
ExecStart = "${miniupnpd}/bin/miniupnpd -f ${configFile}";
|
2018-12-19 22:40:21 +01:00
|
|
|
PIDFile = "/run/miniupnpd.pid";
|
2015-12-13 16:43:31 +01:00
|
|
|
Type = "forking";
|
2015-12-08 10:40:43 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|