From 6aec6da078dc978f7485293d4380e7eec261e0ee Mon Sep 17 00:00:00 2001 From: William Petit Date: Thu, 2 May 2024 09:40:55 +0200 Subject: [PATCH] feat: use github.com/wlynxg/anet instead of net See https://github.com/golang/go/issues/40569 --- cmd/cli/command/app/run.go | 5 +++-- go.mod | 2 +- pkg/module/cast/chromecast/discovery.go | 26 ++++--------------------- 3 files changed, 8 insertions(+), 25 deletions(-) diff --git a/cmd/cli/command/app/run.go b/cmd/cli/command/app/run.go index 6f9fa14..699a07d 100644 --- a/cmd/cli/command/app/run.go +++ b/cmd/cli/command/app/run.go @@ -39,6 +39,7 @@ import ( "github.com/lestrrat-go/jwx/v2/jwk" "github.com/pkg/errors" "github.com/urfave/cli/v2" + "github.com/wlynxg/anet" _ "embed" @@ -360,13 +361,13 @@ func findMatchingDeviceAddress(ctx context.Context, from string, defaultAddr str return defaultAddr, nil } - ifaces, err := net.Interfaces() + ifaces, err := anet.Interfaces() if err != nil { return "", errors.WithStack(err) } for _, ifa := range ifaces { - addrs, err := ifa.Addrs() + addrs, err := anet.InterfaceAddrsByInterface(&ifa) if err != nil { logger.Error( ctx, "could not retrieve iface adresses", diff --git a/go.mod b/go.mod index 847b4d5..5c25b56 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/lestrrat-go/jwx/v2 v2.0.8 github.com/mitchellh/hashstructure/v2 v2.0.2 github.com/ulikunitz/xz v0.5.11 + github.com/wlynxg/anet v0.0.1 go.uber.org/goleak v1.3.0 modernc.org/sqlite v1.20.4 ) @@ -43,7 +44,6 @@ require ( github.com/muesli/reflow v0.3.0 // indirect github.com/muesli/termenv v0.15.2 // indirect github.com/rivo/uniseg v0.4.4 // indirect - github.com/wlynxg/anet v0.0.1 // indirect go.opentelemetry.io/otel v1.21.0 // indirect go.opentelemetry.io/otel/trace v1.21.0 // indirect golang.org/x/sync v0.5.0 // indirect diff --git a/pkg/module/cast/chromecast/discovery.go b/pkg/module/cast/chromecast/discovery.go index c534bb9..09f705e 100644 --- a/pkg/module/cast/chromecast/discovery.go +++ b/pkg/module/cast/chromecast/discovery.go @@ -3,8 +3,6 @@ package chromecast import ( "context" "net" - "regexp" - "strconv" "strings" "sync" "time" @@ -15,6 +13,7 @@ import ( "github.com/barnybug/go-cast/log" "github.com/hashicorp/mdns" "github.com/pkg/errors" + "github.com/wlynxg/anet" ) const ( @@ -151,28 +150,11 @@ func (d *Discovery) listener(ctx context.Context) { case d.found <- client: case <-time.After(time.Second): case <-ctx.Done(): - break + return } } } -func decodeDnsEntry(text string) string { - text = strings.Replace(text, `\.`, ".", -1) - text = strings.Replace(text, `\ `, " ", -1) - - re := regexp.MustCompile(`([\\][0-9][0-9][0-9])`) - text = re.ReplaceAllStringFunc(text, func(source string) string { - i, err := strconv.Atoi(source[1:]) - if err != nil { - return "" - } - - return string([]byte{byte(i)}) - }) - - return text -} - func decodeTxtRecord(txt string) map[string]string { m := make(map[string]string) @@ -196,7 +178,7 @@ func isIPv6(ip net.IP) bool { } func findMulticastInterfaces(ctx context.Context) ([]net.Interface, error) { - ifaces, err := net.Interfaces() + ifaces, err := anet.Interfaces() if err != nil { return nil, nil } @@ -223,7 +205,7 @@ func findMulticastInterfaces(ctx context.Context) ([]net.Interface, error) { } func retrieveSupportedProtocols(iface net.Interface) (bool, bool, error) { - adresses, err := iface.Addrs() + adresses, err := anet.InterfaceAddrsByInterface(&iface) if err != nil { return false, false, errors.WithStack(err) }