Procházet zdrojové kódy

cmd/derper: randomize IPs on refreshBootstrapDNS (#7440)

This is to address a possible DNS failure on startup. Before this
change IPv6 addresses would be listed first, and the client dialer would
fail for hosts without IPv6 connectivity.
shayne před 3 roky
rodič
revize
d92ef4c215
1 změnil soubory, kde provedl 8 přidání a 0 odebrání
  1. 8 0
      cmd/derper/bootstrap_dns.go

+ 8 - 0
cmd/derper/bootstrap_dns.go

@@ -8,6 +8,7 @@ import (
 	"encoding/json"
 	"expvar"
 	"log"
+	"math/rand"
 	"net"
 	"net/http"
 	"strings"
@@ -52,6 +53,13 @@ func refreshBootstrapDNS() {
 	ctx, cancel := context.WithTimeout(context.Background(), refreshTimeout)
 	defer cancel()
 	dnsEntries := resolveList(ctx, strings.Split(*bootstrapDNS, ","))
+	// Randomize the order of the IPs for each name to avoid the client biasing
+	// to IPv6
+	for k := range dnsEntries {
+		ips := dnsEntries[k]
+		rand.Shuffle(len(ips), func(i, j int) { ips[i], ips[j] = ips[j], ips[i] })
+		dnsEntries[k] = ips
+	}
 	j, err := json.MarshalIndent(dnsEntries, "", "\t")
 	if err != nil {
 		// leave the old values in place