Software /
code /
prosody-modules
Comparison
mod_s2s_status/mod_s2s_status.lua @ 4791:b86282953663
mod_s2s_status: Module to track status of s2s connections by domain
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 26 Nov 2021 10:10:02 +0000 |
child | 5811:31c331d05a75 |
comparison
equal
deleted
inserted
replaced
4790:bb66e87a3604 | 4791:b86282953663 |
---|---|
1 local status_out = module:shared("out"); | |
2 | |
3 local errors = require "util.error"; | |
4 | |
5 local function get_session_info(session) | |
6 local direction, peer_host = session.direction; | |
7 if direction == "outgoing" then | |
8 peer_host = session.to_host; | |
9 elseif direction == "incoming" then | |
10 peer_host = session.from_host; | |
11 end | |
12 return peer_host, direction, session.id; | |
13 end | |
14 | |
15 local function get_domain_log_out(peer_domain) | |
16 local domain_log = status_out[peer_domain]; | |
17 if not domain_log then | |
18 domain_log = {}; | |
19 status_out[peer_domain] = domain_log; | |
20 end | |
21 end | |
22 | |
23 local function get_connection_record(domain_log, id) | |
24 for _, record in ipairs(domain_log) do | |
25 if record.id == id then | |
26 return record; | |
27 end | |
28 end | |
29 -- No record for this connection yet, create it | |
30 local record = { id = id }; | |
31 table.insert(domain_log, 1, record); | |
32 return record; | |
33 end | |
34 | |
35 local function log_new_connection_out(peer_domain, id) | |
36 local domain_log = get_domain_log_out(peer_domain); | |
37 local record = get_connection_record(domain_log, id); | |
38 record.status, record.time_started = "connecting", os.time(); | |
39 end | |
40 | |
41 local function log_successful_connection_out(peer_domain, id) | |
42 local domain_log = get_domain_log_out(peer_domain); | |
43 local record = get_connection_record(domain_log, id); | |
44 record.status, record.time_connected = "connected", os.time(); | |
45 end | |
46 | |
47 local function log_ended_connection_out(peer_domain, id, reason) | |
48 local domain_log = get_domain_log_out(peer_domain); | |
49 local record = get_connection_record(domain_log, id); | |
50 | |
51 if record.status == "connecting" then | |
52 record.status = "failed"; | |
53 elseif record.status == "connected" then | |
54 record.status = "disconnected"; | |
55 end | |
56 if reason then | |
57 local e_reason = errors.new(reason); | |
58 record.error = { | |
59 type = e_reason.type; | |
60 condition = e_reason.condition; | |
61 text = e_reason.text; | |
62 }; | |
63 if not record.error.text and type(reason) == "string" then | |
64 record.error.text = reason; | |
65 end | |
66 end | |
67 local now = os.time(); | |
68 record.time_ended = now; | |
69 end | |
70 | |
71 local function s2sout_established(event) | |
72 local peer_domain, _, id = get_session_info(event.session); | |
73 log_successful_connection_out(peer_domain, id); | |
74 end | |
75 | |
76 local function s2sout_destroyed(event) | |
77 local peer_domain, _, id = get_session_info(event.session); | |
78 log_ended_connection_out(peer_domain, id); | |
79 end | |
80 | |
81 local function s2s_created(event) | |
82 local peer_domain, direction, id = get_session_info(event.session); | |
83 if direction == "outgoing" then | |
84 log_new_connection_out(peer_domain, id); | |
85 end | |
86 end | |
87 | |
88 module:hook("s2s-created", s2s_created); | |
89 module:hook("s2sout-established", s2sout_established); | |
90 module:hook("s2sout-destroyed", s2sout_destroyed); |