bwp91 / homebridge-ewelink

Homebridge plugin to integrate eWeLink devices into HomeKit.
MIT License
384 stars 126 forks source link

IP update behavior can incorrectly assign a forwarder IP instead of the actual device IP #517

Open trevorirwin opened 8 months ago

trevorirwin commented 8 months ago

What issue do you have? Please be as thorough and explicit as possible.

Line 94 of lan.js tests if the packet address matches the device info. If it doesn't, line 95 assigns the packet IP to the device info. However, in complex networks (especially those with multiple subnets) this packet address may not actually be the device address! In my network, packet.address is the router address. This means that when the device info IP is updated, it is replaced with my router IP address, which causes the plugin to then send update requests to my router instead of my ewelink devices. In the logs, I am presented with a device unreachable error with my router's IP address.

Tested workaround:

As a workaround, I simply commented out line 95. This stopped the IP from being reassigned to my router IP, and restores correct LAN control behavior on my network.

Proposed fix:

Note that I am using DHCP reservations for all of my ewelink devices, and I am using static IP configuration within the plugin. The above workaround could negatively impact people with simple DHCP setups and no static plugin configuration. Instead, I would propose either of two more appropriate fixes:

  1. Parse the packet for the true origin IP instead of using the packet IP (assuming dns-sd packets include this information.)
  2. Check if static IP plugin configuration is being used for that device, and if so, do not attempt to update the IP at all.

I would submit a PR for one of these fixes, but I'm not yet familiar enough with the plugin structure and dns-sd to implement them properly myself.

Details of your setup.

bwp91 commented 8 months ago

Hi @trevorirwin thanks for the issue here

I made some changes here: https://github.com/bwp91/homebridge-ewelink/commit/70f8c1eb0c04f471b60074e5e221e8f5e9317ca2

and posted a new beta version of the plugin, could you try this?

trevorirwin commented 8 months ago

Hi @bwp91,

I've installed and tested the beta. It is working, with no errors in the logs. However, there is unexpected output in the logs just after initialization:

[10/25/2023, 5:30:50 PM] [eWeLink] [Living Room Fan] changed IP address to [MY_ROUTER_IP].

and periodically:

[10/25/2023, 5:33:06 PM] [eWeLink] [Living Room Fan] receiving update {"light":"on","fan":"off","speed":1,"ip":"MY_ROUTER_IP","online":true,"updateSource":"LAN"}.

(I've replaced the actual router IP in both messages with MY_ROUTER_IP)