changeset 3132:b64dd7c1496d

plugin ip: fixed IP detection with external website
author Goffi <goffi@goffi.org>
date Mon, 27 Jan 2020 19:53:31 +0100 (2020-01-27)
parents d6da17f6e4ce
children 87b8808ac49d
files sat/plugins/plugin_misc_ip.py sat/plugins/plugin_xep_0065.py
diffstat 2 files changed, 15 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_ip.py	Mon Jan 27 19:53:31 2020 +0100
+++ b/sat/plugins/plugin_misc_ip.py	Mon Jan 27 19:53:31 2020 +0100
@@ -164,8 +164,8 @@
     def _insertFirst(self, addresses, ip_addr):
         """Insert ip_addr as first item in addresses
 
+        @param addresses(list): list of IP addresses
         @param ip_addr(str): IP addresse
-        @param addresses(list): list of IP addresses
         """
         if ip_addr in addresses:
             if addresses[0] != ip_addr:
@@ -174,11 +174,11 @@
         else:
             addresses.insert(0, ip_addr)
 
-    def _getIPFromExternal(self, ext_url):
+    async def _getIPFromExternal(self, ext_url):
         """Get local IP by doing a connection on an external url
 
         @param ext_utl(str): url to connect to
-        @return (D(str)): return local IP
+        @return (str, None): return local IP, or None if it's not possible
         """
         url = urllib.parse.urlparse(ext_url)
         port = url.port
@@ -189,20 +189,16 @@
                 port = 443
             else:
                 log.error("Unknown url scheme: {}".format(url.scheme))
-                defer.returnValue(None)
+                return None
         if url.hostname is None:
             log.error("Can't find url hostname for {}".format(GET_IP_PAGE))
 
         point = endpoints.TCP4ClientEndpoint(reactor, url.hostname, port)
 
-        def gotConnection(p):
-            local_ip = p.transport.getHost().host
-            p.transport.loseConnection()
-            return local_ip
-
-        d = endpoints.connectProtocol(point, protocol.Protocol())
-        d.addCallback(gotConnection)
-        return d
+        p = await endpoints.connectProtocol(point, protocol.Protocol())
+        local_ip = p.transport.getHost().host
+        p.transport.loseConnection()
+        return local_ip
 
     @defer.inlineCallbacks
     def getLocalIPs(self, client):
@@ -255,12 +251,14 @@
             defer.returnValue(addresses or localhost)
 
         try:
-            ip_tuple = yield self._getIPFromExternal(GET_IP_PAGE)
+            local_ip = yield defer.ensureDeferred(self._getIPFromExternal(GET_IP_PAGE))
         except (internet_error.DNSLookupError, internet_error.TimeoutError):
             log.warning("Can't access Domain Name System")
-            defer.returnValue(addresses or localhost)
-        self._insertFirst(addresses, ip_tuple.local)
-        defer.returnValue(addresses)
+        else:
+            if local_ip is not None:
+                self._insertFirst(addresses, local_ip)
+
+        defer.returnValue(addresses or localhost)
 
     @defer.inlineCallbacks
     def getExternalIP(self, client):
--- a/sat/plugins/plugin_xep_0065.py	Mon Jan 27 19:53:31 2020 +0100
+++ b/sat/plugins/plugin_xep_0065.py	Mon Jan 27 19:53:31 2020 +0100
@@ -1082,7 +1082,7 @@
         """
         log.info("Socks5 Bytestream: TimeOut reached")
         session = self.getSession(client, session_hash)
-        session[DEFER_KEY].errback(exceptions.TimeOutError)
+        session[DEFER_KEY].errback(exceptions.TimeOutError())
 
     def killSession(self, failure_, session_hash, sid, client):
         """Clean the current session