Skip to content

Commit

Permalink
Allow specification of multiple dns resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
oxtoacart committed Oct 6, 2023
1 parent fc47ce0 commit 8a49af2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 39 deletions.
4 changes: 2 additions & 2 deletions http-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ var (

track = flag.String("track", "", "The track this proxy is running on")

dnsServer = flag.String("dns-server", "172.16.0.53:53", "Optional DNS server to use for DNS lookups (in place of system resolver)")
dnsServers = flag.String("dns-servers", "172.16.0.53:53,8.8.8.8:53", "Optional DNS servers (comma separated) to use for DNS lookups (in place of system resolver)")
)

const (
Expand Down Expand Up @@ -470,7 +470,7 @@ func main() {
BroflakeAddr: *broflakeAddr,
BroflakeCert: os.Getenv("BROFLAKE_CERT"),
BroflakeKey: os.Getenv("BROFLAKE_KEY"),
DNSServer: *dnsServer,
DNSServers: strings.Split(*dnsServers, ","),
}
if *maxmindLicenseKey != "" {
log.Debug("Will use Maxmind for geolocating clients")
Expand Down
88 changes: 51 additions & 37 deletions http_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ type Proxy struct {
BroflakeCert string
BroflakeKey string

DNSServer string
DNSServers []string
throttleConfig throttle.Config
instrument instrument.Instrument
}
Expand Down Expand Up @@ -279,35 +279,6 @@ func (p *Proxy) ListenAndServe(ctx context.Context) error {
// Throttle connections when signaled
srv.AddListenerWrappers(lanternlisteners.NewBitrateListener, bwReporting.wrapper)

if p.DNSServer != "" {
log.Debugf("Will resolve DNS using %v", p.DNSServer)
host, port, err := net.SplitHostPort(p.DNSServer)
if err != nil {
log.Fatalf("invalid dns-server address %v: %v", p.DNSServer, err)
}
r := &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
log.Debug("Dialing custom resolver")
return netx.DialContext(ctx, host, port)
},
}
netx.OverrideResolveIPs(func(host string) ([]net.IP, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

addrs, err := r.LookupIPAddr(ctx, host)
if err != nil {
return nil, err
}
ips := make([]net.IP, 0, len(addrs))
for _, addr := range addrs {
ips = append(ips, addr.IP)
}
return ips, nil
})
}

allListeners := make([]net.Listener, 0)
listenerProtocols := make([]string, 0)
addListenerIfNecessary := func(proto, addr string, fn listenerBuilderFN) error {
Expand Down Expand Up @@ -639,17 +610,60 @@ func (p *Proxy) createFilterChain(bl *blacklist.Blacklist) (filters.Chain, proxy
}
filterChain = filterChain.Append(instrumentedProxyPingFilter)

// Google anomaly detection can be triggered very often over IPv6.
// Prefer IPv4 to mitigate, see issue #97
_dialer := preferIPV4Dialer(timeoutToDialOriginSite)
var resolvers []*net.Resolver
if len(p.DNSServers) == 0 {
resolvers = append(resolvers, &net.Resolver{})
} else {
log.Debugf("Will resolve DNS using %v", p.DNSServers)
for _, _dnsServer := range p.DNSServers {
dnsServer := _dnsServer
r := &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
return netx.DialContext(ctx, "udp", dnsServer)
},
}
resolvers = append(resolvers, r)
}
}

dialer := func(ctx context.Context, network, addr string) (net.Conn, error) {
// resolve separately so that we can track the DNS resolution time
resolvedAddr, resolveErr := netx.Resolve(network, addr)
if resolveErr != nil {
return nil, resolveErr
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, errors.New("invalid address %v: %v", addr, err)
}
ip := net.ParseIP(host)
var resolveErr error
if ip == nil {
resolveLoop:
for _, r := range resolvers {
// Note - 5 seconds is the default Linux DNS timeout
rctx, cancel := context.WithTimeout(ctx, 5*time.Second)
var ips []net.IPAddr
ips, resolveErr = r.LookupIPAddr(rctx, host)
cancel()
if resolveErr == nil {
// Google anomaly detection can be triggered very often over IPv6.
// Prefer IPv4 to mitigate, see issue #97
for _, candidate := range ips {
if candidate.IP.To4() != nil {
ip = candidate.IP
break resolveLoop
}
}
}
}
}
if ip == nil {
return nil, errors.New("unable to resolve host %v, last resolution error: %v", host, resolveErr)
}

conn, dialErr := _dialer(ctx, network, resolvedAddr.String())
resolvedAddr := fmt.Sprintf("%s:%s", ip, port)
d := &net.Dialer{
Deadline: time.Now().Add(timeoutToDialOriginSite),
}
conn, dialErr := d.DialContext(ctx, network, resolvedAddr)
if dialErr != nil {
return nil, dialErr
}
Expand Down
1 change: 1 addition & 0 deletions throttle_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func doTestThrottling(t *testing.T, pro bool, serverAddr string, redisIsUp bool,
TestingLocal: true,
GoogleSearchRegex: "bequiet",
GoogleCaptchaRegex: "bequiet",
DNSServers: []string{"127.0.0.1:2435", "8.8.8.8:53"}, // first one is a bogus DNS server
}
go func() {
assert.NoError(t, proxy.ListenAndServe(context.Background()))
Expand Down

0 comments on commit 8a49af2

Please sign in to comment.