This commit fixes panic when customDialContext fails to resolve the host's address.
This commit is contained in:
Andrey Meshkov 2019-06-18 16:18:13 +03:00
parent 6187871e3b
commit 082354204b
3 changed files with 17 additions and 12 deletions

View File

@ -15,6 +15,8 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/joomcode/errorx"
"github.com/AdguardTeam/dnsproxy/upstream" "github.com/AdguardTeam/dnsproxy/upstream"
"github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/urlfilter" "github.com/AdguardTeam/urlfilter"
@ -674,15 +676,16 @@ func (d *Dnsfilter) createCustomDialContext(resolverAddr string) dialFunctionTyp
return nil, e return nil, e
} }
var firstErr error if len(addrs) == 0 {
firstErr = nil return nil, fmt.Errorf("couldn't lookup host: %s", host)
}
var dialErrs []error
for _, a := range addrs { for _, a := range addrs {
addr = fmt.Sprintf("%s:%s", a.String(), port) addr = fmt.Sprintf("%s:%s", a.String(), port)
con, err := dialer.DialContext(ctx, network, addr) con, err := dialer.DialContext(ctx, network, addr)
if err != nil { if err != nil {
if firstErr == nil { dialErrs = append(dialErrs, err)
firstErr = err
}
continue continue
} }
@ -692,7 +695,7 @@ func (d *Dnsfilter) createCustomDialContext(resolverAddr string) dialFunctionTyp
return con, err return con, err
} }
return nil, firstErr return nil, errorx.DecorateMany(fmt.Sprintf("couldn't dial to %s", addr), dialErrs...)
} }
} }

View File

@ -126,6 +126,7 @@ type tlsConfig struct {
} }
// initialize to default values, will be changed later when reading config or parsing command line // initialize to default values, will be changed later when reading config or parsing command line
// TODO: Get rid of global variables
var config = configuration{ var config = configuration{
ourConfigFilename: "AdGuardHome.yaml", ourConfigFilename: "AdGuardHome.yaml",
BindPort: 3000, BindPort: 3000,

View File

@ -338,20 +338,21 @@ func customDialContext(ctx context.Context, network, addr string) (net.Conn, err
return nil, e return nil, e
} }
var firstErr error if len(addrs) == 0 {
firstErr = nil return nil, fmt.Errorf("couldn't lookup host: %s", host)
}
var dialErrs []error
for _, a := range addrs { for _, a := range addrs {
addr = net.JoinHostPort(a.String(), port) addr = net.JoinHostPort(a.String(), port)
con, err := dialer.DialContext(ctx, network, addr) con, err := dialer.DialContext(ctx, network, addr)
if err != nil { if err != nil {
if firstErr == nil { dialErrs = append(dialErrs, err)
firstErr = err
}
continue continue
} }
return con, err return con, err
} }
return nil, firstErr return nil, errorx.DecorateMany(fmt.Sprintf("couldn't dial to %s", addr), dialErrs...)
} }
// check if error is "address already in use" // check if error is "address already in use"