2020-05-26 16:24:31 +03:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
2024-11-21 20:19:39 +03:00
|
|
|
"context"
|
2020-05-26 16:24:31 +03:00
|
|
|
"encoding/base64"
|
2020-11-20 17:32:41 +03:00
|
|
|
"encoding/json"
|
2023-05-24 16:33:15 +03:00
|
|
|
"fmt"
|
2020-11-27 12:33:25 +03:00
|
|
|
"io"
|
2020-12-21 17:48:07 +03:00
|
|
|
"net"
|
2023-08-23 16:58:24 +03:00
|
|
|
"net/netip"
|
2020-05-26 16:24:31 +03:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-05-21 16:15:47 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2024-03-21 18:45:34 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering/rulelist"
|
2023-05-24 16:33:15 +03:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2024-11-21 20:19:39 +03:00
|
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
2020-12-21 17:48:07 +03:00
|
|
|
"github.com/AdguardTeam/urlfilter/rules"
|
|
|
|
"github.com/miekg/dns"
|
2020-05-26 16:24:31 +03:00
|
|
|
)
|
|
|
|
|
2023-05-24 16:33:15 +03:00
|
|
|
// logEntryHandler represents a handler for decoding json token to the logEntry
|
|
|
|
// struct.
|
2022-03-03 17:52:11 +03:00
|
|
|
type logEntryHandler func(t json.Token, ent *logEntry) error
|
2020-12-07 14:32:06 +03:00
|
|
|
|
2023-05-24 16:33:15 +03:00
|
|
|
// logEntryHandlers is the map of log entry decode handlers for various keys.
|
2020-12-07 14:32:06 +03:00
|
|
|
var logEntryHandlers = map[string]logEntryHandler{
|
2021-01-27 18:32:13 +03:00
|
|
|
"CID": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ent.ClientID = v
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2020-11-20 17:32:41 +03:00
|
|
|
"IP": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2021-01-20 17:27:53 +03:00
|
|
|
|
|
|
|
if ent.IP == nil {
|
|
|
|
ent.IP = net.ParseIP(v)
|
2020-11-20 17:32:41 +03:00
|
|
|
}
|
2021-01-20 17:27:53 +03:00
|
|
|
|
2020-11-20 17:32:41 +03:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
"T": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-07 17:43:51 +03:00
|
|
|
|
2020-11-20 17:32:41 +03:00
|
|
|
var err error
|
|
|
|
ent.Time, err = time.Parse(time.RFC3339, v)
|
2021-12-07 17:43:51 +03:00
|
|
|
|
2020-11-20 17:32:41 +03:00
|
|
|
return err
|
|
|
|
},
|
|
|
|
"QH": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ent.QHost = v
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
"QT": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ent.QType = v
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
"QC": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-07 17:43:51 +03:00
|
|
|
|
2020-11-20 17:32:41 +03:00
|
|
|
ent.QClass = v
|
2021-12-07 17:43:51 +03:00
|
|
|
|
2020-11-20 17:32:41 +03:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
"CP": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-07 17:43:51 +03:00
|
|
|
|
2020-11-20 17:32:41 +03:00
|
|
|
var err error
|
|
|
|
ent.ClientProto, err = NewClientProto(v)
|
2021-12-07 17:43:51 +03:00
|
|
|
|
2020-11-20 17:32:41 +03:00
|
|
|
return err
|
|
|
|
},
|
|
|
|
"Answer": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-07 17:43:51 +03:00
|
|
|
|
2020-11-20 17:32:41 +03:00
|
|
|
var err error
|
|
|
|
ent.Answer, err = base64.StdEncoding.DecodeString(v)
|
2021-12-07 17:43:51 +03:00
|
|
|
|
2020-11-20 17:32:41 +03:00
|
|
|
return err
|
|
|
|
},
|
|
|
|
"OrigAnswer": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-07 17:43:51 +03:00
|
|
|
|
2020-11-20 17:32:41 +03:00
|
|
|
var err error
|
|
|
|
ent.OrigAnswer, err = base64.StdEncoding.DecodeString(v)
|
2021-12-07 17:43:51 +03:00
|
|
|
|
2020-11-20 17:32:41 +03:00
|
|
|
return err
|
|
|
|
},
|
2022-03-03 17:52:11 +03:00
|
|
|
"ECS": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ent.ReqECS = v
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2021-12-07 17:43:51 +03:00
|
|
|
"Cached": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(bool)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ent.Cached = v
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2021-12-13 18:06:01 +03:00
|
|
|
"AD": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(bool)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ent.AuthenticatedData = v
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2020-12-17 13:32:46 +03:00
|
|
|
"Upstream": func(t json.Token, ent *logEntry) error {
|
2020-11-20 17:32:41 +03:00
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-07 17:43:51 +03:00
|
|
|
|
2020-12-17 13:32:46 +03:00
|
|
|
ent.Upstream = v
|
2021-12-07 17:43:51 +03:00
|
|
|
|
2020-11-20 17:32:41 +03:00
|
|
|
return nil
|
|
|
|
},
|
2020-12-17 13:32:46 +03:00
|
|
|
"Elapsed": func(t json.Token, ent *logEntry) error {
|
2020-11-27 12:33:25 +03:00
|
|
|
v, ok := t.(json.Number)
|
2020-11-20 17:32:41 +03:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-07 17:43:51 +03:00
|
|
|
|
2020-11-27 12:33:25 +03:00
|
|
|
i, err := v.Int64()
|
2020-11-20 17:32:41 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-07 17:43:51 +03:00
|
|
|
|
2020-12-17 13:32:46 +03:00
|
|
|
ent.Elapsed = time.Duration(i)
|
2021-12-07 17:43:51 +03:00
|
|
|
|
2020-11-20 17:32:41 +03:00
|
|
|
return nil
|
|
|
|
},
|
2020-12-17 13:32:46 +03:00
|
|
|
}
|
|
|
|
|
2023-05-24 16:33:15 +03:00
|
|
|
// decodeResultRuleKey decodes the token of "Rules" type to logEntry struct.
|
2024-11-21 20:19:39 +03:00
|
|
|
func (l *queryLog) decodeResultRuleKey(
|
|
|
|
ctx context.Context,
|
|
|
|
key string,
|
|
|
|
i int,
|
|
|
|
dec *json.Decoder,
|
|
|
|
ent *logEntry,
|
|
|
|
) {
|
2023-02-27 17:18:56 +03:00
|
|
|
var vToken json.Token
|
2020-12-21 17:48:07 +03:00
|
|
|
switch key {
|
|
|
|
case "FilterListID":
|
2024-11-21 20:19:39 +03:00
|
|
|
ent.Result.Rules, vToken = l.decodeVTokenAndAddRule(ctx, key, i, dec, ent.Result.Rules)
|
2020-12-21 17:48:07 +03:00
|
|
|
if n, ok := vToken.(json.Number); ok {
|
2024-03-21 18:45:34 +03:00
|
|
|
id, _ := n.Int64()
|
|
|
|
ent.Result.Rules[i].FilterListID = rulelist.URLFilterID(id)
|
2020-12-21 17:48:07 +03:00
|
|
|
}
|
|
|
|
case "IP":
|
2024-11-21 20:19:39 +03:00
|
|
|
ent.Result.Rules, vToken = l.decodeVTokenAndAddRule(ctx, key, i, dec, ent.Result.Rules)
|
2020-12-21 17:48:07 +03:00
|
|
|
if ipStr, ok := vToken.(string); ok {
|
2023-08-23 16:58:24 +03:00
|
|
|
if ip, err := netip.ParseAddr(ipStr); err == nil {
|
|
|
|
ent.Result.Rules[i].IP = ip
|
|
|
|
} else {
|
2024-11-21 20:19:39 +03:00
|
|
|
l.logger.DebugContext(ctx, "decoding ip", "value", ipStr, slogutil.KeyError, err)
|
2023-08-23 16:58:24 +03:00
|
|
|
}
|
2020-12-21 17:48:07 +03:00
|
|
|
}
|
|
|
|
case "Text":
|
2024-11-21 20:19:39 +03:00
|
|
|
ent.Result.Rules, vToken = l.decodeVTokenAndAddRule(ctx, key, i, dec, ent.Result.Rules)
|
2020-12-21 17:48:07 +03:00
|
|
|
if s, ok := vToken.(string); ok {
|
|
|
|
ent.Result.Rules[i].Text = s
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Go on.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:33:15 +03:00
|
|
|
// decodeVTokenAndAddRule decodes the "Rules" toke as [filtering.ResultRule]
|
|
|
|
// and then adds the decoded object to the slice of result rules.
|
2024-11-21 20:19:39 +03:00
|
|
|
func (l *queryLog) decodeVTokenAndAddRule(
|
|
|
|
ctx context.Context,
|
2023-02-27 17:18:56 +03:00
|
|
|
key string,
|
|
|
|
i int,
|
|
|
|
dec *json.Decoder,
|
|
|
|
rules []*filtering.ResultRule,
|
|
|
|
) (newRules []*filtering.ResultRule, vToken json.Token) {
|
|
|
|
newRules = rules
|
|
|
|
|
|
|
|
vToken, err := dec.Token()
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
2024-11-21 20:19:39 +03:00
|
|
|
l.logger.DebugContext(
|
|
|
|
ctx,
|
|
|
|
"decoding result rule key",
|
|
|
|
"key", key,
|
|
|
|
slogutil.KeyError, err,
|
|
|
|
)
|
2023-02-27 17:18:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return newRules, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rules) < i+1 {
|
|
|
|
newRules = append(newRules, &filtering.ResultRule{})
|
|
|
|
}
|
|
|
|
|
|
|
|
return newRules, vToken
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:33:15 +03:00
|
|
|
// decodeResultRules parses the dec's tokens into logEntry ent interpreting it
|
|
|
|
// as a slice of the result rules.
|
2024-11-21 20:19:39 +03:00
|
|
|
func (l *queryLog) decodeResultRules(ctx context.Context, dec *json.Decoder, ent *logEntry) {
|
|
|
|
const msgPrefix = "decoding result rules"
|
|
|
|
|
2020-12-21 17:48:07 +03:00
|
|
|
for {
|
|
|
|
delimToken, err := dec.Token()
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
2024-11-21 20:19:39 +03:00
|
|
|
l.logger.DebugContext(ctx, msgPrefix+"; token", slogutil.KeyError, err)
|
2020-12-21 17:48:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:33:15 +03:00
|
|
|
if d, ok := delimToken.(json.Delim); !ok {
|
2020-12-21 17:48:07 +03:00
|
|
|
return
|
2023-05-24 16:33:15 +03:00
|
|
|
} else if d != '[' {
|
2024-11-21 20:19:39 +03:00
|
|
|
l.logger.DebugContext(
|
|
|
|
ctx,
|
|
|
|
msgPrefix,
|
|
|
|
slogutil.KeyError, newUnexpectedDelimiterError(d),
|
|
|
|
)
|
2020-12-21 17:48:07 +03:00
|
|
|
}
|
|
|
|
|
2024-11-21 20:19:39 +03:00
|
|
|
err = l.decodeResultRuleToken(ctx, dec, ent)
|
2023-05-24 16:33:15 +03:00
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF && !errors.Is(err, ErrEndOfToken) {
|
2024-11-21 20:19:39 +03:00
|
|
|
l.logger.DebugContext(ctx, msgPrefix+"; rule token", slogutil.KeyError, err)
|
2020-12-21 17:48:07 +03:00
|
|
|
}
|
|
|
|
|
2023-05-24 16:33:15 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-21 17:48:07 +03:00
|
|
|
|
2023-05-24 16:33:15 +03:00
|
|
|
// decodeResultRuleToken decodes the tokens of "Rules" type to the logEntry ent.
|
2024-11-21 20:19:39 +03:00
|
|
|
func (l *queryLog) decodeResultRuleToken(
|
|
|
|
ctx context.Context,
|
|
|
|
dec *json.Decoder,
|
|
|
|
ent *logEntry,
|
|
|
|
) (err error) {
|
2023-05-24 16:33:15 +03:00
|
|
|
i := 0
|
|
|
|
for {
|
|
|
|
var keyToken json.Token
|
|
|
|
keyToken, err = dec.Token()
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error, because it's informative enough as is.
|
|
|
|
return err
|
|
|
|
}
|
2020-12-21 17:48:07 +03:00
|
|
|
|
2023-05-24 16:33:15 +03:00
|
|
|
if d, ok := keyToken.(json.Delim); ok {
|
|
|
|
switch d {
|
|
|
|
case '}':
|
|
|
|
i++
|
|
|
|
case ']':
|
|
|
|
return ErrEndOfToken
|
|
|
|
default:
|
|
|
|
// Go on.
|
2020-12-21 17:48:07 +03:00
|
|
|
}
|
|
|
|
|
2023-05-24 16:33:15 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
key, ok := keyToken.(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("keyToken is %T (%[1]v) and not string", keyToken)
|
2020-12-21 17:48:07 +03:00
|
|
|
}
|
2023-05-24 16:33:15 +03:00
|
|
|
|
2024-11-21 20:19:39 +03:00
|
|
|
l.decodeResultRuleKey(ctx, key, i, dec, ent)
|
2020-12-21 17:48:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 18:01:48 +03:00
|
|
|
// decodeResultReverseHosts parses the dec's tokens into ent interpreting it as
|
|
|
|
// the result of hosts container's $dnsrewrite rule. It assumes there are no
|
|
|
|
// other occurrences of DNSRewriteResult in the entry since hosts container's
|
|
|
|
// rewrites currently has the highest priority along the entire filtering
|
|
|
|
// pipeline.
|
2024-11-21 20:19:39 +03:00
|
|
|
func (l *queryLog) decodeResultReverseHosts(ctx context.Context, dec *json.Decoder, ent *logEntry) {
|
|
|
|
const msgPrefix = "decoding result reverse hosts"
|
|
|
|
|
2020-12-21 17:48:07 +03:00
|
|
|
for {
|
|
|
|
itemToken, err := dec.Token()
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
2024-11-21 20:19:39 +03:00
|
|
|
l.logger.DebugContext(ctx, msgPrefix+"; token", slogutil.KeyError, err)
|
2020-12-21 17:48:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := itemToken.(type) {
|
|
|
|
case json.Delim:
|
|
|
|
if v == '[' {
|
|
|
|
continue
|
|
|
|
} else if v == ']' {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-21 20:19:39 +03:00
|
|
|
l.logger.DebugContext(
|
|
|
|
ctx,
|
|
|
|
msgPrefix,
|
|
|
|
slogutil.KeyError, newUnexpectedDelimiterError(v),
|
|
|
|
)
|
2020-12-21 17:48:07 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
case string:
|
2021-11-23 18:01:48 +03:00
|
|
|
v = dns.Fqdn(v)
|
|
|
|
if res := &ent.Result; res.DNSRewriteResult == nil {
|
|
|
|
res.DNSRewriteResult = &filtering.DNSRewriteResult{
|
|
|
|
RCode: dns.RcodeSuccess,
|
|
|
|
Response: filtering.DNSRewriteResultResponse{
|
|
|
|
dns.TypePTR: []rules.RRValue{v},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
res.DNSRewriteResult.RCode = dns.RcodeSuccess
|
|
|
|
}
|
|
|
|
|
|
|
|
if rres := ent.Result.DNSRewriteResult; rres.Response == nil {
|
|
|
|
rres.Response = filtering.DNSRewriteResultResponse{dns.TypePTR: []rules.RRValue{v}}
|
|
|
|
} else {
|
|
|
|
rres.Response[dns.TypePTR] = append(rres.Response[dns.TypePTR], v)
|
|
|
|
}
|
2020-12-21 17:48:07 +03:00
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:33:15 +03:00
|
|
|
// decodeResultIPList parses the dec's tokens into logEntry ent interpreting it
|
|
|
|
// as the result IP addresses list.
|
2024-11-21 20:19:39 +03:00
|
|
|
func (l *queryLog) decodeResultIPList(ctx context.Context, dec *json.Decoder, ent *logEntry) {
|
|
|
|
const msgPrefix = "decoding result ip list"
|
|
|
|
|
2020-12-21 17:48:07 +03:00
|
|
|
for {
|
|
|
|
itemToken, err := dec.Token()
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
2024-11-21 20:19:39 +03:00
|
|
|
l.logger.DebugContext(ctx, msgPrefix+"; token", slogutil.KeyError, err)
|
2020-12-21 17:48:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := itemToken.(type) {
|
|
|
|
case json.Delim:
|
|
|
|
if v == '[' {
|
|
|
|
continue
|
|
|
|
} else if v == ']' {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-21 20:19:39 +03:00
|
|
|
l.logger.DebugContext(
|
|
|
|
ctx,
|
|
|
|
msgPrefix,
|
|
|
|
slogutil.KeyError, newUnexpectedDelimiterError(v),
|
|
|
|
)
|
2020-12-21 17:48:07 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
case string:
|
2023-08-23 16:58:24 +03:00
|
|
|
var ip netip.Addr
|
|
|
|
ip, err = netip.ParseAddr(v)
|
|
|
|
if err == nil {
|
2020-12-21 17:48:07 +03:00
|
|
|
ent.Result.IPList = append(ent.Result.IPList, ip)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:33:15 +03:00
|
|
|
// decodeResultDNSRewriteResultKey decodes the token of "DNSRewriteResult" type
|
|
|
|
// to the logEntry struct.
|
2024-11-21 20:19:39 +03:00
|
|
|
func (l *queryLog) decodeResultDNSRewriteResultKey(
|
|
|
|
ctx context.Context,
|
|
|
|
key string,
|
|
|
|
dec *json.Decoder,
|
|
|
|
ent *logEntry,
|
|
|
|
) {
|
|
|
|
const msgPrefix = "decoding result dns rewrite result key"
|
|
|
|
|
2021-03-25 20:30:30 +03:00
|
|
|
var err error
|
|
|
|
|
|
|
|
switch key {
|
|
|
|
case "RCode":
|
|
|
|
var vToken json.Token
|
|
|
|
vToken, err = dec.Token()
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
2024-11-21 20:19:39 +03:00
|
|
|
l.logger.DebugContext(ctx, msgPrefix+"; token", slogutil.KeyError, err)
|
2021-03-25 20:30:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ent.Result.DNSRewriteResult == nil {
|
2021-05-21 16:15:47 +03:00
|
|
|
ent.Result.DNSRewriteResult = &filtering.DNSRewriteResult{}
|
2021-03-25 20:30:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if n, ok := vToken.(json.Number); ok {
|
|
|
|
rcode64, _ := n.Int64()
|
|
|
|
ent.Result.DNSRewriteResult.RCode = rules.RCode(rcode64)
|
|
|
|
}
|
|
|
|
case "Response":
|
|
|
|
if ent.Result.DNSRewriteResult == nil {
|
2021-05-21 16:15:47 +03:00
|
|
|
ent.Result.DNSRewriteResult = &filtering.DNSRewriteResult{}
|
2021-03-25 20:30:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ent.Result.DNSRewriteResult.Response == nil {
|
2021-05-21 16:15:47 +03:00
|
|
|
ent.Result.DNSRewriteResult.Response = filtering.DNSRewriteResultResponse{}
|
2021-03-25 20:30:30 +03:00
|
|
|
}
|
|
|
|
|
2021-11-23 18:01:48 +03:00
|
|
|
// TODO(a.garipov): I give up. This whole file is a mess. Luckily, we
|
|
|
|
// can assume that this field is relatively rare and just use the normal
|
|
|
|
// decoding and correct the values.
|
2021-03-25 20:30:30 +03:00
|
|
|
err = dec.Decode(&ent.Result.DNSRewriteResult.Response)
|
|
|
|
if err != nil {
|
2024-11-21 20:19:39 +03:00
|
|
|
l.logger.DebugContext(ctx, msgPrefix+"; response", slogutil.KeyError, err)
|
2021-03-25 20:30:30 +03:00
|
|
|
}
|
|
|
|
|
2023-05-24 16:33:15 +03:00
|
|
|
ent.parseDNSRewriteResultIPs()
|
2021-03-25 20:30:30 +03:00
|
|
|
default:
|
|
|
|
// Go on.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:33:15 +03:00
|
|
|
// decodeResultDNSRewriteResult parses the dec's tokens into logEntry ent
|
|
|
|
// interpreting it as the result DNSRewriteResult.
|
2024-11-21 20:19:39 +03:00
|
|
|
func (l *queryLog) decodeResultDNSRewriteResult(
|
|
|
|
ctx context.Context,
|
|
|
|
dec *json.Decoder,
|
|
|
|
ent *logEntry,
|
|
|
|
) {
|
|
|
|
const msgPrefix = "decoding result dns rewrite result"
|
|
|
|
|
2020-12-21 17:48:07 +03:00
|
|
|
for {
|
2023-05-24 16:33:15 +03:00
|
|
|
key, err := parseKeyToken(dec)
|
2020-12-21 17:48:07 +03:00
|
|
|
if err != nil {
|
2023-05-24 16:33:15 +03:00
|
|
|
if err != io.EOF && !errors.Is(err, ErrEndOfToken) {
|
2024-11-21 20:19:39 +03:00
|
|
|
l.logger.DebugContext(ctx, msgPrefix+"; token", slogutil.KeyError, err)
|
2020-12-21 17:48:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:33:15 +03:00
|
|
|
if key == "" {
|
2020-12-21 17:48:07 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-11-21 20:19:39 +03:00
|
|
|
l.decodeResultDNSRewriteResultKey(ctx, key, dec, ent)
|
2020-12-21 17:48:07 +03:00
|
|
|
}
|
2020-12-17 13:32:46 +03:00
|
|
|
}
|
|
|
|
|
2021-11-23 18:01:48 +03:00
|
|
|
// translateResult converts some fields of the ent.Result to the format
|
|
|
|
// consistent with current implementation.
|
|
|
|
func translateResult(ent *logEntry) {
|
|
|
|
res := &ent.Result
|
|
|
|
if res.Reason != filtering.RewrittenAutoHosts || len(res.IPList) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.DNSRewriteResult == nil {
|
|
|
|
res.DNSRewriteResult = &filtering.DNSRewriteResult{
|
|
|
|
RCode: dns.RcodeSuccess,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.DNSRewriteResult.Response == nil {
|
|
|
|
res.DNSRewriteResult.Response = filtering.DNSRewriteResultResponse{}
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := res.DNSRewriteResult.Response
|
|
|
|
for _, ip := range res.IPList {
|
|
|
|
qType := dns.TypeAAAA
|
2023-08-23 16:58:24 +03:00
|
|
|
if ip.Is4() {
|
2021-11-23 18:01:48 +03:00
|
|
|
qType = dns.TypeA
|
|
|
|
}
|
|
|
|
|
|
|
|
resp[qType] = append(resp[qType], ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
res.IPList = nil
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:33:15 +03:00
|
|
|
// ErrEndOfToken is an error returned by parse key token when the closing
|
|
|
|
// bracket is found.
|
|
|
|
const ErrEndOfToken errors.Error = "end of token"
|
|
|
|
|
|
|
|
// parseKeyToken parses the dec's token key.
|
|
|
|
func parseKeyToken(dec *json.Decoder) (key string, err error) {
|
|
|
|
keyToken, err := dec.Token()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if d, ok := keyToken.(json.Delim); ok {
|
|
|
|
if d == '}' {
|
|
|
|
return "", ErrEndOfToken
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
key, ok := keyToken.(string)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("keyToken is %T (%[1]v) and not string", keyToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// decodeResult decodes a token of "Result" type to logEntry struct.
|
2024-11-21 20:19:39 +03:00
|
|
|
func (l *queryLog) decodeResult(ctx context.Context, dec *json.Decoder, ent *logEntry) {
|
|
|
|
const msgPrefix = "decoding result"
|
|
|
|
|
2021-11-23 18:01:48 +03:00
|
|
|
defer translateResult(ent)
|
|
|
|
|
2020-12-17 13:32:46 +03:00
|
|
|
for {
|
2023-05-24 16:33:15 +03:00
|
|
|
key, err := parseKeyToken(dec)
|
2020-11-20 17:32:41 +03:00
|
|
|
if err != nil {
|
2023-05-24 16:33:15 +03:00
|
|
|
if err != io.EOF && !errors.Is(err, ErrEndOfToken) {
|
2024-11-21 20:19:39 +03:00
|
|
|
l.logger.DebugContext(ctx, msgPrefix+"; token", slogutil.KeyError, err)
|
2020-12-17 13:32:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2020-11-20 17:32:41 +03:00
|
|
|
}
|
2020-12-17 13:32:46 +03:00
|
|
|
|
2023-05-24 16:33:15 +03:00
|
|
|
if key == "" {
|
2020-12-17 13:32:46 +03:00
|
|
|
continue
|
2020-11-20 17:32:41 +03:00
|
|
|
}
|
2020-12-17 13:32:46 +03:00
|
|
|
|
2024-11-21 20:19:39 +03:00
|
|
|
ok := l.resultDecHandler(ctx, key, dec, ent)
|
2023-04-03 16:29:07 +03:00
|
|
|
if ok {
|
2020-12-21 17:48:07 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-12-17 13:32:46 +03:00
|
|
|
handler, ok := resultHandlers[key]
|
2020-11-20 17:32:41 +03:00
|
|
|
if !ok {
|
2020-12-17 13:32:46 +03:00
|
|
|
continue
|
2020-11-20 17:32:41 +03:00
|
|
|
}
|
2020-12-17 13:32:46 +03:00
|
|
|
|
|
|
|
val, err := dec.Token()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = handler(val, ent); err != nil {
|
2024-11-21 20:19:39 +03:00
|
|
|
l.logger.DebugContext(ctx, msgPrefix+"; handler", slogutil.KeyError, err)
|
2020-12-17 13:32:46 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-11-20 17:32:41 +03:00
|
|
|
}
|
2020-05-26 16:24:31 +03:00
|
|
|
|
2023-05-24 16:33:15 +03:00
|
|
|
// resultHandlers is the map of log entry decode handlers for various keys.
|
2023-04-03 16:29:07 +03:00
|
|
|
var resultHandlers = map[string]logEntryHandler{
|
|
|
|
"IsFiltered": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(bool)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-24 16:33:15 +03:00
|
|
|
|
2023-04-03 16:29:07 +03:00
|
|
|
ent.Result.IsFiltered = v
|
2023-05-24 16:33:15 +03:00
|
|
|
|
2023-04-03 16:29:07 +03:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
"Rule": func(t json.Token, ent *logEntry) error {
|
|
|
|
s, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
l := len(ent.Result.Rules)
|
|
|
|
if l == 0 {
|
|
|
|
ent.Result.Rules = []*filtering.ResultRule{{}}
|
|
|
|
l++
|
|
|
|
}
|
|
|
|
|
|
|
|
ent.Result.Rules[l-1].Text = s
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
"FilterID": func(t json.Token, ent *logEntry) error {
|
|
|
|
n, ok := t.(json.Number)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-21 18:45:34 +03:00
|
|
|
id, err := n.Int64()
|
2023-04-03 16:29:07 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
l := len(ent.Result.Rules)
|
|
|
|
if l == 0 {
|
|
|
|
ent.Result.Rules = []*filtering.ResultRule{{}}
|
|
|
|
l++
|
|
|
|
}
|
|
|
|
|
2024-03-21 18:45:34 +03:00
|
|
|
ent.Result.Rules[l-1].FilterListID = rulelist.URLFilterID(id)
|
2023-04-03 16:29:07 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
"Reason": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(json.Number)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-24 16:33:15 +03:00
|
|
|
|
2023-04-03 16:29:07 +03:00
|
|
|
i, err := v.Int64()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-24 16:33:15 +03:00
|
|
|
|
2023-04-03 16:29:07 +03:00
|
|
|
ent.Result.Reason = filtering.Reason(i)
|
2023-05-24 16:33:15 +03:00
|
|
|
|
2023-04-03 16:29:07 +03:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
"ServiceName": func(t json.Token, ent *logEntry) error {
|
|
|
|
s, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ent.Result.ServiceName = s
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
"CanonName": func(t json.Token, ent *logEntry) error {
|
|
|
|
s, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ent.Result.CanonName = s
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-11-21 20:19:39 +03:00
|
|
|
// resultDecHandlers calls a decode handler for key if there is one.
|
|
|
|
func (l *queryLog) resultDecHandler(
|
|
|
|
ctx context.Context,
|
|
|
|
name string,
|
|
|
|
dec *json.Decoder,
|
|
|
|
ent *logEntry,
|
|
|
|
) (ok bool) {
|
|
|
|
ok = true
|
|
|
|
switch name {
|
|
|
|
case "ReverseHosts":
|
|
|
|
l.decodeResultReverseHosts(ctx, dec, ent)
|
|
|
|
case "IPList":
|
|
|
|
l.decodeResultIPList(ctx, dec, ent)
|
|
|
|
case "Rules":
|
|
|
|
l.decodeResultRules(ctx, dec, ent)
|
|
|
|
case "DNSRewriteResult":
|
|
|
|
l.decodeResultDNSRewriteResult(ctx, dec, ent)
|
|
|
|
default:
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok
|
2023-04-03 16:29:07 +03:00
|
|
|
}
|
|
|
|
|
2023-05-24 16:33:15 +03:00
|
|
|
// decodeLogEntry decodes string str to logEntry ent.
|
2024-11-21 20:19:39 +03:00
|
|
|
func (l *queryLog) decodeLogEntry(ctx context.Context, ent *logEntry, str string) {
|
|
|
|
const msgPrefix = "decoding log entry"
|
|
|
|
|
2020-11-20 17:32:41 +03:00
|
|
|
dec := json.NewDecoder(strings.NewReader(str))
|
2020-11-27 12:33:25 +03:00
|
|
|
dec.UseNumber()
|
2023-05-24 16:33:15 +03:00
|
|
|
|
2020-11-27 12:33:25 +03:00
|
|
|
for {
|
2020-11-20 17:32:41 +03:00
|
|
|
keyToken, err := dec.Token()
|
2020-05-26 16:24:31 +03:00
|
|
|
if err != nil {
|
2020-11-27 12:33:25 +03:00
|
|
|
if err != io.EOF {
|
2024-11-21 20:19:39 +03:00
|
|
|
l.logger.DebugContext(ctx, msgPrefix+"; token", slogutil.KeyError, err)
|
2020-11-27 12:33:25 +03:00
|
|
|
}
|
2020-12-17 13:32:46 +03:00
|
|
|
|
2020-11-20 17:32:41 +03:00
|
|
|
return
|
|
|
|
}
|
2020-12-17 13:32:46 +03:00
|
|
|
|
2020-11-20 17:32:41 +03:00
|
|
|
if _, ok := keyToken.(json.Delim); ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-07 14:32:06 +03:00
|
|
|
|
|
|
|
key, ok := keyToken.(string)
|
|
|
|
if !ok {
|
2024-11-21 20:19:39 +03:00
|
|
|
err = fmt.Errorf("%s: keyToken is %T (%[2]v) and not string", msgPrefix, keyToken)
|
|
|
|
l.logger.DebugContext(ctx, msgPrefix, slogutil.KeyError, err)
|
2020-12-17 13:32:46 +03:00
|
|
|
|
2020-12-07 14:32:06 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-17 13:32:46 +03:00
|
|
|
if key == "Result" {
|
2024-11-21 20:19:39 +03:00
|
|
|
l.decodeResult(ctx, dec, ent)
|
2020-12-17 13:32:46 +03:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-11-20 17:32:41 +03:00
|
|
|
handler, ok := logEntryHandlers[key]
|
2020-11-27 12:33:25 +03:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-07 14:32:06 +03:00
|
|
|
|
2020-11-27 12:33:25 +03:00
|
|
|
val, err := dec.Token()
|
2020-11-20 17:32:41 +03:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-12-07 14:32:06 +03:00
|
|
|
|
|
|
|
if err = handler(val, ent); err != nil {
|
2024-11-21 20:19:39 +03:00
|
|
|
l.logger.DebugContext(ctx, msgPrefix+"; handler", slogutil.KeyError, err)
|
2020-12-17 13:32:46 +03:00
|
|
|
|
2020-11-27 12:33:25 +03:00
|
|
|
return
|
2020-05-26 16:24:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-21 20:19:39 +03:00
|
|
|
|
|
|
|
// newUnexpectedDelimiterError is a helper for creating informative errors.
|
|
|
|
func newUnexpectedDelimiterError(d json.Delim) (err error) {
|
|
|
|
return fmt.Errorf("unexpected delimiter: %q", d)
|
|
|
|
}
|