Add lease_time as parameter #25

Merged
PhilippHomann merged 2 commits from master into master 2020-12-21 20:54:52 +01:00
2 changed files with 10 additions and 6 deletions

View file

@ -107,7 +107,7 @@ class UPnP:
return await self.gateway.commands.GetExternalIPAddress() return await self.gateway.commands.GetExternalIPAddress()
async def add_port_mapping(self, external_port: int, protocol: str, internal_port: int, lan_address: str, async def add_port_mapping(self, external_port: int, protocol: str, internal_port: int, lan_address: str,
description: str) -> None: description: str, lease_time: int = 0) -> None:
""" """
Add a new port mapping Add a new port mapping
@ -116,12 +116,13 @@ class UPnP:
:param internal_port: (int) internal port :param internal_port: (int) internal port
:param lan_address: (str) internal lan address :param lan_address: (str) internal lan address
:param description: (str) mapping description :param description: (str) mapping description
:param lease_time: (int) lease time in seconds
:return: None :return: None
""" """
await self.gateway.commands.AddPortMapping( await self.gateway.commands.AddPortMapping(
NewRemoteHost='', NewExternalPort=external_port, NewProtocol=protocol, NewRemoteHost='', NewExternalPort=external_port, NewProtocol=protocol,
NewInternalPort=internal_port, NewInternalClient=lan_address, NewInternalPort=internal_port, NewInternalClient=lan_address,
NewEnabled=1, NewPortMappingDescription=description, NewLeaseDuration='0' NewEnabled=1, NewPortMappingDescription=description, NewLeaseDuration=str(lease_time)
) )
return None return None
@ -208,7 +209,7 @@ class UPnP:
return None return None
async def get_next_mapping(self, port: int, protocol: str, description: str, async def get_next_mapping(self, port: int, protocol: str, description: str,
internal_port: Optional[int] = None) -> int: internal_port: Optional[int] = None, lease_time: int = 0) -> int:
""" """
Get a new port mapping. If the requested port is not available, increment until the next free port is mapped Get a new port mapping. If the requested port is not available, increment until the next free port is mapped
@ -216,6 +217,7 @@ class UPnP:
:param protocol: (str) UDP | TCP :param protocol: (str) UDP | TCP
:param description: (str) mapping description :param description: (str) mapping description
:param internal_port: (int) internal port :param internal_port: (int) internal port
:param lease_time: (int) lease time in seconds
:return: (int) mapped port :return: (int) mapped port
""" """
@ -235,7 +237,7 @@ class UPnP:
if int_host == self.lan_address and int_port == requested_port and desc == description: if int_host == self.lan_address and int_port == requested_port and desc == description:
return port return port
port += 1 port += 1
await self.add_port_mapping(port, protocol, _internal_port, self.lan_address, description) await self.add_port_mapping(port, protocol, _internal_port, self.lan_address, description, lease_time)
return port return port
async def gather_debug_info(self) -> str: # pragma: no cover async def gather_debug_info(self) -> str: # pragma: no cover

View file

@ -62,7 +62,7 @@ Get the external ip address from the gateway
""" """
expected_add_port_mapping_usage = """aioupnp [-h] [--debug_logging] add_port_mapping [--external_port=<int>] [--protocol=<str>] expected_add_port_mapping_usage = """aioupnp [-h] [--debug_logging] add_port_mapping [--external_port=<int>] [--protocol=<str>]
[--internal_port=<int>] [--lan_address=<str>] [--description=<str>] [--internal_port=<int>] [--lan_address=<str>] [--description=<str>] [--lease_time=<int>]
Add a new port mapping Add a new port mapping
@ -71,12 +71,13 @@ Add a new port mapping
:param internal_port: (int) internal port :param internal_port: (int) internal port
:param lan_address: (str) internal lan address :param lan_address: (str) internal lan address
:param description: (str) mapping description :param description: (str) mapping description
:param lease_time: (int) lease time in seconds
:return: None :return: None
""" """
expected_get_next_mapping_usage = """aioupnp [-h] [--debug_logging] get_next_mapping [--port=<int>] [--protocol=<str>] expected_get_next_mapping_usage = """aioupnp [-h] [--debug_logging] get_next_mapping [--port=<int>] [--protocol=<str>]
[--description=<str>] [--internal_port=<typing.Union[int, NoneType]>] [--description=<str>] [--internal_port=<typing.Union[int, NoneType]>] [--lease_time=<int>]
Get a new port mapping. If the requested port is not available, increment until the next free port is mapped Get a new port mapping. If the requested port is not available, increment until the next free port is mapped
@ -84,6 +85,7 @@ Get a new port mapping. If the requested port is not available, increment until
:param protocol: (str) UDP | TCP :param protocol: (str) UDP | TCP
:param description: (str) mapping description :param description: (str) mapping description
:param internal_port: (int) internal port :param internal_port: (int) internal port
:param lease_time: (int) lease time in seconds
:return: (int) mapped port :return: (int) mapped port