From 5adb924aae795f4c2447884fd03c499efb302691 Mon Sep 17 00:00:00 2001 From: brostos <67740566+brostosjoined@users.noreply.github.com> Date: Sat, 2 Mar 2024 12:14:20 +0300 Subject: [PATCH 01/16] Add files via upload --- plugins/utilities/natpmp_upnp.py | 256 +++++++++++++++++++++++++++++++ 1 file changed, 256 insertions(+) create mode 100644 plugins/utilities/natpmp_upnp.py diff --git a/plugins/utilities/natpmp_upnp.py b/plugins/utilities/natpmp_upnp.py new file mode 100644 index 0000000..0700a49 --- /dev/null +++ b/plugins/utilities/natpmp_upnp.py @@ -0,0 +1,256 @@ +# ba_meta require api 8 + +import babase +import bauiv1 as bui +import bascenev1 + +import shutil +import hashlib +import threading +import ast +import time +from pathlib import Path +from os import remove, getcwd +from urllib.request import urlretrieve, urlopen + + +# Plucked from https://github.com/ethereum/upnp-port-forward/blob/master/upnp_port_forward/ +WAN_SERVICE_NAMES = ( + "WANIPConn1", + "WANIPConnection.1", # Nighthawk C7800 + "WANPPPConnection.1", # CenturyLink C1100Z + "WANPPPConn1", # Huawei B528s-23a +) + + +def threaded(func): + def wrapper(*args, **kwargs): + thread = threading.Thread( + target=func, args=args, kwargs=kwargs, name=func.__name__ + ) + thread.start() + + return wrapper + + +@threaded +def get_modules() -> None: + install_path = Path(f"{getcwd()}/ba_data/python") # For the guys like me on windows + upnpy_path = Path(f"{install_path}/upnp.tar.gz") + nat_pmp_path = Path(f"{install_path}/natpmp.tar.gz") + upnpy_file_path = Path(f"{install_path}/upnpy") + nat_pmp_file_path = Path(f"{install_path}/natpmp") + nat_pmp_source_dir = Path(f"{install_path}/NAT-PMP-1.3.2/natpmp") + upnpy_source_dir = Path(f"{install_path}/UPnPy-1.1.8/upnpy") + if ( + not Path(f"{nat_pmp_file_path}/__init__.py").exists() + and not Path(f"{upnpy_file_path}/__init__.py").exists() + ): # YouKnowDev + nat_pmp_url = "https://files.pythonhosted.org/packages/dc/0c/28263fb4a623e6718a179bca1f360a6ae38f0f716a6cacdf47e15a5fa23e/NAT-PMP-1.3.2.tar.gz" + upnpy_url = "https://files.pythonhosted.org/packages/80/66/d4e721ff8766ea3e78730574669f6feeb71e438a8c2d7a62b2c3456a5c12/UPnPy-1.1.8.tar.gz" + try: + # fix issue where the file delete themselves + try: + shutil.rmtree(nat_pmp_file_path) + shutil.rmtree(upnpy_file_path) + except: + pass + nat_pmp_filename, headers = urlretrieve(nat_pmp_url, filename=nat_pmp_path) + upnpy_filename, headers = urlretrieve(upnpy_url, filename=upnpy_path) + with open(nat_pmp_filename, "rb") as f: + content = f.read() + assert ( + hashlib.md5(content).hexdigest() + == "7e5faa22acb0935f75664e9c4941fda4" + ) + with open(upnpy_filename, "rb") as f: + content = f.read() + assert ( + hashlib.md5(content).hexdigest() + == "b33ad0b38e39af258e2c8f38813abf7b" + ) + shutil.unpack_archive(nat_pmp_filename, install_path) + shutil.unpack_archive(upnpy_filename, install_path) + remove(upnpy_path) + remove(nat_pmp_path) + shutil.copytree(nat_pmp_source_dir, nat_pmp_file_path) + shutil.copytree(upnpy_source_dir, upnpy_file_path) + shutil.rmtree(Path(f"{install_path}/NAT-PMP-1.3.2")) + shutil.rmtree(Path(f"{install_path}/UPnPy-1.1.8")) + except Exception as e: + if type(e) == shutil.Error: + shutil.rmtree(Path(f"{install_path}/NAT-PMP-1.3.2")) + shutil.rmtree(Path(f"{install_path}/UPnPy-1.1.8")) + else: + pass + + # Patch to natpmp to work without netifaces + with open(f"{nat_pmp_file_path}/__init__.py", "r") as f: + lines = f.readlines() + # Define the new function as a string + new_function = ''' +# Plucked from https://github.com/tenable/upnp_info/blob/d20a1fda8ca4877d61b89fe7126077a3a5f0b322/upnp_info.py#L23 +def get_gateway_addr(): + """Returns the gateway ip of the router if upnp service is available""" + locations = set() + location_regex = re.compile("location:[ ]*(.+)"+ chr(13) + chr(10), re.IGNORECASE) + ssdpDiscover = ( + "M-SEARCH * HTTP/1.1"+ chr(13) + chr(10) + + "HOST: 239.255.255.250:1900"+ chr(13) + chr(10) + + 'MAN: "ssdp:discover"'+ chr(13) + chr(10) + + "MX: 1"+ chr(13) + chr(10) + + "ST: ssdp:all"+ chr(13) + chr(10) + + chr(13) + chr(10) + ) + + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + sock.sendto(ssdpDiscover.encode("ASCII"), ("239.255.255.250", 1900)) + sock.settimeout(3) + try: + while True: + data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes + location_result = location_regex.search(data.decode("ASCII")) + if location_result and (location_result.group(1) in locations) == False: + locations.add(location_result.group(1)) + except socket.error: + sock.close() + if locations: + for location in locations: + parsed_url = urlparse(location) + if parsed_url.path.endswith("xml"): + gateway_ip_address = parsed_url.netloc.split(':')[0] + return gateway_ip_address + +''' + + # Replace the function + lines[224:229] = new_function + lines[21] = "import socket\nimport re\nfrom urllib.parse import urlparse" + + with open(f"{nat_pmp_file_path}/__init__.py", "w") as f: + f.writelines(lines) + + add_port_mapping() + + +@threaded +def confirm_port(): + time.sleep(5) + with urlopen("https://legacy.ballistica.net/bsAccessCheck") as resp: + resp = resp.read().decode() + resp = ast.literal_eval(resp) + return resp["accessible"] + + +@threaded +def add_port_mapping(): + # Try to add UDP port using NAT-PMP + import socket + import natpmp + from natpmp import NATPMPUnsupportedError, NATPMPNetworkError + + try: + natpmp.map_port( + natpmp.NATPMP_PROTOCOL_UDP, + 43210, + 43210, + 0, + gateway_ip=natpmp.get_gateway_addr(), + ) + if confirm_port(): + babase.screenmessage( + "You are now joinable from the internet", (0.2, 1, 0.2) + ) + except (NATPMPUnsupportedError, NATPMPNetworkError): + import upnpy + + upnp = upnpy.UPnP() + devices = upnp.discover() + + local_ip = ( + ( + [ + ip + for ip in socket.gethostbyname_ex(socket.gethostname())[2] + if not ip.startswith("127.") + ] + or [ + [ + (s.connect(("8.8.8.8", 53)), s.getsockname()[0], s.close()) + for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)] + ][0][1] + ] + ) + + ["no IP found"] + )[0] + for upnp_dev in devices: + for service in upnp_dev.services: + if service in WAN_SERVICE_NAMES: + service = upnp_dev[service] + service.AddPortMapping( + NewRemoteHost="", + NewExternalPort=43210, + NewProtocol="UDP", + NewInternalPort=43210, + NewInternalClient=str(local_ip), + NewEnabled="1", + NewPortMappingDescription="Bombsquad", + NewLeaseDuration=0, + ) + if confirm_port(): + babase.screenmessage( + "You are now joinable from the internet", (0.2, 1, 0.2) + ) + bui.getsound("shieldUp").play() + + +@threaded +def check_port(): + import upnpy + from upnpy.exceptions import SOAPError + + upnp = upnpy.UPnP() + devices = upnp.discover() + + if devices == []: + babase.screenmessage( + "Please enable upnp service on your router", (1.00, 0.15, 0.15) + ) + # bui.getsound('shieldDown').play() -> RuntimeError : Sound creation failed + return + + for upnp_dev in devices: + for service in upnp_dev.services: + if service in WAN_SERVICE_NAMES: + service = upnp_dev[service] + if service.GetSpecificPortMappingEntry: + try: + result = service.GetSpecificPortMappingEntry( + NewRemoteHost="", NewExternalPort=43210, NewProtocol="UDP" + ) + if result and confirm_port(): + return True + elif result and not confirm_port(): + if babase.do_once(): + babase.screenmessage( + "Oops seems like your network doesn't support upnp", + (1.0, 0.15, 0.15), + ) + babase.pushcall( + bui.getsound("error").play(), from_other_thread=True + ) + return True + elif not result and not confirm_port(): + return False + except SOAPError: + pass + + +# ba_meta export babase.Plugin +class Joinable(babase.Plugin): + def on_app_running(self) -> None: + get_modules() + if confirm_port(): + return + elif not check_port(): + add_port_mapping() From dfd7f41e2b39bbb095b3bc69f8e6d4f8354d2235 Mon Sep 17 00:00:00 2001 From: brostos <67740566+brostosjoined@users.noreply.github.com> Date: Sat, 2 Mar 2024 12:19:53 +0300 Subject: [PATCH 02/16] Update utilities.json --- plugins/utilities.json | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/plugins/utilities.json b/plugins/utilities.json index aca55db..aa8d4fb 100644 --- a/plugins/utilities.json +++ b/plugins/utilities.json @@ -1254,6 +1254,20 @@ "md5sum": "079e857197979aabf53f232b3cce56ba" } } + }, + "natpmp_upnp": { + "description": "Automatic port forwarding if upnp is enabled", + "external_url": "", + "authors": [ + { + "name": "brostosjoined", + "email": "", + "discord": "brostos" + } + ], + "versions": { + "1.0.0": null, + } } } -} \ No newline at end of file +} From 685c122a2d07bece3a4f0acb4f376893e00f0996 Mon Sep 17 00:00:00 2001 From: brostos <67740566+brostosjoined@users.noreply.github.com> Date: Sat, 2 Mar 2024 12:25:31 +0300 Subject: [PATCH 03/16] Update utilities.json --- plugins/utilities.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/utilities.json b/plugins/utilities.json index aa8d4fb..588fecd 100644 --- a/plugins/utilities.json +++ b/plugins/utilities.json @@ -1266,7 +1266,7 @@ } ], "versions": { - "1.0.0": null, + "1.0.0": null } } } From 5484215e3c670eac7b501ce493de0605fa4d095f Mon Sep 17 00:00:00 2001 From: brostosjoined Date: Sat, 2 Mar 2024 09:25:56 +0000 Subject: [PATCH 04/16] [ci] apply-version-metadata --- plugins/utilities.json | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/plugins/utilities.json b/plugins/utilities.json index 588fecd..0ac2d8b 100644 --- a/plugins/utilities.json +++ b/plugins/utilities.json @@ -1266,8 +1266,13 @@ } ], "versions": { - "1.0.0": null + "1.0.0": { + "api_version": 8, + "commit_sha": "685c122", + "released_on": "02-03-2024", + "md5sum": "47ae46352706ad22355f9b11df3c4c37" + } } } } -} +} \ No newline at end of file From e445036a2047afe19088d525eac1846dbea0ad11 Mon Sep 17 00:00:00 2001 From: brostos <67740566+brostosjoined@users.noreply.github.com> Date: Sun, 3 Mar 2024 19:32:22 +0300 Subject: [PATCH 05/16] Update natpmp_upnp.py --- plugins/utilities/natpmp_upnp.py | 183 +++++++++++++++++-------------- 1 file changed, 103 insertions(+), 80 deletions(-) diff --git a/plugins/utilities/natpmp_upnp.py b/plugins/utilities/natpmp_upnp.py index 0700a49..22fe39a 100644 --- a/plugins/utilities/natpmp_upnp.py +++ b/plugins/utilities/natpmp_upnp.py @@ -2,7 +2,7 @@ import babase import bauiv1 as bui -import bascenev1 +import bascenev1 as bs import shutil import hashlib @@ -21,6 +21,7 @@ WAN_SERVICE_NAMES = ( "WANPPPConnection.1", # CenturyLink C1100Z "WANPPPConn1", # Huawei B528s-23a ) +BS_PORT = bs.get_game_port() def threaded(func): @@ -92,34 +93,37 @@ def get_modules() -> None: # Plucked from https://github.com/tenable/upnp_info/blob/d20a1fda8ca4877d61b89fe7126077a3a5f0b322/upnp_info.py#L23 def get_gateway_addr(): """Returns the gateway ip of the router if upnp service is available""" - locations = set() - location_regex = re.compile("location:[ ]*(.+)"+ chr(13) + chr(10), re.IGNORECASE) - ssdpDiscover = ( - "M-SEARCH * HTTP/1.1"+ chr(13) + chr(10) - + "HOST: 239.255.255.250:1900"+ chr(13) + chr(10) - + 'MAN: "ssdp:discover"'+ chr(13) + chr(10) - + "MX: 1"+ chr(13) + chr(10) - + "ST: ssdp:all"+ chr(13) + chr(10) - + chr(13) + chr(10) - ) - - sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - sock.sendto(ssdpDiscover.encode("ASCII"), ("239.255.255.250", 1900)) - sock.settimeout(3) try: - while True: - data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes - location_result = location_regex.search(data.decode("ASCII")) - if location_result and (location_result.group(1) in locations) == False: - locations.add(location_result.group(1)) - except socket.error: - sock.close() - if locations: - for location in locations: - parsed_url = urlparse(location) - if parsed_url.path.endswith("xml"): - gateway_ip_address = parsed_url.netloc.split(':')[0] - return gateway_ip_address + locations = set() + location_regex = re.compile("location:[ ]*(.+)"+ chr(13) + chr(10), re.IGNORECASE) + ssdpDiscover = ( + "M-SEARCH * HTTP/1.1"+ chr(13) + chr(10) + + "HOST: 239.255.255.250:1900"+ chr(13) + chr(10) + + 'MAN: "ssdp:discover"'+ chr(13) + chr(10) + + "MX: 1"+ chr(13) + chr(10) + + "ST: ssdp:all"+ chr(13) + chr(10) + + chr(13) + chr(10) + ) + + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + sock.sendto(ssdpDiscover.encode("ASCII"), ("239.255.255.250", 1900)) + sock.settimeout(3) + try: + while True: + data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes + location_result = location_regex.search(data.decode("ASCII")) + if location_result and (location_result.group(1) in locations) == False: + locations.add(location_result.group(1)) + except socket.error: + sock.close() + if locations: + for location in locations: + parsed_url = urlparse(location) + if parsed_url.path.endswith("xml"): + gateway_ip_address = parsed_url.netloc.split(':')[0] + return gateway_ip_address + except: + pass ''' @@ -130,7 +134,7 @@ def get_gateway_addr(): with open(f"{nat_pmp_file_path}/__init__.py", "w") as f: f.writelines(lines) - add_port_mapping() + add_port_mapping() @threaded @@ -163,9 +167,19 @@ def add_port_mapping(): ) except (NATPMPUnsupportedError, NATPMPNetworkError): import upnpy + from upnpy.exceptions import SOAPError + from urllib.error import HTTPError upnp = upnpy.UPnP() devices = upnp.discover() + + if devices == []: + babase.screenmessage( + "Please enable upnp service on your router", (1.00, 0.15, 0.15) + ) + # bui.getsound('shieldDown').play() -> RuntimeError : Sound creation failed + return + local_ip = ( ( @@ -183,67 +197,67 @@ def add_port_mapping(): ) + ["no IP found"] )[0] - for upnp_dev in devices: - for service in upnp_dev.services: - if service in WAN_SERVICE_NAMES: - service = upnp_dev[service] - service.AddPortMapping( - NewRemoteHost="", - NewExternalPort=43210, - NewProtocol="UDP", - NewInternalPort=43210, - NewInternalClient=str(local_ip), - NewEnabled="1", - NewPortMappingDescription="Bombsquad", - NewLeaseDuration=0, - ) - if confirm_port(): - babase.screenmessage( - "You are now joinable from the internet", (0.2, 1, 0.2) + try: + for upnp_dev in devices: + for service in upnp_dev.services: + if service in WAN_SERVICE_NAMES: + service = upnp_dev[service] + try: + result = service.GetSpecificPortMappingEntry( + NewRemoteHost="", NewExternalPort=BS_PORT, NewProtocol="UDP" + ) + if result and not confirm_port(): + if babase.do_once(): + babase.screenmessage( + "Oops seems like your network doesn't support upnp", + (1.0, 0.15, 0.15), + ) + babase.pushcall( + bui.getsound("error").play(), from_other_thread=True + ) + return + except SOAPError: + if not confirm_port(): + return + service.AddPortMapping( + NewRemoteHost="", + NewExternalPort=BS_PORT, + NewProtocol="UDP", + NewInternalPort=BS_PORT, + NewInternalClient=str(local_ip), + NewEnabled="1", + NewPortMappingDescription="Bombsquad", + NewLeaseDuration=0, ) - bui.getsound("shieldUp").play() + if confirm_port(): + babase.screenmessage( + "You are now joinable from the internet", (0.2, 1, 0.2) + ) + bui.getsound("shieldUp").play() + except (SOAPError, HTTPError): + babase.screenmessage('You will need to manualy add the port at the router :(') + @threaded -def check_port(): +def delete_port_mapping(): import upnpy from upnpy.exceptions import SOAPError upnp = upnpy.UPnP() devices = upnp.discover() - + if devices == []: - babase.screenmessage( - "Please enable upnp service on your router", (1.00, 0.15, 0.15) - ) - # bui.getsound('shieldDown').play() -> RuntimeError : Sound creation failed return - - for upnp_dev in devices: - for service in upnp_dev.services: - if service in WAN_SERVICE_NAMES: - service = upnp_dev[service] - if service.GetSpecificPortMappingEntry: - try: - result = service.GetSpecificPortMappingEntry( - NewRemoteHost="", NewExternalPort=43210, NewProtocol="UDP" - ) - if result and confirm_port(): - return True - elif result and not confirm_port(): - if babase.do_once(): - babase.screenmessage( - "Oops seems like your network doesn't support upnp", - (1.0, 0.15, 0.15), - ) - babase.pushcall( - bui.getsound("error").play(), from_other_thread=True - ) - return True - elif not result and not confirm_port(): - return False - except SOAPError: - pass + + try: + for upnp_dev in devices: + for service in upnp_dev.services: + if service in WAN_SERVICE_NAMES: + service = upnp_dev[service] + service.DeletePortMapping(NewRemoteHost="", NewExternalPort=BS_PORT, NewProtocol="UDP") + except: + pass # ba_meta export babase.Plugin @@ -252,5 +266,14 @@ class Joinable(babase.Plugin): get_modules() if confirm_port(): return - elif not check_port(): + else: add_port_mapping() + + def on_app_shutdown(self) -> None: + delete_port_mapping() + + def on_app_pause(self) -> None: + delete_port_mapping() + + def on_app_resume(self) -> None: + add_port_mapping() From 47c1d93e85d1e52055f85651279752f14793a8a5 Mon Sep 17 00:00:00 2001 From: brostosjoined Date: Sun, 3 Mar 2024 16:32:44 +0000 Subject: [PATCH 06/16] [ci] auto-format --- plugins/utilities/natpmp_upnp.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/plugins/utilities/natpmp_upnp.py b/plugins/utilities/natpmp_upnp.py index 22fe39a..d35397c 100644 --- a/plugins/utilities/natpmp_upnp.py +++ b/plugins/utilities/natpmp_upnp.py @@ -168,18 +168,17 @@ def add_port_mapping(): except (NATPMPUnsupportedError, NATPMPNetworkError): import upnpy from upnpy.exceptions import SOAPError - from urllib.error import HTTPError + from urllib.error import HTTPError upnp = upnpy.UPnP() devices = upnp.discover() - + if devices == []: babase.screenmessage( "Please enable upnp service on your router", (1.00, 0.15, 0.15) ) # bui.getsound('shieldDown').play() -> RuntimeError : Sound creation failed return - local_ip = ( ( @@ -236,7 +235,6 @@ def add_port_mapping(): bui.getsound("shieldUp").play() except (SOAPError, HTTPError): babase.screenmessage('You will need to manualy add the port at the router :(') - @threaded @@ -246,16 +244,17 @@ def delete_port_mapping(): upnp = upnpy.UPnP() devices = upnp.discover() - + if devices == []: return - + try: for upnp_dev in devices: for service in upnp_dev.services: if service in WAN_SERVICE_NAMES: service = upnp_dev[service] - service.DeletePortMapping(NewRemoteHost="", NewExternalPort=BS_PORT, NewProtocol="UDP") + service.DeletePortMapping( + NewRemoteHost="", NewExternalPort=BS_PORT, NewProtocol="UDP") except: pass @@ -268,7 +267,7 @@ class Joinable(babase.Plugin): return else: add_port_mapping() - + def on_app_shutdown(self) -> None: delete_port_mapping() From 08c2165d0d88226c1491aed5874a472730241e3a Mon Sep 17 00:00:00 2001 From: brostos <67740566+brostosjoined@users.noreply.github.com> Date: Sun, 3 Mar 2024 19:34:24 +0300 Subject: [PATCH 07/16] Update utilities.json --- plugins/utilities.json | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/plugins/utilities.json b/plugins/utilities.json index 0ac2d8b..588fecd 100644 --- a/plugins/utilities.json +++ b/plugins/utilities.json @@ -1266,13 +1266,8 @@ } ], "versions": { - "1.0.0": { - "api_version": 8, - "commit_sha": "685c122", - "released_on": "02-03-2024", - "md5sum": "47ae46352706ad22355f9b11df3c4c37" - } + "1.0.0": null } } } -} \ No newline at end of file +} From 6f5e4a7edcad331eb64f2e0f82869d7433077428 Mon Sep 17 00:00:00 2001 From: brostosjoined Date: Sun, 3 Mar 2024 16:34:47 +0000 Subject: [PATCH 08/16] [ci] apply-version-metadata --- plugins/utilities.json | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/plugins/utilities.json b/plugins/utilities.json index 588fecd..5bba90f 100644 --- a/plugins/utilities.json +++ b/plugins/utilities.json @@ -1266,8 +1266,13 @@ } ], "versions": { - "1.0.0": null + "1.0.0": { + "api_version": 8, + "commit_sha": "08c2165", + "released_on": "03-03-2024", + "md5sum": "4c38b48e5ba95e9f28491a5a447a46aa" + } } } } -} +} \ No newline at end of file From a8096be23221aa13f801d140fbe8203c7a99852f Mon Sep 17 00:00:00 2001 From: brostos <67740566+brostosjoined@users.noreply.github.com> Date: Sun, 3 Mar 2024 19:41:36 +0300 Subject: [PATCH 09/16] Update natpmp_upnp.py --- plugins/utilities/natpmp_upnp.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/plugins/utilities/natpmp_upnp.py b/plugins/utilities/natpmp_upnp.py index d35397c..4e6a506 100644 --- a/plugins/utilities/natpmp_upnp.py +++ b/plugins/utilities/natpmp_upnp.py @@ -216,8 +216,7 @@ def add_port_mapping(): ) return except SOAPError: - if not confirm_port(): - return + pass service.AddPortMapping( NewRemoteHost="", NewExternalPort=BS_PORT, @@ -234,7 +233,7 @@ def add_port_mapping(): ) bui.getsound("shieldUp").play() except (SOAPError, HTTPError): - babase.screenmessage('You will need to manualy add the port at the router :(') + babase.screenmessage('You will need to manualy add the port on the router :(') @threaded From 06d5bd41df917dd4cef809a434c46cbcf2a2f7bd Mon Sep 17 00:00:00 2001 From: brostos <67740566+brostosjoined@users.noreply.github.com> Date: Sun, 3 Mar 2024 19:42:12 +0300 Subject: [PATCH 10/16] Update utilities.json --- plugins/utilities.json | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/plugins/utilities.json b/plugins/utilities.json index 5bba90f..588fecd 100644 --- a/plugins/utilities.json +++ b/plugins/utilities.json @@ -1266,13 +1266,8 @@ } ], "versions": { - "1.0.0": { - "api_version": 8, - "commit_sha": "08c2165", - "released_on": "03-03-2024", - "md5sum": "4c38b48e5ba95e9f28491a5a447a46aa" - } + "1.0.0": null } } } -} \ No newline at end of file +} From e508c4537c1300d7b94473b8ab3c80d42bb8269d Mon Sep 17 00:00:00 2001 From: brostosjoined Date: Sun, 3 Mar 2024 16:42:33 +0000 Subject: [PATCH 11/16] [ci] apply-version-metadata --- plugins/utilities.json | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/plugins/utilities.json b/plugins/utilities.json index 588fecd..a04eb31 100644 --- a/plugins/utilities.json +++ b/plugins/utilities.json @@ -1266,8 +1266,13 @@ } ], "versions": { - "1.0.0": null + "1.0.0": { + "api_version": 8, + "commit_sha": "06d5bd4", + "released_on": "03-03-2024", + "md5sum": "29b4a034c2e9075225b0a698bb03dd1a" + } } } } -} +} \ No newline at end of file From f469c3d30261df19a9e482c0e641e663f46ae4a1 Mon Sep 17 00:00:00 2001 From: brostos <67740566+brostosjoined@users.noreply.github.com> Date: Sun, 3 Mar 2024 20:27:09 +0300 Subject: [PATCH 12/16] Update natpmp_upnp.py --- plugins/utilities/natpmp_upnp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/utilities/natpmp_upnp.py b/plugins/utilities/natpmp_upnp.py index 4e6a506..7ab7b92 100644 --- a/plugins/utilities/natpmp_upnp.py +++ b/plugins/utilities/natpmp_upnp.py @@ -232,7 +232,7 @@ def add_port_mapping(): "You are now joinable from the internet", (0.2, 1, 0.2) ) bui.getsound("shieldUp").play() - except (SOAPError, HTTPError): + except (SOAPError, HTTPError, UnicodeDecodeError): babase.screenmessage('You will need to manualy add the port on the router :(') From 2b70c7e9069a936b0fe473d204dd107e5db62821 Mon Sep 17 00:00:00 2001 From: brostos <67740566+brostosjoined@users.noreply.github.com> Date: Sun, 3 Mar 2024 20:55:34 +0300 Subject: [PATCH 13/16] Update natpmp_upnp.py --- plugins/utilities/natpmp_upnp.py | 65 ++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 25 deletions(-) diff --git a/plugins/utilities/natpmp_upnp.py b/plugins/utilities/natpmp_upnp.py index 7ab7b92..a9143ca 100644 --- a/plugins/utilities/natpmp_upnp.py +++ b/plugins/utilities/natpmp_upnp.py @@ -156,9 +156,9 @@ def add_port_mapping(): try: natpmp.map_port( natpmp.NATPMP_PROTOCOL_UDP, - 43210, - 43210, - 0, + BS_PORT, + BS_PORT, + 14400, gateway_ip=natpmp.get_gateway_addr(), ) if confirm_port(): @@ -168,17 +168,18 @@ def add_port_mapping(): except (NATPMPUnsupportedError, NATPMPNetworkError): import upnpy from upnpy.exceptions import SOAPError - from urllib.error import HTTPError + from urllib.error import HTTPError upnp = upnpy.UPnP() devices = upnp.discover() - + if devices == []: babase.screenmessage( "Please enable upnp service on your router", (1.00, 0.15, 0.15) ) # bui.getsound('shieldDown').play() -> RuntimeError : Sound creation failed return + local_ip = ( ( @@ -216,7 +217,8 @@ def add_port_mapping(): ) return except SOAPError: - pass + if confirm_port(): + return service.AddPortMapping( NewRemoteHost="", NewExternalPort=BS_PORT, @@ -225,7 +227,7 @@ def add_port_mapping(): NewInternalClient=str(local_ip), NewEnabled="1", NewPortMappingDescription="Bombsquad", - NewLeaseDuration=0, + NewLeaseDuration=14400, ) if confirm_port(): babase.screenmessage( @@ -233,29 +235,42 @@ def add_port_mapping(): ) bui.getsound("shieldUp").play() except (SOAPError, HTTPError, UnicodeDecodeError): - babase.screenmessage('You will need to manualy add the port on the router :(') + babase.screenmessage('You will need to manualy add the port at the router :(') + @threaded def delete_port_mapping(): - import upnpy - from upnpy.exceptions import SOAPError - - upnp = upnpy.UPnP() - devices = upnp.discover() - - if devices == []: - return + import socket + import natpmp + from natpmp import NATPMPUnsupportedError, NATPMPNetworkError try: - for upnp_dev in devices: - for service in upnp_dev.services: - if service in WAN_SERVICE_NAMES: - service = upnp_dev[service] - service.DeletePortMapping( - NewRemoteHost="", NewExternalPort=BS_PORT, NewProtocol="UDP") - except: - pass + natpmp.map_port( + natpmp.NATPMP_PROTOCOL_UDP, + BS_PORT, + BS_PORT, + 0, + gateway_ip=natpmp.get_gateway_addr(), + ) + except (NATPMPUnsupportedError, NATPMPNetworkError): + import upnpy + from upnpy.exceptions import SOAPError + + upnp = upnpy.UPnP() + devices = upnp.discover() + + if devices == []: + return + + try: + for upnp_dev in devices: + for service in upnp_dev.services: + if service in WAN_SERVICE_NAMES: + service = upnp_dev[service] + service.DeletePortMapping(NewRemoteHost="", NewExternalPort=BS_PORT, NewProtocol="UDP") + except: + pass # ba_meta export babase.Plugin @@ -266,7 +281,7 @@ class Joinable(babase.Plugin): return else: add_port_mapping() - + def on_app_shutdown(self) -> None: delete_port_mapping() From a23f69997c7b2e4bee0e8c463030784b0a60ad1d Mon Sep 17 00:00:00 2001 From: brostosjoined Date: Sun, 3 Mar 2024 17:55:55 +0000 Subject: [PATCH 14/16] [ci] auto-format --- plugins/utilities/natpmp_upnp.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/plugins/utilities/natpmp_upnp.py b/plugins/utilities/natpmp_upnp.py index a9143ca..f8aa684 100644 --- a/plugins/utilities/natpmp_upnp.py +++ b/plugins/utilities/natpmp_upnp.py @@ -168,18 +168,17 @@ def add_port_mapping(): except (NATPMPUnsupportedError, NATPMPNetworkError): import upnpy from upnpy.exceptions import SOAPError - from urllib.error import HTTPError + from urllib.error import HTTPError upnp = upnpy.UPnP() devices = upnp.discover() - + if devices == []: babase.screenmessage( "Please enable upnp service on your router", (1.00, 0.15, 0.15) ) # bui.getsound('shieldDown').play() -> RuntimeError : Sound creation failed return - local_ip = ( ( @@ -236,7 +235,6 @@ def add_port_mapping(): bui.getsound("shieldUp").play() except (SOAPError, HTTPError, UnicodeDecodeError): babase.screenmessage('You will need to manualy add the port at the router :(') - @threaded @@ -259,16 +257,17 @@ def delete_port_mapping(): upnp = upnpy.UPnP() devices = upnp.discover() - + if devices == []: return - + try: for upnp_dev in devices: for service in upnp_dev.services: if service in WAN_SERVICE_NAMES: service = upnp_dev[service] - service.DeletePortMapping(NewRemoteHost="", NewExternalPort=BS_PORT, NewProtocol="UDP") + service.DeletePortMapping( + NewRemoteHost="", NewExternalPort=BS_PORT, NewProtocol="UDP") except: pass @@ -281,7 +280,7 @@ class Joinable(babase.Plugin): return else: add_port_mapping() - + def on_app_shutdown(self) -> None: delete_port_mapping() From 48bd0dafb30635ede57ef70e8ba38486bef704c5 Mon Sep 17 00:00:00 2001 From: brostos <67740566+brostosjoined@users.noreply.github.com> Date: Sun, 3 Mar 2024 20:56:14 +0300 Subject: [PATCH 15/16] Update utilities.json --- plugins/utilities.json | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/plugins/utilities.json b/plugins/utilities.json index a04eb31..588fecd 100644 --- a/plugins/utilities.json +++ b/plugins/utilities.json @@ -1266,13 +1266,8 @@ } ], "versions": { - "1.0.0": { - "api_version": 8, - "commit_sha": "06d5bd4", - "released_on": "03-03-2024", - "md5sum": "29b4a034c2e9075225b0a698bb03dd1a" - } + "1.0.0": null } } } -} \ No newline at end of file +} From 674939eee06c784441e4f86702c89e47faf3a904 Mon Sep 17 00:00:00 2001 From: brostosjoined Date: Sun, 3 Mar 2024 17:56:35 +0000 Subject: [PATCH 16/16] [ci] apply-version-metadata --- plugins/utilities.json | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/plugins/utilities.json b/plugins/utilities.json index 588fecd..a275c5b 100644 --- a/plugins/utilities.json +++ b/plugins/utilities.json @@ -1266,8 +1266,13 @@ } ], "versions": { - "1.0.0": null + "1.0.0": { + "api_version": 8, + "commit_sha": "48bd0da", + "released_on": "03-03-2024", + "md5sum": "15f969c23d19118d4898570cfae71c7b" + } } } } -} +} \ No newline at end of file