Skip to content

Commit

Permalink
Merge branch 'master' into 6321-boot-ttl-vol.1
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Jan 11, 2024
2 parents 329e3e3 + a87a3df commit fb8f98b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
18 changes: 8 additions & 10 deletions upstream/doh.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,9 @@ func (p *dnsOverHTTPS) exchangeHTTPS(client *http.Client, req *dns.Msg) (resp *d
}

logBegin(p.addrRedacted, n, req)
resp, err = p.exchangeHTTPSClient(client, req)
logFinish(p.addrRedacted, n, err)
defer func() { logFinish(p.addrRedacted, n, err) }()

return resp, err
return p.exchangeHTTPSClient(client, req)
}

// exchangeHTTPSClient sends the DNS query to a DoH resolver using the specified
Expand Down Expand Up @@ -277,13 +276,12 @@ func (p *dnsOverHTTPS) exchangeHTTPSClient(
}

if httpResp.StatusCode != http.StatusOK {
return nil,
fmt.Errorf(
"expected status %d, got %d from %s",
http.StatusOK,
httpResp.StatusCode,
p.addrRedacted,
)
return nil, fmt.Errorf(
"expected status %d, got %d from %s",
http.StatusOK,
httpResp.StatusCode,
p.addrRedacted,
)
}

resp = &dns.Msg{}
Expand Down
9 changes: 7 additions & 2 deletions upstream/quic.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,20 @@ func (p *dnsOverQUIC) Close() (err error) {

// exchangeQUIC attempts to open a QUIC connection, send the DNS message
// through it and return the response it got from the server.
func (p *dnsOverQUIC) exchangeQUIC(m *dns.Msg) (resp *dns.Msg, err error) {
func (p *dnsOverQUIC) exchangeQUIC(req *dns.Msg) (resp *dns.Msg, err error) {
addr := p.Address()

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

var conn quic.Connection
conn, err = p.getConnection(true)
if err != nil {
return nil, err
}

var buf []byte
buf, err = m.Pack()
buf, err = req.Pack()
if err != nil {
return nil, fmt.Errorf("failed to pack DNS message for DoQ: %w", err)
}
Expand Down
46 changes: 37 additions & 9 deletions upstream/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import (
"net"
"net/netip"
"net/url"
"os"
"strconv"
"strings"
"time"

"github.com/AdguardTeam/dnsproxy/internal/bootstrap"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/netutil"
"github.com/ameshkov/dnscrypt/v2"
Expand Down Expand Up @@ -273,25 +275,51 @@ func addPort(u *url.URL, port uint16) {
// 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(upsAddr string, n network, req *dns.Msg) {
qtype := ""
target := ""
func logBegin(addr string, n network, req *dns.Msg) {
var qtype dns.Type
var qname string
if len(req.Question) != 0 {
qtype = dns.Type(req.Question[0].Qtype).String()
target = req.Question[0].Name
qtype = dns.Type(req.Question[0].Qtype)
qname = req.Question[0].Name
}

log.Debug("dnsproxy: %s: sending request over %s: %s %s", upsAddr, n, qtype, target)
log.Debug("dnsproxy: sending request to %s over %s: %s %q", addr, n, qtype, qname)
}

// Write to log about the result of DNS request
func logFinish(upsAddr string, n network, err error) {
// logFinish logs the end of DNS request resolution. It should be called right
// after receiving the response from the upstream or the failing action. n is
// the [network] that was used to send the request.
func logFinish(addr string, n network, err error) {
logRoutine := log.Debug

status := "ok"
if err != nil {
status = err.Error()
if isTimeout(err) {
// Notify user about the timeout.
logRoutine = log.Error
}
}

log.Debug("dnsproxy: %s: response received over %s: %q", upsAddr, n, status)
logRoutine("dnsproxy: %s: response received over %s: %q", addr, n, status)
}

// isTimeout returns true if err is a timeout error.
//
// TODO(e.burkov): Move to golibs.
func isTimeout(err error) (ok bool) {
var netErr net.Error
switch {
case
errors.Is(err, context.Canceled),
errors.Is(err, context.DeadlineExceeded),
errors.Is(err, os.ErrDeadlineExceeded):
return true
case errors.As(err, &netErr):
return netErr.Timeout()
default:
return false
}
}

// DialerInitializer returns the handler that it creates.
Expand Down

0 comments on commit fb8f98b

Please sign in to comment.