plugins/mod_admin_telnet.lua
author Kim Alvefur <zash@zash.se>
Sat, 23 Mar 2024 20:48:19 +0100
changeset 13465 c673ff1075bd
parent 12981 74b9e05af71e
permissions -rw-r--r--
mod_posix: Move everything to util.startup This allows greater control over the order of events. Notably, the internal ordering between daemonization, initialization of libunbound and setup of signal handling is sensitive. libunbound starts a separate thread for processing DNS requests. If this thread is started before signal handling has been set up, it will not inherit the signal handlers and instead behave as it would have before signal handlers were set up, i.e. cause the whole process to immediately exit. libunbound is usually initialized on the first DNS request, usually triggered by an outgoing s2s connection attempt. If daemonization happens before signals have been set up, signals may not be processed at all.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1506
diff changeset
     1
-- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2870
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2870
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5770
diff changeset
     4
--
758
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 736
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
b1885732e979 GPL->MIT!
Matthew Wild <mwild1@gmail.com>
parents: 736
diff changeset
     6
-- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
     7
--
9407
f40b0cd41a87 mod_admin_telnet: Remove or rename various unused arguments and variables [luacheck]
Kim Alvefur <zash@zash.se>
parents: 9406
diff changeset
     8
-- luacheck: ignore 212/self
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 461
diff changeset
     9
4623
403b56b78018 mod_posix, mod_bosh, mod_admin_telnet: Use module:set_global()
Kim Alvefur <zash@zash.se>
parents: 4582
diff changeset
    10
module:set_global();
10861
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    11
module:depends("admin_shell");
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    12
4989
573123ff2ab0 mod_admin_telnet: Always handle commands terminated by line feeds - ensures consistency even when packets are joined or split on the network
Matthew Wild <mwild1@gmail.com>
parents: 4979
diff changeset
    13
local console_listener = { default_port = 5582; default_mode = "*a"; interface = "127.0.0.1" };
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    14
12981
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 10863
diff changeset
    15
local async = require "prosody.util.async";
74b9e05af71e plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 10863
diff changeset
    16
local st = require "prosody.util.stanza";
1315
bfcd3f0a49df mod_console: Much improved module load/unload/reload commands
Matthew Wild <mwild1@gmail.com>
parents: 1241
diff changeset
    17
10861
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    18
local def_env = module:shared("admin_shell/env");
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    19
local default_env_mt = { __index = def_env };
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    20
10861
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    21
local function printbanner(session)
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    22
	local option = module:get_option_string("console_banner", "full");
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    23
	if option == "full" or option == "graphic" then
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    24
		session.print [[
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    25
                   ____                \   /     _
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    26
                    |  _ \ _ __ ___  ___  _-_   __| |_   _
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    27
                    | |_) | '__/ _ \/ __|/ _ \ / _` | | | |
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    28
                    |  __/| | | (_) \__ \ |_| | (_| | |_| |
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    29
                    |_|   |_|  \___/|___/\___/ \__,_|\__, |
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    30
                    A study in simplicity            |___/
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    31
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    32
]]
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    33
	end
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    34
	if option == "short" or option == "full" then
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    35
	session.print("Welcome to the Prosody administration console. For a list of commands, type: help");
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    36
	session.print("You may find more help on using this console in our online documentation at ");
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    37
	session.print("https://prosody.im/doc/console\n");
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    38
	end
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    39
	if option ~= "short" and option ~= "full" and option ~= "graphic" then
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    40
		session.print(option);
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    41
	end
1342
947d94e3619f mod_console: Redirect print() to console session when executing commands in global environment
Matthew Wild <mwild1@gmail.com>
parents: 1341
diff changeset
    42
end
947d94e3619f mod_console: Redirect print() to console session when executing commands in global environment
Matthew Wild <mwild1@gmail.com>
parents: 1341
diff changeset
    43
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    44
console = {};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    45
9739
2d8ca54ecbc6 mod_admin_telnet: Enable async processing using util.async
Kim Alvefur <zash@zash.se>
parents: 9738
diff changeset
    46
local runner_callbacks = {};
2d8ca54ecbc6 mod_admin_telnet: Enable async processing using util.async
Kim Alvefur <zash@zash.se>
parents: 9738
diff changeset
    47
2d8ca54ecbc6 mod_admin_telnet: Enable async processing using util.async
Kim Alvefur <zash@zash.se>
parents: 9738
diff changeset
    48
function runner_callbacks:ready()
2d8ca54ecbc6 mod_admin_telnet: Enable async processing using util.async
Kim Alvefur <zash@zash.se>
parents: 9738
diff changeset
    49
	self.data.conn:resume();
2d8ca54ecbc6 mod_admin_telnet: Enable async processing using util.async
Kim Alvefur <zash@zash.se>
parents: 9738
diff changeset
    50
end
2d8ca54ecbc6 mod_admin_telnet: Enable async processing using util.async
Kim Alvefur <zash@zash.se>
parents: 9738
diff changeset
    51
2d8ca54ecbc6 mod_admin_telnet: Enable async processing using util.async
Kim Alvefur <zash@zash.se>
parents: 9738
diff changeset
    52
function runner_callbacks:waiting()
2d8ca54ecbc6 mod_admin_telnet: Enable async processing using util.async
Kim Alvefur <zash@zash.se>
parents: 9738
diff changeset
    53
	self.data.conn:pause();
2d8ca54ecbc6 mod_admin_telnet: Enable async processing using util.async
Kim Alvefur <zash@zash.se>
parents: 9738
diff changeset
    54
end
2d8ca54ecbc6 mod_admin_telnet: Enable async processing using util.async
Kim Alvefur <zash@zash.se>
parents: 9738
diff changeset
    55
2d8ca54ecbc6 mod_admin_telnet: Enable async processing using util.async
Kim Alvefur <zash@zash.se>
parents: 9738
diff changeset
    56
function runner_callbacks:error(err)
2d8ca54ecbc6 mod_admin_telnet: Enable async processing using util.async
Kim Alvefur <zash@zash.se>
parents: 9738
diff changeset
    57
	module:log("error", "Traceback[telnet]: %s", err);
10072
73a8192058f9 mod_admin_telnet: Move error handling to thread callback (fixes #1391)
Kim Alvefur <zash@zash.se>
parents: 10071
diff changeset
    58
73a8192058f9 mod_admin_telnet: Move error handling to thread callback (fixes #1391)
Kim Alvefur <zash@zash.se>
parents: 10071
diff changeset
    59
	self.data.print("Fatal error while running command, it did not complete");
73a8192058f9 mod_admin_telnet: Move error handling to thread callback (fixes #1391)
Kim Alvefur <zash@zash.se>
parents: 10071
diff changeset
    60
	self.data.print("Error: "..tostring(err));
9739
2d8ca54ecbc6 mod_admin_telnet: Enable async processing using util.async
Kim Alvefur <zash@zash.se>
parents: 9738
diff changeset
    61
end
2d8ca54ecbc6 mod_admin_telnet: Enable async processing using util.async
Kim Alvefur <zash@zash.se>
parents: 9738
diff changeset
    62
2d8ca54ecbc6 mod_admin_telnet: Enable async processing using util.async
Kim Alvefur <zash@zash.se>
parents: 9738
diff changeset
    63
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    64
function console:new_session(conn)
2145
daeb6ebf304c mod_console: Update for new net.server API
Matthew Wild <mwild1@gmail.com>
parents: 2087
diff changeset
    65
	local w = function(s) conn:write(s:gsub("\n", "\r\n")); end;
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    66
	local session = { conn = conn;
10861
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    67
			send = function (t)
10863
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10861
diff changeset
    68
				if st.is_stanza(t) and (t.name == "repl-result" or t.name == "repl-output") then
10861
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    69
					t = "| "..t:get_text().."\n";
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    70
				end
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    71
				w(tostring(t));
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
    72
			end;
3404
33c81ee280e3 mod_console: Added support for multiple arguments to print().
Waqas Hussain <waqas20@gmail.com>
parents: 3044
diff changeset
    73
			print = function (...)
33c81ee280e3 mod_console: Added support for multiple arguments to print().
Waqas Hussain <waqas20@gmail.com>
parents: 3044
diff changeset
    74
				local t = {};
33c81ee280e3 mod_console: Added support for multiple arguments to print().
Waqas Hussain <waqas20@gmail.com>
parents: 3044
diff changeset
    75
				for i=1,select("#", ...) do
33c81ee280e3 mod_console: Added support for multiple arguments to print().
Waqas Hussain <waqas20@gmail.com>
parents: 3044
diff changeset
    76
					t[i] = tostring(select(i, ...));
33c81ee280e3 mod_console: Added support for multiple arguments to print().
Waqas Hussain <waqas20@gmail.com>
parents: 3044
diff changeset
    77
				end
33c81ee280e3 mod_console: Added support for multiple arguments to print().
Waqas Hussain <waqas20@gmail.com>
parents: 3044
diff changeset
    78
				w("| "..table.concat(t, "\t").."\n");
33c81ee280e3 mod_console: Added support for multiple arguments to print().
Waqas Hussain <waqas20@gmail.com>
parents: 3044
diff changeset
    79
			end;
10800
89d810a23ee0 mod_admin_telnet: Reuse existing pretty printing setup
Kim Alvefur <zash@zash.se>
parents: 10799
diff changeset
    80
			serialize = tostring;
2253
a3537266a916 mod_console: Update for new server API, fixes traceback when closing console sessions
Matthew Wild <mwild1@gmail.com>
parents: 2145
diff changeset
    81
			disconnect = function () conn:close(); end;
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    82
			};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    83
	session.env = setmetatable({}, default_env_mt);
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5770
diff changeset
    84
9739
2d8ca54ecbc6 mod_admin_telnet: Enable async processing using util.async
Kim Alvefur <zash@zash.se>
parents: 9738
diff changeset
    85
	session.thread = async.runner(function (line)
2d8ca54ecbc6 mod_admin_telnet: Enable async processing using util.async
Kim Alvefur <zash@zash.se>
parents: 9738
diff changeset
    86
		console:process_line(session, line);
2d8ca54ecbc6 mod_admin_telnet: Enable async processing using util.async
Kim Alvefur <zash@zash.se>
parents: 9738
diff changeset
    87
		session.send(string.char(0));
2d8ca54ecbc6 mod_admin_telnet: Enable async processing using util.async
Kim Alvefur <zash@zash.se>
parents: 9738
diff changeset
    88
	end, runner_callbacks, session);
2d8ca54ecbc6 mod_admin_telnet: Enable async processing using util.async
Kim Alvefur <zash@zash.se>
parents: 9738
diff changeset
    89
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    90
	-- Load up environment with helper objects
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    91
	for name, t in pairs(def_env) do
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    92
		if type(t) == "table" then
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    93
			session.env[name] = setmetatable({ session = session }, { __index = t });
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    94
		end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    95
	end
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5770
diff changeset
    96
10800
89d810a23ee0 mod_admin_telnet: Reuse existing pretty printing setup
Kim Alvefur <zash@zash.se>
parents: 10799
diff changeset
    97
	session.env.output:configure();
89d810a23ee0 mod_admin_telnet: Reuse existing pretty printing setup
Kim Alvefur <zash@zash.se>
parents: 10799
diff changeset
    98
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
    99
	return session;
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   100
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   101
5186
ad898e50b8f3 mod_admin_telnet: Refactor so that command processing is performed in a separate function (usable from other modules)
Matthew Wild <mwild1@gmail.com>
parents: 5168
diff changeset
   102
function console:process_line(session, line)
10861
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
   103
	line = line:gsub("\r?\n$", "");
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
   104
	if line == "bye" or line == "quit" or line == "exit" or line:byte() == 4 then
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
   105
		session.print("See you!");
826023c3322b mod_admin_telnet: Become a front for mod_admin_shell
Matthew Wild <mwild1@gmail.com>
parents: 10849
diff changeset
   106
		session:disconnect();
10071
598befab492e mod_admin_telnet: Check for simple commands before executing in sandbox
Kim Alvefur <zash@zash.se>
parents: 10048
diff changeset
   107
		return;
598befab492e mod_admin_telnet: Check for simple commands before executing in sandbox
Kim Alvefur <zash@zash.se>
parents: 10048
diff changeset
   108
	end
10863
8de0057b4279 mod_admin_shell, mod_admin_telnet, util.prosodyctl.shell: Separate output from final result
Matthew Wild <mwild1@gmail.com>
parents: 10861
diff changeset
   109
	return module:fire_event("admin/repl-input", { origin = session, stanza = st.stanza("repl-input"):text(line) });
5186
ad898e50b8f3 mod_admin_telnet: Refactor so that command processing is performed in a separate function (usable from other modules)
Matthew Wild <mwild1@gmail.com>
parents: 5168
diff changeset
   110
end
ad898e50b8f3 mod_admin_telnet: Refactor so that command processing is performed in a separate function (usable from other modules)
Matthew Wild <mwild1@gmail.com>
parents: 5168
diff changeset
   111
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   112
local sessions = {};
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   113
10849
785fa0112411 mod_admin_telnet: Update existing sessions on reload
Kim Alvefur <zash@zash.se>
parents: 10802
diff changeset
   114
function module.save()
785fa0112411 mod_admin_telnet: Update existing sessions on reload
Kim Alvefur <zash@zash.se>
parents: 10802
diff changeset
   115
	return { sessions = sessions }
785fa0112411 mod_admin_telnet: Update existing sessions on reload
Kim Alvefur <zash@zash.se>
parents: 10802
diff changeset
   116
end
785fa0112411 mod_admin_telnet: Update existing sessions on reload
Kim Alvefur <zash@zash.se>
parents: 10802
diff changeset
   117
785fa0112411 mod_admin_telnet: Update existing sessions on reload
Kim Alvefur <zash@zash.se>
parents: 10802
diff changeset
   118
function module.restore(data)
785fa0112411 mod_admin_telnet: Update existing sessions on reload
Kim Alvefur <zash@zash.se>
parents: 10802
diff changeset
   119
	if data.sessions then
785fa0112411 mod_admin_telnet: Update existing sessions on reload
Kim Alvefur <zash@zash.se>
parents: 10802
diff changeset
   120
		for conn in pairs(data.sessions) do
785fa0112411 mod_admin_telnet: Update existing sessions on reload
Kim Alvefur <zash@zash.se>
parents: 10802
diff changeset
   121
			conn:setlistener(console_listener);
785fa0112411 mod_admin_telnet: Update existing sessions on reload
Kim Alvefur <zash@zash.se>
parents: 10802
diff changeset
   122
			local session = console:new_session(conn);
785fa0112411 mod_admin_telnet: Update existing sessions on reload
Kim Alvefur <zash@zash.se>
parents: 10802
diff changeset
   123
			sessions[conn] = session;
785fa0112411 mod_admin_telnet: Update existing sessions on reload
Kim Alvefur <zash@zash.se>
parents: 10802
diff changeset
   124
		end
785fa0112411 mod_admin_telnet: Update existing sessions on reload
Kim Alvefur <zash@zash.se>
parents: 10802
diff changeset
   125
	end
785fa0112411 mod_admin_telnet: Update existing sessions on reload
Kim Alvefur <zash@zash.se>
parents: 10802
diff changeset
   126
end
785fa0112411 mod_admin_telnet: Update existing sessions on reload
Kim Alvefur <zash@zash.se>
parents: 10802
diff changeset
   127
3009
06f7d8054065 mod_console: Make use of the new onconnect callback to initialise session and send banner
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   128
function console_listener.onconnect(conn)
06f7d8054065 mod_console: Make use of the new onconnect callback to initialise session and send banner
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   129
	-- Handle new connection
06f7d8054065 mod_console: Make use of the new onconnect callback to initialise session and send banner
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   130
	local session = console:new_session(conn);
06f7d8054065 mod_console: Make use of the new onconnect callback to initialise session and send banner
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   131
	sessions[conn] = session;
06f7d8054065 mod_console: Make use of the new onconnect callback to initialise session and send banner
Matthew Wild <mwild1@gmail.com>
parents: 2925
diff changeset
   132
	printbanner(session);
669
9255abbb3068 mod_console: replace all \n with \r\n in the output, and send \0 as a marker character after every response
Waqas Hussain <waqas20@gmail.com>
parents: 615
diff changeset
   133
	session.send(string.char(0));
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   134
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   135
2145
daeb6ebf304c mod_console: Update for new net.server API
Matthew Wild <mwild1@gmail.com>
parents: 2087
diff changeset
   136
function console_listener.onincoming(conn, data)
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   137
	local session = sessions[conn];
1317
f6e56a555c37 mod_console: Allow running code in the global environment by prefixing with '>'
Matthew Wild <mwild1@gmail.com>
parents: 1316
diff changeset
   138
4989
573123ff2ab0 mod_admin_telnet: Always handle commands terminated by line feeds - ensures consistency even when packets are joined or split on the network
Matthew Wild <mwild1@gmail.com>
parents: 4979
diff changeset
   139
	local partial = session.partial_data;
573123ff2ab0 mod_admin_telnet: Always handle commands terminated by line feeds - ensures consistency even when packets are joined or split on the network
Matthew Wild <mwild1@gmail.com>
parents: 4979
diff changeset
   140
	if partial then
573123ff2ab0 mod_admin_telnet: Always handle commands terminated by line feeds - ensures consistency even when packets are joined or split on the network
Matthew Wild <mwild1@gmail.com>
parents: 4979
diff changeset
   141
		data = partial..data;
573123ff2ab0 mod_admin_telnet: Always handle commands terminated by line feeds - ensures consistency even when packets are joined or split on the network
Matthew Wild <mwild1@gmail.com>
parents: 4979
diff changeset
   142
	end
573123ff2ab0 mod_admin_telnet: Always handle commands terminated by line feeds - ensures consistency even when packets are joined or split on the network
Matthew Wild <mwild1@gmail.com>
parents: 4979
diff changeset
   143
573123ff2ab0 mod_admin_telnet: Always handle commands terminated by line feeds - ensures consistency even when packets are joined or split on the network
Matthew Wild <mwild1@gmail.com>
parents: 4979
diff changeset
   144
	for line in data:gmatch("[^\n]*[\n\004]") do
5278
f79be67e5666 mod_admin_telnet: Stop processing lines when session is closed
Kim Alvefur <zash@zash.se>
parents: 5270
diff changeset
   145
		if session.closed then return end
9739
2d8ca54ecbc6 mod_admin_telnet: Enable async processing using util.async
Kim Alvefur <zash@zash.se>
parents: 9738
diff changeset
   146
		session.thread:run(line);
4989
573123ff2ab0 mod_admin_telnet: Always handle commands terminated by line feeds - ensures consistency even when packets are joined or split on the network
Matthew Wild <mwild1@gmail.com>
parents: 4979
diff changeset
   147
	end
573123ff2ab0 mod_admin_telnet: Always handle commands terminated by line feeds - ensures consistency even when packets are joined or split on the network
Matthew Wild <mwild1@gmail.com>
parents: 4979
diff changeset
   148
	session.partial_data = data:match("[^\n]+$");
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   149
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   150
6169
cb15eac75b50 mod_admin_telnet: Send NUL byte as keepalive on read timeouts
Kim Alvefur <zash@zash.se>
parents: 6067
diff changeset
   151
function console_listener.onreadtimeout(conn)
cb15eac75b50 mod_admin_telnet: Send NUL byte as keepalive on read timeouts
Kim Alvefur <zash@zash.se>
parents: 6067
diff changeset
   152
	local session = sessions[conn];
cb15eac75b50 mod_admin_telnet: Send NUL byte as keepalive on read timeouts
Kim Alvefur <zash@zash.se>
parents: 6067
diff changeset
   153
	if session then
cb15eac75b50 mod_admin_telnet: Send NUL byte as keepalive on read timeouts
Kim Alvefur <zash@zash.se>
parents: 6067
diff changeset
   154
		session.send("\0");
cb15eac75b50 mod_admin_telnet: Send NUL byte as keepalive on read timeouts
Kim Alvefur <zash@zash.se>
parents: 6067
diff changeset
   155
		return true;
cb15eac75b50 mod_admin_telnet: Send NUL byte as keepalive on read timeouts
Kim Alvefur <zash@zash.se>
parents: 6067
diff changeset
   156
	end
cb15eac75b50 mod_admin_telnet: Send NUL byte as keepalive on read timeouts
Kim Alvefur <zash@zash.se>
parents: 6067
diff changeset
   157
end
cb15eac75b50 mod_admin_telnet: Send NUL byte as keepalive on read timeouts
Kim Alvefur <zash@zash.se>
parents: 6067
diff changeset
   158
9407
f40b0cd41a87 mod_admin_telnet: Remove or rename various unused arguments and variables [luacheck]
Kim Alvefur <zash@zash.se>
parents: 9406
diff changeset
   159
function console_listener.ondisconnect(conn, err) -- luacheck: ignore 212/err
2054
a43aea9b0bd1 mod_console: Added proper cleanup for disconnected console sessions.
Waqas Hussain <waqas20@gmail.com>
parents: 2010
diff changeset
   160
	local session = sessions[conn];
a43aea9b0bd1 mod_console: Added proper cleanup for disconnected console sessions.
Waqas Hussain <waqas20@gmail.com>
parents: 2010
diff changeset
   161
	if session then
a43aea9b0bd1 mod_console: Added proper cleanup for disconnected console sessions.
Waqas Hussain <waqas20@gmail.com>
parents: 2010
diff changeset
   162
		session.disconnect();
a43aea9b0bd1 mod_console: Added proper cleanup for disconnected console sessions.
Waqas Hussain <waqas20@gmail.com>
parents: 2010
diff changeset
   163
		sessions[conn] = nil;
a43aea9b0bd1 mod_console: Added proper cleanup for disconnected console sessions.
Waqas Hussain <waqas20@gmail.com>
parents: 2010
diff changeset
   164
	end
736
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   165
end
7cbae2d16fd6 mod_console: Make global
Matthew Wild <mwild1@gmail.com>
parents: 712
diff changeset
   166
6380
4220ffb87b22 net.http, net.http.server, mod_c2s, mod_s2s, mod_component, mod_admin_telnet, mod_net_multiplex: Add ondetach to release connection from 'sessions' table (or equivalent)
Matthew Wild <mwild1@gmail.com>
parents: 6311
diff changeset
   167
function console_listener.ondetach(conn)
4220ffb87b22 net.http, net.http.server, mod_c2s, mod_s2s, mod_component, mod_admin_telnet, mod_net_multiplex: Add ondetach to release connection from 'sessions' table (or equivalent)
Matthew Wild <mwild1@gmail.com>
parents: 6311
diff changeset
   168
	sessions[conn] = nil;
4220ffb87b22 net.http, net.http.server, mod_c2s, mod_s2s, mod_component, mod_admin_telnet, mod_net_multiplex: Add ondetach to release connection from 'sessions' table (or equivalent)
Matthew Wild <mwild1@gmail.com>
parents: 6311
diff changeset
   169
end
4220ffb87b22 net.http, net.http.server, mod_c2s, mod_s2s, mod_component, mod_admin_telnet, mod_net_multiplex: Add ondetach to release connection from 'sessions' table (or equivalent)
Matthew Wild <mwild1@gmail.com>
parents: 6311
diff changeset
   170
5120
bcabea740c00 mod_{admin_telnet,c2s,component,http,net_multiplex,s2s}: Use module:provides() instead of module:add_item().
Waqas Hussain <waqas20@gmail.com>
parents: 5030
diff changeset
   171
module:provides("net", {
4674
f44726a910a0 mod_admin_telnet: Add initial port:list() and port:close() commands
Matthew Wild <mwild1@gmail.com>
parents: 4647
diff changeset
   172
	name = "console";
4550
1c41e4a846a2 mod_admin_telnet: Port to portmanager
Matthew Wild <mwild1@gmail.com>
parents: 4540
diff changeset
   173
	listener = console_listener;
1c41e4a846a2 mod_admin_telnet: Port to portmanager
Matthew Wild <mwild1@gmail.com>
parents: 4540
diff changeset
   174
	default_port = 5582;
4571
32d532b95dc7 mod_admin_telnet: make service private.
Marco Cirillo <maranda@lightwitch.org>
parents: 4550
diff changeset
   175
	private = true;
4550
1c41e4a846a2 mod_admin_telnet: Port to portmanager
Matthew Wild <mwild1@gmail.com>
parents: 4540
diff changeset
   176
});