From 2b495bca257c1c6b618b5aa0f9d9e17fe57e1180 Mon Sep 17 00:00:00 2001 From: Jason Cox Date: Mon, 8 Jul 2024 00:07:08 -0700 Subject: [PATCH] Add network configuration for force scan --- server/server.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/server/server.py b/server/server.py index 97f9b88..240bf1c 100644 --- a/server/server.py +++ b/server/server.py @@ -50,6 +50,7 @@ import urllib.parse from http.server import BaseHTTPRequestHandler, HTTPServer from socketserver import ThreadingMixIn +import ipaddress # Terminal color capability for all platforms try: @@ -82,12 +83,20 @@ DEBUGMODE = os.getenv("DEBUGMODE", "no").lower() == "yes" HOST = os.getenv("HOST", None) BROADCAST = os.getenv("BROADCAST", None) - -# Set up broadcast address -if HOST and not BROADCAST: - BROADCAST = HOST.split('.') - BROADCAST[3] = '255' - BROADCAST = '.'.join(BROADCAST) +NETWORK = None + +# If HOST specified, set up broadcast address and calculate network +if HOST: + if not BROADCAST: + BROADCAST = HOST.split('.') + BROADCAST[3] = '255' + BROADCAST = '.'.join(BROADCAST) + host_ip = ipaddress.IPv4Address(HOST) + broadcast_ip = ipaddress.IPv4Address(BROADCAST) + host_bits = int(host_ip) + broadcast_bits = int(broadcast_ip) + mask_length = 32 - (broadcast_bits - host_bits).bit_length() + NETWORK = str(ipaddress.IPv4Network(f"{host_ip}/{mask_length}", strict=False)) # Logging log = logging.getLogger(__name__) @@ -677,6 +686,8 @@ def api(port): print(" Using Host IP: %s%s%s" % (cyan, HOST, dim)) if BROADCAST: print(" Using Broadcast IP: %s%s%s" % (cyan, BROADCAST, dim)) + if NETWORK: + print(" Using Network: %s%s%s" % (cyan, NETWORK, dim)) print(" UDP Ports: %s%d%s, %s%d%s, %s%d%s" % (cyan, UDPPORT, dim, cyan, UDPPORTS, dim, cyan, UDPPORTAPP, dim)) print(" TCP Port: %s%d%s" % (cyan, TCPPORT, dim)) print(" API Port: %s%d%s" % (cyan, APIPORT, dim)) @@ -719,7 +730,10 @@ def api(port): # discover=True, wantips=None, wantids=None, snapshot=None, assume_yes=False, tuyadevices=[], # maxdevices=0) try: - found = scanner.devices(forcescan=True, verbose=False, discover=False, assume_yes=True, tuyadevices=tuyadevices) + if NETWORK: + found = scanner.devices(forcescan=[NETWORK], verbose=False, discover=False, assume_yes=True, tuyadevices=tuyadevices) + else: + found = scanner.devices(forcescan=True, verbose=False, discover=False, assume_yes=True, tuyadevices=tuyadevices) except Exception as err: log.error(f"Error during scanner.devices() {err}") found = []