From ac7385391ef304da1516d5707e6966b7d52d98f8 Mon Sep 17 00:00:00 2001
From: bol-van <none@none.none>
Date: Tue, 19 Nov 2024 22:09:40 +0300
Subject: [PATCH] 10-keenetic-udp-fix: auto discover wan interfaces

---
 .../custom.d.examples/10-keenetic-udp-fix     | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/init.d/sysv/custom.d.examples/10-keenetic-udp-fix b/init.d/sysv/custom.d.examples/10-keenetic-udp-fix
index b87766c..e04706c 100644
--- a/init.d/sysv/custom.d.examples/10-keenetic-udp-fix
+++ b/init.d/sysv/custom.d.examples/10-keenetic-udp-fix
@@ -2,19 +2,18 @@
 # Keenetic uses proprietary ndmmark and does not masquerade without this mark
 # If not masqueraded packets go to WAN with LAN IP and get dropped by ISP
 
-# !!! MUST set IFACE_WAN in config !!!
+# It's advised to set IFACE_WAN in config
 
 zapret_custom_firewall()
 {
-        # $1 - 1 - add, 0 - stop
+	# $1 - 1 - add, 0 - stop
 
-	local wan
+	local wan wanif
 
-        ipt_print_op $1 "-j MASQUERADE" "keenetic udp fix"
-
-        if [ -n "$IFACE_WAN" ] ; then
-                for wan in $IFACE_WAN; do
-                        ipt_add_del $1 POSTROUTING -t nat -o $wan -j MASQUERADE
-                done
-        fi
+	# use IFACE_WAN if defined. if not - search for interfaces with default route.
+	wanif=${IFACE_WAN:-$(sed -nre 's/^([^\t]+)\t00000000\t[0-9A-F]{8}\t[0-9A-F]{4}\t[0-9]+\t[0-9]+\t[0-9]+\t00000000.*$/\1/p' /proc/net/route | sort -u | xargs)}
+	for wan in $wanif; do
+		ipt_print_op $1 "-o $wan -j MASQUERADE" "keenetic udp fix"
+		ipt_add_del $1 POSTROUTING -t nat -o $wan -j MASQUERADE
+	done
 }