check for commands being found, try to fallback to miniupnpc if they arent

This commit is contained in:
Jack Robison 2018-08-08 13:48:24 -04:00
parent 576734d2cc
commit ba87ce9496
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
2 changed files with 25 additions and 3 deletions

View file

@ -1,4 +1,4 @@
__version__ = "0.0.1a9" __version__ = "0.0.1a10"
__name__ = "txupnp" __name__ = "txupnp"
__author__ = "Jack Robison" __author__ = "Jack Robison"
__maintainer__ = "Jack Robison" __maintainer__ = "Jack Robison"

View file

@ -4,7 +4,6 @@ from twisted.internet import defer
from txupnp.fault import UPnPError from txupnp.fault import UPnPError
from txupnp.soap import SOAPServiceManager from txupnp.soap import SOAPServiceManager
from txupnp.scpd import UPnPFallback from txupnp.scpd import UPnPFallback
from txupnp.util import DeferredDict
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -24,7 +23,17 @@ class UPnP(object):
@property @property
def commands(self): def commands(self):
try: try:
return self.soap_manager.get_runner() runner = self.soap_manager.get_runner()
required_commands = [
"GetExternalIPAddress",
"AddPortMapping",
"GetSpecificPortMappingEntry",
"GetGenericPortMappingEntry",
"DeletePortMapping"
]
if all((command in runner._registered_commands for command in required_commands)):
return runner
raise UPnPError("required commands not found")
except UPnPError as err: except UPnPError as err:
if self.try_miniupnpc_fallback and self.miniupnpc_runner: if self.try_miniupnpc_fallback and self.miniupnpc_runner:
return self.miniupnpc_runner return self.miniupnpc_runner
@ -57,6 +66,19 @@ class UPnP(object):
finally: finally:
if not keep_listening: if not keep_listening:
self.soap_manager.sspd_factory.disconnect() self.soap_manager.sspd_factory.disconnect()
if found:
try:
runner = self.soap_manager.get_runner()
required_commands = [
"GetExternalIPAddress",
"AddPortMapping",
"GetSpecificPortMappingEntry",
"GetGenericPortMappingEntry",
"DeletePortMapping"
]
found = all((command in runner._registered_commands for command in required_commands))
except UPnPError:
found = False
if not found and self.try_miniupnpc_fallback: if not found and self.try_miniupnpc_fallback:
found = yield self.start_miniupnpc_fallback() found = yield self.start_miniupnpc_fallback()
defer.returnValue(found) defer.returnValue(found)