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