comparison libervia/backend/plugins/plugin_xep_0320.py @ 4071:4b842c1fb686

refactoring: renamed `sat` package to `libervia.backend`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 11:49:51 +0200
parents sat/plugins/plugin_xep_0320.py@c23cad65ae99
children
comparison
equal deleted inserted replaced
4070:d10748475025 4071:4b842c1fb686
1 #!/usr/bin/env python3
2
3 # Libervia plugin
4 # Copyright (C) 2009-2023 Jérôme Poisson (goffi@goffi.org)
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Affero General Public License for more details.
15
16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
20 from twisted.words.xish import domish
21 from wokkel import disco, iwokkel
22 from zope.interface import implementer
23
24 from libervia.backend.core.constants import Const as C
25 from libervia.backend.core.i18n import _
26 from libervia.backend.core.log import getLogger
27
28
29 log = getLogger(__name__)
30
31 NS_JINGLE_DTLS = "urn:xmpp:jingle:apps:dtls:0"
32
33 PLUGIN_INFO = {
34 C.PI_NAME: "Use of DTLS-SRTP in Jingle Sessions",
35 C.PI_IMPORT_NAME: "XEP-0320",
36 C.PI_TYPE: "XEP",
37 C.PI_MODES: C.PLUG_MODE_BOTH,
38 C.PI_PROTOCOLS: ["XEP-0320"],
39 C.PI_DEPENDENCIES: ["XEP-0176"],
40 C.PI_RECOMMENDATIONS: [],
41 C.PI_MAIN: "XEP_0320",
42 C.PI_HANDLER: "yes",
43 C.PI_DESCRIPTION: _("""Use of DTLS-SRTP with RTP (for e2ee of A/V calls)"""),
44 }
45
46
47 class XEP_0320:
48 def __init__(self, host):
49 log.info(f"plugin {PLUGIN_INFO[C.PI_NAME]!r} initialization")
50 host.trigger.add("XEP-0176_parse_transport", self._parse_transport_trigger)
51 host.trigger.add("XEP-0176_build_transport", self._build_transport_trigger)
52
53 def get_handler(self, client):
54 return XEP_0320_handler()
55
56 def _parse_transport_trigger(
57 self, transport_elt: domish.Element, ice_data: dict
58 ) -> bool:
59 """Parse the <fingerprint> element"""
60 fingerprint_elt = next(
61 transport_elt.elements(NS_JINGLE_DTLS, "fingerprint"), None
62 )
63 if fingerprint_elt is not None:
64 try:
65 ice_data["fingerprint"] = {
66 "hash": fingerprint_elt["hash"],
67 "setup": fingerprint_elt["setup"],
68 "fingerprint": str(fingerprint_elt),
69 }
70 except KeyError as e:
71 log.warning(
72 f"invalid <fingerprint> (attribue {e} is missing): "
73 f"{fingerprint_elt.toXml()})"
74 )
75
76 return True
77
78 def _build_transport_trigger(
79 self, tranport_elt: domish.Element, ice_data: dict
80 ) -> bool:
81 """Build the <fingerprint> element if possible"""
82 try:
83 fingerprint_data = ice_data["fingerprint"]
84 hash_ = fingerprint_data["hash"]
85 setup = fingerprint_data["setup"]
86 fingerprint = fingerprint_data["fingerprint"]
87 except KeyError:
88 pass
89 else:
90 fingerprint_elt = tranport_elt.addElement(
91 (NS_JINGLE_DTLS, "fingerprint"), content=fingerprint
92 )
93 fingerprint_elt["hash"] = hash_
94 fingerprint_elt["setup"] = setup
95
96 return True
97
98
99 @implementer(iwokkel.IDisco)
100 class XEP_0320_handler(XMPPHandler):
101 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
102 return [disco.DiscoFeature(NS_JINGLE_DTLS)]
103
104 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
105 return []