Software /
code /
prosody
Comparison
core/s2smanager.lua @ 190:1e993b7deae7
General fixes for s2s, to make it more robust (I hope), sending data to remote hosts sane (s2ssession.send() works as expected), recycle outgoing dialback connections, etc.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 01 Nov 2008 18:28:46 +0000 |
parent | 186:bfa8a30ea488 |
child | 191:e64c8a44060f |
comparison
equal
deleted
inserted
replaced
189:3f0e3a07b491 | 190:1e993b7deae7 |
---|---|
1 | 1 |
2 local hosts = hosts; | 2 local hosts = hosts; |
3 local sessions = sessions; | 3 local sessions = sessions; |
4 local socket = require "socket"; | 4 local socket = require "socket"; |
5 local format = string.format; | 5 local format = string.format; |
6 local t_insert = table.insert; | |
6 local tostring, pairs, ipairs, getmetatable, print, newproxy, error, tonumber | 7 local tostring, pairs, ipairs, getmetatable, print, newproxy, error, tonumber |
7 = tostring, pairs, ipairs, getmetatable, print, newproxy, error, tonumber; | 8 = tostring, pairs, ipairs, getmetatable, print, newproxy, error, tonumber; |
8 | 9 |
9 local connlisteners_get = require "net.connlisteners".get; | 10 local connlisteners_get = require "net.connlisteners".get; |
10 local wraptlsclient = require "net.server".wraptlsclient; | 11 local wraptlsclient = require "net.server".wraptlsclient; |
18 | 19 |
19 local md5_hash = require "util.hashes".md5; | 20 local md5_hash = require "util.hashes".md5; |
20 | 21 |
21 local dialback_secret = "This is very secret!!! Ha!"; | 22 local dialback_secret = "This is very secret!!! Ha!"; |
22 | 23 |
23 local srvmap = { ["gmail.com"] = "talk.google.com", ["identi.ca"] = "longlance.controlezvous.ca" }; | 24 local srvmap = { ["gmail.com"] = "talk.google.com", ["identi.ca"] = "longlance.controlezvous.ca", ["cdr.se"] = "jabber.cdr.se" }; |
24 | 25 |
25 module "s2smanager" | 26 module "s2smanager" |
26 | 27 |
27 function connect_host(from_host, to_host) | 28 function connect_host(from_host, to_host) |
28 end | 29 end |
29 | 30 |
30 function send_to_host(from_host, to_host, data) | 31 function send_to_host(from_host, to_host, data) |
31 if hosts[to_host] then | 32 local host = hosts[to_host]; |
33 if host then | |
32 -- Write to connection | 34 -- Write to connection |
33 hosts[to_host].sends2s(data); | 35 if host.type == "s2sout_unauthed" and not host.notopen and not host.dialback_key then |
34 log("debug", "stanza sent over s2s"); | 36 log("debug", "trying to send over unauthed s2sout to "..to_host..", authing it now..."); |
37 initiate_dialback(host); | |
38 if not host.sendq then host.sendq = { data }; | |
39 else t_insert(host.sendq, data); end | |
40 else | |
41 log("debug", "going to send stanza to "..to_host.." from "..from_host); | |
42 hosts[to_host].sends2s(data); | |
43 log("debug", "stanza sent over "..hosts[to_host].type); | |
44 end | |
35 else | 45 else |
36 log("debug", "opening a new outgoing connection for this stanza"); | 46 log("debug", "opening a new outgoing connection for this stanza"); |
37 local host_session = new_outgoing(from_host, to_host); | 47 local host_session = new_outgoing(from_host, to_host); |
38 -- Store in buffer | 48 -- Store in buffer |
39 host_session.sendq = { data }; | 49 host_session.sendq = { data }; |
59 end | 69 end |
60 | 70 |
61 function new_outgoing(from_host, to_host) | 71 function new_outgoing(from_host, to_host) |
62 local host_session = { to_host = to_host, from_host = from_host, notopen = true, type = "s2sout_unauthed", direction = "outgoing" }; | 72 local host_session = { to_host = to_host, from_host = from_host, notopen = true, type = "s2sout_unauthed", direction = "outgoing" }; |
63 hosts[to_host] = host_session; | 73 hosts[to_host] = host_session; |
64 | |
65 local cl = connlisteners_get("xmppserver"); | 74 local cl = connlisteners_get("xmppserver"); |
66 | 75 |
67 local conn, handler = socket.tcp() | 76 local conn, handler = socket.tcp() |
68 --FIXME: Below parameters (ports/ip) are incorrect (use SRV) | 77 --FIXME: Below parameters (ports/ip) are incorrect (use SRV) |
69 to_host = srvmap[to_host] or to_host; | 78 to_host = srvmap[to_host] or to_host; |
111 print(session, session.from_host, "incoming s2s stream opened"); | 120 print(session, session.from_host, "incoming s2s stream opened"); |
112 send("<?xml version='1.0'?>"); | 121 send("<?xml version='1.0'?>"); |
113 send(format("<stream:stream xmlns='jabber:server' xmlns:db='jabber:server:dialback' xmlns:stream='http://etherx.jabber.org/streams' id='%s' from='%s'>", session.streamid, session.to_host)); | 122 send(format("<stream:stream xmlns='jabber:server' xmlns:db='jabber:server:dialback' xmlns:stream='http://etherx.jabber.org/streams' id='%s' from='%s'>", session.streamid, session.to_host)); |
114 elseif session.direction == "outgoing" then | 123 elseif session.direction == "outgoing" then |
115 -- If we are just using the connection for verifying dialback keys, we won't try and auth it | 124 -- If we are just using the connection for verifying dialback keys, we won't try and auth it |
125 if not attr.id then error("stream response did not give us a streamid!!!"); end | |
126 session.streamid = attr.id; | |
127 | |
116 if not session.dialback_verifying then | 128 if not session.dialback_verifying then |
117 -- generate dialback key | 129 initiate_dialback(session); |
118 if not attr.id then error("stream response did not give us a streamid!!!"); end | |
119 session.streamid = attr.id; | |
120 session.dialback_key = generate_dialback(session.streamid, session.to_host, session.from_host); | |
121 session.sends2s(format("<db:result from='%s' to='%s'>%s</db:result>", session.from_host, session.to_host, session.dialback_key)); | |
122 session.log("info", "sent dialback key on outgoing s2s stream"); | |
123 else | 130 else |
124 mark_connected(session); | 131 mark_connected(session); |
125 end | 132 end |
126 end | 133 end |
127 --[[ | 134 --[[ |
135 end | 142 end |
136 | 143 |
137 send("</stream:features>");]] | 144 send("</stream:features>");]] |
138 log("info", "s2s stream opened successfully"); | 145 log("info", "s2s stream opened successfully"); |
139 session.notopen = nil; | 146 session.notopen = nil; |
147 end | |
148 | |
149 function initiate_dialback(session) | |
150 -- generate dialback key | |
151 session.dialback_key = generate_dialback(session.streamid, session.to_host, session.from_host); | |
152 session.sends2s(format("<db:result from='%s' to='%s'>%s</db:result>", session.from_host, session.to_host, session.dialback_key)); | |
153 session.log("info", "sent dialback key on outgoing s2s stream"); | |
140 end | 154 end |
141 | 155 |
142 function generate_dialback(id, to, from) | 156 function generate_dialback(id, to, from) |
143 return md5_hash(id..to..from..dialback_secret); -- FIXME: See XEP-185 and XEP-220 | 157 return md5_hash(id..to..from..dialback_secret); -- FIXME: See XEP-185 and XEP-220 |
144 end | 158 end |
165 function mark_connected(session) | 179 function mark_connected(session) |
166 local sendq, send = session.sendq, session.sends2s; | 180 local sendq, send = session.sendq, session.sends2s; |
167 | 181 |
168 local from, to = session.from_host, session.to_host; | 182 local from, to = session.from_host, session.to_host; |
169 | 183 |
170 session.log("debug", session.direction.." s2s connection "..session.from_host.."->"..session.to_host.." is now complete"); | 184 session.log("debug", session.direction.." s2s connection "..from.."->"..to.." is now complete"); |
171 | 185 |
172 local send_to_host = send_to_host; | 186 local send_to_host = send_to_host; |
173 function session.send(data) send_to_host(from, to, data); end | 187 function session.send(data) send_to_host(to, from, data); end |
174 | 188 |
175 if sendq then | 189 |
176 session.log("debug", "sending queued stanzas across new outgoing connection to "..session.to_host); | 190 if session.direction == "outgoing" then |
177 for i, data in ipairs(sendq) do | 191 hosts[to] = session; |
178 send(data); | 192 if sendq then |
179 sendq[i] = nil; | 193 session.log("debug", "sending queued stanzas across new outgoing connection to "..session.to_host); |
180 end | 194 for i, data in ipairs(sendq) do |
181 session.sendq = nil; | 195 send(data); |
196 sendq[i] = nil; | |
197 end | |
198 session.sendq = nil; | |
199 end | |
182 end | 200 end |
183 end | 201 end |
184 | 202 |
185 function destroy_session(session) | 203 function destroy_session(session) |
186 (session.log or log)("info", "Destroying "..tostring(session.direction).." session "..tostring(session.from_host).."->"..tostring(session.to_host)); | 204 (session.log or log)("info", "Destroying "..tostring(session.direction).." session "..tostring(session.from_host).."->"..tostring(session.to_host)); |