Annotate

util/adminstream.lua @ 10875:09674bbb833f

util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
author Matthew Wild <mwild1@gmail.com>
date Tue, 02 Jun 2020 08:28:39 +0100
parent 10855:70ac7d23673d
child 10876:c01c39a2c7a2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10855
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local st = require "util.stanza";
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local new_xmpp_stream = require "util.xmppstream".new;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local sessionlib = require "util.session";
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local gettime = require "util.time".now;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 local runner = require "util.async".runner;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local add_task = require "util.timer".add_task;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local events = require "util.events";
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local stream_close_timeout = 5;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local log = require "util.logger".init("adminstream");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local xmlns_xmpp_streams = "urn:ietf:params:xml:ns:xmpp-streams";
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local stream_callbacks = { default_ns = "xmpp:prosody.im/admin" };
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 function stream_callbacks.streamopened(session, attr)
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 -- run _streamopened in async context
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 session.thread:run({ stream = "opened", attr = attr });
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 function stream_callbacks._streamopened(session, attr) --luacheck: ignore 212/attr
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 if session.type ~= "client" then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 session:open_stream();
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 session.notopen = nil;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 function stream_callbacks.streamclosed(session, attr)
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 -- run _streamclosed in async context
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 session.thread:run({ stream = "closed", attr = attr });
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 function stream_callbacks._streamclosed(session)
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 session.log("debug", "Received </stream:stream>");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 session:close(false);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 function stream_callbacks.error(session, error, data)
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 if error == "no-stream" then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 session.log("debug", "Invalid opening stream header (%s)", (data:gsub("^([^\1]+)\1", "{%1}")));
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 session:close("invalid-namespace");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 elseif error == "parse-error" then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 session.log("debug", "Client XML parse error: %s", data);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 session:close("not-well-formed");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 elseif error == "stream-error" then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 local condition, text = "undefined-condition";
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 for child in data:childtags(nil, xmlns_xmpp_streams) do
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 if child.name ~= "text" then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 condition = child.name;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 else
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 text = child:get_text();
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 if condition ~= "undefined-condition" and text then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 break;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 text = condition .. (text and (" ("..text..")") or "");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 session.log("info", "Session closed by remote with error: %s", text);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 session:close(nil, text);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 function stream_callbacks.handlestanza(session, stanza)
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 session.thread:run(stanza);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 local runner_callbacks = {};
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 function runner_callbacks:error(err)
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 self.data.log("error", "Traceback[c2s]: %s", err);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 local function destroy_session(session, reason)
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 if session.destroyed then return; end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 session.destroyed = true;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 session.log("debug", "Destroying session: %s", reason or "unknown reason");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82 local function session_close(session, reason)
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 local log = session.log or log;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 if session.conn then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85 if session.notopen then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 session:open_stream();
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 if reason then -- nil == no err, initiated by us, false == initiated by client
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 local stream_error = st.stanza("stream:error");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 if type(reason) == "string" then -- assume stream error
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 stream_error:tag(reason, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' });
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 elseif type(reason) == "table" then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 if reason.condition then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94 stream_error:tag(reason.condition, stream_xmlns_attr):up();
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 if reason.text then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 stream_error:tag("text", stream_xmlns_attr):text(reason.text):up();
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 if reason.extra then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 stream_error:add_child(reason.extra);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 elseif reason.name then -- a stanza
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 stream_error = reason;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 stream_error = tostring(stream_error);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106 log("debug", "Disconnecting client, <stream:error> is: %s", stream_error);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 session.send(stream_error);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 session.send("</stream:stream>");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 function session.send() return false; end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 local reason_text = (reason and (reason.name or reason.text or reason.condition)) or reason;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 session.log("debug", "c2s stream for %s closed: %s", session.full_jid or session.ip or "<unknown>", reason_text or "session closed");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 -- Authenticated incoming stream may still be sending us stanzas, so wait for </stream:stream> from remote
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 local conn = session.conn;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 if reason_text == nil and not session.notopen and session.type == "c2s" then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119 -- Grace time to process data from authenticated cleanly-closed stream
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 add_task(stream_close_timeout, function ()
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 if not session.destroyed then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 session.log("warn", "Failed to receive a stream close response, closing connection anyway...");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 destroy_session(session);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 conn:close();
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 end);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 else
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 destroy_session(session, reason_text);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129 conn:close();
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 else
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132 local reason_text = (reason and (reason.name or reason.text or reason.condition)) or reason;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 destroy_session(session, reason_text);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 --- Public methods
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138
10875
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
139 local function new_connection(socket_path, listeners)
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
140 local have_unix, unix = pcall(require, "socket.unix");
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
141 if type(unix) ~= "table" then
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
142 have_unix = false;
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
143 end
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
144 local conn, sock;
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
145
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
146 return {
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
147 connect = function ()
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
148 if not have_unix then
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
149 return nil, "no unix socket support";
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
150 end
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
151 if sock or conn then
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
152 return nil, "already connected";
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
153 end
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
154 sock = unix.stream();
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
155 sock:settimeout(0);
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
156 local ok, err = sock:connect(socket_path);
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
157 if not ok then
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
158 return nil, err;
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
159 end
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
160 conn = server.wrapclient(sock, nil, nil, listeners, "*a");
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
161 return true;
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
162 end;
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
163 disconnect = function ()
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
164 if conn then
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
165 conn:close();
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
166 conn = nil;
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
167 end
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
168 if sock then
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
169 sock:close();
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
170 sock = nil;
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
171 end
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
172 return true;
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
173 end;
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
174 };
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
175 end
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
176
10855
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177 local function new_server(sessions, stanza_handler)
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 local listeners = {};
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 function listeners.onconnect(conn)
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 log("debug", "New connection");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 local session = sessionlib.new("admin");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 sessionlib.set_id(session);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 sessionlib.set_logger(session);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 sessionlib.set_conn(session, conn);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187 session.conntime = gettime();
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 session.type = "admin";
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 local stream = new_xmpp_stream(session, stream_callbacks);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 session.stream = stream;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 session.notopen = true;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 session.thread = runner(function (stanza)
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 if st.is_stanza(stanza) then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 stanza_handler(session, stanza);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197 elseif stanza.stream == "opened" then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
198 stream_callbacks._streamopened(session, stanza.attr);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
199 elseif stanza.stream == "closed" then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
200 stream_callbacks._streamclosed(session, stanza.attr);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
201 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
202 end, runner_callbacks, session);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
203
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 function session.data(data)
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
205 -- Parse the data, which will store stanzas in session.pending_stanzas
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
206 if data then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
207 local ok, err = stream:feed(data);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
208 if not ok then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
209 session.log("debug", "Received invalid XML (%s) %d bytes: %q", err, #data, data:sub(1, 300));
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
210 session:close("not-well-formed");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
211 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
212 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
214
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
215 session.close = session_close;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
216
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
217 session.send = function (t)
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
218 session.log("debug", "Sending[%s]: %s", session.type, t.top_tag and t:top_tag() or t:match("^[^>]*>?"));
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
219 return session.rawsend(tostring(t));
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
220 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
221
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
222 function session.rawsend(t)
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
223 local ret, err = conn:write(t);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
224 if not ret then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
225 session.log("debug", "Error writing to connection: %s", err);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
226 return false, err;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
227 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
228 return true;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
229 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
230
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
231 sessions[conn] = session;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
232 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
233
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
234 function listeners.onincoming(conn, data)
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
235 local session = sessions[conn];
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
236 if session then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
237 session.data(data);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
238 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
239 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
240
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
241 function listeners.ondisconnect(conn, err)
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
242 local session = sessions[conn];
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
243 if session then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
244 session.log("info", "Admin client disconnected: %s", err or "connection closed");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
245 session.conn = nil;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
246 sessions[conn] = nil;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
247 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
248 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
249 return {
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
250 listeners = listeners;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
251 };
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
252 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
253
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 local function new_client()
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
255 local client = {
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
256 type = "client";
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
257 events = events.new();
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
258 log = log;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
259 };
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
260
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
261 local listeners = {};
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
262
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
263 function listeners.onconnect(conn)
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
264 log("debug", "Connected");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
265 client.conn = conn;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
266
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
267 local stream = new_xmpp_stream(client, stream_callbacks);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
268 client.stream = stream;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
269 client.notopen = true;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
270
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
271 client.thread = runner(function (stanza)
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
272 if st.is_stanza(stanza) then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
273 client.events.fire_event("received", stanza);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
274 elseif stanza.stream == "opened" then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
275 stream_callbacks._streamopened(client, stanza.attr);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
276 client.events.fire_event("connected");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
277 elseif stanza.stream == "closed" then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
278 client.events.fire_event("disconnected");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
279 stream_callbacks._streamclosed(client, stanza.attr);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
280 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
281 end, runner_callbacks, client);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
282
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
283 client.close = session_close;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
284
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
285 function client.send(t)
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
286 client.log("debug", "Sending: %s", t.top_tag and t:top_tag() or t:match("^[^>]*>?"));
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
287 return client.rawsend(tostring(t));
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
288 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
289
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
290 function client.rawsend(t)
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
291 local ret, err = conn:write(t);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
292 if not ret then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
293 client.log("debug", "Error writing to connection: %s", err);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
294 return false, err;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
295 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
296 return true;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
297 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
298 client.log("debug", "Opening stream...");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
299 client:open_stream();
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
300 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
301
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
302 function listeners.onincoming(conn, data) --luacheck: ignore 212/conn
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
303 local ok, err = client.stream:feed(data);
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
304 if not ok then
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
305 client.log("debug", "Received invalid XML (%s) %d bytes: %q", err, #data, data:sub(1, 300));
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
306 client:close("not-well-formed");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
307 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
308 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
310 function listeners.ondisconnect(conn, err) --luacheck: ignore 212/conn
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
311 client.log("info", "Admin client disconnected: %s", err or "connection closed");
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
312 client.conn = nil;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
313 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
314
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
315 client.listeners = listeners;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
316
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
317 return client;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
318 end
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
319
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
320 return {
10875
09674bbb833f util.prosodyctl.shell, util.adminstream: Move connection logic into adminstream for easier reuse
Matthew Wild <mwild1@gmail.com>
parents: 10855
diff changeset
321 connection = new_connection;
10855
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
322 server = new_server;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
323 client = new_client;
70ac7d23673d mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
324 };