From: t123yh Date: Wed, 3 Aug 2016 10:19:04 +0000 (+0800) Subject: luci-app-commands: Allow executing without downloading on public links X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=077ac32635dc0048a684ee0a40345a262e661ecc;p=oweals%2Fluci.git luci-app-commands: Allow executing without downloading on public links Signed-off-by: t123yh --- diff --git a/applications/luci-app-commands/luasrc/controller/commands.lua b/applications/luci-app-commands/luasrc/controller/commands.lua index 16528d117..ca91813b1 100644 --- a/applications/luci-app-commands/luasrc/controller/commands.lua +++ b/applications/luci-app-commands/luasrc/controller/commands.lua @@ -153,8 +153,8 @@ local function parse_cmdline(cmdid, args) end end -function action_run(...) - local fs = require "nixio.fs" +function execute_command(callback, ...) + local fs = require "nixio.fs" local argv = parse_cmdline(...) if argv then local outfile = os.tmpname() @@ -169,8 +169,8 @@ function action_run(...) local binary = not not (stdout:match("[%z\1-\8\14-\31]")) - luci.http.prepare_content("application/json") - luci.http.write_json({ + callback({ + ok = true, command = table.concat(argv, " "), stdout = not binary and stdout, stderr = stderr, @@ -178,10 +178,41 @@ function action_run(...) binary = binary }) else - luci.http.status(404, "No such command") + callback({ + ok = false, + code = 404, + reason = "No such command" + }) + end +end + +function return_json(result) + if result.ok then + luci.http.prepare_content("application/json") + luci.http.write_json(result) + else + luci.http.status(result.code, result.reason) end end +function action_run(...) + execute_command(return_json, ...) +end + +function return_html(result) + if result.ok then + require("luci.template") + luci.template.render("commands_public", { + exitcode = result.exitcode, + stdout = result.stdout, + stderr = result.stderr + }) + else + luci.http.status(result.code, result.reason) + end + +end + function action_download(...) local fs = require "nixio.fs" local argv = parse_cmdline(...) @@ -192,11 +223,11 @@ function action_download(...) local name if chunk:match("[%z\1-\8\14-\31]") then luci.http.header("Content-Disposition", "attachment; filename=%s" - % fs.basename(argv[1]):gsub("%W+", ".") .. ".bin") + % fs.basename(argv[1]):gsub("%W+", ".") .. ".bin") luci.http.prepare_content("application/octet-stream") else luci.http.header("Content-Disposition", "attachment; filename=%s" - % fs.basename(argv[1]):gsub("%W+", ".") .. ".txt") + % fs.basename(argv[1]):gsub("%W+", ".") .. ".txt") luci.http.prepare_content("text/plain") end @@ -214,14 +245,24 @@ function action_download(...) end end + function action_public(cmdid, args) + local disp = false + if string.sub(cmdid, -1) == "s" then + disp = true + cmdid = string.sub(cmdid, 1, -2) + end local uci = require "luci.model.uci".cursor() if cmdid and - uci:get("luci", cmdid) == "command" and - uci:get("luci", cmdid, "public") == "1" - then - action_download(cmdid, args) - else - luci.http.status(403, "Access to command denied") + uci:get("luci", cmdid) == "command" and + uci:get("luci", cmdid, "public") == "1" + then + if disp then + execute_command(return_html, cmdid, args) + else + action_download(cmdid, args) + end + else + luci.http.status(403, "Access to command denied") + end end -end diff --git a/applications/luci-app-commands/luasrc/view/commands.htm b/applications/luci-app-commands/luasrc/view/commands.htm index 73b9e6a2c..f094e186d 100644 --- a/applications/luci-app-commands/luasrc/view/commands.htm +++ b/applications/luci-app-commands/luasrc/view/commands.htm @@ -108,16 +108,19 @@ if (legend && output) { - var link = location.protocol + '//' + location.hostname + + var prefix = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '') + - location.pathname.split(';')[0] + 'command/' + - id + (args ? '/' + args : ''); - + location.pathname.split(';')[0] + 'command/'; + var suffix = (args ? '/' + args : ''); + + var link = prefix + id + suffix; + var link_nodownload = prefix + id + "s" + suffix; + legend.style.display = 'none'; output.parentNode.style.display = 'block'; output.innerHTML = String.format( - '
<%:Access command with%> %s
', - link, link + '

<%:Download execution result%> %s

<%:Or display result%> %s

', + link, link, link_nodownload, link_nodownload ); location.hash = '#output'; diff --git a/applications/luci-app-commands/luasrc/view/commands_public.htm b/applications/luci-app-commands/luasrc/view/commands_public.htm new file mode 100644 index 000000000..f20799d40 --- /dev/null +++ b/applications/luci-app-commands/luasrc/view/commands_public.htm @@ -0,0 +1,50 @@ +<%# + Copyright 2016 t123yh + Licensed to the public under the Apache License 2.0. +-%> + +<% css = [[ +.alert-success { + color: #3c763d; + background-color: #dff0d8; + border-color: #d6e9c6; +} + +.alert { + padding: 15px; + margin-bottom: 20px; + border: 1px solid transparent; + border-radius: 4px; +} + +.alert-warning { + color: #8a6d3b; + background-color: #fcf8e3; + border-color: #faebcc; +} +]] -%> + +<%+header%> + +<% if exitcode == 0 then %> + +<% else %> + +<% end %> + +<% if stdout ~= "" then %> +

<%:Standard Output%>

+
<%= stdout %>
+<% end %> + +<% if stderr ~= "" then %> +

<%:Standard Error%>

+
<%= stderr %>
+<% end %> + + + +<%+footer%> \ No newline at end of file diff --git a/applications/luci-app-commands/po/en/commands.po b/applications/luci-app-commands/po/en/commands.po index 754a229c1..c40994f34 100644 --- a/applications/luci-app-commands/po/en/commands.po +++ b/applications/luci-app-commands/po/en/commands.po @@ -11,9 +11,6 @@ msgstr "" msgid "A short textual description of the configured command" msgstr "A short textual description of the configured command" -msgid "Access command with" -msgstr "Access command with" - msgid "" "Allow executing the command and downloading its output without prior " "authentication" @@ -93,3 +90,21 @@ msgstr "" msgid "Waiting for command to complete..." msgstr "Waiting for command to complete..." + +msgid "Command executed successfully." +msgstr "Command executed successfully." + +msgid "Command exited with status code " +msgstr "Command exited with status code " + +msgid "Standard Output" +msgstr "Standard Output" + +msgid "Standard Error" +msgstr "Standard Error" + +msgid "Download execution result" +msgstr "Download execution result" + +msgid "Or display result" +msgstr "Or display result" \ No newline at end of file diff --git a/applications/luci-app-commands/po/zh-cn/commands.po b/applications/luci-app-commands/po/zh-cn/commands.po index 8b2b032b6..1d17fc3b3 100644 --- a/applications/luci-app-commands/po/zh-cn/commands.po +++ b/applications/luci-app-commands/po/zh-cn/commands.po @@ -14,9 +14,6 @@ msgstr "" msgid "A short textual description of the configured command" msgstr "简短描述命令用途" -msgid "Access command with" -msgstr "访问命令" - msgid "" "Allow executing the command and downloading its output without prior " "authentication" @@ -92,3 +89,21 @@ msgstr "此页面允许您配置自定义Shell命令,并可以从Web界面调 msgid "Waiting for command to complete..." msgstr "等待命令执行完成... ..." + +msgid "Command executed successfully." +msgstr "命令成功执行。" + +msgid "Command exited with status code " +msgstr "命令退出,状态码:" + +msgid "Standard Output" +msgstr "标准输出流" + +msgid "Standard Error" +msgstr "标准错误流" + +msgid "Download execution result" +msgstr "下载执行结果" + +msgid "Or display result" +msgstr "显示执行结果" \ No newline at end of file