From 92c0196887eb81f9b2b2c6c71315fe7047c3faa1 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Thu, 1 Aug 2019 13:19:56 +0300 Subject: [PATCH] Add self peer into knownnodes if detected external IP with UPnP --- src/upnp.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/upnp.py b/src/upnp.py index da075c65..d4ffce36 100644 --- a/src/upnp.py +++ b/src/upnp.py @@ -16,6 +16,7 @@ from random import randint from urlparse import urlparse from xml.dom.minidom import Document, parseString +import knownnodes import queues import state import tr @@ -166,9 +167,11 @@ class Router: # pylint: disable=old-style-class def GetExternalIPAddress(self): """Get the external address""" - resp = self.soapRequest(self.upnp_schema + ':1', 'GetExternalIPAddress') - dom = parseString(resp) - return dom.getElementsByTagName('NewExternalIPAddress')[0].childNodes[0].data + resp = self.soapRequest( + self.upnp_schema + ':1', 'GetExternalIPAddress') + dom = parseString(resp.read()) + return dom.getElementsByTagName( + 'NewExternalIPAddress')[0].childNodes[0].data def soapRequest(self, service, action, arguments=None): """Make a request to a router""" @@ -261,6 +264,17 @@ class uPnPThread(threading.Thread, StoppableThread): logger.debug("Found UPnP router at %s", ip) self.routers.append(newRouter) self.createPortMapping(newRouter) + try: + self_peer = state.Peer( + newRouter.GetExternalIPAddress(), + self.extPort + ) + except: + logger.debug('Failed to get external IP') + else: + with knownnodes.knownNodesLock: + knownnodes.addKnownNode( + 1, self_peer, is_self=True) queues.UISignalQueue.put(('updateStatusBar', tr._translate( "MainWindow", 'UPnP port mapping established on port %1' ).arg(str(self.extPort))))