Skip to content

Commit

Permalink
upstream: imp logging, test
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Jun 8, 2023
1 parent 7fa6a9d commit 2614653
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
4 changes: 2 additions & 2 deletions upstream/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,12 @@ func logBegin(upstreamAddress string, n network, req *dns.Msg) {
}

// Write to log about the result of DNS request
func logFinish(upstreamAddress string, err error) {
func logFinish(upstreamAddress string, n network, err error) {
status := "ok"
if err != nil {
status = err.Error()
}
log.Debug("%s: response: %s", upstreamAddress, status)
log.Debug("%s: response received over %s: %s", upstreamAddress, n, status)
}

// DialerInitializer returns the handler that it creates. All the subsequent
Expand Down
9 changes: 7 additions & 2 deletions upstream/upstream_doh.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,14 @@ 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, networkTCP, req)
n := networkTCP
if isHTTP3(client) {
n = networkUDP
}

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

return resp, 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 @@ -199,7 +199,7 @@ func (p *dnsOverTLS) exchangeWithConn(conn net.Conn, m *dns.Msg) (reply *dns.Msg
addr := p.Address()

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

dnsConn := dns.Conn{Conn: conn}

Expand Down
2 changes: 1 addition & 1 deletion upstream/upstream_plain.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (p *plainDNS) dialExchange(
}

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

ctx := context.Background()
conn.Conn, err = dial(ctx, string(network), "")
Expand Down
10 changes: 10 additions & 0 deletions upstream/upstream_plain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"net"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -79,23 +80,30 @@ func TestUpstream_plainDNS_fallback(t *testing.T) {
testCases := []struct {
udpResp *dns.Msg
name string
wantNum int
}{{
udpResp: goodResp,
name: "all_right",
wantNum: 1,
}, {
udpResp: truncResp,
name: "truncated_response",
wantNum: 2,
}, {
udpResp: badQNameResp,
name: "bad_qname",
wantNum: 2,
}, {
udpResp: badQTypeResp,
name: "bad_qtype",
wantNum: 2,
}}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var reqNum uint32
srv := startDNSServer(t, func(w dns.ResponseWriter, _ *dns.Msg) {
atomic.AddUint32(&reqNum, 1)
resp := goodResp
if w.RemoteAddr().Network() == string(networkUDP) {
resp = tc.udpResp
Expand All @@ -116,6 +124,8 @@ func TestUpstream_plainDNS_fallback(t *testing.T) {
resp, err := u.Exchange(req)
require.NoError(t, err)
requireResponse(t, req, resp)

assert.Equal(t, tc.wantNum, int(reqNum))
})
}
}
Expand Down

0 comments on commit 2614653

Please sign in to comment.