aioupnp/txupnp/upnp.py

109 lines
3.6 KiB
Python
Raw Normal View History

2018-07-27 01:49:33 +02:00
import logging
from twisted.internet import defer
from txupnp.fault import UPnPError
2018-07-29 04:08:24 +02:00
from txupnp.soap import SOAPServiceManager
2018-07-27 01:49:33 +02:00
log = logging.getLogger(__name__)
2018-07-29 04:08:24 +02:00
class UPnP(object):
def __init__(self, reactor):
self._reactor = reactor
self.soap_manager = SOAPServiceManager(reactor)
2018-07-27 01:49:33 +02:00
@property
2018-07-29 04:08:24 +02:00
def lan_address(self):
return self.soap_manager.lan_address
2018-07-27 01:49:33 +02:00
@property
2018-07-29 04:08:24 +02:00
def commands(self):
return self.soap_manager.get_runner()
2018-07-27 01:49:33 +02:00
2018-07-29 04:08:24 +02:00
def m_search(self, address, ttl=30, max_devices=2):
2018-07-27 01:49:33 +02:00
"""
Perform a HTTP over UDP M-SEARCH query
2018-07-29 04:08:24 +02:00
returns (list) [{
'server: <gateway os and version string>
2018-07-27 01:49:33 +02:00
'location': <upnp gateway url>,
'cache-control': <max age>,
'date': <server time>,
'usn': <usn>
2018-07-29 04:08:24 +02:00
}, ...]
2018-07-27 01:49:33 +02:00
"""
2018-07-29 04:08:24 +02:00
return self.soap_manager.sspd_factory.m_search(address, ttl=ttl, max_devices=max_devices)
2018-07-27 01:49:33 +02:00
@defer.inlineCallbacks
2018-07-29 04:08:24 +02:00
def discover(self, ttl=30, max_devices=2):
2018-07-27 01:49:33 +02:00
try:
2018-07-29 04:08:24 +02:00
yield self.soap_manager.discover_services(ttl=ttl, max_devices=max_devices)
2018-07-27 01:49:33 +02:00
except defer.TimeoutError:
2018-07-29 04:08:24 +02:00
log.warning("failed to find upnp gateway")
2018-07-27 01:49:33 +02:00
defer.returnValue(False)
defer.returnValue(True)
def get_external_ip(self):
return self.commands.GetExternalIPAddress()
def add_port_mapping(self, external_port, protocol, internal_port, lan_address, description, lease_duration):
return self.commands.AddPortMapping(
NewRemoteHost=None, NewExternalPort=external_port, NewProtocol=protocol,
NewInternalPort=internal_port, NewInternalClient=lan_address,
NewEnabled=1, NewPortMappingDescription=description,
NewLeaseDuration=lease_duration
)
def get_port_mapping_by_index(self, index):
return self.commands.GetGenericPortMappingEntry(NewPortMappingIndex=index)
@defer.inlineCallbacks
def get_redirects(self):
redirects = []
cnt = 0
while True:
try:
redirect = yield self.get_port_mapping_by_index(cnt)
redirects.append(redirect)
cnt += 1
except UPnPError:
break
defer.returnValue(redirects)
def get_specific_port_mapping(self, external_port, protocol):
2018-07-29 04:08:24 +02:00
"""
:param external_port: (int) external port to listen on
:param protocol: (str) 'UDP' | 'TCP'
:return: (int) <internal port>, (str) <lan ip>, (bool) <enabled>, (str) <description>, (int) <lease time>
"""
2018-07-27 01:49:33 +02:00
return self.commands.GetSpecificPortMappingEntry(
NewRemoteHost=None, NewExternalPort=external_port, NewProtocol=protocol
)
def delete_port_mapping(self, external_port, protocol):
2018-07-29 04:08:24 +02:00
"""
:param external_port: (int) external port to listen on
:param protocol: (str) 'UDP' | 'TCP'
:return: None
"""
2018-07-27 01:49:33 +02:00
return self.commands.DeletePortMapping(
NewRemoteHost=None, NewExternalPort=external_port, NewProtocol=protocol
)
2018-07-29 04:08:24 +02:00
def get_rsip_nat_status(self):
"""
:return: (bool) NewRSIPAvailable, (bool) NewNATEnabled
"""
return self.commands.GetNATRSIPStatus()
def get_status_info(self):
"""
:return: (str) NewConnectionStatus, (str) NewLastConnectionError, (int) NewUptime
"""
return self.commands.GetStatusInfo()
def get_connection_type_info(self):
"""
:return: (str) NewConnectionType (str), NewPossibleConnectionTypes (str)
"""
return self.commands.GetConnectionTypeInfo()