From ab07b8c4d960925fe3467ea666871965b5b34259 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Sun, 15 Dec 2019 22:35:21 +0100 Subject: [PATCH] luci-mod-network: reimplement diagnostics page as client side view Signed-off-by: Jo-Philipp Wich --- .../root/usr/share/rpcd/acl.d/luci-base.json | 11 +- .../resources/view/network/diagnostics.js | 137 ++++++++++++++++++ .../luasrc/controller/admin/network.lua | 42 ------ .../luasrc/view/admin_network/diagnostics.htm | 117 --------------- .../share/luci/menu.d/luci-mod-network.json | 49 +------ 5 files changed, 149 insertions(+), 207 deletions(-) create mode 100644 modules/luci-mod-network/htdocs/luci-static/resources/view/network/diagnostics.js delete mode 100644 modules/luci-mod-network/luasrc/view/admin_network/diagnostics.htm diff --git a/modules/luci-base/root/usr/share/rpcd/acl.d/luci-base.json b/modules/luci-base/root/usr/share/rpcd/acl.d/luci-base.json index 50ddc299f..e215cf945 100644 --- a/modules/luci-base/root/usr/share/rpcd/acl.d/luci-base.json +++ b/modules/luci-base/root/usr/share/rpcd/acl.d/luci-base.json @@ -38,7 +38,16 @@ "/proc/sys/kernel/hostname": [ "read" ], "/proc/sys/net/netfilter/nf_conntrack_*": [ "read" ], "/proc/mounts": [ "read" ], - "/usr/lib/lua/luci/version.lua": [ "read" ] + "/usr/lib/lua/luci/version.lua": [ "read" ], + "/bin/ping *": [ "exec" ], + "/bin/ping6 *": [ "exec" ], + "/bin/traceroute *": [ "exec" ], + "/bin/traceroute6 *": [ "exec" ], + "/usr/bin/ping *": [ "exec" ], + "/usr/bin/ping6 *": [ "exec" ], + "/usr/bin/traceroute *": [ "exec" ], + "/usr/bin/traceroute6 *": [ "exec" ], + "/usr/bin/nslookup *": [ "exec" ] }, "ubus": { "file": [ "list", "read", "stat" ], diff --git a/modules/luci-mod-network/htdocs/luci-static/resources/view/network/diagnostics.js b/modules/luci-mod-network/htdocs/luci-static/resources/view/network/diagnostics.js new file mode 100644 index 000000000..ee2a46615 --- /dev/null +++ b/modules/luci-mod-network/htdocs/luci-static/resources/view/network/diagnostics.js @@ -0,0 +1,137 @@ +'use strict'; +'require fs'; +'require ui'; +'require uci'; + +return L.view.extend({ + handleCommand: function(exec, args) { + var buttons = document.querySelectorAll('.diag-action > .cbi-button'); + + for (var i = 0; i < buttons.length; i++) + buttons[i].setAttribute('disabled', 'true'); + + return fs.exec(exec, args).then(function(res) { + var out = document.querySelector('.command-output'); + out.style.display = ''; + + L.dom.content(out, [ res.stdout || '', res.stderr || '' ]); + }).catch(function(err) { + ui.addNotification(null, E('p', [ err ])) + }).finally(function() { + for (var i = 0; i < buttons.length; i++) + buttons[i].removeAttribute('disabled'); + }); + }, + + handlePing: function(ev, cmd) { + var exec = cmd || 'ping', + addr = ev.currentTarget.parentNode.previousSibling.value, + args = (exec == 'ping') ? [ '-c', '5', '-W', '1', addr ] : [ '-c', '5', addr ]; + + return this.handleCommand(exec, args); + }, + + handleTraceroute: function(ev, cmd) { + var exec = cmd || 'traceroute', + addr = ev.currentTarget.parentNode.previousSibling.value, + args = (exec == 'traceroute') ? [ '-q', '1', '-w', '1', '-n', addr ] : [ '-q', '1', '-w', '2', '-n', addr ]; + + return this.handleCommand(exec, args); + }, + + handleNslookup: function(ev, cmd) { + var addr = ev.currentTarget.parentNode.previousSibling.value; + + return this.handleCommand('nslookup', [ addr ]); + }, + + load: function() { + return Promise.all([ + L.resolveDefault(fs.stat('/bin/ping6'), {}), + L.resolveDefault(fs.stat('/usr/bin/ping6'), {}), + L.resolveDefault(fs.stat('/bin/traceroute6'), {}), + L.resolveDefault(fs.stat('/usr/bin/traceroute6'), {}), + uci.load('luci') + ]); + }, + + render: function(res) { + var has_ping6 = res[0].path || res[1].path, + has_traceroute6 = res[2].path || res[3].path, + dns_host = uci.get('luci', 'diag', 'dns') || 'openwrt.org', + ping_host = uci.get('luci', 'diag', 'ping') || 'openwrt.org', + route_host = uci.get('luci', 'diag', 'route') || 'openwrt.org'; + + return E([], [ + E('h2', {}, [ _('Network Utilities') ]), + E('div', { 'class': 'table' }, [ + E('div', { 'class': 'tr' }, [ + E('div', { 'class': 'td left' }, [ + E('input', { + 'style': 'margin:5px 0', + 'type': 'text', + 'value': ping_host + }), + E('span', { 'class': 'diag-action' }, [ + has_ping6 ? new ui.ComboButton('ping', { + 'ping': '%s %s'.format(_('IPv4'), _('Ping')), + 'ping6': '%s %s'.format(_('IPv6'), _('Ping')), + }, { + 'click': ui.createHandlerFn(this, 'handlePing'), + 'classes': { + 'ping': 'cbi-button cbi-button-action', + 'ping6': 'cbi-button cbi-button-action' + } + }).render() : E('button', { + 'class': 'cbi-button cbi-button-action', + 'click': ui.createHandlerFn(this, 'handlePing') + }, [ _('Ping') ]) + ]) + ]), + + E('div', { 'class': 'td left' }, [ + E('input', { + 'style': 'margin:5px 0', + 'type': 'text', + 'value': route_host + }), + E('span', { 'class': 'diag-action' }, [ + has_traceroute6 ? new ui.ComboButton('traceroute', { + 'traceroute': '%s %s'.format(_('IPv4'), _('Traceroute')), + 'traceroute6': '%s %s'.format(_('IPv6'), _('Traceroute')), + }, { + 'click': ui.createHandlerFn(this, 'handleTraceroute'), + 'classes': { + 'traceroute': 'cbi-button cbi-button-action', + 'traceroute6': 'cbi-button cbi-button-action' + } + }).render() : E('button', { + 'class': 'cbi-button cbi-button-action', + 'click': ui.createHandlerFn(this, 'handleTraceroute') + }, [ _('Traceroute') ]) + ]) + ]), + + E('div', { 'class': 'td left' }, [ + E('input', { + 'style': 'margin:5px 0', + 'type': 'text', + 'value': dns_host + }), + E('span', { 'class': 'diag-action' }, [ + E('button', { + 'class': 'cbi-button cbi-button-action', + 'click': ui.createHandlerFn(this, 'handleNslookup') + }, [ _('Nslookup') ]) + ]) + ]) + ]) + ]), + E('pre', { 'class': 'command-output', 'style': 'display:none' }) + ]); + }, + + handleSaveApply: null, + handleSave: null, + handleReset: null +}); diff --git a/modules/luci-mod-network/luasrc/controller/admin/network.lua b/modules/luci-mod-network/luasrc/controller/admin/network.lua index 4e9d27e8a..109c59f2a 100644 --- a/modules/luci-mod-network/luasrc/controller/admin/network.lua +++ b/modules/luci-mod-network/luasrc/controller/admin/network.lua @@ -66,45 +66,3 @@ function remote_addr() luci.http.prepare_content("application/json") luci.http.write_json(result) end - -function diag_command(cmd, addr) - if addr and addr:match("^[a-zA-Z0-9%-%.:_]+$") then - luci.http.prepare_content("text/plain") - - local util = io.popen(cmd % luci.util.shellquote(addr)) - if util then - while true do - local ln = util:read("*l") - if not ln then break end - luci.http.write(ln) - luci.http.write("\n") - end - - util:close() - end - - return - end - - luci.http.status(500, "Bad address") -end - -function diag_ping(addr) - diag_command("ping -c 5 -W 1 %s 2>&1", addr) -end - -function diag_traceroute(addr) - diag_command("traceroute -q 1 -w 1 -n %s 2>&1", addr) -end - -function diag_nslookup(addr) - diag_command("nslookup %s 2>&1", addr) -end - -function diag_ping6(addr) - diag_command("ping6 -c 5 %s 2>&1", addr) -end - -function diag_traceroute6(addr) - diag_command("traceroute6 -q 1 -w 2 -n %s 2>&1", addr) -end diff --git a/modules/luci-mod-network/luasrc/view/admin_network/diagnostics.htm b/modules/luci-mod-network/luasrc/view/admin_network/diagnostics.htm deleted file mode 100644 index 03dd5aab2..000000000 --- a/modules/luci-mod-network/luasrc/view/admin_network/diagnostics.htm +++ /dev/null @@ -1,117 +0,0 @@ -<%# - Copyright 2010 Jo-Philipp Wich - Licensed to the public under the Apache License 2.0. --%> - -<%+header%> - -<% -local fs = require "nixio.fs" -local has_ping6 = fs.access("/bin/ping6") or fs.access("/usr/bin/ping6") -local has_traceroute6 = fs.access("/bin/traceroute6") or fs.access("/usr/bin/traceroute6") - -local dns_host = luci.config.diag and luci.config.diag.dns or "dev.openwrt.org" -local ping_host = luci.config.diag and luci.config.diag.ping or "dev.openwrt.org" -local route_host = luci.config.diag and luci.config.diag.route or "dev.openwrt.org" -%> - - - -
-
-

<%:Diagnostics%>

- -
- <%:Network Utilities%> - -
-
-
-
- <% if has_ping6 then %> - - - - - <% else %> - - <% end %> -
- -
-
- <% if has_traceroute6 then %> - - - - - <% else %> - - <% end %> - <% if not has_traceroute6 then %> -

 

-

<%:Install iputils-traceroute6 for IPv6 traceroute%>

- <% end %> -
- -
-
- -
-
-
-
-
- - -
- -<%+footer%> diff --git a/modules/luci-mod-network/root/usr/share/luci/menu.d/luci-mod-network.json b/modules/luci-mod-network/root/usr/share/luci/menu.d/luci-mod-network.json index 990c5d35e..670f2c1a4 100644 --- a/modules/luci-mod-network/root/usr/share/luci/menu.d/luci-mod-network.json +++ b/modules/luci-mod-network/root/usr/share/luci/menu.d/luci-mod-network.json @@ -78,53 +78,8 @@ "title": "Diagnostics", "order": 60, "action": { - "type": "template", - "path": "admin_network/diagnostics" - } - }, - - "admin/network/diag_ping/*": { - "action": { - "post": true, - "type": "call", - "module": "luci.controller.admin.network", - "function": "diag_ping" - } - }, - - "admin/network/diag_nslookup/*": { - "action": { - "post": true, - "type": "call", - "module": "luci.controller.admin.network", - "function": "diag_nslookup" - } - }, - - "admin/network/diag_traceroute/*": { - "action": { - "post": true, - "type": "call", - "module": "luci.controller.admin.network", - "function": "diag_traceroute" - } - }, - - "admin/network/diag_ping6/*": { - "action": { - "post": true, - "type": "call", - "module": "luci.controller.admin.network", - "function": "diag_ping6" - } - }, - - "admin/network/diag_traceroute6/*": { - "action": { - "post": true, - "type": "call", - "module": "luci.controller.admin.network", - "function": "diag_traceroute6" + "type": "view", + "path": "network/diagnostics" } } } -- 2.25.1