From e436e5f4837e63399ec2140d4f8d81cee16524e8 Mon Sep 17 00:00:00 2001 From: Philipp Homann Date: Sun, 20 Dec 2020 14:48:55 +0100 Subject: [PATCH 1/2] Add lease_time as parameter --- aioupnp/upnp.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/aioupnp/upnp.py b/aioupnp/upnp.py index 365f152..ecfa229 100644 --- a/aioupnp/upnp.py +++ b/aioupnp/upnp.py @@ -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=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 From 2b4295b33672f3ae05fc143e0781c8fff4145109 Mon Sep 17 00:00:00 2001 From: Philipp Homann Date: Sun, 20 Dec 2020 15:29:20 +0100 Subject: [PATCH 2/2] Fix typing and test errors --- aioupnp/upnp.py | 2 +- tests/test_cli.py | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/aioupnp/upnp.py b/aioupnp/upnp.py index ecfa229..b11f310 100644 --- a/aioupnp/upnp.py +++ b/aioupnp/upnp.py @@ -122,7 +122,7 @@ class UPnP: await self.gateway.commands.AddPortMapping( NewRemoteHost='', NewExternalPort=external_port, NewProtocol=protocol, NewInternalPort=internal_port, NewInternalClient=lan_address, - NewEnabled=1, NewPortMappingDescription=description, NewLeaseDuration=lease_time + NewEnabled=1, NewPortMappingDescription=description, NewLeaseDuration=str(lease_time) ) return None diff --git a/tests/test_cli.py b/tests/test_cli.py index dc7b0a1..a6399fa 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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=] [--protocol=] - [--internal_port=] [--lan_address=] [--description=] + [--internal_port=] [--lan_address=] [--description=] [--lease_time=] 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=] [--protocol=] - [--description=] [--internal_port=] + [--description=] [--internal_port=] [--lease_time=] 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