Compare commits

...

3 commits

Author SHA1 Message Date
Jack Robison
e6d5c86efb
Merge pull request #25 from PhilippHomann/master
Add lease_time as parameter
2020-12-21 14:54:52 -05:00
Philipp Homann
2b4295b336
Fix typing and test errors 2020-12-20 15:29:20 +01:00
Philipp Homann
e436e5f483
Add lease_time as parameter 2020-12-20 14:48:55 +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()
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
@ -116,12 +116,13 @@ class UPnP:
:param internal_port: (int) internal port
:param lan_address: (str) internal lan address
:param description: (str) mapping description
:param lease_time: (int) lease time in seconds
:return: None
"""
await self.gateway.commands.AddPortMapping(
NewRemoteHost='', NewExternalPort=external_port, NewProtocol=protocol,
NewInternalPort=internal_port, NewInternalClient=lan_address,
NewEnabled=1, NewPortMappingDescription=description, NewLeaseDuration='0'
NewEnabled=1, NewPortMappingDescription=description, NewLeaseDuration=str(lease_time)
)
return None
@ -208,7 +209,7 @@ class UPnP:
return None
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
@ -216,6 +217,7 @@ class UPnP:
:param protocol: (str) UDP | TCP
:param description: (str) mapping description
:param internal_port: (int) internal port
:param lease_time: (int) lease time in seconds
:return: (int) mapped port
"""
@ -235,7 +237,7 @@ class UPnP:
if int_host == self.lan_address and int_port == requested_port and desc == description:
return port
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
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>]
[--internal_port=<int>] [--lan_address=<str>] [--description=<str>]
[--internal_port=<int>] [--lan_address=<str>] [--description=<str>] [--lease_time=<int>]
Add a new port mapping
@ -71,12 +71,13 @@ Add a new port mapping
:param internal_port: (int) internal port
:param lan_address: (str) internal lan address
:param description: (str) mapping description
:param lease_time: (int) lease time in seconds
:return: None
"""
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
@ -84,6 +85,7 @@ Get a new port mapping. If the requested port is not available, increment until
:param protocol: (str) UDP | TCP
:param description: (str) mapping description
:param internal_port: (int) internal port
:param lease_time: (int) lease time in seconds
:return: (int) mapped port