diff libervia/backend/plugins/plugin_xep_0065.py @ 4180:b86912d3fd33

plugin IP: fix use of legacy URL + coroutine use: An https:/salut-a-toi.org URL was used to retrieve external IP, but it's not valid anymore, resulting in an exception. This feature is currently disabled. Also moved several methods from legacy inline callbacks to coroutines.
author Goffi <goffi@goffi.org>
date Sat, 09 Dec 2023 14:30:54 +0100
parents 4b842c1fb686
children 0d7bb4df2343
line wrap: on
line diff
--- a/libervia/backend/plugins/plugin_xep_0065.py	Sat Dec 09 14:05:02 2023 +0100
+++ b/libervia/backend/plugins/plugin_xep_0065.py	Sat Dec 09 14:30:54 2023 +0100
@@ -784,8 +784,7 @@
             )
         return self._server_factory
 
-    @defer.inlineCallbacks
-    def get_proxy(self, client, local_jid):
+    async def get_proxy(self, client, local_jid):
         """Return the proxy available for this profile
 
         cache is used between clients using the same server
@@ -802,12 +801,12 @@
 
         server = client.host if client.is_component else client.jid.host
         try:
-            defer.returnValue(self._cache_proxies[server])
+            return self._cache_proxies[server]
         except KeyError:
             pass
         try:
             proxy = (
-                yield self.host.find_service_entities(client, "proxy", "bytestreams")
+                await self.host.find_service_entities(client, "proxy", "bytestreams")
             ).pop()
         except (defer.CancelledError, StopIteration, KeyError):
             notFound(server)
@@ -817,7 +816,7 @@
         iq_elt.addElement((NS_BS, "query"))
 
         try:
-            result_elt = yield iq_elt.send()
+            result_elt = await iq_elt.send()
         except jabber_error.StanzaError as failure:
             log.warning(
                 "Error while requesting proxy info on {jid}: {error}".format(
@@ -841,10 +840,9 @@
 
         proxy_infos = self._cache_proxies[server] = ProxyInfos(host, jid_, port)
         log.info("Proxy found: {}".format(proxy_infos))
-        defer.returnValue(proxy_infos)
+        return proxy_infos
 
-    @defer.inlineCallbacks
-    def _get_network_data(self, client):
+    async def _get_network_data(self, client):
         """Retrieve information about network
 
         @param client: %(doc_client)s
@@ -852,8 +850,8 @@
         """
         self.get_socks_5_server_factory()
         local_port = self._server_factory_port
-        external_ip = yield self._ip.get_external_ip(client)
-        local_ips = yield self._ip.get_local_i_ps(client)
+        external_ip = await self._ip.get_external_ip(client)
+        local_ips = await self._ip.get_local_ips(client)
 
         if external_ip is not None and self._external_port is None:
             if external_ip != local_ips[0]:
@@ -861,7 +859,7 @@
                 if self._np is None:
                     log.warning("NAT port plugin not available, we can't map port")
                 else:
-                    ext_port = yield self._np.map_port(
+                    ext_port = await self._np.map_port(
                         local_port, desc="SaT socks5 stream"
                     )
                     if ext_port is None:
@@ -869,10 +867,9 @@
                     else:
                         self._external_port = ext_port
 
-        defer.returnValue((local_port, self._external_port, local_ips, external_ip))
+        return (local_port, self._external_port, local_ips, external_ip)
 
-    @defer.inlineCallbacks
-    def get_candidates(self, client, local_jid):
+    async def get_candidates(self, client, local_jid):
         """Return a list of our stream candidates
 
         @param local_jid(jid.JID): jid to use as local jid
@@ -881,10 +878,10 @@
             client.jid would be file.example.net)
         @return (D(list[Candidate])): list of candidates, ordered by priority
         """
-        server_factory = yield self.get_socks_5_server_factory()
-        local_port, ext_port, local_ips, external_ip = yield self._get_network_data(client)
+        server_factory = self.get_socks_5_server_factory()
+        local_port, ext_port, local_ips, external_ip = await self._get_network_data(client)
         try:
-            proxy = yield self.get_proxy(client, local_jid)
+            proxy = await self.get_proxy(client, local_jid)
         except exceptions.NotFound:
             proxy = None
 
@@ -948,7 +945,7 @@
 
         # should be already sorted, but just in case the priorities get weird
         candidates.sort(key=lambda c: c.priority, reverse=True)
-        defer.returnValue(candidates)
+        return candidates
 
     def _add_connector(self, connector, candidate):
         """Add connector used to connect to candidate, and return client factory's connection Deferred
@@ -1164,7 +1161,7 @@
             args = [client, session_data, local_jid]
             d.addCallbacks(self._iq_negotiation_cb, self._iq_negotiation_eb, args, None, args)
 
-        self.get_candidates(client, local_jid).addCallback(got_candidates)
+        defer.ensureDeferred(self.get_candidates(client, local_jid)).addCallback(got_candidates)
         return session_data[DEFER_KEY]
 
     def _iq_negotiation_cb(self, iq_elt, client, session_data, local_jid):