Skip to content

Commit 3710153

Browse files
edwardrfedw-defang
andauthored
Handle multiple CNAME resolve and debug log http redirect (#675)
* Handle multiple CNAME resolve and debug log http redirect * Add root resolver test --------- Co-authored-by: Edward J <[email protected]>
1 parent 02f465d commit 3710153

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

src/pkg/cli/cert.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ var (
4949
TLSHandshakeTimeout: 10 * time.Second,
5050
ExpectContinueTimeout: 1 * time.Second,
5151
},
52+
CheckRedirect: func(req *http.Request, via []*http.Request) error {
53+
term.Debugf("Redirecting from %v to %v", via[len(via)-1].URL, req.URL)
54+
return nil
55+
},
5256
}
5357
)
5458

src/pkg/dns/resolver.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dns
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"math/rand"
78
"net"
@@ -37,15 +38,19 @@ var rootServers = []*net.NS{
3738
}
3839

3940
func (r RootResolver) LookupIPAddr(ctx context.Context, domain string) ([]net.IPAddr, error) {
40-
ips, err := r.getResolver(ctx, domain).LookupIPAddr(ctx, domain)
41-
if err != nil {
42-
if err, ok := err.(ErrCNAMEFound); ok {
43-
return r.getResolver(ctx, err.CNAME()).LookupIPAddr(ctx, err.CNAME())
44-
} else {
45-
return nil, err
41+
for i := 0; i < 10; i++ {
42+
ips, err := r.getResolver(ctx, domain).LookupIPAddr(ctx, domain)
43+
if err != nil {
44+
if cnameErr, ok := err.(ErrCNAMEFound); ok {
45+
domain = cnameErr.CNAME()
46+
continue
47+
} else {
48+
return nil, err
49+
}
4650
}
51+
return ips, nil
4752
}
48-
return ips, nil
53+
return nil, errors.New("too many CNAME lookups")
4954
}
5055

5156
func (r RootResolver) LookupCNAME(ctx context.Context, domain string) (string, error) {

src/pkg/dns/resolver_integration_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,15 @@ func TestRootResolver(t *testing.T) {
4343
t.Errorf("Expected same IP addresses, got %v <> %v", ips, nIPs)
4444
}
4545
})
46+
47+
t.Run("LookupIPAddr follows multiple CNAME redirects", func(t *testing.T) {
48+
r := RootResolver{}
49+
ips, err := r.LookupIPAddr(context.Background(), "cname-a.gnafed.click")
50+
if err != nil {
51+
t.Errorf("Expected no error, got %v", err)
52+
}
53+
if !SameIPs(IpAddrsToIPs(ips), []net.IP{net.ParseIP("1.2.3.4")}) {
54+
t.Errorf("Expected IP address 1.2.3.4 got %v", ips)
55+
}
56+
})
4657
}

0 commit comments

Comments
 (0)