diff --git a/README.md b/README.md index ba74ef8..a5dc179 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,25 @@ To compile `hap-z2m`, use: go build -v -trimpath -ldflags="-s -w" ./cmd/... +Configuration +============== + +The MQTT broker that Zigbee2MQTT connects to is specified using the following: + +- Server +- Username +- Password + +Additionally, these options control networking aspects for the bridge: + +- `ListenAddr` can be used to bind to a specific port and/or address, + useful for selecting a fixed port for firewall rules. + +- `Interfaces` limit which interfaces mDNS broadcasts appear on, + and which addresses to use when broadcasting. + +These settings are optional and can be left blank. + License ======== diff --git a/bridge.go b/bridge.go index 1c70818..24bd257 100644 --- a/bridge.go +++ b/bridge.go @@ -51,6 +51,10 @@ type Bridge struct { Username string Password string + // address and interfaces to bind to + ListenAddr string + Interfaces []string + DebugMode bool ctx context.Context @@ -118,6 +122,9 @@ func (br *Bridge) StartHAP() error { return err } + br.server.Addr = br.ListenAddr + br.server.Ifaces = br.Interfaces + if br.DebugMode { haplog.Debug.Enable() } diff --git a/cmd/hapz2m/main.go b/cmd/hapz2m/main.go index c084122..a3f3b2b 100644 --- a/cmd/hapz2m/main.go +++ b/cmd/hapz2m/main.go @@ -8,6 +8,7 @@ import ( "flag" "fmt" "log" + "net" "net/http" "os" "os/signal" @@ -29,7 +30,11 @@ var ( debugMode = flag.Bool("debug", false, "enable debug messages") ) +// config struct type config struct { + ListenAddr string + Interfaces []string + Server, Username, Password string } @@ -73,6 +78,17 @@ func main() { br.Password = cfg.Password br.DebugMode = *debugMode + // validate ListenAddr if specified + if cfg.ListenAddr != "" { + _, _, err := net.SplitHostPort(cfg.ListenAddr) + if err != nil { + log.Fatalf("invalid ListenAddr: %v", err) + } + br.ListenAddr = cfg.ListenAddr + } + + br.Interfaces = cfg.Interfaces + err = br.ConnectMQTT() if err != nil { log.Printf("cannot connect to MQTT: %s", err) diff --git a/hapz2m.conf b/hapz2m.conf index f868ec0..ba8c4b6 100644 --- a/hapz2m.conf +++ b/hapz2m.conf @@ -1,4 +1,12 @@ { + // specify a host:port to listen on + // if not specified, a random port will be used + //"ListenAddr": ":8585", + + // limit interfaces to use for DNS-SD (mDNS) + // leave blank for all interfaces + //"Interfaces": ["eth0", "eth1"], + // MQTT server credentials "Server": "tcp://localhost:1883", "Username": "hapz2m",