Skip to content

Commit

Permalink
fix: check only for IPv4 in DNS lookup, fixes ddev#6592 (ddev#6593)
Browse files Browse the repository at this point in the history
  • Loading branch information
stasadev authored Oct 10, 2024
1 parent 8d829bd commit 3a6e225
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
5 changes: 3 additions & 2 deletions pkg/ddevapp/hostname_mgt.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ddevapp

import (
"context"
"fmt"
"net"
"os"
Expand Down Expand Up @@ -94,11 +95,11 @@ func (app *DdevApp) AddHostsEntriesIfNeeded() error {
if app.UseDNSWhenPossible && globalconfig.IsInternetActive() {
// If they have provided "*.<name>" then look up the suffix
checkName := strings.TrimPrefix(name, "*.")
hostIPs, err := net.LookupHost(checkName)
hostIPs, err := net.DefaultResolver.LookupIP(context.Background(), "ip4", checkName)

// If we had successful lookup and the IP address looked up is local
// then we don't have to add it to the /etc/hosts.
if err == nil && len(hostIPs) > 0 && netutil.IsLocalIP(hostIPs[0]) {
if err == nil && len(hostIPs) > 0 && netutil.HasLocalIP(hostIPs) {
continue
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/dockerutil/dockerutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -919,9 +919,9 @@ func GetDockerIP() (string, error) {
addr := net.ParseIP(hostPart)
if addr == nil {
// If it wasn't an IP address, look it up to get IP address
ip, err := net.LookupHost(hostPart)
ip, err := net.DefaultResolver.LookupIP(context.Background(), "ip4", hostPart)
if err == nil && len(ip) > 0 {
hostPart = ip[0]
hostPart = ip[0].String()
} else {
return "", fmt.Errorf("failed to look up IP address for $DOCKER_HOST=%s, hostname=%s: %v", dockerHostRawURL, hostPart, err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/globalconfig/global_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ var IsInternetActiveResult = false
// In order to override net.DefaultResolver with a stub, we have to define an
// interface on our own since there is none from the standard library.
var IsInternetActiveNetResolver interface {
LookupHost(ctx context.Context, host string) (addrs []string, err error)
LookupIP(ctx context.Context, network, host string) ([]net.IP, error)
} = net.DefaultResolver

// IsInternetActive checks to see if we have a viable
Expand All @@ -837,7 +837,7 @@ func IsInternetActive() bool {
// Using a random URL is more conclusive, but it's more intrusive because
// DNS may take some time, and it's really annoying.
testURL := "test.ddev.site"
addrs, err := IsInternetActiveNetResolver.LookupHost(ctx, testURL)
addrs, err := IsInternetActiveNetResolver.LookupIP(ctx, "ip4", testURL)

// Internet is active (active == true) if both err and ctx.Err() were nil
active := err == nil && ctx.Err() == nil
Expand Down
10 changes: 5 additions & 5 deletions pkg/globalconfig/global_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ type internetActiveNetResolverStub struct {
err error
}

// LookupHost is a custom version of net.LookupHost that wastes some time and then returns
func (t internetActiveNetResolverStub) LookupHost(ctx context.Context, _ string) ([]string, error) {
// LookupIP is a custom version of net.LookupIP that wastes some time and then returns
func (t internetActiveNetResolverStub) LookupIP(ctx context.Context, _, _ string) ([]net.IP, error) {
select {
case <-time.After(t.sleepTime):
case <-ctx.Done():
Expand All @@ -150,7 +150,7 @@ func internetActiveResetVariables() {
globalconfig.DdevGlobalConfig.InternetDetectionTimeout = nodeps.InternetDetectionTimeoutDefault
}

// TestIsInternetActiveErrorOccurred tests if IsInternetActive() returns false when LookupHost returns an error
// TestIsInternetActiveErrorOccurred tests if IsInternetActive() returns false when LookupIP returns an error
func TestIsInternetActiveErrorOccurred(t *testing.T) {
internetActiveResetVariables()

Expand Down Expand Up @@ -184,8 +184,8 @@ func TestIsInternetActiveAlreadyChecked(t *testing.T) {
asrt.True(t, globalconfig.IsInternetActive())
}

// TestIsInternetActive tests if IsInternetActive() returns true, when the LookupHost call goes well
// and if it properly sets the globals so it won't execute the LookupHost again.
// TestIsInternetActive tests if IsInternetActive() returns true, when the LookupIP call goes well
// and if it properly sets the globals so it won't execute the LookupIP again.
func TestIsInternetActive(t *testing.T) {
internetActiveResetVariables()

Expand Down
23 changes: 15 additions & 8 deletions pkg/netutil/netutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,20 @@ func IsPortActive(port string) bool {
return false
}

// IsLocalIP returns true if the provided IP address is
// HasLocalIP returns true if at least one of the provided IP addresses is
// assigned to the local computer
func IsLocalIP(ipAddress string) bool {
func HasLocalIP(ipAddresses []net.IP) bool {
dockerIP, err := dockerutil.GetDockerIP()

if err != nil {
util.Warning("Failed to get Docker IP address: %v", err)
return false
}

if ipAddress == dockerIP {
return true
for _, ipAddress := range ipAddresses {
if ipAddress.String() == dockerIP {
return true
}
}

localIPs, err := GetLocalIPs()
Expand All @@ -66,10 +68,15 @@ func IsLocalIP(ipAddress string) bool {
}

// Check if the parsed IP address is local using slices.Contains
return slices.Contains(localIPs, ipAddress)
for _, ipAddress := range ipAddresses {
if slices.Contains(localIPs, ipAddress.String()) {
return true
}
}
return false
}

// GetLocalIPs() returns IP addresses associated with the machine
// GetLocalIPs returns IP addresses associated with the machine
func GetLocalIPs() ([]string, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
Expand All @@ -80,12 +87,12 @@ func GetLocalIPs() ([]string, error) {
for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPNet:
if v.IP.IsLoopback() {
if v.IP.IsLoopback() || v.IP.To4() == nil {
continue
}
localIPs = append(localIPs, v.IP.String())
case *net.IPAddr:
if v.IP.IsLoopback() {
if v.IP.IsLoopback() || v.IP.To4() == nil {
continue
}
localIPs = append(localIPs, v.IP.String())
Expand Down

0 comments on commit 3a6e225

Please sign in to comment.