Software /
code /
prosody
Annotate
plugins/mod_admin_socket.lua @ 10855:70ac7d23673d
mod_admin_socket, util.adminstream: New module to manage a local unix domain socket for admin functionality
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 01 Jun 2020 15:42:19 +0100 |
child | 10862:1cfae9e85021 |
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 module:set_global(); |
70ac7d23673d
mod_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 |
70ac7d23673d
mod_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 have_unix, unix = pcall(require, "socket.unix"); |
70ac7d23673d
mod_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 |
70ac7d23673d
mod_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 if not have_unix or type(unix) ~= "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
|
6 module:log_status("error", "LuaSocket unix socket support not available or incompatible, ensure it is up to date"); |
70ac7d23673d
mod_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 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
|
8 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
|
9 |
70ac7d23673d
mod_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 local server = require "net.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
|
11 |
70ac7d23673d
mod_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 local adminstream = require "util.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
|
13 |
70ac7d23673d
mod_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 local socket_path = module:get_option_string("admin_socket", prosody.paths.data.."/prosody.sock"); |
70ac7d23673d
mod_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 |
70ac7d23673d
mod_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 local sessions = module:shared("sessions"); |
70ac7d23673d
mod_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 |
70ac7d23673d
mod_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 local function fire_admin_event(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
|
19 local event_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
|
20 origin = session, stanza = 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
|
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 local event_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
|
23 if stanza.attr.xmlns 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 event_name = "admin/"..stanza.attr.xmlns..":"..stanza.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
|
25 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
|
26 event_name = "admin/"..stanza.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
|
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 module:log("debug", "Firing %s", event_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
|
29 return module:fire_event(event_name, event_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
|
30 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
|
31 |
70ac7d23673d
mod_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 module:hook("server-stopping", 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
|
33 for _, session in pairs(sessions) 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
|
34 session:close("system-shutdown"); |
70ac7d23673d
mod_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 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
|
36 os.remove(socket_path); |
70ac7d23673d
mod_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 --- Unix domain socket management |
70ac7d23673d
mod_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 |
70ac7d23673d
mod_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 local conn, sock; |
70ac7d23673d
mod_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 |
70ac7d23673d
mod_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 local listeners = adminstream.server(sessions, fire_admin_event).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
|
44 |
70ac7d23673d
mod_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 local function accept_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
|
46 module:log("debug", "accepting..."); |
70ac7d23673d
mod_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 client = sock:accept(); |
70ac7d23673d
mod_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 if not client 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
|
49 server.wrapclient(client, "unix", 0, listeners, "*a"); |
70ac7d23673d
mod_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 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
|
51 |
70ac7d23673d
mod_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 function module.load() |
70ac7d23673d
mod_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 sock = unix.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
|
54 sock:settimeout(0); |
70ac7d23673d
mod_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 os.remove(socket_path); |
70ac7d23673d
mod_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 assert(sock:bind(socket_path)); |
70ac7d23673d
mod_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 assert(sock:listen()); |
70ac7d23673d
mod_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 conn = server.watchfd(sock:getfd(), accept_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
|
59 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
|
60 |
70ac7d23673d
mod_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 function module.unload() |
70ac7d23673d
mod_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 if 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
|
63 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
|
64 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
|
65 if sock 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
|
66 sock: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
|
67 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
|
68 os.remove(socket_path); |
70ac7d23673d
mod_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 end |