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"
"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