From b91b3fc82000fbdc47fabb91c3007dac69d31cd0 Mon Sep 17 00:00:00 2001 From: Alexander Falkenstern Date: Fri, 9 Feb 2024 15:58:56 +0100 Subject: [PATCH] Fix gateway address estimation --- BridgeEmulator/configManager/configInit.py | 43 ++++++++++++++++----- BridgeEmulator/flaskUI/v2restapi.py | 45 +++++++--------------- requirements.txt | 2 +- 3 files changed, 48 insertions(+), 42 deletions(-) diff --git a/BridgeEmulator/configManager/configInit.py b/BridgeEmulator/configManager/configInit.py index 8555d7c94..6a56f2526 100644 --- a/BridgeEmulator/configManager/configInit.py +++ b/BridgeEmulator/configManager/configInit.py @@ -1,21 +1,46 @@ +import logManager import uuid +import netifaces from random import randrange - +logging = logManager.logger.get_logger(__name__) def _generate_unique_id(): rand_bytes = [randrange(0, 256) for _ in range(3)] return "00:17:88:01:00:%02x:%02x:%02x-0b" % (rand_bytes[0],rand_bytes[1],rand_bytes[2]) - def write_args(args, yaml_config): - host_ip = args["HOST_IP"] - ip_pieces = host_ip.split(".") - yaml_config["config"]["ipaddress"] = host_ip - yaml_config["config"]["gateway"] = ip_pieces[0] + "." + ip_pieces[1] + "." + ip_pieces[2] + ".1" - yaml_config["config"]["mac"] = args["FULLMAC"] - yaml_config["config"]["bridgeid"] = (args["MAC"][:6] + 'FFFE' + args["MAC"][-6:]).upper() - return yaml_config + host_ip = args['HOST_IP'] + + devices = [] + for interface in netifaces.interfaces(): + for family, addresses in netifaces.ifaddresses(interface).items(): + if family not in (netifaces.AF_INET, netifaces.AF_INET6): + continue + for address in addresses: + if address['addr'] == host_ip: + devices.append((family, interface)) + logging.debug('Found network devices ' + str(devices)) + gateway_ips = [] + for family, gateways in netifaces.gateways().items(): + for device in devices: + if family != device[0]: + continue + for gateway in gateways: + if gateway[1] == device[1]: + gateway_ips.append(gateway[0]) + logging.debug('Found gateways ' + str(gateway_ips)) + + if not gateway_ips: + ip_pieces = host_ip.split('.') + gateway_ips.append(ip_pieces[0] + '.' + ip_pieces[1] + '.' + ip_pieces[2] + '.1' + logging.info('Found no gateways and use fallback ' + str(gateway_ips)) + + yaml_config['config']['ipaddress'] = host_ip + yaml_config['config']['gateway'] = gateway_ips[0] + yaml_config['config']['mac'] = args['FULLMAC'] + yaml_config['config']['bridgeid'] = (args['MAC'][:6] + 'FFFE' + args['MAC'][-6:]).upper() + return yaml_config def generate_security_key(yaml_config): # generate security key for Hue Essentials remote access diff --git a/BridgeEmulator/flaskUI/v2restapi.py b/BridgeEmulator/flaskUI/v2restapi.py index 22e4d088a..eaa510cda 100644 --- a/BridgeEmulator/flaskUI/v2restapi.py +++ b/BridgeEmulator/flaskUI/v2restapi.py @@ -142,24 +142,17 @@ def v2BridgeHome(): def v2Bridge(): + bridge_id = bridgeConfig["config"]["bridgeid"] return { - "bridge_id": bridgeConfig["config"]["bridgeid"].lower(), - "id": str(uuid.uuid5(uuid.NAMESPACE_URL, bridgeConfig["config"]["bridgeid"] + 'bridge')), + "bridge_id": bridge_id.lower(), + "id": str(uuid.uuid5(uuid.NAMESPACE_URL, bridge_id + 'bridge')), "id_v1": "", "identify": {}, - "owner": { - "rid": str(uuid.uuid5( - uuid.NAMESPACE_URL, bridgeConfig["config"]["bridgeid"] + 'device')), - "rtype": "device" - }, - "time_zone": { - "time_zone": bridgeConfig["config"]["timezone"] - }, - + "owner": {"rid": str(uuid.uuid5(uuid.NAMESPACE_URL, bridge_id + 'device')), "rtype": "device"}, + "time_zone": {"time_zone": bridgeConfig["config"]["timezone"]}, "type": "bridge" } - def geoLocation(): return { "id": str(uuid.uuid5(uuid.NAMESPACE_URL, bridgeConfig["config"]["bridgeid"] + 'geolocation')), @@ -169,38 +162,26 @@ def geoLocation(): def v2BridgeDevice(): - result = {"id": str(uuid.uuid5( - uuid.NAMESPACE_URL, bridgeConfig["config"]["bridgeid"] + 'device')), "type": "device"} + config = bridgeConfig["config"] + bridge_id = config["bridgeid"] + result = {"id": str(uuid.uuid5(uuid.NAMESPACE_URL, bridge_id + 'device')), "type": "device"} result["id_v1"] = "" - result["metadata"] = { - "archetype": "bridge_v2", - "name": bridgeConfig["config"]["name"] - } + result["metadata"] = {"archetype": "bridge_v2", "name": config["name"]} result["product_data"] = { "certified": True, "manufacturer_name": "Signify Netherlands B.V.", "model_id": "BSB002", "product_archetype": "bridge_v2", "product_name": "Philips hue", - "software_version": bridgeConfig["config"]["apiversion"][:5] + bridgeConfig["config"]["swversion"] + "software_version": config["apiversion"][:5] + config["swversion"] } result["services"] = [ - { - "rid": str(uuid.uuid5(uuid.NAMESPACE_URL, bridgeConfig["config"]["bridgeid"] + 'bridge')), - "rtype": "bridge" - }, - { - "rid": str(uuid.uuid5(uuid.NAMESPACE_URL, bridgeConfig["config"]["bridgeid"] + 'zigbee_connectivity')), - "rtype": "zigbee_connectivity" - }, - { - "rid": str(uuid.uuid5(uuid.NAMESPACE_URL, bridgeConfig["config"]["bridgeid"] + 'entertainment')), - "rtype": "entertainment" - } + {"rid": str(uuid.uuid5(uuid.NAMESPACE_URL, bridge_id + 'bridge')), "rtype": "bridge"}, + {"rid": str(uuid.uuid5(uuid.NAMESPACE_URL, bridge_id + 'zigbee_connectivity')), "rtype": "zigbee_connectivity"}, + {"rid": str(uuid.uuid5(uuid.NAMESPACE_URL, bridge_id + 'entertainment')), "rtype": "entertainment"} ] return result - class AuthV1(Resource): def get(self): authorisation = authorizeV2(request.headers) diff --git a/requirements.txt b/requirements.txt index e55f333c3..42d80526e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -17,4 +17,4 @@ yeelight python-kasa==0.4.1 bleak==0.14.3 rgbxy==0.5 - +netifaces==0.11.0