Software / code / prosody
Comparison
core/s2smanager.lua @ 11200:bf8f2da84007
Merge 0.11->trunk
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Thu, 05 Nov 2020 22:31:25 +0100 |
| parent | 10425:42cf93ff4618 |
| child | 11418:f4b76e500768 |
comparison
equal
deleted
inserted
replaced
| 11199:6c7c50a4de32 | 11200:bf8f2da84007 |
|---|---|
| 7 -- | 7 -- |
| 8 | 8 |
| 9 | 9 |
| 10 | 10 |
| 11 local hosts = prosody.hosts; | 11 local hosts = prosody.hosts; |
| 12 local tostring, pairs, setmetatable | 12 local pairs, setmetatable = pairs, setmetatable; |
| 13 = tostring, pairs, setmetatable; | |
| 14 | 13 |
| 15 local logger_init = require "util.logger".init; | 14 local logger_init = require "util.logger".init; |
| 15 local sessionlib = require "util.session"; | |
| 16 | 16 |
| 17 local log = logger_init("s2smanager"); | 17 local log = logger_init("s2smanager"); |
| 18 | 18 |
| 19 local prosody = _G.prosody; | 19 local prosody = _G.prosody; |
| 20 local incoming_s2s = {}; | 20 local incoming_s2s = {}; |
| 24 | 24 |
| 25 local _ENV = nil; | 25 local _ENV = nil; |
| 26 -- luacheck: std none | 26 -- luacheck: std none |
| 27 | 27 |
| 28 local function new_incoming(conn) | 28 local function new_incoming(conn) |
| 29 local session = { conn = conn, type = "s2sin_unauthed", direction = "incoming", hosts = {} }; | 29 local host_session = sessionlib.new("s2sin"); |
| 30 session.log = logger_init("s2sin"..tostring(session):match("[a-f0-9]+$")); | 30 sessionlib.set_id(host_session); |
| 31 incoming_s2s[session] = true; | 31 sessionlib.set_logger(host_session); |
| 32 return session; | 32 sessionlib.set_conn(host_session, conn); |
| 33 host_session.direction = "incoming"; | |
| 34 host_session.incoming = true; | |
| 35 host_session.hosts = {}; | |
| 36 incoming_s2s[host_session] = true; | |
| 37 return host_session; | |
| 33 end | 38 end |
| 34 | 39 |
| 35 local function new_outgoing(from_host, to_host) | 40 local function new_outgoing(from_host, to_host) |
| 36 local host_session = { to_host = to_host, from_host = from_host, host = from_host, | 41 local host_session = sessionlib.new("s2sout"); |
| 37 notopen = true, type = "s2sout_unauthed", direction = "outgoing" }; | 42 sessionlib.set_id(host_session); |
| 43 sessionlib.set_logger(host_session); | |
| 44 host_session.to_host = to_host; | |
| 45 host_session.from_host = from_host; | |
| 46 host_session.host = from_host; | |
| 47 host_session.notopen = true; | |
| 48 host_session.direction = "outgoing"; | |
| 49 host_session.outgoing = true; | |
| 50 host_session.hosts = {}; | |
| 38 hosts[from_host].s2sout[to_host] = host_session; | 51 hosts[from_host].s2sout[to_host] = host_session; |
| 39 local conn_name = "s2sout"..tostring(host_session):match("[a-f0-9]*$"); | |
| 40 host_session.log = logger_init(conn_name); | |
| 41 return host_session; | 52 return host_session; |
| 42 end | 53 end |
| 43 | 54 |
| 44 local resting_session = { -- Resting, not dead | 55 local resting_session = { -- Resting, not dead |
| 45 destroyed = true; | 56 destroyed = true; |
| 47 open_stream = function (session) | 58 open_stream = function (session) |
| 48 session.log("debug", "Attempt to open stream on resting session"); | 59 session.log("debug", "Attempt to open stream on resting session"); |
| 49 end; | 60 end; |
| 50 close = function (session) | 61 close = function (session) |
| 51 session.log("debug", "Attempt to close already-closed session"); | 62 session.log("debug", "Attempt to close already-closed session"); |
| 63 end; | |
| 64 reset_stream = function (session) | |
| 65 session.log("debug", "Attempt to reset stream of already-closed session"); | |
| 52 end; | 66 end; |
| 53 filter = function (type, data) return data; end; --luacheck: ignore 212/type | 67 filter = function (type, data) return data; end; --luacheck: ignore 212/type |
| 54 }; resting_session.__index = resting_session; | 68 }; resting_session.__index = resting_session; |
| 55 | 69 |
| 56 local function retire_session(session, reason) | 70 local function retire_session(session, reason) |
| 61 end | 75 end |
| 62 end | 76 end |
| 63 | 77 |
| 64 session.destruction_reason = reason; | 78 session.destruction_reason = reason; |
| 65 | 79 |
| 66 function session.send(data) log("debug", "Discarding data sent to resting session: %s", tostring(data)); end | 80 function session.send(data) log("debug", "Discarding data sent to resting session: %s", data); end |
| 67 function session.data(data) log("debug", "Discarding data received from resting session: %s", tostring(data)); end | 81 function session.data(data) log("debug", "Discarding data received from resting session: %s", data); end |
| 68 session.thread = { run = function (_, data) return session.data(data) end }; | 82 session.thread = { run = function (_, data) return session.data(data) end }; |
| 69 session.sends2s = session.send; | 83 session.sends2s = session.send; |
| 70 return setmetatable(session, resting_session); | 84 return setmetatable(session, resting_session); |
| 71 end | 85 end |
| 72 | 86 |
| 73 local function destroy_session(session, reason) | 87 local function destroy_session(session, reason, bounce_reason) |
| 74 if session.destroyed then return; end | 88 if session.destroyed then return; end |
| 75 (session.log or log)("debug", "Destroying "..tostring(session.direction) | 89 local log = session.log or log; |
| 76 .." session "..tostring(session.from_host).."->"..tostring(session.to_host) | 90 log("debug", "Destroying %s session %s->%s%s%s", session.direction, session.from_host, session.to_host, reason and ": " or "", reason or ""); |
| 77 ..(reason and (": "..reason) or "")); | |
| 78 | 91 |
| 79 if session.direction == "outgoing" then | 92 if session.direction == "outgoing" then |
| 80 hosts[session.from_host].s2sout[session.to_host] = nil; | 93 hosts[session.from_host].s2sout[session.to_host] = nil; |
| 81 session:bounce_sendq(reason); | 94 session:bounce_sendq(bounce_reason or reason); |
| 82 elseif session.direction == "incoming" then | 95 elseif session.direction == "incoming" then |
| 96 if session.outgoing then | |
| 97 hosts[session.to_host].s2sout[session.from_host] = nil; | |
| 98 end | |
| 83 incoming_s2s[session] = nil; | 99 incoming_s2s[session] = nil; |
| 84 end | 100 end |
| 85 | 101 |
| 86 local event_data = { session = session, reason = reason }; | 102 local event_data = { session = session, reason = reason }; |
| 87 if session.type == "s2sout" then | 103 if session.type == "s2sout" then |