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 5, 2023
1 parent fc47ce0 commit 7c319e7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 36 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
79 changes: 45 additions & 34 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,57 @@ func (p *Proxy) createFilterChain(bl *blacklist.Blacklist) (filters.Chain, proxy
}
filterChain = filterChain.Append(instrumentedProxyPingFilter)

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 {
host, port, err := net.SplitHostPort(dnsServer)
if err != nil {
return nil, nil, errors.New("invalid dns-server address %v: %v", dnsServer, err)
}
r := &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
log.Debugf("Dialing custom resolver at %v", dnsServer)
return netx.DialContext(ctx, host, port)
},
}
resolvers = append(resolvers, r)
}
}

// Google anomaly detection can be triggered very often over IPv6.
// Prefer IPv4 to mitigate, see issue #97
_dialer := preferIPV4Dialer(timeoutToDialOriginSite)
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 {
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 && len(ips) > 0 {
ip = ips[0].IP
break
}
}
}
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)
conn, dialErr := _dialer(ctx, network, resolvedAddr)
if dialErr != nil {
return nil, dialErr
}
Expand Down

0 comments on commit 7c319e7

Please sign in to comment.