Software /
code /
prosody
Comparison
plugins/mod_admin_telnet.lua @ 10787:459efb1afbfe
mod_admin_telnet: Pretty-print values returned from commands
This makes it much nicer to inspect Prosody internals.
Existing textual status messages from commands are not serialized to
preserve existing behavior. Explicit serialization of configuration is
kept in order to make it clear that returned strings are serialized
strings that would look like what's actually in the config file.
The default maxdepth of 2 seems ought to be an okay default, balanced
between showing enough structure to continue exploring and DoS-ing your
terminal.
Thanks to Ge0rG for the motivation to finally do this.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 29 Apr 2020 22:23:05 +0200 |
parent | 10701:929c95e518f0 |
child | 10788:3fc4863227a9 |
comparison
equal
deleted
inserted
replaced
10786:a1b633ba9bd9 | 10787:459efb1afbfe |
---|---|
30 local cert_verify_identity = require "util.x509".verify_identity; | 30 local cert_verify_identity = require "util.x509".verify_identity; |
31 local envload = require "util.envload".envload; | 31 local envload = require "util.envload".envload; |
32 local envloadfile = require "util.envload".envloadfile; | 32 local envloadfile = require "util.envload".envloadfile; |
33 local has_pposix, pposix = pcall(require, "util.pposix"); | 33 local has_pposix, pposix = pcall(require, "util.pposix"); |
34 local async = require "util.async"; | 34 local async = require "util.async"; |
35 local serialize = require "util.serialization".new({ fatal = false, unquoted = true}); | 35 local serialization = require "util.serialization"; |
36 local serialize_config = serialization.new ({ fatal = false, unquoted = true}); | |
36 local time = require "util.time"; | 37 local time = require "util.time"; |
37 | 38 |
38 local commands = module:shared("commands") | 39 local commands = module:shared("commands") |
39 local def_env = module:shared("env"); | 40 local def_env = module:shared("env"); |
40 local default_env_mt = { __index = def_env }; | 41 local default_env_mt = { __index = def_env }; |
78 for i=1,select("#", ...) do | 79 for i=1,select("#", ...) do |
79 t[i] = tostring(select(i, ...)); | 80 t[i] = tostring(select(i, ...)); |
80 end | 81 end |
81 w("| "..table.concat(t, "\t").."\n"); | 82 w("| "..table.concat(t, "\t").."\n"); |
82 end; | 83 end; |
84 serialize = serialization.new({ fatal = false, unquoted = true, maxdepth = 2}); | |
83 disconnect = function () conn:close(); end; | 85 disconnect = function () conn:close(); end; |
84 }; | 86 }; |
85 session.env = setmetatable({}, default_env_mt); | 87 session.env = setmetatable({}, default_env_mt); |
86 | 88 |
87 session.thread = async.runner(function (line) | 89 session.thread = async.runner(function (line) |
139 end | 141 end |
140 | 142 |
141 local taskok, message = chunk(); | 143 local taskok, message = chunk(); |
142 | 144 |
143 if not message then | 145 if not message then |
144 session.print("Result: "..tostring(taskok)); | 146 if type(taskok) ~= "string" then |
147 taskok = session.serialize(taskok); | |
148 end | |
149 session.print("Result: "..taskok); | |
145 return; | 150 return; |
146 elseif (not taskok) and message then | 151 elseif (not taskok) and message then |
147 session.print("Command completed with a problem"); | 152 session.print("Command completed with a problem"); |
148 session.print("Message: "..tostring(message)); | 153 session.print("Message: "..tostring(message)); |
149 return; | 154 return; |
150 end | 155 end |
151 | 156 |
152 session.print("OK: "..tostring(message)); | 157 if type(message) ~= "string" then |
158 message = session.serialize(message); | |
159 end | |
160 | |
161 session.print("OK: "..message); | |
153 end | 162 end |
154 | 163 |
155 local sessions = {}; | 164 local sessions = {}; |
156 | 165 |
157 function console_listener.onconnect(conn) | 166 function console_listener.onconnect(conn) |
525 function def_env.config:get(host, key) | 534 function def_env.config:get(host, key) |
526 if key == nil then | 535 if key == nil then |
527 host, key = "*", host; | 536 host, key = "*", host; |
528 end | 537 end |
529 local config_get = require "core.configmanager".get | 538 local config_get = require "core.configmanager".get |
530 return true, serialize(config_get(host, key)); | 539 return true, serialize_config(config_get(host, key)); |
531 end | 540 end |
532 | 541 |
533 function def_env.config:reload() | 542 function def_env.config:reload() |
534 local ok, err = prosody.reload_config(); | 543 local ok, err = prosody.reload_config(); |
535 return ok, (ok and "Config reloaded (you may need to reload modules to take effect)") or tostring(err); | 544 return ok, (ok and "Config reloaded (you may need to reload modules to take effect)") or tostring(err); |