-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdiscover.go
More file actions
37 lines (31 loc) · 743 Bytes
/
discover.go
File metadata and controls
37 lines (31 loc) · 743 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package envoy
import (
"context"
"time"
"github.com/brutella/dnssd"
)
func Discover() (string, error) {
discovered := "envoy"
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
found := func(e dnssd.BrowseEntry) {
// look through the list of IPs, pick something IPv4
for _, ipa := range e.IPs {
if ipa.To4() != nil {
discovered = ipa.String()
cancel()
return
}
}
}
if err := dnssd.LookupType(ctx, "_enphase-envoy._tcp.local.", found, reject); err != nil {
if err.Error() != "context canceled" {
elogger.Printf("discovery: %v\n", err)
return "", err
}
}
return discovered, nil
}
func reject(e dnssd.BrowseEntry) {
elogger.Printf("dnssd-lookup: %+v", e)
}