From 9378db1979c2b81ab0d6ffc2ad64ed65eab4be1e Mon Sep 17 00:00:00 2001 From: decentral1se Date: Wed, 4 Aug 2021 22:51:07 +0200 Subject: [PATCH] fix: look up ipv4 from host correctly We use a custom resolver now instead of relying on the baked-in Golang resolver which has issues. We use a friendly librehoster DNS resolver and not Google because fuck that. --- cli/server/init.go | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/cli/server/init.go b/cli/server/init.go index f423844b7..d9a44d676 100644 --- a/cli/server/init.go +++ b/cli/server/init.go @@ -5,11 +5,13 @@ import ( "errors" "fmt" "net" + "time" "coopcloud.tech/abra/cli/internal" "coopcloud.tech/abra/client" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/swarm" + "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" ) @@ -36,20 +38,31 @@ later for more advanced use cases. return err } - var ipv4 net.IP - ips, _ := net.LookupIP(host) - for _, ip := range ips { - ipv4 = ip.To4() - } - - if string(ipv4) == "" { - return fmt.Errorf("unable to retrieve ipv4 address for %s", host) + resolver := &net.Resolver{ + PreferGo: false, + Dial: func(ctx context.Context, network, address string) (net.Conn, error) { + d := net.Dialer{ + Timeout: time.Millisecond * time.Duration(10000), + } + // comrade librehosters DNS resolver https://snopyta.org/service/dns/ + return d.DialContext(ctx, "udp", "95.216.24.230:53") + }, } ctx := context.Background() + ips, err := resolver.LookupIPAddr(ctx, host) + if err != nil { + logrus.Fatal(err) + } + + if len(ips) == 0 { + return fmt.Errorf("unable to retrieve ipv4 address for %s", host) + } + ipv4 := ips[0].IP.To4().String() + initReq := swarm.InitRequest{ ListenAddr: "0.0.0.0:2377", - AdvertiseAddr: string(ipv4), + AdvertiseAddr: ipv4, } if _, err := cl.SwarmInit(ctx, initReq); err != nil { return err