diff --git a/README.md b/README.md index 238a603..2a23d37 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ All parameters can be passed via cmdline arguments or via environment variables. - ``-polling`` / ``POLLING`` Refresh interval in seconds. (optional, 300 seconds as default) - ``-tempchange`` / ``TEMPCHANGE`` Temperature change warning in hours. (optional, 12 hours as default) - ``-full`` / ``FULL`` Provide any information to broker, most times this is not necessary. (optional) +- ``-dns`` / ``DNS`` Use internal DNS cache. (optional, default: true) - ``-verbose`` / ``VERBOSE`` Provide more verbose logging. (optional) diff --git a/go.mod b/go.mod index e6c2c06..bf5d46f 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,7 @@ module github.com/misery/HeatingMqttBridge require github.com/eclipse/paho.mqtt.golang v1.4.2 +require github.com/ncruces/go-dns v1.2.0 require ( github.com/gorilla/websocket v1.5.0 // indirect diff --git a/go.sum b/go.sum index 33cb333..473e242 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,8 @@ github.com/eclipse/paho.mqtt.golang v1.4.2/go.mod h1:JGt0RsEwEX+Xa/agj90YJ9d9DH2 github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/ncruces/go-dns v1.2.0 h1:eSiRR6vRBaXtTbvS8gy4/RFk5ahO3W5VCipA5iBTylc= +github.com/ncruces/go-dns v1.2.0/go.mod h1:sZV4lxB+nXBYekemnR/EKewk8pZFTdFzF+LK2d7JPFg= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20220909164309-bea034e7d591 h1:D0B/7al0LLrVC8aWF4+oxpv/m8bc7ViFfVS8/gXGdqI= diff --git a/main.go b/main.go index decf48a..3643ea2 100644 --- a/main.go +++ b/main.go @@ -32,6 +32,7 @@ import ( "fmt" "io" "log" + "net" "net/http" "net/url" "os" @@ -42,6 +43,7 @@ import ( "time" MQTT "github.com/eclipse/paho.mqtt.golang" + DNS "github.com/ncruces/go-dns" ) var bridge *bridgeCfg @@ -636,6 +638,7 @@ func createBridge() *bridgeCfg { polling := flag.Int("polling", 300, "Refresh interval in seconds") tempchange := flag.Int("tempchange", 12, "Temperature change warning in hours") full := flag.Bool("full", false, "Provide full information to broker") + dnsCache := flag.Bool("dns", true, "Use internal DNS cache") verbose := flag.Bool("verbose", false, "Provide verbose log information") flag.Parse() @@ -648,6 +651,7 @@ func createBridge() *bridgeCfg { if *env { setBoolParam(clean, "clean") setBoolParam(full, "full") + setBoolParam(dnsCache, "dns") setBoolParam(verbose, "verbose") if !isFlagPassed("polling") { @@ -671,6 +675,11 @@ func createBridge() *bridgeCfg { *tempchange = 12 } + if *dnsCache { + log.Println("Use internal DNS cache") + net.DefaultResolver = DNS.NewCachingResolver(net.DefaultResolver) + } + return &bridgeCfg{ Client: MQTT.NewClient(createClientOptions(*broker, *user, *password, *clean, *topic)), KeepRunning: make(chan bool),