Skip to content

Commit 433e3ed

Browse files
miniupnpd: refactoring by separate service start and config-gen
- Remove `config_foreach upnpd "upnpd"` and replace it with regular function call, as init was not designed for a multi-instance setup, as the same `tmpconf` will be used/overwritten, and non-anonymous section - Move code to make the custom vs. config file generation decision earlier, and only perform external interface detection with the second one - Exit with 1 on errors to get an inactive service status - Replace unnecessary if cases with elif in init/hotplug - Rename function `upnp` to `upnpd_generate_config` - Do not restart daemon in hotplug when using a custom config file, as this file will not regenerated on restarts (to merge with prior) Signed-off-by: Self-Hosting-Group <[email protected]>
1 parent b7f083e commit 433e3ed

File tree

2 files changed

+62
-82
lines changed

2 files changed

+62
-82
lines changed

net/miniupnpd/files/miniupnpd.hotplug

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1+
#!/bin/sh
12
/etc/init.d/miniupnpd enabled || exit 0
23

3-
# If miniupnpd is not running:
4-
# - check on _any_ event (event updates may contribute to network_find_wan*)
5-
6-
# If miniupnpd _is_ running:
7-
# - check only on ifup (otherwise lease updates etc would cause
8-
# miniupnpd state loss)
4+
# If daemon is:
5+
# - not running: check on any event (event updates may contribute to network_find_wan*)
6+
# - running: check only on ifup (otherwise lease updates etc. would cause daemon state loss)
97

108
[ "$ACTION" != "ifup" ] && /etc/init.d/miniupnpd running && exit 0
9+
uci -q get upnpd.settings.config_file >/dev/null && exit 0
1110

1211
tmpconf="/var/etc/miniupnpd.conf"
1312
external_iface=$(uci -q get upnpd.settings.external_iface)
@@ -16,26 +15,19 @@ external_zone=$(uci -q get upnpd.settings.external_zone)
1615
[ -x "$(command -v nft)" ] && FW="fw4" || FW="fw3"
1716

1817
. /lib/functions/network.sh
19-
20-
if [ -n "$external_iface" ] ; then
18+
if [ -n "$external_iface" ]; then
2119
network_get_device ifname "$external_iface"
20+
elif [ -n "$external_zone" ]; then
21+
ifname=$($FW -q zone "$external_zone" 2>/dev/null | head -1)
2222
else
23-
if [ -n "$external_zone" ] ; then
24-
ifname=$($FW -q zone "$external_zone" 2>/dev/null | head -1)
25-
else
26-
network_find_wan external_iface && \
27-
network_get_device ifname "$external_iface"
28-
fi
23+
network_find_wan external_iface && network_get_device ifname "$external_iface"
2924
fi
30-
if [ -n "$external_iface6" ] ; then
25+
if [ -n "$external_iface6" ]; then
3126
network_get_device ifname6 "$external_iface6"
27+
elif [ -n "$external_zone" ]; then
28+
ifname6=$($FW -q zone "$external_zone" 2>/dev/null | head -1)
3229
else
33-
if [ -n "$external_zone" ] ; then
34-
ifname6=$($FW -q zone "$external_zone" 2>/dev/null | head -1)
35-
else
36-
network_find_wan6 external_iface6 && \
37-
network_get_device ifname6 "$external_iface6"
38-
fi
30+
network_find_wan6 external_iface6 && network_get_device ifname6 "$external_iface6"
3931
fi
4032

4133
[ "$DEVICE" != "$ifname" ] && [ "$DEVICE" != "$ifname6" ] && exit 0

net/miniupnpd/files/miniupnpd.init

Lines changed: 49 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,16 @@ upnpd_add_custom_acl_entry() {
3737
echo "$action $ext_port $int_addr $int_port${desc_filter} # $comment"
3838
}
3939

40-
upnpd() {
41-
config_load "upnpd"
42-
local enabled
43-
config_get enabled settings enabled 0
44-
[ "$enabled" != "1" ] && log "Service disabled, UCI enabled is not set" && exit 1
45-
40+
upnpd_generate_config() {
4641
# Daemon
47-
local enabled_protocols allow_cgnat_use stun_host allow_third_party_mapping ipv6_disable system_uptime log_output lease_file config_file
42+
local enabled_protocols allow_cgnat_use stun_host allow_third_party_mapping ipv6_disable system_uptime lease_file
4843
config_get enabled_protocols settings enabled_protocols all
4944
config_get allow_cgnat_use settings allow_cgnat_use 0
5045
config_get stun_host settings stun_host stun.nextcloud.com
5146
config_get allow_third_party_mapping settings allow_third_party_mapping 0
5247
config_get ipv6_disable settings ipv6_disable 0
5348
config_get system_uptime settings system_uptime 1
54-
config_get log_output settings log_output
5549
config_get lease_file settings lease_file /run/miniupnpd.leases
56-
config_get config_file settings config_file
5750

5851
# UPnP IGD
5952
local upnp_igd_compat upnp_igd_download upnp_igd_upload upnp_igd_friendly_name model_number serial_number presentation_url uuid upnp_igd_http_port notify_interval
@@ -75,47 +68,34 @@ upnpd() {
7568
config_get external_zone settings external_zone
7669
config_get external_ip settings external_ip
7770

78-
local conf ifname ifname6
71+
local ifname ifname6
7972
. /lib/functions/network.sh
80-
81-
if [ -n "$external_iface" ] ; then
73+
if [ -n "$external_iface" ]; then
8274
network_get_device ifname "$external_iface"
75+
elif [ -n "$external_zone" ]; then
76+
ifname=$($FW -q zone "$external_zone" 2>/dev/null | head -1)
8377
else
84-
if [ -n "$external_zone" ] ; then
85-
ifname=$($FW -q zone "$external_zone" 2>/dev/null | head -1)
86-
else
87-
network_find_wan external_iface && \
88-
network_get_device ifname "$external_iface"
89-
fi
78+
network_find_wan external_iface && network_get_device ifname "$external_iface"
9079
fi
91-
if [ -n "$external_iface6" ] ; then
80+
if [ -n "$external_iface6" ]; then
9281
network_get_device ifname6 "$external_iface6"
82+
elif [ -n "$external_zone" ]; then
83+
ifname6=$($FW -q zone "$external_zone" 2>/dev/null | head -1)
9384
else
94-
if [ -n "$external_zone" ] ; then
95-
ifname6=$($FW -q zone "$external_zone" 2>/dev/null | head -1)
96-
else
97-
network_find_wan6 external_iface6 && \
98-
network_get_device ifname6 "$external_iface6"
99-
fi
85+
network_find_wan6 external_iface6 && network_get_device ifname6 "$external_iface6"
10086
fi
10187

102-
if [ -n "$config_file" ]; then
103-
conf="$config_file"
104-
else
105-
local tmpconf="/var/etc/miniupnpd.conf"
106-
conf="$tmpconf"
107-
mkdir -p /var/etc
108-
[ "$ifname" = "" ] && log "No external network interface found, not starting" daemon.err && exit 1
109-
! uci -q get upnpd.@internal_network[0].interface >/dev/null && log "No internal networks configured, not starting" daemon.err && exit 1
110-
# Only perform an STUN CGNAT test when necessary with a private/CGNAT external IPv4
111-
local extipv4 extipv4private
112-
network_get_ipaddr extipv4 "$ifname"
113-
case "$extipv4" in 10.* | 172.1[6-9].* | 172.2[0-9].* | 172.3[0-1].* | 192.168.* | 100.6[4-9].* | 100.[7-9][0-9].* | 100.1[0-1][0-9].* | 100.12[0-7].*)
114-
extipv4private=1
115-
;;
116-
esac
117-
118-
{
88+
[ "$ifname" = "" ] && log "No external network interface found, not starting" daemon.err && exit 1
89+
! uci -q get upnpd.@internal_network[0].interface >/dev/null && log "No internal networks configured, not starting" daemon.err && exit 1
90+
# Only perform an STUN CGNAT test when necessary with a private/CGNAT external IPv4
91+
local extipv4 extipv4private
92+
network_get_ipaddr extipv4 "$ifname"
93+
case "$extipv4" in 10.* | 172.1[6-9].* | 172.2[0-9].* | 172.3[0-1].* | 192.168.* | 100.6[4-9].* | 100.[7-9][0-9].* | 100.1[0-1][0-9].* | 100.12[0-7].*)
94+
extipv4private=1
95+
;;
96+
esac
97+
98+
{
11999
echo "# Daemon"
120100
[ "$enabled_protocols" = "all" ] && echo "enable_upnp=yes" && echo "enable_pcp_pmp=yes"
121101
[ "$enabled_protocols" = "upnp-igd" ] && echo "enable_upnp=yes" && echo "enable_pcp_pmp=no"
@@ -178,24 +158,7 @@ upnpd() {
178158
config_foreach upnpd_add_int_network_preset internal_network postcustom
179159
echo "deny 1-65535 0.0.0.0/0 1-65535 # Reject ACL by default"
180160

181-
} > "$tmpconf"
182-
fi
183-
184-
if [ -n "$ifname" ]; then
185-
if [ "$FW" = "fw4" ]; then
186-
nft -s -t -n list chain inet fw4 upnp_forward >/dev/null 2>&1 || fw4 reload
187-
else
188-
iptables -L MINIUPNPD >/dev/null 2>&1 || fw3 reload
189-
fi
190-
fi
191-
192-
procd_open_instance
193-
procd_set_param file "$conf" "/etc/config/firewall"
194-
procd_set_param command "$PROG"
195-
procd_append_param command -f "$conf"
196-
[ "$log_output" = "info" ] && procd_append_param command -v
197-
[ "$log_output" = "debug" ] && procd_append_param command -d
198-
procd_close_instance
161+
} >"$1"
199162
}
200163

201164
stop_service() {
@@ -214,7 +177,32 @@ stop_service() {
214177
start_service() {
215178
upnpd_uci_migration
216179
config_load "upnpd"
217-
config_foreach upnpd "upnpd"
180+
local enabled config_file log_output conf
181+
config_get enabled settings enabled 0
182+
config_get config_file settings config_file
183+
config_get log_output settings log_output
184+
[ "$enabled" != "1" ] && log "Service disabled, UCI enabled is not set" && exit 1
185+
if [ -n "$config_file" ]; then
186+
conf="$config_file"
187+
else
188+
local tmpconf="/var/etc/miniupnpd.conf"
189+
conf="$tmpconf"
190+
mkdir -p /var/etc
191+
upnpd_generate_config "$tmpconf"
192+
fi
193+
if [ "$FW" = "fw4" ]; then
194+
nft -s -t -n list chain inet fw4 upnp_forward >/dev/null 2>&1 || fw4 reload
195+
else
196+
iptables -L MINIUPNPD >/dev/null 2>&1 || fw3 reload
197+
fi
198+
199+
procd_open_instance
200+
procd_set_param file "$conf" "/etc/config/firewall"
201+
procd_set_param command "$PROG"
202+
procd_append_param command -f "$conf"
203+
[ "$log_output" = "info" ] && procd_append_param command -v
204+
[ "$log_output" = "debug" ] && procd_append_param command -d
205+
procd_close_instance
218206
}
219207

220208
service_triggers() {

0 commit comments

Comments
 (0)