From f5f6b3e4f8e3faed4e9c9926e283c9cf226d4bd8 Mon Sep 17 00:00:00 2001 From: Florian Eckert Date: Fri, 2 Jul 2021 15:23:39 +0200 Subject: luci-app-mwan3: convert to JS Signed-off-by: Florian Eckert --- .../luci-app-mwan3/root/usr/libexec/luci-mwan3 | 199 +++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100755 applications/luci-app-mwan3/root/usr/libexec/luci-mwan3 (limited to 'applications/luci-app-mwan3/root/usr/libexec') diff --git a/applications/luci-app-mwan3/root/usr/libexec/luci-mwan3 b/applications/luci-app-mwan3/root/usr/libexec/luci-mwan3 new file mode 100755 index 0000000000..8db3e4723f --- /dev/null +++ b/applications/luci-app-mwan3/root/usr/libexec/luci-mwan3 @@ -0,0 +1,199 @@ +#!/bin/sh +# +# Copyright (C) 2021 TDT AG +# +# This is free software, licensed under the GNU General Public License v2. +# See https://www.gnu.org/licenses/gpl-2.0.txt for more information. +# + +. /lib/functions.sh +. /lib/functions/network.sh +. /usr/share/libubox/jshn.sh + +IIF=1000 +FWMARK=2000 +ID=0 + +usage() { + local status="$1" + local msg="$2" + if [ -n "$msg" ]; then + echo "$msg" + echo "" + fi + echo "Usage: $(basename "$0") " + echo "command:" + echo " diag: diagnostic commands" + echo " ipset: ipset commands" + echo "" + echo "diag " + echo "command:" + echo " gateway : ping interface gateway" + echo " tracking : ping interface tracking targets" + echo " rules : check interface routing rules" + echo " routes : check interface routing tables" + echo "" + echo "ipset " + echo "command:" + echo " dump: show all configured ipset names" + + exit "$status" +} + +diag_gateway() { + local iface="$1" + + local gw + + network_get_gateway gw "${iface}" + [ -z "$gw" ] && network_get_gateway gw "${iface}_4" + + [ -z "$gw" ] && { + echo "No gateway for interface ${iface} found." + exit 2 + } + + mwan3 use "$iface" "ping" "-c" "5" "-W" "1" "$gw" +} + +diag_tracking() { + local iface="$1" + + checkips() { + local ip="$1" + local iface="$2" + + mwan3 use "$iface" "ping" "-c" "5" "-W" "1" "$ip" + } + + config_load mwan3 + config_list_foreach "$iface" "track_ip" checkips "$iface" +} + +iface_number() { + local cfg="$1" + local iface="$2" + + let number++ + + [ "$cfg" = "$iface" ] && { + ID="$number" + } +} + +diag_rules() { + local iface="$1" + + local number=0 + local iif=0 + local fwmark=0 + + local iif_rule iif_result + local fwmark_rule fwmark_result + + config_load mwan3 + config_foreach iface_number 'interface' "$iface" + + [ "$ID" = "0" ] && { + echo "Unable to get mwan3 interface number for \"$iface\"." + exit 2 + } + + let "iif=$IIF+$ID" + let "fwmark=$FWMARK+$ID" + + iif_rule="$(ip rule | grep ${iif})" + iif_result="$?" + + fwmark_rule="$(ip rule | grep ${fwmark})" + fwmark_result="$?" + + if [ "$fwmark_result" = 0 ] && [ "$iif_result" = 0 ]; then + echo "All required IP rules for interface \"$iface\" found" + echo "$fwmark_rule" + echo "$iif_rule" + elif [ "$fwmark_result" = 1 ] && [ "$iif_result" = 0 ]; then + echo "Only iif IP rule for interface \"$iface\" found" + echo "$iif_rule" + elif [ "$fwmark_result" = 0 ] && [ "$iif_result" = 1 ]; then + echo "Only fwmark IP rule for interface \"$iface\" found" + echo "$fwmark_rule" + else + echo "Missing fwmark and iif IP rule for interface \"$iface\"" + fi +} + +diag_routes() { + local iface="$1" + + local table table_result + + config_load mwan3 + config_foreach iface_number 'interface' "$iface" + + [ "$ID" = "0" ] && { + echo "Unable to get mwan3 interface number for \"$iface\"." + exit 2 + } + + table="$(ip route list table $ID)" + table_result="$?" + + if [ "$table_result" = 0 ]; then + echo "Routing table \"$ID\" for interface \"$iface\" found" + echo "$table" + else + echo "Routing table \"$ID\" for interface \"$iface\" not found" + fi +} + +diag_cmd() { + case "$1" in + gateway) + diag_gateway "$2" + ;; + tracking) + diag_tracking "$2" + ;; + rules) + diag_rules "$2" + ;; + routes) + diag_routes "$2" + ;; + *) + usage "1" "Command not supported" + ;; + esac +} + +ipset_dump() { + ipset -n -L 2>/dev/null | grep -v mwan3_ | sort -u +} + +ipset_cmd() { + case "$1" in + dump) + ipset_dump + ;; + *) + usage "1" "Command not supported" + ;; + esac +} + +main () { + case "$1" in + diag) + diag_cmd "$2" "$3" + ;; + ipset) + ipset_cmd "$2" + ;; + *) + usage "1" "Command not supported" + ;; + esac +} + +main "$@" -- cgit v1.2.3