diff libervia/backend/plugins/plugin_misc_ip.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_misc_ip.py	Sat Dec 09 14:05:02 2023 +0100
+++ b/libervia/backend/plugins/plugin_misc_ip.py	Sat Dec 09 14:30:54 2023 +0100
@@ -200,8 +200,7 @@
         p.transport.loseConnection()
         return local_ip
 
-    @defer.inlineCallbacks
-    def get_local_i_ps(self, client):
+    async def get_local_ips(self, client):
         """Try do discover local area network IPs
 
         @return (deferred): list of lan IP addresses
@@ -210,7 +209,7 @@
         """
         # TODO: manage permission requesting (e.g. for UMTS link)
         if self._local_ip_cache is not None:
-            defer.returnValue(self._local_ip_cache)
+            return self._local_ip_cache
         addresses = []
         localhost = ["127.0.0.1"]
 
@@ -232,51 +231,50 @@
         ip = client.xmlstream.transport.getHost().host
         if self._filter_addresse(ip):
             self._insert_first(addresses, ip)
-            defer.returnValue(addresses)
+            return addresses
 
         # if server is local, we try with NAT-Port
         if self._nat is not None:
-            nat_ip = yield self._nat.get_ip(local=True)
+            nat_ip = await self._nat.get_ip(local=True)
             if nat_ip is not None:
                 self._insert_first(addresses, nat_ip)
-                defer.returnValue(addresses)
+                return addresses
 
             if addresses:
-                defer.returnValue(addresses)
+                return addresses
 
         # still not luck, we need to contact external website
-        allow_get_ip = yield self._external_allowed(client)
+        allow_get_ip = await self._external_allowed(client)
 
         if not allow_get_ip:
-            defer.returnValue(addresses or localhost)
+            return addresses or localhost
 
         try:
-            local_ip = yield defer.ensureDeferred(self._get_ip_from_external(GET_IP_PAGE))
+            local_ip = await defer.ensureDeferred(self._get_ip_from_external(GET_IP_PAGE))
         except (internet_error.DNSLookupError, internet_error.TimeoutError):
             log.warning("Can't access Domain Name System")
         else:
             if local_ip is not None:
                 self._insert_first(addresses, local_ip)
 
-        defer.returnValue(addresses or localhost)
+        return addresses or localhost
 
-    @defer.inlineCallbacks
-    def get_external_ip(self, client):
+    async def get_external_ip(self, client):
         """Try to discover external IP
 
         @return (deferred): external IP address or None if it can't be discovered
         """
         if self._external_ip_cache is not None:
-            defer.returnValue(self._external_ip_cache)
+            return self._external_ip_cache
 
         # we first try with XEP-0279
-        ip_check = yield self.host.hasFeature(client, NS_IP_CHECK)
+        ip_check = await self.host.hasFeature(client, NS_IP_CHECK)
         if ip_check:
             log.debug("Server IP Check available, we use it to retrieve our IP")
             iq_elt = client.IQ("get")
             iq_elt.addElement((NS_IP_CHECK, "address"))
             try:
-                result_elt = yield iq_elt.send()
+                result_elt = await iq_elt.send()
                 address_elt = next(result_elt.elements(NS_IP_CHECK, "address"))
                 ip_elt = next(address_elt.elements(NS_IP_CHECK, "ip"))
             except StopIteration:
@@ -291,33 +289,37 @@
                 external_ip = str(ip_elt)
                 log.debug("External IP found: {}".format(external_ip))
                 self._external_ip_cache = external_ip
-                defer.returnValue(self._external_ip_cache)
+                return self._external_ip_cache
 
         # then with NAT-Port
         if self._nat is not None:
-            nat_ip = yield self._nat.get_ip()
+            nat_ip = await self._nat.get_ip()
             if nat_ip is not None:
                 self._external_ip_cache = nat_ip
-                defer.returnValue(nat_ip)
+                return nat_ip
 
         # and finally by requesting external website
-        allow_get_ip = yield self._external_allowed(client)
-        try:
-            ip = ((yield webclient.getPage(GET_IP_PAGE.encode('utf-8')))
-                  if allow_get_ip else None)
-        except (internet_error.DNSLookupError, internet_error.TimeoutError):
-            log.warning("Can't access Domain Name System")
-            ip = None
-        except web_error.Error as e:
-            log.warning(
-                "Error while retrieving IP on {url}: {message}".format(
-                    url=GET_IP_PAGE, message=e
-                )
-            )
-            ip = None
-        else:
-            self._external_ip_cache = ip
-        defer.returnValue(ip)
+        allow_get_ip = await self._external_allowed(client)
+
+        ip = None
+
+        # FIXME: following code is deprecated, check it
+        # try:
+        #     ip = ((await webclient.getPage(GET_IP_PAGE.encode('utf-8')))
+        #           if allow_get_ip else None)
+        # except (internet_error.DNSLookupError, internet_error.TimeoutError):
+        #     log.warning("Can't access Domain Name System")
+        #     ip = None
+        # except web_error.Error as e:
+        #     log.warning(
+        #         "Error while retrieving IP on {url}: {message}".format(
+        #             url=GET_IP_PAGE, message=e
+        #         )
+        #     )
+        #     ip = None
+        # else:
+        #     self._external_ip_cache = ip
+        return ip
 
 
 @implementer(iwokkel.IDisco)