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.
This commit is contained in:
decentral1se 2021-08-04 22:51:07 +02:00
parent efb9d6f6a5
commit 9378db1979
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
1 changed files with 22 additions and 9 deletions

View File

@ -5,11 +5,13 @@ import (
"errors" "errors"
"fmt" "fmt"
"net" "net"
"time"
"coopcloud.tech/abra/cli/internal" "coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/client" "coopcloud.tech/abra/client"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/swarm" "github.com/docker/docker/api/types/swarm"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
@ -36,20 +38,31 @@ later for more advanced use cases.
return err return err
} }
var ipv4 net.IP resolver := &net.Resolver{
ips, _ := net.LookupIP(host) PreferGo: false,
for _, ip := range ips { Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
ipv4 = ip.To4() d := net.Dialer{
} Timeout: time.Millisecond * time.Duration(10000),
}
if string(ipv4) == "" { // comrade librehosters DNS resolver https://snopyta.org/service/dns/
return fmt.Errorf("unable to retrieve ipv4 address for %s", host) return d.DialContext(ctx, "udp", "95.216.24.230:53")
},
} }
ctx := context.Background() 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{ initReq := swarm.InitRequest{
ListenAddr: "0.0.0.0:2377", ListenAddr: "0.0.0.0:2377",
AdvertiseAddr: string(ipv4), AdvertiseAddr: ipv4,
} }
if _, err := cl.SwarmInit(ctx, initReq); err != nil { if _, err := cl.SwarmInit(ctx, initReq); err != nil {
return err return err