dnsforward -- support tcp:// schema

This commit is contained in:
Eugene Bujak 2018-12-05 21:33:32 +03:00
parent 2a1059107a
commit ff1c19cac5
1 changed files with 13 additions and 2 deletions

View File

@ -30,6 +30,7 @@ type Upstream interface {
//
type plainDNS struct {
address string
preferTCP bool
}
var defaultUDPClient = dns.Client{
@ -46,6 +47,11 @@ var defaultTCPClient = dns.Client{
func (p *plainDNS) Address() string { return p.address }
func (p *plainDNS) Exchange(m *dns.Msg) (*dns.Msg, error) {
if p.preferTCP {
reply, _, err := defaultTCPClient.Exchange(m, p.address)
return reply, err
}
reply, _, err := defaultUDPClient.Exchange(m, p.address)
if err != nil && reply != nil && reply.Truncated {
log.Printf("Truncated message was received, retrying over TCP, question: %s", m.Question[0].String())
@ -183,7 +189,12 @@ func GetUpstream(address string) (Upstream, error) {
if url.Port() == "" {
url.Host += ":53"
}
return &plainDNS{address: url.String()}, nil
return &plainDNS{address: url.Host}, nil
case "tcp":
if url.Port() == "" {
url.Host += ":53"
}
return &plainDNS{address: url.Host, preferTCP: true}, nil
case "tls":
if url.Port() == "" {
url.Host += ":853"