plugin ip: fixed IP detection with external website
This commit is contained in:
parent
5ffa208962
commit
655361db1b
2 changed files with 15 additions and 17 deletions
|
|
@ -164,8 +164,8 @@ class IPPlugin(object):
|
|||
def _insertFirst(self, addresses, ip_addr):
|
||||
"""Insert ip_addr as first item in addresses
|
||||
|
||||
@param ip_addr(str): IP addresse
|
||||
@param addresses(list): list of IP addresses
|
||||
@param ip_addr(str): IP addresse
|
||||
"""
|
||||
if ip_addr in addresses:
|
||||
if addresses[0] != ip_addr:
|
||||
|
|
@ -174,11 +174,11 @@ class IPPlugin(object):
|
|||
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 @@ class IPPlugin(object):
|
|||
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 @@ class IPPlugin(object):
|
|||
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):
|
||||
|
|
|
|||
|
|
@ -1082,7 +1082,7 @@ class XEP_0065(object):
|
|||
"""
|
||||
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
|
||||
|
|
|
|||
Loading…
Reference in a new issue