Skip to content

Commit

Permalink
upstream: fix fallback to tcp
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Jun 8, 2023
1 parent 20fd473 commit 7fa6a9d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
8 changes: 5 additions & 3 deletions upstream/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,17 @@ func addPort(u *url.URL, port int) {
}
}

// Write to log DNS request information that we are going to send
func logBegin(upstreamAddress string, req *dns.Msg) {
// logBegin logs the start of DNS request resolution. It should be called right
// before dialing the connection to the upstream. n is the [network] that will
// be used to send the request.
func logBegin(upstreamAddress string, n network, req *dns.Msg) {
qtype := ""
target := ""
if len(req.Question) != 0 {
qtype = dns.Type(req.Question[0].Qtype).String()
target = req.Question[0].Name
}
log.Debug("%s: sending request %s %s", upstreamAddress, qtype, target)
log.Debug("%s: sending request over %s: %s %s", upstreamAddress, n, qtype, target)
}

// Write to log about the result of DNS request
Expand Down
2 changes: 1 addition & 1 deletion upstream/upstream_doh.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (p *dnsOverHTTPS) closeClient(client *http.Client) (err error) {
func (p *dnsOverHTTPS) exchangeHTTPS(client *http.Client, req *dns.Msg) (resp *dns.Msg, err error) {
addr := p.Address()

logBegin(addr, req)
logBegin(addr, networkTCP, req)
resp, err = p.exchangeHTTPSClient(client, req)
logFinish(addr, err)

Expand Down
2 changes: 1 addition & 1 deletion upstream/upstream_dot.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (p *dnsOverTLS) putBack(conn net.Conn) {
func (p *dnsOverTLS) exchangeWithConn(conn net.Conn, m *dns.Msg) (reply *dns.Msg, err error) {
addr := p.Address()

logBegin(addr, m)
logBegin(addr, networkTCP, m)
defer func() { logFinish(addr, err) }()

dnsConn := dns.Conn{Conn: conn}
Expand Down
14 changes: 12 additions & 2 deletions upstream/upstream_plain.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (p *plainDNS) dialExchange(
conn.UDPSize = dns.MinMsgSize
}

logBegin(addr, req)
logBegin(addr, network, req)
defer func() { logFinish(addr, err) }()

ctx := context.Background()
Expand Down Expand Up @@ -144,20 +144,30 @@ func (p *plainDNS) Exchange(req *dns.Msg) (resp *dns.Msg, err error) {

resp, err = p.dialExchange(p.net, dial, req)
if p.net != networkUDP {
// The network is already TCP.
return resp, err
}

if resp == nil {
// There is likely an error with the upstream.
return resp, err
}

if errors.Is(err, errQuestion) {
// The upstream responds with malformed messages, so try TCP.
log.Debug("plain %s: %s, using tcp", addr, err)

return p.dialExchange(networkTCP, dial, req)
} else if resp.Truncated {
// Fallback to TCP on truncated responses.
log.Debug("plain %s: resp for %s is truncated, using tcp", &req.Question[0], addr)

return p.dialExchange(networkTCP, dial, req)
}

return p.dialExchange(networkTCP, dial, req)
// There is either no error or the error isn't related to the received
// message.
return resp, err
}

// Close implements the [Upstream] interface for *plainDNS.
Expand Down

0 comments on commit 7fa6a9d

Please sign in to comment.