Pull request: * dnsforward, querylog: set client_proto for logs correctly

Merge in DNS/adguard-home from 2241-doq-logs to master

Squashed commit of the following:

commit a15cab05358e3c0b97f8257f8b9628fa590e7e7d
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Nov 3 14:22:25 2020 +0300

    * all: update dnsproxy

commit 5fb0919a7528dc6ee7a433a8096b550f3691771c
Merge: b22b1dff4 64c1a68fb
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Nov 3 14:22:15 2020 +0300

    Merge branch 'master' into 2241-doq-logs

commit b22b1dff43e541d77160fd5c234483bbf0f6d8de
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Nov 3 12:37:23 2020 +0300

    * dnsforward, querylog: set client_proto for logs correctly
This commit is contained in:
Ainar Garipov
2020-11-03 15:39:55 +03:00
parent 64c1a68fb9
commit df34ee5c09
9 changed files with 108 additions and 45 deletions

View File

@@ -1,3 +1,4 @@
// Package dnsforward contains a DNS forwarding server.
package dnsforward
import (

View File

@@ -816,7 +816,6 @@ func sendTestMessageAsync(t *testing.T, conn *dns.Conn, g *sync.WaitGroup) {
err := conn.WriteMsg(req)
if err != nil {
panic(fmt.Sprintf("cannot write message: %s", err))
}
res, err := conn.ReadMsg()
@@ -917,20 +916,23 @@ func publicKey(priv interface{}) interface{} {
}
func TestValidateUpstream(t *testing.T) {
invalidUpstreams := []string{"1.2.3.4.5",
invalidUpstreams := []string{
"1.2.3.4.5",
"123.3.7m",
"htttps://google.com/dns-query",
"[/host.com]tls://dns.adguard.com",
"[host.ru]#",
}
validDefaultUpstreams := []string{"1.1.1.1",
validDefaultUpstreams := []string{
"1.1.1.1",
"tls://1.1.1.1",
"https://dns.adguard.com/dns-query",
"sdns://AQMAAAAAAAAAFDE3Ni4xMDMuMTMwLjEzMDo1NDQzINErR_JS3PLCu_iZEIbq95zkSV2LFsigxDIuUso_OQhzIjIuZG5zY3J5cHQuZGVmYXVsdC5uczEuYWRndWFyZC5jb20",
}
validUpstreams := []string{"[/host.com/]1.1.1.1",
validUpstreams := []string{
"[/host.com/]1.1.1.1",
"[//]tls://1.1.1.1",
"[/www.host.com/]#",
"[/host.com/google.com/]8.8.8.8",
@@ -976,7 +978,8 @@ func TestValidateUpstreamsSet(t *testing.T) {
assert.Nil(t, err, "comments should not be validated")
// Set of valid upstreams. There is no default upstream specified
upstreamsSet = []string{"[/host.com/]1.1.1.1",
upstreamsSet = []string{
"[/host.com/]1.1.1.1",
"[//]tls://1.1.1.1",
"[/www.host.com/]#",
"[/host.com/google.com/]8.8.8.8",
@@ -1030,9 +1033,7 @@ func (d *testDHCP) Leases(flags int) []dhcpd.Lease {
l.Hostname = "localhost"
return []dhcpd.Lease{l}
}
func (d *testDHCP) SetOnLeaseChanged(onLeaseChanged dhcpd.OnLeaseChangedT) {
return
}
func (d *testDHCP) SetOnLeaseChanged(onLeaseChanged dhcpd.OnLeaseChangedT) {}
func TestPTRResponseFromDHCPLeases(t *testing.T) {
dhcp := &testDHCP{}

View File

@@ -5,12 +5,11 @@ import (
"strings"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/querylog"
"github.com/miekg/dns"
"github.com/AdguardTeam/AdGuardHome/internal/dnsfilter"
"github.com/AdguardTeam/AdGuardHome/internal/querylog"
"github.com/AdguardTeam/AdGuardHome/internal/stats"
"github.com/AdguardTeam/dnsproxy/proxy"
"github.com/miekg/dns"
)
// Write Stats data and logs
@@ -40,10 +39,16 @@ func processQueryLogsAndStats(ctx *dnsContext) int {
ClientIP: getIP(d.Addr),
}
if d.Proto == "https" {
p.ClientProto = "doh"
} else if d.Proto == "tls" {
p.ClientProto = "dot"
switch d.Proto {
case proxy.ProtoHTTPS:
p.ClientProto = querylog.ClientProtoDOH
case proxy.ProtoQUIC:
p.ClientProto = querylog.ClientProtoDOQ
case proxy.ProtoTLS:
p.ClientProto = querylog.ClientProtoDOT
default:
// Consider this a plain DNS-over-UDP or DNS-over-TCL
// request.
}
if d.Upstream != nil {

View File

@@ -38,7 +38,7 @@ func decodeLogEntry(ent *logEntry, str string) {
ent.QClass = v
case "CP":
ent.ClientProto = v
ent.ClientProto, err = NewClientProto(v)
case "Answer":
ent.Answer, err = base64.StdEncoding.DecodeString(v)

View File

@@ -1,6 +1,8 @@
// Package querylog provides query log functions and interfaces.
package querylog
import (
"fmt"
"os"
"path/filepath"
"strings"
@@ -29,6 +31,33 @@ type queryLog struct {
fileWriteLock sync.Mutex
}
// ClientProto values are names of the client protocols.
type ClientProto string
// Client protocol names.
const (
ClientProtoDOH ClientProto = "doh"
ClientProtoDOQ ClientProto = "doq"
ClientProtoDOT ClientProto = "dot"
ClientProtoPlain ClientProto = ""
)
// NewClientProto validates that the client protocol name is valid and returns
// the name as a ClientProto.
func NewClientProto(s string) (cp ClientProto, err error) {
switch cp = ClientProto(s); cp {
case
ClientProtoDOH,
ClientProtoDOQ,
ClientProtoDOT,
ClientProtoPlain:
return cp, nil
default:
return "", fmt.Errorf("invalid client proto: %q", s)
}
}
// logEntry - represents a single log entry
type logEntry struct {
IP string `json:"IP"` // Client IP
@@ -38,7 +67,7 @@ type logEntry struct {
QType string `json:"QT"`
QClass string `json:"QC"`
ClientProto string `json:"CP"` // "" or "doh"
ClientProto ClientProto `json:"CP"`
Answer []byte `json:",omitempty"` // sometimes empty answers happen like binerdunt.top or rev2.globalrootservers.net
OrigAnswer []byte `json:",omitempty"`
@@ -158,7 +187,6 @@ func (l *queryLog) Add(params AddParams) {
// writing to file is disabled - just remove the oldest entry from array
l.buffer = l.buffer[1:]
}
} else if !l.flushPending {
needFlush = len(l.buffer) >= int(l.conf.MemSize)
if needFlush {

View File

@@ -48,7 +48,7 @@ type AddParams struct {
Elapsed time.Duration // Time spent for processing the request
ClientIP net.IP
Upstream string // Upstream server URL
ClientProto string // Protocol for the client connection: "" (plain), "doh", "dot"
ClientProto ClientProto
}
// New - create a new instance of the query log