brutella / hc

hc is a lightweight framework to develop HomeKit accessories in Go.
Apache License 2.0
1.74k stars 189 forks source link

specify advertised IP for dnssd #199

Open dbarrosop opened 3 years ago

dbarrosop commented 3 years ago

Hello, in some cases it is necessary to advertise a different IP to the one belonging to the host where the service is running. For instance if you are running the service in a docker container without using host networking or if you need the service outside of the connected network but routing isn't an option and you need to rely on NAT instead.

Currently, dnssd supports specifying an arbitrary IP and hc actually sets the necessary parameter if you set the configuration option Config.IP. This works great, however, Config.IP's documentation states it is a deprecated option. As I think running an hc service inside docker is probably a common use case I'd suggest removing the deprecation notice in the Config.IP option. Alternatively, allowing a custom dnssd configuration but I don't think there are any other parameters that might be worth overriding so the first option seems more convenient. A third option would be to allow specifying a dns entry and resolve it instead of passing an IP address, this would keep things dynamic while allowing you to override the advertised IP.

Thanks for the awesome work :)

Regards David

brutella commented 2 years ago

FYI I've refactored hc and made a new library out of it. The new library is available as hap.

In hap you can specify the the ip and port of the hap server.

s, err := hap.NewServer(...)
s.Addr = "192.168.0.10:12345"
...

https://pkg.go.dev/github.com/brutella/hap#Server

Does this resolve your issue?

dbarrosop commented 2 years ago

I don't think it's the same, based on the docstring:

        // Addr specifies the tcp address for the server
    // to listen to in form of "host:port".
    // If empty, a random port is used.

What I was suggesting (which was already possible) was decoupling the ip:port you are binding to and the ip:port you are advertising. Simplest use case is running inside a docker container (without using networking in host mode), in which case, you'd probably be binding to 0.0.0.0:12345 but you'd want to announce the IP of the host machine.