luci-app-mwan3: bugfix
[project/luci.git] / applications / luci-app-mwan3 / luasrc / controller / mwan3.lua
index 69e72b7..d5fc4a3 100644 (file)
@@ -1,3 +1,7 @@
+-- Copyright 2014 Aedan Renner <chipdankly@gmail.com>
+-- Copyright 2018 Florian Eckert <fe@dev.tdt.de>
+-- Licensed to the public under the GNU General Public License v2.
+
 module("luci.controller.mwan3", package.seeall)
 
 sys = require "luci.sys"
@@ -52,7 +56,7 @@ function index()
                arcombine(cbi("mwan/rule"), cbi("mwan/ruleconfig")),
                _("Rules"), 40).leaf = true
        entry({"admin", "network", "mwan", "notify"},
-               cbi("mwan/notify"),
+               form("mwan/notify"),
                _("Notification"), 50).leaf = true
 end
 
@@ -81,7 +85,8 @@ function diagnosticsData(interface, task)
        function getInterfaceNumber(interface)
                local number = 0
                local interfaceNumber
-               uci.cursor():foreach("mwan3", "interface",
+               local uci = require "luci.model.uci".cursor()
+               uci:foreach("mwan3", "interface",
                        function (section)
                                number = number+1
                                if section[".name"] == interface then
@@ -94,7 +99,7 @@ function diagnosticsData(interface, task)
 
        function diag_command(cmd, addr)
                if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then
-                       local util = io.popen(cmd % addr)
+                       local util = io.popen(cmd % ut.shellquote(addr))
                        if util then
                                while true do
                                        local ln = util:read("*l")
@@ -111,11 +116,11 @@ function diagnosticsData(interface, task)
        function get_gateway(inteface)
                local dump = require("luci.util").ubus("network.interface.%s" % interface, "status", {})
                local gateway
-               if dump then
+               if dump and dump.route then
                        local _, route
                        for _, route in ipairs(dump.route) do
                                if dump.route[_].target == "0.0.0.0" then
-                                       gateway =  dump.route[_].nexthop
+                                       gateway = dump.route[_].nexthop
                                end
                        end
                end
@@ -126,7 +131,7 @@ function diagnosticsData(interface, task)
        local results = ""
        local number = getInterfaceNumber(interface)
 
-       local uci = uci.cursor(nil, "/var/state")
+       local uci = require "luci.model.uci".cursor(nil, "/var/state")
        local device = uci:get("network", interface, "ifname")
 
        luci.http.prepare_content("text/plain")
@@ -134,7 +139,7 @@ function diagnosticsData(interface, task)
                if task == "ping_gateway" then
                        local gateway = get_gateway(interface)
                        if gateway ~= nil then
-                               diag_command("ping -c 5 -W 1 %q 2>&1", gateway)
+                               diag_command("ping -c 5 -W 1 %s 2>&1", gateway)
                        else
                                luci.http.prepare_content("text/plain")
                                luci.http.write(string.format("No gateway for interface %s found.", interface))
@@ -143,7 +148,7 @@ function diagnosticsData(interface, task)
                        local trackips = uci:get("mwan3", interface, "track_ip")
                        if #trackips > 0 then
                                for i in pairs(trackips) do
-                                       diag_command("ping -c 5 -W 1 %q 2>&1", trackips[i])
+                                       diag_command("ping -c 5 -W 1 %s 2>&1", trackips[i])
                                end
                        else
                                luci.http.write(string.format("No tracking Hosts for interface %s defined.", interface))
@@ -181,10 +186,10 @@ function diagnosticsData(interface, task)
                                luci.http.write(string.format("Routing table %s for interface %s not found", number, interface))
                        end
                elseif task == "hotplug_ifup" then
-                       os.execute(string.format("/usr/sbin/mwan3 ifup %s", interface))
+                       os.execute(string.format("/usr/sbin/mwan3 ifup %s", ut.shellquote(interface)))
                        luci.http.write(string.format("Hotplug ifup sent to interface %s", interface))
                elseif task == "hotplug_ifdown" then
-                       os.execute(string.format("/usr/sbin/mwan3 ifdown %s", interface))
+                       os.execute(string.format("/usr/sbin/mwan3 ifdown %s", ut.shellquote(interface)))
                        luci.http.write(string.format("Hotplug ifdown sent to interface %s", interface))
                else
                        luci.http.write("Unknown task")
@@ -198,104 +203,104 @@ end
 
 function troubleshootingData()
        local ver = require "luci.version"
+       local dash = "-------------------------------------------------"
 
-       local mArray = {}
-
-       -- software versions
-       local wrtRelease = ut.trim(ver.distversion)
-               if wrtRelease ~= "" then
-                       wrtRelease = "OpenWrt - " .. wrtRelease
-               else
-                       wrtRelease = "OpenWrt - unknown"
-               end
-       local luciRelease = ut.trim(ver.luciversion)
-               if luciRelease ~= "" then
-                       luciRelease = "\nLuCI - " .. luciRelease
-               else
-                       luciRelease = "\nLuCI - unknown"
-               end
-       local mwanVersion = ut.trim(sys.exec("opkg info mwan3 | grep Version | awk '{print $2}'"))
-               if mwanVersion ~= "" then
-                       mwanVersion = "\n\nmwan3 - " .. mwanVersion
-               else
-                       mwanVersion = "\n\nmwan3 - unknown"
-               end
-       local mwanLuciVersion = ut.trim(sys.exec("opkg info luci-app-mwan3 | grep Version | awk '{print $2}'"))
-               if mwanLuciVersion ~= "" then
-                       mwanLuciVersion = "\nmwan3-luci - " .. mwanLuciVersion
-               else
-                       mwanLuciVersion = "\nmwan3-luci - unknown"
-               end
-       mArray.versions = { wrtRelease .. luciRelease .. mwanVersion .. mwanLuciVersion }
-
-       -- mwan config
-       local mwanConfig = ut.trim(sys.exec("cat /etc/config/mwan3"))
-               if mwanConfig == "" then
-                       mwanConfig = "No data found"
-               end
-       mArray.mwanconfig = { mwanConfig }
-
-       -- network config
-       local networkConfig = ut.trim(sys.exec("cat /etc/config/network | sed -e 's/.*username.*/       USERNAME HIDDEN/' -e 's/.*password.*/   PASSWORD HIDDEN/'"))
-               if networkConfig == "" then
-                       networkConfig = "No data found"
-               end
-       mArray.netconfig = { networkConfig }
+       luci.http.prepare_content("text/plain")
 
-       -- wireless config
-       local wirelessConfig = ut.trim(sys.exec("cat /etc/config/wireless | sed -e 's/.*username.*/     USERNAME HIDDEN/' -e 's/.*password.*/   PASSWORD HIDDEN/' -e 's/.*key.*/        KEY HIDDEN/'"))
-               if wirelessConfig == "" then
-                       wirelessConfig = "No data found"
-               end
-       mArray.wificonfig = { wirelessConfig }
-       
-       -- ifconfig
-       local ifconfig = ut.trim(sys.exec("ifconfig"))
-               if ifconfig == "" then
-                       ifconfig = "No data found"
-               end
-       mArray.ifconfig = { ifconfig }
+       luci.http.write("\n")
+       luci.http.write("\n")
+       luci.http.write("Software-Version")
+       luci.http.write("\n")
+       luci.http.write(dash)
+       luci.http.write("\n")
+       if ver.distversion then
+               luci.http.write(string.format("OpenWrt - %s", ver.distversion))
+               luci.http.write("\n")
+       else
+               luci.http.write("OpenWrt - unknown")
+               luci.http.write("\n")
+       end
 
-       -- route -n
-       local routeShow = ut.trim(sys.exec("route -n"))
-               if routeShow == "" then
-                       routeShow = "No data found"
-               end
-       mArray.routeshow = { routeShow }
+       if ver.luciversion then
+               luci.http.write(string.format("LuCI - %s", ver.luciversion))
+               luci.http.write("\n")
+       else
+               luci.http.write("LuCI - unknown")
+               luci.http.write("\n")
+       end
 
-       -- ip rule show
-       local ipRuleShow = ut.trim(sys.exec(ip .. "rule show"))
-               if ipRuleShow == "" then
-                       ipRuleShow = "No data found"
-               end
-       mArray.iprule = { ipRuleShow }
+       luci.http.write("\n")
+       luci.http.write("\n")
+       local output = ut.trim(sys.exec("ip a show"))
+       luci.http.write("Output of \"ip a show\"")
+       luci.http.write("\n")
+       luci.http.write(dash)
+       luci.http.write("\n")
+       if output ~= "" then
+               luci.http.write(output)
+               luci.http.write("\n")
+       else
+               luci.http.write("No data found")
+               luci.http.write("\n")
+       end
 
-       -- ip route list table 1-250
-       local routeList, routeString = ut.trim(sys.exec(ip .. "rule | sed 's/://g' 2>/dev/null | awk '$1>=2001 && $1<=2250' | awk '{print $NF}'")), ""
-               if routeList ~= "" then
-                       for line in routeList:gmatch("[^\r\n]+") do
-                               routeString = routeString .. line .. "\n" .. sys.exec(ip .. "route list table " .. line)
-                       end
-                       routeString = ut.trim(routeString)
-               else
-                       routeString = "No data found"
-               end
-       mArray.routelist = { routeString }
+       luci.http.write("\n")
+       luci.http.write("\n")
+       local output = ut.trim(sys.exec("ip route show"))
+       luci.http.write("Output of \"ip route show\"")
+       luci.http.write("\n")
+       luci.http.write(dash)
+       luci.http.write("\n")
+       if output ~= "" then
+               luci.http.write(output)
+               luci.http.write("\n")
+       else
+               luci.http.write("No data found")
+               luci.http.write("\n")
+       end
 
-       -- default firewall output policy
-       local firewallOut = ut.trim(sys.exec("uci -q -p /var/state get firewall.@defaults[0].output"))
-               if firewallOut == "" then
-                       firewallOut = "No data found"
-               end
-       mArray.firewallout = { firewallOut }
+       luci.http.write("\n")
+       luci.http.write("\n")
+       local output = ut.trim(sys.exec("ip rule show"))
+       luci.http.write("Output of \"ip rule show\"")
+       luci.http.write("\n")
+       luci.http.write(dash)
+       luci.http.write("\n")
+       if output ~= "" then
+               luci.http.write(output)
+               luci.http.write("\n")
+       else
+               luci.http.write("No data found")
+               luci.http.write("\n")
+       end
 
-       -- iptables
-       local iptables = ut.trim(sys.exec("iptables -L -t mangle -v -n"))
-               if iptables == "" then
-                       iptables = "No data found"
+       luci.http.write("\n")
+       luci.http.write("\n")
+       luci.http.write("Output of \"ip route list table 1-250\"")
+       luci.http.write("\n")
+       luci.http.write(dash)
+       luci.http.write("\n")
+       for i=1,250 do
+               local output = ut.trim(sys.exec(string.format("ip route list table %d", i)))
+               if output ~= "" then
+                       luci.http.write(string.format("Table %s: ", i))
+                       luci.http.write(output)
+                       luci.http.write("\n")
                end
-       mArray.iptables = { iptables }
+       end
 
-       luci.http.prepare_content("application/json")
-       luci.http.write_json(mArray)
+       luci.http.write("\n")
+       luci.http.write("\n")
+       local output = ut.trim(sys.exec("iptables -L -t mangle -v -n"))
+       luci.http.write("Output of \"iptables -L -t mangle -v -n\"")
+       luci.http.write("\n")
+       luci.http.write(dash)
+       luci.http.write("\n")
+       if output ~= "" then
+               luci.http.write(output)
+               luci.http.write("\n")
+       else
+               luci.http.write("No data found")
+               luci.http.write("\n")
+       end
 end