summaryrefslogtreecommitdiffhomepage
path: root/modules/luci-mod-network/root/usr
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2019-12-18 21:32:48 +0100
committerJo-Philipp Wich <jo@mein.io>2019-12-18 21:33:29 +0100
commit3a657b8ab585f6e0fef5c9128dc84c5a008a6969 (patch)
treee97c804ffd2acf0e0d04a5dd6405091f05934302 /modules/luci-mod-network/root/usr
parente884b63916ebf6d1a7e4f7c92240a76964ecaa85 (diff)
luci-mod-network: replace controller address check action with cgi-io script
This change removes the last bit of Lua code from luci-mod-network. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'modules/luci-mod-network/root/usr')
-rwxr-xr-xmodules/luci-mod-network/root/usr/libexec/luci-peeraddr46
1 files changed, 46 insertions, 0 deletions
diff --git a/modules/luci-mod-network/root/usr/libexec/luci-peeraddr b/modules/luci-mod-network/root/usr/libexec/luci-peeraddr
new file mode 100755
index 0000000000..84a0158fd5
--- /dev/null
+++ b/modules/luci-mod-network/root/usr/libexec/luci-peeraddr
@@ -0,0 +1,46 @@
+#!/bin/sh
+
+NL="
+"
+
+function ifaces_by_device() {
+ ubus call network.interface dump 2>/dev/null | \
+ jsonfilter -e "@.interface[@.device='$1' || @.l3_device='$1'].interface"
+}
+
+function device_by_addr() {
+ set -- $(ip route get "$1" ${2:+from "$2"} 2>/dev/null)
+ echo "$5"
+}
+
+for inbound_device in $(device_by_addr "$REMOTE_ADDR" "$SERVER_ADDR"); do
+ inbound_devices="$inbound_device"
+ inbound_interfaces=""
+
+ for iface in $(ifaces_by_device "$inbound_device"); do
+ inbound_interfaces="${inbound_interfaces:+$inbound_interfaces$NL}$iface"
+
+ for peeraddr in $(uci get "network.$iface.peeraddr"); do
+ for ipaddr in $(resolveip -t 1 "$peeraddr" 2>/dev/null); do
+ for peerdev in $(device_by_addr "$ipaddr"); do
+ for iface in $(ifaces_by_device "$peerdev"); do
+ inbound_devices="${inbound_devices:+$inbound_devices$NL}$peerdev"
+ inbound_interfaces="${inbound_interfaces:+$inbound_interfaces$NL}$iface"
+ done
+ done
+ done
+ done
+ done
+done
+
+inbound_devices="$(echo "$inbound_devices" | sort -u | sed ':a;N;$!ba;s/\n/", "/g')"
+inbound_interfaces="$(echo "$inbound_interfaces" | sort -u | sed ':a;N;$!ba;s/\n/", "/g')"
+
+cat <<JSON
+{
+ "remote_addr": "$REMOTE_ADDR",
+ "server_addr": "$SERVER_ADDR",
+ "inbound_devices": [ ${inbound_devices:+\"$inbound_devices\"} ],
+ "inbound_interfaces": [ ${inbound_interfaces:+\"$inbound_interfaces\"} ]
+}
+JSON