]> Git — Sourcephile - sourcephile-nix.git/blob - nixos/modules/services/networking/upnpc.nix
nix: enable autoOptimiseStore by default
[sourcephile-nix.git] / nixos / modules / services / networking / upnpc.nix
1 { pkgs, lib, config, ... }:
2 with lib;
3 let
4 inherit (config.users) users groups;
5 cfg = config.services.upnpc;
6 getInfo = ''
7 while IFS=: read -r k v; do
8 k=$(printf %s "$k" | sed -e 's/^\s*//' -e 's/\s*$//')
9 v=$(printf %s "$v" | sed -e 's/^\s*//' -e 's/\s*$//')
10 case $k in
11 (desc) desc=$v;;
12 ("Local LAN ip address") localIP=$v;;
13 esac
14 done <<EOF
15 $(upnpc -s)
16 EOF
17 '';
18 in
19 {
20 options.services.upnpc = {
21 redirections = mkOption {
22 description = "UPnP redirections to request.";
23 default = [];
24 type = types.listOf (types.submodule ({config, ...}: {
25 options.externalPort = mkOption {
26 description = "External port to open on the redirecting device.";
27 type = types.port;
28 };
29 options.internalPort = mkOption {
30 description = "Internal port, target of the redirection.";
31 type = types.port;
32 default = config.externalPort;
33 };
34 options.protocol = mkOption {
35 description = "Protocol to redirect.";
36 type = with types; enum ["TCP" "UDP"];
37 default = "TCP";
38 };
39 options.description = mkOption {
40 description = "Description of the port mapping";
41 type = types.str;
42 default = "";
43 };
44 options.duration = mkOption {
45 description = "Duration of the redirection, in seconds. 0 means indefinitely.";
46 type = types.int;
47 default = 0;
48 };
49 options.maintainPeriod = mkOption {
50 description = "Period (in seconds) between runs to maintain the redirection.";
51 type = with types; nullOr int;
52 default = if config.duration > 0 then config.duration / 2 else null;
53 defaultText = "if duration > 0 then duration / 2 else null";
54 };
55 options.override = mkOption {
56 description = "Try to override the redirection in case of conflict in mapping entry.";
57 type = types.bool;
58 default = true;
59 };
60 options.service = mkOption {
61 description = "Configuration specific to the systemd service handling this UPnP redirecting.";
62 type = types.attrs;
63 default = {};
64 };
65 }));
66 };
67 };
68 config = {
69 systemd.services = listToAttrs (map (r:
70 nameValuePair "upnpc-${toString r.internalPort}" (mkMerge [
71 { description = "UPnP ${toString r.internalPort}";
72 after = [ "network-online.target" ];
73 #wantedBy = [ "multi-user.target" ];
74 path = [ pkgs.miniupnpc ];
75 serviceConfig = {
76 Type = if r.maintainPeriod == null then "oneshot" else "simple";
77 RemainAfterExit = r.maintainPeriod == null;
78 ExecStart = pkgs.writeShellScript "upnpc-start-${toString r.internalPort}" ''
79 set -eu
80 redirect () {
81 result=
82 while IFS= read -r line; do
83 echo >&2 -E "$line"
84 case $line in
85 (*" is redirected to internal $localIP:${toString r.internalPort}"*) result=ok ;;
86 (*ConflictInMappingEntry*) result=conflict ;;
87 esac
88 done <<EOF
89 $(upnpc -u "$desc" ${optionalString (r.description != "") "-e \"${r.description}\""} \
90 -a "$localIP" ${toString r.internalPort} ${toString r.externalPort} ${r.protocol} ${toString r.duration} 2>&1)
91 EOF
92 }
93 while true; do
94 ${getInfo}
95 redirect
96 ${optionalString r.override ''
97 test "$result" != conflict || {
98 upnpc -u "$desc" -d ${toString r.externalPort} ${r.protocol}
99 redirect
100 }
101 ''}
102 case $result in
103 (ok) ${if r.maintainPeriod == null then "break" else "sleep " + toString r.maintainPeriod} ;;
104 (*) exit 1 ;;
105 esac
106 done
107 '';
108 ExecStop = "${pkgs.miniupnpc}/bin/upnpc -d ${toString r.externalPort} ${r.protocol}";
109 Restart = "on-failure";
110 RestartSec = mkDefault r.maintainPeriod;
111 DynamicUser = true;
112 User = users."upnpc".name;
113 };
114 }
115 r.service
116 ])
117 ) cfg.redirections);
118
119 # This enables to match on the uid in the firewall.
120 users.users."upnpc" = {
121 isSystemUser = true;
122 group = groups."upnpc".name;
123 };
124 users.groups."upnpc" = {};
125 };
126 meta.maintainers = with maintainers; [ julm ];
127 }