resolver: replace nopResolver with lookupStaticHost · saucelabs/forwarder@0b35f78

2 min read Original article ↗

3 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -433,26 +433,8 @@ func (hp *HTTPProxy) directLocalhost(fn ProxyFunc) ProxyFunc {

433433

}

434434

}

435435
436-

// nopResolver is a dns resolver that does not ever dial.

437-

// It uses only the local resolver.

438-

// It will look for the address in the /etc/hosts file.

439-

var localhostResolver = nopResolver() //nolint:gochecknoglobals // This is a local resolver.

440-
441436

func (hp *HTTPProxy) isLocalhost(req *http.Request) bool {

442-

h := req.URL.Hostname()

443-
444-

// Plain old localhost.

445-

if h == "localhost" {

446-

return true

447-

}

448-
449-

if addrs, err := localhostResolver.LookupHost(context.Background(), h); err == nil {

450-

if ip := net.ParseIP(addrs[0]); ip != nil {

451-

return ip.IsLoopback()

452-

}

453-

}

454-
455-

return false

437+

return isLocalhost(req.URL.Hostname())

456438

}

457439
458440

func (hp *HTTPProxy) setBasicAuth(req *http.Request) error {

Original file line numberDiff line numberDiff line change

@@ -7,16 +7,31 @@

77

package forwarder

88
99

import (

10-

"context"

11-

"errors"

1210

"net"

11+

_ "unsafe" // for go:linkname

1312

)

1413
15-

func nopResolver() *net.Resolver {

16-

return &net.Resolver{

17-

PreferGo: true,

18-

Dial: func(ctx context.Context, network, address string) (net.Conn, error) {

19-

return nil, errors.New("no DNS resolver configured")

20-

},

14+

// lookupStaticHost looks up the addresses and the canonical name for the given host from /etc/hosts.

15+

// It automatically updates the data cache.

16+

//

17+

//go:linkname lookupStaticHost net.lookupStaticHost

18+

func lookupStaticHost(string) ([]string, string)

19+
20+

func isLocalhost(host string) bool {

21+

if host == "localhost" {

22+

return true

23+

}

24+
25+

if ip := net.ParseIP(host); ip != nil {

26+

return ip.IsLoopback()

2127

}

28+
29+

addrs, _ := lookupStaticHost(host)

30+

if len(addrs) > 0 {

31+

if ip := net.ParseIP(addrs[0]); ip != nil {

32+

return ip.IsLoopback()

33+

}

34+

}

35+
36+

return false

2237

}

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,37 @@

1+

// Copyright 2022-2024 Sauce Labs Inc., all rights reserved.

2+

//

3+

// This Source Code Form is subject to the terms of the Mozilla Public

4+

// License, v. 2.0. If a copy of the MPL was not distributed with this

5+

// file, You can obtain one at https://mozilla.org/MPL/2.0/.

6+
7+

package forwarder

8+
9+

import (

10+

"testing"

11+

)

12+
13+

func TestIsLocalhost(t *testing.T) {

14+

tests := []struct {

15+

host string

16+

localhost bool

17+

}{

18+

{"127.0.0.1", true},

19+

{"127.10.20.30", true},

20+

{"localhost", true},

21+
22+

{"notlocalhost", false},

23+

{"broadcasthost", false},

24+
25+

{"::1", true},

26+
27+

{"::10", false},

28+

{"2001:0db8:85a3:0000:0000:8a2e:0370:7334", false},

29+

}

30+
31+

for i := range tests {

32+

tc := tests[i]

33+

if lh := isLocalhost(tc.host); lh != tc.localhost {

34+

t.Errorf("isLocalhost(%q) = %v; want %v", tc.host, lh, tc.localhost)

35+

}

36+

}

37+

}