Mercurial > libervia-backend
annotate src/plugins/plugin_xep_0260.py @ 1629:a34d7f621944
plugin XEP-0065: renamed _killSession to killSession as it make sense to kill a session from an other plugin
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 19 Nov 2015 18:15:35 +0100 |
parents | c668081eba1c |
children | 25906c0dbc63 |
rev | line source |
---|---|
1560 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT plugin for Jingle (XEP-0260) | |
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (goffi@goffi.org) | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 from sat.core.i18n import _ | |
21 from sat.core.log import getLogger | |
22 log = getLogger(__name__) | |
23 from sat.core import exceptions | |
24 from wokkel import disco, iwokkel | |
25 from zope.interface import implements | |
26 from twisted.words.xish import domish | |
27 from twisted.words.protocols.jabber import jid | |
28 from twisted.internet import defer | |
29 import uuid | |
30 | |
31 try: | |
32 from twisted.words.protocols.xmlstream import XMPPHandler | |
33 except ImportError: | |
34 from wokkel.subprotocols import XMPPHandler | |
35 | |
36 | |
37 NS_JINGLE_S5B = 'urn:xmpp:jingle:transports:s5b:1' | |
38 | |
39 PLUGIN_INFO = { | |
40 "name": "Jingle SOCKS5 Bytestreams", | |
41 "import_name": "XEP-0260", | |
42 "type": "XEP", | |
43 "protocols": ["XEP-0260"], | |
44 "dependencies": ["XEP-0166", "XEP-0065"], | |
45 "main": "XEP_0260", | |
46 "handler": "yes", | |
47 "description": _("""Implementation of Jingle SOCKS5 Bytestreams""") | |
48 } | |
49 | |
50 | |
1570
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
51 class ProxyError(Exception): |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
52 pass |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
53 |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
54 |
1560 | 55 class XEP_0260(object): |
56 # TODO: udp handling | |
57 | |
58 def __init__(self, host): | |
59 log.info(_("plugin Jingle SOCKS5 Bytestreams")) | |
60 self.host = host | |
61 self._j = host.plugins["XEP-0166"] # shortcut to access jingle | |
62 self._s5b = host.plugins["XEP-0065"] # and socks5 bytestream | |
63 self._j.registerTransport(NS_JINGLE_S5B, self._j.TRANSPORT_STREAMING, self, 100) | |
64 | |
65 def getHandler(self, profile): | |
66 return XEP_0260_handler() | |
67 | |
68 def _parseCandidates(self, transport_elt): | |
69 """Parse <candidate> elements | |
70 | |
71 @param transport_elt(domish.Element): parent <transport> element | |
72 @return (list[plugin_xep_0065.Candidate): list of parsed candidates | |
73 """ | |
74 candidates = [] | |
75 for candidate_elt in transport_elt.elements(NS_JINGLE_S5B, 'candidate'): | |
76 try: | |
77 cid = candidate_elt['cid'] | |
78 host = candidate_elt['host'] | |
79 jid_= jid.JID(candidate_elt['jid']) | |
80 port = int(candidate_elt.getAttribute('port', 1080)) | |
81 priority = int(candidate_elt['priority']) | |
82 type_ = candidate_elt.getAttribute('type', self._s5b.TYPE_DIRECT) | |
83 except (KeyError, ValueError): | |
84 raise exceptions.DataError() | |
85 candidate = self._s5b.Candidate(host, port, type_, priority, jid_, cid) | |
86 candidates.append(candidate) | |
87 # self._s5b.registerCandidate(candidate) | |
88 return candidates | |
89 | |
90 def _buildCandidates(self, session, candidates, sid, session_hash, client, mode=None): | |
91 """Build <transport> element with candidates | |
92 | |
93 @param session(dict): jingle session data | |
94 @param candidates(iterator[plugin_xep_0065.Candidate]): iterator of candidates to add | |
95 @param sid(unicode): transport stream id | |
96 @param client: %(doc_client)s | |
97 @param mode(str, None): 'tcp' or 'udp', or None to have no attribute | |
98 @return (domish.Element): parent <transport> element where <candidate> elements must be added | |
99 """ | |
100 proxy = next((candidate for candidate in candidates if candidate.type == self._s5b.TYPE_PROXY), None) | |
101 transport_elt = domish.Element((NS_JINGLE_S5B, "transport")) | |
102 transport_elt['sid'] = sid | |
103 if proxy is not None: | |
104 transport_elt['dstaddr'] = session_hash | |
105 if mode is not None: | |
106 transport_elt['mode'] = 'tcp' # XXX: we only manage tcp for now | |
107 | |
108 for candidate in candidates: | |
109 log.debug(u"Adding candidate: {}".format(candidate)) | |
110 candidate_elt = transport_elt.addElement('candidate', NS_JINGLE_S5B) | |
111 if candidate.id is None: | |
112 candidate.id = unicode(uuid.uuid4()) | |
113 candidate_elt['cid'] = candidate.id | |
114 candidate_elt['host'] = candidate.host | |
115 candidate_elt['jid'] = candidate.jid.full() | |
116 candidate_elt['port'] = unicode(candidate.port) | |
117 candidate_elt['priority'] = unicode(candidate.priority) | |
118 candidate_elt['type'] = candidate.type | |
119 return transport_elt | |
120 | |
121 @defer.inlineCallbacks | |
122 def jingleSessionInit(self, session, content_name, profile): | |
123 client = self.host.getClient(profile) | |
124 content_data = session['contents'][content_name] | |
125 transport_data = content_data['transport_data'] | |
126 sid = transport_data['sid'] = unicode(uuid.uuid4()) | |
1567
268fda4236ca
plugins XE0166, XEP-0234, XEP-0260, XEP-0261: renamed session key managing other peer's jid to "peer_jid" instead of "to_jid"
Goffi <goffi@goffi.org>
parents:
1560
diff
changeset
|
127 session_hash = transport_data['session_hash'] = self._s5b.getSessionHash(client.jid, session['peer_jid'], sid) |
1560 | 128 candidates = transport_data['candidates'] = yield self._s5b.getCandidates(profile) |
129 mode = 'tcp' # XXX: we only manage tcp for now | |
130 transport_elt = self._buildCandidates(session, candidates, sid, session_hash, client, mode) | |
131 | |
132 defer.returnValue(transport_elt) | |
133 | |
1570
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
134 def _proxyActivatedCb(self, iq_result_elt, candidate, session, content_name, profile): |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
135 """Called when activation confirmation has been received from proxy |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
136 |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
137 cf XEP-0260 § 2.4 |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
138 """ |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
139 # now that the proxy is activated, we have to inform other peer |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
140 iq_elt, transport_elt = self._j.buildAction(self._j.A_TRANSPORT_INFO, session, content_name, profile) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
141 activated_elt = transport_elt.addElement('activated') |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
142 activated_elt['cid'] = candidate.id |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
143 iq_elt.send |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
144 |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
145 def _proxyActivatedEb(self, stanza_error, candidate, session, content_name, profile): |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
146 """Called when activation error has been received from proxy |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
147 |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
148 cf XEP-0260 § 2.4 |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
149 """ |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
150 # TODO: fallback to IBB |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
151 # now that the proxy is activated, we have to inform other peer |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
152 iq_elt, transport_elt = self._j.buildAction(self._j.A_TRANSPORT_INFO, session, content_name, profile) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
153 transport_elt.addElement('proxy-error') |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
154 iq_elt.send |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
155 return stanza_error |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
156 |
1560 | 157 def _foundPeerCandidate(self, candidate, session, transport_data, content_name, client): |
158 """Called when the best candidate from other peer is found | |
159 | |
160 @param candidate(XEP_0065.Candidate, None): selected candidate, | |
161 or None if no candidate is accessible | |
162 @param session(dict): session data | |
163 @param transport_data(dict): transport data | |
1570
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
164 @param content_name(unicode): name of the current content |
1560 | 165 @param client(unicode): %(doc_client)s |
166 """ | |
167 | |
168 transport_data['best_candidate'] = candidate | |
169 # we need to disconnect all non selected candidates before removing them | |
170 for c in transport_data['peer_candidates']: | |
171 if c is None or c is candidate: | |
172 continue | |
173 c.discard() | |
174 del transport_data['peer_candidates'] | |
175 iq_elt, transport_elt = self._j.buildAction(self._j.A_TRANSPORT_INFO, session, content_name, client.profile) | |
176 if candidate is None: | |
177 log.warning(u"Can't connect to any peer candidate") | |
178 candidate_elt = transport_elt.addElement('candidate-error') | |
179 else: | |
180 log.info(u"Found best peer candidate: {}".format(unicode(candidate))) | |
181 candidate_elt = transport_elt.addElement('candidate-used') | |
182 candidate_elt['cid'] = candidate.id | |
183 iq_elt.send() # TODO: check result stanza | |
1570
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
184 self._checkCandidates(session, content_name, transport_data, client) |
1560 | 185 |
1570
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
186 def _checkCandidates(self, session, content_name, transport_data, client): |
1560 | 187 """Called when a candidate has been choosed |
188 | |
189 if we have both candidates, we select one, or fallback to an other transport | |
190 @param session(dict): session data | |
1570
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
191 @param content_name(unicode): name of the current content |
1560 | 192 @param transport_data(dict): transport data |
193 @param client(unicode): %(doc_client)s | |
194 """ | |
1570
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
195 content_data = session['contents'][content_name] |
1560 | 196 try: |
197 best_candidate = transport_data['best_candidate'] | |
198 except KeyError: | |
199 # we have not our best candidate yet | |
200 return | |
201 try: | |
202 peer_best_candidate = transport_data['peer_best_candidate'] | |
203 except KeyError: | |
204 # we have not peer best candidate yet | |
205 return | |
206 | |
207 # at this point we have both candidates, it's time to choose one | |
208 if best_candidate is None or peer_best_candidate is None: | |
209 choosed_candidate = best_candidate or peer_best_candidate | |
210 else: | |
211 if best_candidate.priority == peer_best_candidate.priority: | |
212 # same priority, we choose initiator one according to XEP-0260 §2.4 #4 | |
213 log.debug(u"Candidates have same priority, we choose the initiator one") | |
214 if session['initiator'] == client.jid: | |
215 choosed_candidate = best_candidate | |
216 else: | |
217 choosed_candidate = peer_best_candidate | |
218 else: | |
219 choosed_candidate = max(best_candidate, peer_best_candidate, key=lambda c:c.priority) | |
220 | |
221 if choosed_candidate is None: | |
222 log.warning(u"Socks5 negociation failed, we need to fallback to IBB") | |
223 else: | |
1571
c668081eba1c
plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents:
1570
diff
changeset
|
224 if choosed_candidate == peer_best_candidate: |
c668081eba1c
plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents:
1570
diff
changeset
|
225 # peer_best_candidate was choosed from the candidates we have sent |
c668081eba1c
plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents:
1570
diff
changeset
|
226 # so our_candidate is true if choosed_candidate is peer_best_candidate |
c668081eba1c
plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents:
1570
diff
changeset
|
227 our_candidate = True |
c668081eba1c
plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents:
1570
diff
changeset
|
228 # than also mean that best_candidate must be discarded ! |
c668081eba1c
plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents:
1570
diff
changeset
|
229 try: |
c668081eba1c
plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents:
1570
diff
changeset
|
230 best_candidate.discard() |
c668081eba1c
plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents:
1570
diff
changeset
|
231 except AttributeError: # but it can be None |
c668081eba1c
plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents:
1570
diff
changeset
|
232 pass |
c668081eba1c
plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents:
1570
diff
changeset
|
233 else: |
c668081eba1c
plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents:
1570
diff
changeset
|
234 our_candidate = False |
1560 | 235 |
236 log.info(u"Socks5 negociation successful, {who} candidate will be used: {candidate}".format( | |
1570
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
237 who = u'our' if our_candidate else u'other peer', |
1560 | 238 candidate = choosed_candidate)) |
239 del transport_data['best_candidate'] | |
240 del transport_data['peer_best_candidate'] | |
1570
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
241 |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
242 if choosed_candidate.type == self._s5b.TYPE_PROXY: |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
243 # the file transfer need to wait for proxy activation |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
244 # (see XEP-0260 § 2.4) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
245 if our_candidate: |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
246 d = self._s5b.connectCandidate(choosed_candidate, transport_data['session_hash'], profile=client.profile) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
247 d.addCallback(lambda dummy: choosed_candidate.activate(transport_data['sid'], session['peer_jid'], client)) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
248 args = [choosed_candidate, session, content_name, client.profile] |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
249 d.addCallbacks(self._proxyActivatedCb, self._proxyActivatedEb, args, None, args) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
250 else: |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
251 # this Deferred will be called when we'll receive activation confirmation from other peer |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
252 d = transport_data['activation_d'] = defer.Deferred() |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
253 else: |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
254 d = defer.succeed(None) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
255 |
1560 | 256 if content_data['senders'] == session['role']: |
1570
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
257 # we can now start the file transfer (or start it after proxy activation) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
258 d.addCallback(lambda dummy: choosed_candidate.startTransfer(transport_data['session_hash'])) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
259 |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
260 def _candidateInfo(self, candidate_elt, session, content_name, transport_data, client): |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
261 """Called when best candidate has been received from peer (or if none is working) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
262 |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
263 @param candidate_elt(domish.Element): candidate-used or candidate-error element |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
264 (see XEP-0260 §2.3) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
265 @param session(dict): session data |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
266 @param content_name(unicode): name of the current content |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
267 @param transport_data(dict): transport data |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
268 @param client(unicode): %(doc_client)s |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
269 """ |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
270 if candidate_elt.name == 'candidate-error': |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
271 # candidate-error, no candidate worked |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
272 transport_data['peer_best_candidate'] = None |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
273 else: |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
274 # candidate-used, one candidate was choosed |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
275 try: |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
276 cid = candidate_elt.attributes['cid'] |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
277 except KeyError: |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
278 log.warning(u"No cid found in <candidate-used>") |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
279 raise exceptions.DataError |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
280 try: |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
281 candidate = (c for c in transport_data['candidates'] if c.id == cid).next() |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
282 except StopIteration: |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
283 log.warning(u"Given cid doesn't correspond to any known candidate !") |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
284 raise exceptions.DataError # TODO: send an error to other peer, and use better exception |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
285 except KeyError: |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
286 # a transport-info can also be intentionaly sent too early by other peer |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
287 # but there is little probability |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
288 log.error(u'"candidates" key doesn\'t exists in transport_data, it should at this point') |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
289 raise exceptions.InternalError |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
290 # at this point we have the candidate choosed by other peer |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
291 transport_data['peer_best_candidate'] = candidate |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
292 log.info(u"Other peer best candidate: {}".format(candidate)) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
293 |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
294 del transport_data['candidates'] |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
295 self._checkCandidates(session, content_name, transport_data, client) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
296 |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
297 def _proxyActivationInfo(self, proxy_elt, session, content_name, transport_data, client): |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
298 """Called when proxy has been activated (or has sent an error) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
299 |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
300 @param proxy_elt(domish.Element): <activated/> or <proxy-error/> element |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
301 (see XEP-0260 §2.4) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
302 @param session(dict): session data |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
303 @param content_name(unicode): name of the current content |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
304 @param transport_data(dict): transport data |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
305 @param client(unicode): %(doc_client)s |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
306 """ |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
307 try: |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
308 activation_d = transport_data.pop('activation_d') |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
309 except KeyError: |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
310 log.warning(u"Received unexpected transport-info for proxy activation") |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
311 |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
312 if proxy_elt.name == 'activated': |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
313 activation_d.callback(None) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
314 else: |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
315 activation_d.errback(ProxyError) |
1560 | 316 |
317 @defer.inlineCallbacks | |
318 def jingleHandler(self, action, session, content_name, transport_elt, profile): | |
319 client = self.host.getClient(profile) | |
320 content_data = session['contents'][content_name] | |
321 transport_data = content_data['transport_data'] | |
322 | |
323 if action in (self._j.A_ACCEPTED_ACK,): | |
324 pass | |
325 | |
326 elif action == self._j.A_SESSION_ACCEPT: | |
327 # initiator side, we select a candidate in the ones sent by responder | |
328 assert 'peer_candidates' not in transport_data | |
329 transport_data['peer_candidates'] = self._parseCandidates(transport_elt) | |
330 | |
331 # elif action == self._j.A_START: | |
332 elif action == self._j.A_START: | |
333 session_hash = transport_data['session_hash'] | |
334 peer_candidates = transport_data['peer_candidates'] | |
335 file_obj = content_data['file_obj'] | |
336 stream_d = self._s5b.registerHash(session_hash, file_obj, profile) | |
1571
c668081eba1c
plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents:
1570
diff
changeset
|
337 stream_d.chainDeferred(content_data['finished_d']) |
1560 | 338 d = self._s5b.getBestCandidate(peer_candidates, session_hash, profile) |
339 d.addCallback(self._foundPeerCandidate, session, transport_data, content_name, client) | |
340 | |
341 elif action == self._j.A_SESSION_INITIATE: | |
342 # responder side, we select a candidate in the ones sent by initiator | |
343 # and we give our candidates | |
344 assert 'peer_candidates' not in transport_data | |
345 sid = transport_data['sid'] = transport_elt['sid'] | |
1567
268fda4236ca
plugins XE0166, XEP-0234, XEP-0260, XEP-0261: renamed session key managing other peer's jid to "peer_jid" instead of "to_jid"
Goffi <goffi@goffi.org>
parents:
1560
diff
changeset
|
346 session_hash = transport_data['session_hash'] = self._s5b.getSessionHash(session['peer_jid'], client.jid, sid) |
1560 | 347 peer_candidates = transport_data['peer_candidates'] = self._parseCandidates(transport_elt) |
348 file_obj = content_data['file_obj'] | |
349 stream_d = self._s5b.registerHash(session_hash, file_obj, profile) | |
1571
c668081eba1c
plugins XEP-0234, XEP-0260, XEP-0261: jingle session termination is managed by application (XEP-0234) instead of transport
Goffi <goffi@goffi.org>
parents:
1570
diff
changeset
|
350 stream_d.chainDeferred(content_data['finished_d']) |
1560 | 351 d = self._s5b.getBestCandidate(peer_candidates, session_hash, profile) |
352 d.addCallback(self._foundPeerCandidate, session, transport_data, content_name, client) | |
353 candidates = yield self._s5b.getCandidates(profile) | |
354 # we remove duplicate candidates | |
355 candidates = [candidate for candidate in candidates if candidate not in peer_candidates] | |
356 | |
357 transport_data['candidates'] = candidates | |
358 # we can now build a new <transport> element with our candidates | |
359 transport_elt = self._buildCandidates(session, candidates, sid, session_hash, client) | |
360 | |
361 elif action == self._j.A_TRANSPORT_INFO: | |
1570
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
362 # transport-info can be about candidate or proxy activation |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
363 candidate_elt = None |
1560 | 364 |
1570
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
365 for method, names in ((self._candidateInfo, ('candidate-used', 'candidate-error')), |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
366 (self._proxyActivationInfo, ('activated', 'proxy-error'))): |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
367 for name in names: |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
368 try: |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
369 candidate_elt = transport_elt.elements(NS_JINGLE_S5B, name).next() |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
370 except StopIteration: |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
371 continue |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
372 else: |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
373 method(candidate_elt, session, content_name, transport_data, client) |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
374 break |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
375 |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
376 if candidate_elt is None: |
37d4be4a9fed
plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents:
1567
diff
changeset
|
377 log.warning(u"Unexpected transport element: {}".format(transport_elt.toXml())) |
1560 | 378 |
379 else: | |
380 log.warning(u"FIXME: unmanaged action {}".format(action)) | |
381 | |
382 defer.returnValue(transport_elt) | |
383 | |
384 | |
385 class XEP_0260_handler(XMPPHandler): | |
386 implements(iwokkel.IDisco) | |
387 | |
388 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): | |
389 return [disco.DiscoFeature(NS_JINGLE_S5B)] | |
390 | |
391 def getDiscoItems(self, requestor, target, nodeIdentifier=''): | |
392 return [] |