annotate libervia/backend/plugins/plugin_xep_0260.py @ 4335:430d5d99a740

plugin XEP-0358: "Publishing Available Jingle Sessions" implementation: rel 453
author Goffi <goffi@goffi.org>
date Tue, 03 Dec 2024 00:13:07 +0100
parents 0d7bb4df2343
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin for Jingle (XEP-0260)
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4044
diff changeset
20 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4044
diff changeset
21 from libervia.backend.core.constants import Const as C
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4044
diff changeset
22 from libervia.backend.core.log import getLogger
4231
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4180
diff changeset
23 from libervia.backend.plugins.plugin_xep_0166.models import BaseTransportHandler
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
24
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 log = getLogger(__name__)
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4044
diff changeset
26 from libervia.backend.core import exceptions
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from wokkel import disco, iwokkel
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
28 from zope.interface import implementer
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from twisted.words.xish import domish
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from twisted.words.protocols.jabber import jid
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from twisted.internet import defer
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32 import uuid
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34 try:
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from twisted.words.protocols.xmlstream import XMPPHandler
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
36 except ImportError:
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37 from wokkel.subprotocols import XMPPHandler
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
40 NS_JINGLE_S5B = "urn:xmpp:jingle:transports:s5b:1"
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
43 C.PI_NAME: "Jingle SOCKS5 Bytestreams",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
44 C.PI_IMPORT_NAME: "XEP-0260",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
45 C.PI_TYPE: "XEP",
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents: 2489
diff changeset
46 C.PI_MODES: C.PLUG_MODE_BOTH,
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
47 C.PI_PROTOCOLS: ["XEP-0260"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
48 C.PI_DEPENDENCIES: ["XEP-0166", "XEP-0065"],
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
49 C.PI_RECOMMENDATIONS: ["XEP-0261"], # needed for fallback
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
50 C.PI_MAIN: "XEP_0260",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 2144
diff changeset
51 C.PI_HANDLER: "yes",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
52 C.PI_DESCRIPTION: _("""Implementation of Jingle SOCKS5 Bytestreams"""),
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53 }
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
56 class ProxyError(Exception):
1756
061011fad5b1 plugin XEP-0260: better proxy error handling:
Goffi <goffi@goffi.org>
parents: 1755
diff changeset
57 def __str__(self):
061011fad5b1 plugin XEP-0260: better proxy error handling:
Goffi <goffi@goffi.org>
parents: 1755
diff changeset
58 return "an error happened while trying to use the proxy"
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
59
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
60
4231
e11b13418ba6 plugin XEP-0353, XEP-0234, jingle: WebRTC data channel signaling implementation:
Goffi <goffi@goffi.org>
parents: 4180
diff changeset
61 class XEP_0260(BaseTransportHandler):
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62 # TODO: udp handling
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
64 def __init__(self, host):
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
65 log.info(_("plugin Jingle SOCKS5 Bytestreams"))
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
66 self.host = host
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
67 self._j = host.plugins["XEP-0166"] # shortcut to access jingle
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
68 self._s5b = host.plugins["XEP-0065"] # and socks5 bytestream
1631
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
69 try:
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
70 self._jingle_ibb = host.plugins["XEP-0261"]
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
71 except KeyError:
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
72 self._jingle_ibb = None
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
73 self._j.register_transport(NS_JINGLE_S5B, self._j.TRANSPORT_STREAMING, self, 100)
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
74
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
75 def get_handler(self, client):
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
76 return XEP_0260_handler()
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
77
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
78 def _parse_candidates(self, transport_elt):
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
79 """Parse <candidate> elements
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
80
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
81 @param transport_elt(domish.Element): parent <transport> element
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
82 @return (list[plugin_xep_0065.Candidate): list of parsed candidates
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
83 """
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
84 candidates = []
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
85 for candidate_elt in transport_elt.elements(NS_JINGLE_S5B, "candidate"):
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
86 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
87 cid = candidate_elt["cid"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
88 host = candidate_elt["host"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
89 jid_ = jid.JID(candidate_elt["jid"])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
90 port = int(candidate_elt.getAttribute("port", 1080))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
91 priority = int(candidate_elt["priority"])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
92 type_ = candidate_elt.getAttribute("type", self._s5b.TYPE_DIRECT)
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
93 except (KeyError, ValueError):
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
94 raise exceptions.DataError()
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
95 candidate = self._s5b.Candidate(host, port, type_, priority, jid_, cid)
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
96 candidates.append(candidate)
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
97 # self._s5b.registerCandidate(candidate)
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
98 return candidates
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
99
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
100 def _build_candidates(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
101 self, session, candidates, sid, session_hash, client, mode=None
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
102 ):
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
103 """Build <transport> element with candidates
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
104
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
105 @param session(dict): jingle session data
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
106 @param candidates(iterator[plugin_xep_0065.Candidate]): iterator of candidates to add
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
107 @param sid(unicode): transport stream id
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
108 @param client: %(doc_client)s
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
109 @param mode(str, None): 'tcp' or 'udp', or None to have no attribute
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
110 @return (domish.Element): parent <transport> element where <candidate> elements must be added
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
111 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
112 proxy = next(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
113 (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
114 candidate
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
115 for candidate in candidates
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
116 if candidate.type == self._s5b.TYPE_PROXY
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
117 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
118 None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
119 )
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
120 transport_elt = domish.Element((NS_JINGLE_S5B, "transport"))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
121 transport_elt["sid"] = sid
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
122 if proxy is not None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
123 transport_elt["dstaddr"] = session_hash
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
124 if mode is not None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
125 transport_elt["mode"] = "tcp" # XXX: we only manage tcp for now
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
126
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
127 for candidate in candidates:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
128 log.debug("Adding candidate: {}".format(candidate))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
129 candidate_elt = transport_elt.addElement("candidate", NS_JINGLE_S5B)
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
130 if candidate.id is None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
131 candidate.id = str(uuid.uuid4())
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
132 candidate_elt["cid"] = candidate.id
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
133 candidate_elt["host"] = candidate.host
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
134 candidate_elt["jid"] = candidate.jid.full()
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
135 candidate_elt["port"] = str(candidate.port)
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
136 candidate_elt["priority"] = str(candidate.priority)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
137 candidate_elt["type"] = candidate.type
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
138 return transport_elt
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
139
4180
b86912d3fd33 plugin IP: fix use of legacy URL + coroutine use:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
140 async def jingle_session_init(self, client, session, content_name):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
141 content_data = session["contents"][content_name]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
142 transport_data = content_data["transport_data"]
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
143 sid = transport_data["sid"] = str(uuid.uuid4())
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
144 session_hash = transport_data["session_hash"] = self._s5b.get_session_hash(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
145 session["local_jid"], session["peer_jid"], sid
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
146 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
147 transport_data["peer_session_hash"] = self._s5b.get_session_hash(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
148 session["peer_jid"], session["local_jid"], sid
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
149 ) # requester and target are inversed for peer candidates
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
150 transport_data["stream_d"] = self._s5b.register_hash(client, session_hash, None)
4180
b86912d3fd33 plugin IP: fix use of legacy URL + coroutine use:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
151 candidates = transport_data["candidates"] = await self._s5b.get_candidates(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
152 client, session["local_jid"]
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
153 )
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
154 mode = "tcp" # XXX: we only manage tcp for now
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
155 transport_elt = self._build_candidates(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
156 session, candidates, sid, session_hash, client, mode
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
157 )
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
158
4180
b86912d3fd33 plugin IP: fix use of legacy URL + coroutine use:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
159 return transport_elt
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
160
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
161 def _proxy_activated_cb(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
162 self, iq_result_elt, client, candidate, session, content_name
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
163 ):
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
164 """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
165
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
166 cf XEP-0260 § 2.4
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
167 """
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
168 # now that the proxy is activated, we have to inform other peer
4044
3900626bc100 plugin XEP-0166: refactoring, and various improvments:
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
169 content_data = session["contents"][content_name]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
170 iq_elt, transport_elt = self._j.build_action(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
171 client, self._j.A_TRANSPORT_INFO, session, content_name
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
172 )
4044
3900626bc100 plugin XEP-0166: refactoring, and various improvments:
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
173 transport_elt["sid"] = content_data["transport_data"]["sid"]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
174 activated_elt = transport_elt.addElement("activated")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
175 activated_elt["cid"] = candidate.id
1631
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
176 iq_elt.send()
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
177
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
178 def _proxy_activated_eb(self, stanza_error, client, candidate, session, content_name):
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
179 """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
180
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
181 cf XEP-0260 § 2.4
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
182 """
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
183 # TODO: fallback to IBB
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
184 # now that the proxy is activated, we have to inform other peer
4044
3900626bc100 plugin XEP-0166: refactoring, and various improvments:
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
185 content_data = session["contents"][content_name]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
186 iq_elt, transport_elt = self._j.build_action(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
187 client, self._j.A_TRANSPORT_INFO, session, content_name
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
188 )
4044
3900626bc100 plugin XEP-0166: refactoring, and various improvments:
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
189 transport_elt["sid"] = content_data["transport_data"]["sid"]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
190 transport_elt.addElement("proxy-error")
1631
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
191 iq_elt.send()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
192 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
193 "Can't activate proxy, we need to fallback to IBB: {reason}".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
194 reason=stanza_error.value.condition
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
195 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
196 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
197 self.do_fallback(session, content_name, client)
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
198
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
199 def _found_peer_candidate(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
200 self, candidate, session, transport_data, content_name, client
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
201 ):
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
202 """Called when the best candidate from other peer is found
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
203
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
204 @param candidate(XEP_0065.Candidate, None): selected candidate,
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
205 or None if no candidate is accessible
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
206 @param session(dict): session data
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
207 @param transport_data(dict): transport data
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
208 @param content_name(unicode): name of the current content
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
209 @param client(unicode): %(doc_client)s
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
210 """
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
211
4044
3900626bc100 plugin XEP-0166: refactoring, and various improvments:
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
212 content_data = session["contents"][content_name]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
213 transport_data["best_candidate"] = candidate
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
214 # we need to disconnect all non selected candidates before removing them
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
215 for c in transport_data["peer_candidates"]:
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
216 if c is None or c is candidate:
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
217 continue
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
218 c.discard()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
219 del transport_data["peer_candidates"]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
220 iq_elt, transport_elt = self._j.build_action(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
221 client, self._j.A_TRANSPORT_INFO, session, content_name
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
222 )
4044
3900626bc100 plugin XEP-0166: refactoring, and various improvments:
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
223 transport_elt["sid"] = content_data["transport_data"]["sid"]
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
224 if candidate is None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
225 log.warning("Can't connect to any peer candidate")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
226 candidate_elt = transport_elt.addElement("candidate-error")
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
227 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
228 log.info("Found best peer candidate: {}".format(str(candidate)))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
229 candidate_elt = transport_elt.addElement("candidate-used")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
230 candidate_elt["cid"] = candidate.id
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
231 iq_elt.send() # TODO: check result stanza
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
232 self._check_candidates(session, content_name, transport_data, client)
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
233
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
234 def _check_candidates(self, session, content_name, transport_data, client):
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
235 """Called when a candidate has been choosed
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
236
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
237 if we have both candidates, we select one, or fallback to an other transport
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
238 @param session(dict): session data
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
239 @param content_name(unicode): name of the current content
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
240 @param transport_data(dict): transport data
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
241 @param client(unicode): %(doc_client)s
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
242 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
243 content_data = session["contents"][content_name]
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
244 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
245 best_candidate = transport_data["best_candidate"]
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
246 except KeyError:
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
247 # we have not our best candidate yet
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
248 return
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
249 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
250 peer_best_candidate = transport_data["peer_best_candidate"]
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
251 except KeyError:
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
252 # we have not peer best candidate yet
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
253 return
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
254
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
255 # at this point we have both candidates, it's time to choose one
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
256 if best_candidate is None or peer_best_candidate is None:
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
257 choosed_candidate = best_candidate or peer_best_candidate
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
258 else:
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
259 if best_candidate.priority == peer_best_candidate.priority:
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
260 # same priority, we choose initiator one according to XEP-0260 §2.4 #4
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
261 log.debug(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
262 "Candidates have same priority, we select the one choosed by initiator"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
263 )
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
264 if session["initiator"] == session["local_jid"]:
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
265 choosed_candidate = best_candidate
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
266 else:
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
267 choosed_candidate = peer_best_candidate
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
268 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
269 choosed_candidate = max(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
270 best_candidate, peer_best_candidate, key=lambda c: c.priority
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
271 )
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
272
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
273 if choosed_candidate is None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
274 log.warning("Socks5 negociation failed, we need to fallback to IBB")
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
275 self.do_fallback(session, content_name, client)
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
276 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
277 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
278 # 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
279 # 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
280 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
281 # 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
282 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
283 best_candidate.discard()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
284 except AttributeError: # but it can be None
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
285 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
286 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
287 our_candidate = False
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
288
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
289 log.info(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
290 "Socks5 negociation successful, {who} candidate will be used: {candidate}".format(
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
291 who="our" if our_candidate else "other peer",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
292 candidate=choosed_candidate,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
293 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
294 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
295 del transport_data["best_candidate"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
296 del transport_data["peer_best_candidate"]
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
297
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
298 if choosed_candidate.type == self._s5b.TYPE_PROXY:
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
299 # the stream transfer need to wait for proxy activation
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
300 # (see XEP-0260 § 2.4)
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
301 if our_candidate:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
302 d = self._s5b.connect_candidate(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
303 client, choosed_candidate, transport_data["session_hash"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
304 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
305 d.addCallback(
2765
378188abe941 misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
306 lambda __: choosed_candidate.activate(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
307 transport_data["sid"], session["peer_jid"], client
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
308 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
309 )
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
310 args = [client, choosed_candidate, session, content_name]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
311 d.addCallbacks(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
312 self._proxy_activated_cb,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
313 self._proxy_activated_eb,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
314 args,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
315 None,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
316 args,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
317 )
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
318 else:
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
319 # this Deferred will be called when we'll receive activation confirmation from other peer
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
320 d = transport_data["activation_d"] = defer.Deferred()
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
321 else:
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
322 d = defer.succeed(None)
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
323
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
324 if content_data["senders"] == session["role"]:
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
325 # we can now start the stream transfer (or start it after proxy activation)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
326 d.addCallback(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
327 lambda __: choosed_candidate.start_transfer(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
328 transport_data["session_hash"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
329 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
330 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
331 d.addErrback(self._start_eb, session, content_name, client)
1756
061011fad5b1 plugin XEP-0260: better proxy error handling:
Goffi <goffi@goffi.org>
parents: 1755
diff changeset
332
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
333 def _start_eb(self, fail, session, content_name, client):
1756
061011fad5b1 plugin XEP-0260: better proxy error handling:
Goffi <goffi@goffi.org>
parents: 1755
diff changeset
334 """Called when it's not possible to start the transfer
061011fad5b1 plugin XEP-0260: better proxy error handling:
Goffi <goffi@goffi.org>
parents: 1755
diff changeset
335
061011fad5b1 plugin XEP-0260: better proxy error handling:
Goffi <goffi@goffi.org>
parents: 1755
diff changeset
336 Will try to fallback to IBB
061011fad5b1 plugin XEP-0260: better proxy error handling:
Goffi <goffi@goffi.org>
parents: 1755
diff changeset
337 """
061011fad5b1 plugin XEP-0260: better proxy error handling:
Goffi <goffi@goffi.org>
parents: 1755
diff changeset
338 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
339 reason = str(fail.value)
1756
061011fad5b1 plugin XEP-0260: better proxy error handling:
Goffi <goffi@goffi.org>
parents: 1755
diff changeset
340 except AttributeError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
341 reason = str(fail)
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
342 log.warning("Cant start transfert, we'll try fallback method: {}".format(reason))
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
343 self.do_fallback(session, content_name, client)
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
344
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
345 def _candidate_info(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
346 self, candidate_elt, session, content_name, transport_data, client
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
347 ):
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
348 """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
349
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
350 @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
351 (see XEP-0260 §2.3)
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
352 @param session(dict): session data
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
353 @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
354 @param transport_data(dict): transport data
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
355 @param client(unicode): %(doc_client)s
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
356 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
357 if candidate_elt.name == "candidate-error":
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
358 # candidate-error, no candidate worked
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
359 transport_data["peer_best_candidate"] = None
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
360 else:
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
361 # candidate-used, one candidate was choosed
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
362 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
363 cid = candidate_elt.attributes["cid"]
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
364 except KeyError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
365 log.warning("No cid found in <candidate-used>")
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
366 raise exceptions.DataError
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
367 try:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
368 candidate = next((c for c in transport_data["candidates"] if c.id == cid))
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
369 except StopIteration:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
370 log.warning("Given cid doesn't correspond to any known candidate !")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
371 raise exceptions.DataError # TODO: send an error to other peer, and use better exception
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
372 except KeyError:
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
373 # 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
374 # but there is little probability
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
375 log.error(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
376 '"candidates" key doesn\'t exists in transport_data, it should at this point'
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
377 )
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
378 raise exceptions.InternalError
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
379 # at this point we have the candidate choosed by other peer
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
380 transport_data["peer_best_candidate"] = candidate
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
381 log.info("Other peer best candidate: {}".format(candidate))
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
382
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
383 del transport_data["candidates"]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
384 self._check_candidates(session, content_name, transport_data, client)
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
385
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
386 def _proxy_activation_info(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
387 self, proxy_elt, session, content_name, transport_data, client
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
388 ):
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
389 """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
390
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
391 @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
392 (see XEP-0260 §2.4)
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
393 @param session(dict): session data
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
394 @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
395 @param transport_data(dict): transport data
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
396 @param client(unicode): %(doc_client)s
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
397 """
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
398 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
399 activation_d = transport_data.pop("activation_d")
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
400 except KeyError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
401 log.warning("Received unexpected transport-info for proxy activation")
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
402
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
403 if proxy_elt.name == "activated":
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
404 activation_d.callback(None)
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
405 else:
1756
061011fad5b1 plugin XEP-0260: better proxy error handling:
Goffi <goffi@goffi.org>
parents: 1755
diff changeset
406 activation_d.errback(ProxyError())
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
407
4180
b86912d3fd33 plugin IP: fix use of legacy URL + coroutine use:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
408 async def jingle_handler(self, client, action, session, content_name, transport_elt):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
409 content_data = session["contents"][content_name]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
410 transport_data = content_data["transport_data"]
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
411
1631
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
412 if action in (self._j.A_ACCEPTED_ACK, self._j.A_PREPARE_RESPONDER):
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
413 pass
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
414
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
415 elif action == self._j.A_SESSION_ACCEPT:
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
416 # initiator side, we select a candidate in the ones sent by responder
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
417 assert "peer_candidates" not in transport_data
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
418 transport_data["peer_candidates"] = self._parse_candidates(transport_elt)
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
419
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
420 elif action == self._j.A_START:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
421 session_hash = transport_data["session_hash"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
422 peer_candidates = transport_data["peer_candidates"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
423 stream_object = content_data["stream_object"]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
424 self._s5b.associate_stream_object(client, session_hash, stream_object)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
425 stream_d = transport_data.pop("stream_d")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
426 stream_d.chainDeferred(content_data["finished_d"])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
427 peer_session_hash = transport_data["peer_session_hash"]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
428 d = self._s5b.get_best_candidate(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
429 client, peer_candidates, session_hash, peer_session_hash
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
430 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
431 d.addCallback(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
432 self._found_peer_candidate, session, transport_data, content_name, client
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
433 )
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
434
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
435 elif action == self._j.A_SESSION_INITIATE:
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
436 # responder side, we select a candidate in the ones sent by initiator
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
437 # and we give our candidates
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
438 assert "peer_candidates" not in transport_data
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
439 sid = transport_data["sid"] = transport_elt["sid"]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
440 session_hash = transport_data["session_hash"] = self._s5b.get_session_hash(
2927
69e4716d6268 plugins (jingle) file transfer: use initial "from" attribute as local jid instead of client.jid:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
441 session["local_jid"], session["peer_jid"], sid
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
442 )
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
443 peer_session_hash = transport_data["peer_session_hash"] = (
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
444 self._s5b.get_session_hash(session["peer_jid"], session["local_jid"], sid)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
445 ) # requester and target are inversed for peer candidates
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
446 peer_candidates = transport_data["peer_candidates"] = self._parse_candidates(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
447 transport_elt
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
448 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
449 stream_object = content_data["stream_object"]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
450 stream_d = self._s5b.register_hash(client, session_hash, stream_object)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
451 stream_d.chainDeferred(content_data["finished_d"])
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
452 d = self._s5b.get_best_candidate(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
453 client, peer_candidates, session_hash, peer_session_hash
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
454 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
455 d.addCallback(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
456 self._found_peer_candidate, session, transport_data, content_name, client
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
457 )
4180
b86912d3fd33 plugin IP: fix use of legacy URL + coroutine use:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
458 candidates = await self._s5b.get_candidates(client, session["local_jid"])
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
459 # we remove duplicate candidates
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
460 candidates = [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
461 candidate for candidate in candidates if candidate not in peer_candidates
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
462 ]
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
463
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
464 transport_data["candidates"] = candidates
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
465 # we can now build a new <transport> element with our candidates
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
466 transport_elt = self._build_candidates(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
467 session, candidates, sid, session_hash, client
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
468 )
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
469
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
470 elif action == self._j.A_TRANSPORT_INFO:
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
471 # 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
472 candidate_elt = None
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
473
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
474 for method, names in (
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
475 (self._candidate_info, ("candidate-used", "candidate-error")),
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
476 (self._proxy_activation_info, ("activated", "proxy-error")),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
477 ):
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
478 for name in names:
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
479 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
480 candidate_elt = next(transport_elt.elements(NS_JINGLE_S5B, name))
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
481 except StopIteration:
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
482 continue
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
483 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
484 method(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
485 candidate_elt, session, content_name, transport_data, client
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
486 )
1570
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
487 break
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
488
37d4be4a9fed plugins XEP-0260, XEP-0065: proxy handling:
Goffi <goffi@goffi.org>
parents: 1567
diff changeset
489 if candidate_elt is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
490 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
491 "Unexpected transport element: {}".format(transport_elt.toXml())
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
492 )
1631
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
493 elif action == self._j.A_DESTROY:
1758
a66d34353f34 plugin XEP-0260, XEP-0065: fixed session hash handling:
Goffi <goffi@goffi.org>
parents: 1757
diff changeset
494 # the transport is replaced (fallback ?), We need mainly to kill XEP-0065 session.
1631
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
495 # note that sid argument is not necessary for sessions created by this plugin
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
496 self._s5b.kill_session(None, transport_data["session_hash"], None, client)
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
497 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
498 log.warning("FIXME: unmanaged action {}".format(action))
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
499
4180
b86912d3fd33 plugin IP: fix use of legacy URL + coroutine use:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
500 return transport_elt
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
501
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
502 def jingle_terminate(self, client, action, session, content_name, reason_elt):
1755
d2e023da2983 plugin XEP-0260: kill s5b session if session is declined
Goffi <goffi@goffi.org>
parents: 1631
diff changeset
503 if reason_elt.decline:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
504 log.debug("Session declined, deleting S5B session")
1755
d2e023da2983 plugin XEP-0260: kill s5b session if session is declined
Goffi <goffi@goffi.org>
parents: 1631
diff changeset
505 # we just need to clean the S5B session if it is declined
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
506 content_data = session["contents"][content_name]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
507 transport_data = content_data["transport_data"]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
508 self._s5b.kill_session(None, transport_data["session_hash"], None, client)
1755
d2e023da2983 plugin XEP-0260: kill s5b session if session is declined
Goffi <goffi@goffi.org>
parents: 1631
diff changeset
509
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
510 def _do_fallback(self, feature_checked, session, content_name, client):
1631
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
511 """Do the fallback, method called once feature is checked
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
512
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
513 @param feature_checked(bool): True if other peer can do IBB
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4231
diff changeset
514 """
1631
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
515 if not feature_checked:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
516 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
517 "Other peer can't manage jingle IBB, be have to terminate the session"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
518 )
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
519 self._j.terminate(client, self._j.REASON_CONNECTIVITY_ERROR, session)
1631
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
520 else:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
521 self._j.transport_replace(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
522 client, self._jingle_ibb.NAMESPACE, session, content_name
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
523 )
1631
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
524
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
525 def do_fallback(self, session, content_name, client):
1631
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
526 """Fallback to IBB transport, used in last resort
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
527
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
528 @param session(dict): session data
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
529 @param content_name(unicode): name of the current content
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
530 @param client(unicode): %(doc_client)s
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
531 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
532 if session["role"] != self._j.ROLE_INITIATOR:
1631
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
533 # only initiator must do the fallback, see XEP-0260 §3
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
534 return
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
535 if self._jingle_ibb is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
536 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
537 "Jingle IBB (XEP-0261) plugin is not available, we have to close the session"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
538 )
2489
e2a7bb875957 plugin pipe/stream, file transfert: refactoring and improvments:
Goffi <goffi@goffi.org>
parents: 2483
diff changeset
539 self._j.terminate(client, self._j.REASON_CONNECTIVITY_ERROR, session)
1631
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
540 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
541 d = self.host.hasFeature(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
542 client, self._jingle_ibb.NAMESPACE, session["peer_jid"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
543 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
544 d.addCallback(self._do_fallback, session, content_name, client)
1631
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
545 return d
25906c0dbc63 plugin XEP-0260, XEP-0261: fallback from S5B to IBB is implemented
Goffi <goffi@goffi.org>
parents: 1571
diff changeset
546
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
547
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2927
diff changeset
548 @implementer(iwokkel.IDisco)
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
549 class XEP_0260_handler(XMPPHandler):
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
550
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
551 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
552 return [disco.DiscoFeature(NS_JINGLE_S5B)]
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
553
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
554 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
1560
dcce63810733 plugin XEP-0260: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
555 return []