Add config options for ListenAddr & Interfaces

These options are helpful when the bridge is running on a multi-homed
device like a wireless router.
This commit is contained in:
Darell Tan
2023-08-03 23:28:52 +08:00
parent 3cfc3f68b0
commit 59b049faea
4 changed files with 50 additions and 0 deletions

View File

@@ -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
========

View File

@@ -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()
}

View File

@@ -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)

View File

@@ -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",