2020-05-26 14:24:31 +01:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
2020-11-20 14:32:41 +00:00
|
|
|
"encoding/json"
|
2020-11-27 09:33:25 +00:00
|
|
|
"io"
|
2020-12-21 14:48:07 +00:00
|
|
|
"net"
|
2020-05-26 14:24:31 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-05-21 14:15:47 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2020-05-26 14:24:31 +01:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2020-12-21 14:48:07 +00:00
|
|
|
"github.com/AdguardTeam/urlfilter/rules"
|
|
|
|
"github.com/miekg/dns"
|
2020-05-26 14:24:31 +01:00
|
|
|
)
|
|
|
|
|
2022-03-03 14:52:11 +00:00
|
|
|
type logEntryHandler func(t json.Token, ent *logEntry) error
|
2020-12-07 11:32:06 +00:00
|
|
|
|
|
|
|
var logEntryHandlers = map[string]logEntryHandler{
|
2021-01-27 15:32:13 +00: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 14:32:41 +00:00
|
|
|
"IP": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2021-01-20 14:27:53 +00:00
|
|
|
|
|
|
|
if ent.IP == nil {
|
|
|
|
ent.IP = net.ParseIP(v)
|
2020-11-20 14:32:41 +00:00
|
|
|
}
|
2021-01-20 14:27:53 +00:00
|
|
|
|
2020-11-20 14:32:41 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
"T": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-07 14:43:51 +00:00
|
|
|
|
2020-11-20 14:32:41 +00:00
|
|
|
var err error
|
|
|
|
ent.Time, err = time.Parse(time.RFC3339, v)
|
2021-12-07 14:43:51 +00:00
|
|
|
|
2020-11-20 14:32:41 +00: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 14:43:51 +00:00
|
|
|
|
2020-11-20 14:32:41 +00:00
|
|
|
ent.QClass = v
|
2021-12-07 14:43:51 +00:00
|
|
|
|
2020-11-20 14:32:41 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
"CP": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-07 14:43:51 +00:00
|
|
|
|
2020-11-20 14:32:41 +00:00
|
|
|
var err error
|
|
|
|
ent.ClientProto, err = NewClientProto(v)
|
2021-12-07 14:43:51 +00:00
|
|
|
|
2020-11-20 14:32:41 +00:00
|
|
|
return err
|
|
|
|
},
|
|
|
|
"Answer": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-07 14:43:51 +00:00
|
|
|
|
2020-11-20 14:32:41 +00:00
|
|
|
var err error
|
|
|
|
ent.Answer, err = base64.StdEncoding.DecodeString(v)
|
2021-12-07 14:43:51 +00:00
|
|
|
|
2020-11-20 14:32:41 +00:00
|
|
|
return err
|
|
|
|
},
|
|
|
|
"OrigAnswer": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-07 14:43:51 +00:00
|
|
|
|
2020-11-20 14:32:41 +00:00
|
|
|
var err error
|
|
|
|
ent.OrigAnswer, err = base64.StdEncoding.DecodeString(v)
|
2021-12-07 14:43:51 +00:00
|
|
|
|
2020-11-20 14:32:41 +00:00
|
|
|
return err
|
|
|
|
},
|
2022-03-03 14:52:11 +00: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 14:43:51 +00: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 15:06:01 +00: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 10:32:46 +00:00
|
|
|
"Upstream": func(t json.Token, ent *logEntry) error {
|
2020-11-20 14:32:41 +00:00
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-07 14:43:51 +00:00
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
ent.Upstream = v
|
2021-12-07 14:43:51 +00:00
|
|
|
|
2020-11-20 14:32:41 +00:00
|
|
|
return nil
|
|
|
|
},
|
2020-12-17 10:32:46 +00:00
|
|
|
"Elapsed": func(t json.Token, ent *logEntry) error {
|
2020-11-27 09:33:25 +00:00
|
|
|
v, ok := t.(json.Number)
|
2020-11-20 14:32:41 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-07 14:43:51 +00:00
|
|
|
|
2020-11-27 09:33:25 +00:00
|
|
|
i, err := v.Int64()
|
2020-11-20 14:32:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-07 14:43:51 +00:00
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
ent.Elapsed = time.Duration(i)
|
2021-12-07 14:43:51 +00:00
|
|
|
|
2020-11-20 14:32:41 +00:00
|
|
|
return nil
|
|
|
|
},
|
2020-12-17 10:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var resultHandlers = map[string]logEntryHandler{
|
|
|
|
"IsFiltered": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(bool)
|
2020-11-20 14:32:41 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-17 10:32:46 +00:00
|
|
|
ent.Result.IsFiltered = v
|
2020-11-20 14:32:41 +00:00
|
|
|
return nil
|
|
|
|
},
|
2020-12-17 10:32:46 +00:00
|
|
|
"Rule": func(t json.Token, ent *logEntry) error {
|
|
|
|
s, ok := t.(string)
|
2020-11-20 14:32:41 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-17 10:32:46 +00:00
|
|
|
|
|
|
|
l := len(ent.Result.Rules)
|
|
|
|
if l == 0 {
|
2021-05-21 14:15:47 +01:00
|
|
|
ent.Result.Rules = []*filtering.ResultRule{{}}
|
2020-12-17 10:32:46 +00:00
|
|
|
l++
|
|
|
|
}
|
|
|
|
|
|
|
|
ent.Result.Rules[l-1].Text = s
|
|
|
|
|
2020-11-20 14:32:41 +00:00
|
|
|
return nil
|
|
|
|
},
|
2020-12-17 10:32:46 +00:00
|
|
|
"FilterID": func(t json.Token, ent *logEntry) error {
|
|
|
|
n, ok := t.(json.Number)
|
2020-11-20 14:32:41 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
2020-05-26 14:24:31 +01:00
|
|
|
}
|
2020-12-17 10:32:46 +00:00
|
|
|
|
|
|
|
i, err := n.Int64()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
l := len(ent.Result.Rules)
|
|
|
|
if l == 0 {
|
2021-05-21 14:15:47 +01:00
|
|
|
ent.Result.Rules = []*filtering.ResultRule{{}}
|
2020-12-17 10:32:46 +00:00
|
|
|
l++
|
|
|
|
}
|
|
|
|
|
|
|
|
ent.Result.Rules[l-1].FilterListID = i
|
|
|
|
|
2020-11-20 14:32:41 +00:00
|
|
|
return nil
|
|
|
|
},
|
2020-12-17 10:32:46 +00:00
|
|
|
"Reason": func(t json.Token, ent *logEntry) error {
|
2020-11-27 09:33:25 +00:00
|
|
|
v, ok := t.(json.Number)
|
2020-11-20 14:32:41 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-27 09:33:25 +00:00
|
|
|
i, err := v.Int64()
|
2020-11-20 14:32:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-21 14:15:47 +01:00
|
|
|
ent.Result.Reason = filtering.Reason(i)
|
2020-11-27 09:33:25 +00:00
|
|
|
return nil
|
|
|
|
},
|
2020-12-17 10:32:46 +00:00
|
|
|
"ServiceName": func(t json.Token, ent *logEntry) error {
|
2020-12-21 14:48:07 +00:00
|
|
|
s, ok := t.(string)
|
2020-11-20 14:32:41 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-21 14:48:07 +00:00
|
|
|
|
|
|
|
ent.Result.ServiceName = s
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
return nil
|
|
|
|
},
|
2020-12-21 14:48:07 +00:00
|
|
|
"CanonName": func(t json.Token, ent *logEntry) error {
|
|
|
|
s, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ent.Result.CanonName = s
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeResultRuleKey(key string, i int, dec *json.Decoder, ent *logEntry) {
|
|
|
|
switch key {
|
|
|
|
case "FilterListID":
|
|
|
|
vToken, err := dec.Token()
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
log.Debug("decodeResultRuleKey %s err: %s", key, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ent.Result.Rules) < i+1 {
|
2021-05-21 14:15:47 +01:00
|
|
|
ent.Result.Rules = append(ent.Result.Rules, &filtering.ResultRule{})
|
2020-12-21 14:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if n, ok := vToken.(json.Number); ok {
|
|
|
|
ent.Result.Rules[i].FilterListID, _ = n.Int64()
|
|
|
|
}
|
|
|
|
case "IP":
|
|
|
|
vToken, err := dec.Token()
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
log.Debug("decodeResultRuleKey %s err: %s", key, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ent.Result.Rules) < i+1 {
|
2021-05-21 14:15:47 +01:00
|
|
|
ent.Result.Rules = append(ent.Result.Rules, &filtering.ResultRule{})
|
2020-12-21 14:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ipStr, ok := vToken.(string); ok {
|
|
|
|
ent.Result.Rules[i].IP = net.ParseIP(ipStr)
|
|
|
|
}
|
|
|
|
case "Text":
|
|
|
|
vToken, err := dec.Token()
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
log.Debug("decodeResultRuleKey %s err: %s", key, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ent.Result.Rules) < i+1 {
|
2021-05-21 14:15:47 +01:00
|
|
|
ent.Result.Rules = append(ent.Result.Rules, &filtering.ResultRule{})
|
2020-12-21 14:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if s, ok := vToken.(string); ok {
|
|
|
|
ent.Result.Rules[i].Text = s
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Go on.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeResultRules(dec *json.Decoder, ent *logEntry) {
|
|
|
|
for {
|
|
|
|
delimToken, err := dec.Token()
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
log.Debug("decodeResultRules err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if d, ok := delimToken.(json.Delim); ok {
|
|
|
|
if d != '[' {
|
|
|
|
log.Debug("decodeResultRules: unexpected delim %q", d)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for {
|
2021-03-11 17:36:54 +00:00
|
|
|
var keyToken json.Token
|
|
|
|
keyToken, err = dec.Token()
|
2020-12-21 14:48:07 +00:00
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
log.Debug("decodeResultRules err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if d, ok := keyToken.(json.Delim); ok {
|
2021-11-23 15:01:48 +00:00
|
|
|
switch d {
|
|
|
|
case '}':
|
2020-12-21 14:48:07 +00:00
|
|
|
i++
|
2021-11-23 15:01:48 +00:00
|
|
|
case ']':
|
2020-12-21 14:48:07 +00:00
|
|
|
return
|
2021-11-23 15:01:48 +00:00
|
|
|
default:
|
|
|
|
// Go on.
|
2020-12-21 14:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
key, ok := keyToken.(string)
|
|
|
|
if !ok {
|
|
|
|
log.Debug("decodeResultRules: keyToken is %T (%[1]v) and not string", keyToken)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
decodeResultRuleKey(key, i, dec, ent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 15:01:48 +00: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.
|
2020-12-21 14:48:07 +00:00
|
|
|
func decodeResultReverseHosts(dec *json.Decoder, ent *logEntry) {
|
|
|
|
for {
|
|
|
|
itemToken, err := dec.Token()
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
log.Debug("decodeResultReverseHosts err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := itemToken.(type) {
|
|
|
|
case json.Delim:
|
|
|
|
if v == '[' {
|
|
|
|
continue
|
|
|
|
} else if v == ']' {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("decodeResultReverseHosts: unexpected delim %q", v)
|
|
|
|
|
|
|
|
return
|
|
|
|
case string:
|
2021-11-23 15:01:48 +00: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 14:48:07 +00:00
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeResultIPList(dec *json.Decoder, ent *logEntry) {
|
|
|
|
for {
|
|
|
|
itemToken, err := dec.Token()
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
log.Debug("decodeResultIPList err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := itemToken.(type) {
|
|
|
|
case json.Delim:
|
|
|
|
if v == '[' {
|
|
|
|
continue
|
|
|
|
} else if v == ']' {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("decodeResultIPList: unexpected delim %q", v)
|
|
|
|
|
|
|
|
return
|
|
|
|
case string:
|
|
|
|
ip := net.ParseIP(v)
|
|
|
|
if ip != nil {
|
|
|
|
ent.Result.IPList = append(ent.Result.IPList, ip)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 17:30:30 +00:00
|
|
|
func decodeResultDNSRewriteResultKey(key string, dec *json.Decoder, ent *logEntry) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
switch key {
|
|
|
|
case "RCode":
|
|
|
|
var vToken json.Token
|
|
|
|
vToken, err = dec.Token()
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
log.Debug("decodeResultDNSRewriteResultKey err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ent.Result.DNSRewriteResult == nil {
|
2021-05-21 14:15:47 +01:00
|
|
|
ent.Result.DNSRewriteResult = &filtering.DNSRewriteResult{}
|
2021-03-25 17:30:30 +00: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 14:15:47 +01:00
|
|
|
ent.Result.DNSRewriteResult = &filtering.DNSRewriteResult{}
|
2021-03-25 17:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ent.Result.DNSRewriteResult.Response == nil {
|
2021-05-21 14:15:47 +01:00
|
|
|
ent.Result.DNSRewriteResult.Response = filtering.DNSRewriteResultResponse{}
|
2021-03-25 17:30:30 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 15:01:48 +00: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 17:30:30 +00:00
|
|
|
err = dec.Decode(&ent.Result.DNSRewriteResult.Response)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("decodeResultDNSRewriteResultKey response err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for rrType, rrValues := range ent.Result.DNSRewriteResult.Response {
|
|
|
|
switch rrType {
|
|
|
|
case
|
|
|
|
dns.TypeA,
|
|
|
|
dns.TypeAAAA:
|
|
|
|
for i, v := range rrValues {
|
|
|
|
s, _ := v.(string)
|
|
|
|
rrValues[i] = net.ParseIP(s)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Go on.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Go on.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 14:48:07 +00:00
|
|
|
func decodeResultDNSRewriteResult(dec *json.Decoder, ent *logEntry) {
|
|
|
|
for {
|
|
|
|
keyToken, err := dec.Token()
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
log.Debug("decodeResultDNSRewriteResult err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if d, ok := keyToken.(json.Delim); ok {
|
|
|
|
if d == '}' {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
key, ok := keyToken.(string)
|
|
|
|
if !ok {
|
|
|
|
log.Debug("decodeResultDNSRewriteResult: keyToken is %T (%[1]v) and not string", keyToken)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-25 17:30:30 +00:00
|
|
|
decodeResultDNSRewriteResultKey(key, dec, ent)
|
2020-12-21 14:48:07 +00:00
|
|
|
}
|
2020-12-17 10:32:46 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 15:01:48 +00: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
|
|
|
|
if ip.To4() != nil {
|
|
|
|
qType = dns.TypeA
|
|
|
|
}
|
|
|
|
|
|
|
|
resp[qType] = append(resp[qType], ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
res.IPList = nil
|
|
|
|
}
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
func decodeResult(dec *json.Decoder, ent *logEntry) {
|
2021-11-23 15:01:48 +00:00
|
|
|
defer translateResult(ent)
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
for {
|
|
|
|
keyToken, err := dec.Token()
|
2020-11-20 14:32:41 +00:00
|
|
|
if err != nil {
|
2020-12-17 10:32:46 +00:00
|
|
|
if err != io.EOF {
|
|
|
|
log.Debug("decodeResult err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2020-11-20 14:32:41 +00:00
|
|
|
}
|
2020-12-17 10:32:46 +00:00
|
|
|
|
|
|
|
if d, ok := keyToken.(json.Delim); ok {
|
|
|
|
if d == '}' {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
2020-11-20 14:32:41 +00:00
|
|
|
}
|
2020-12-17 10:32:46 +00:00
|
|
|
|
|
|
|
key, ok := keyToken.(string)
|
|
|
|
if !ok {
|
|
|
|
log.Debug("decodeResult: keyToken is %T (%[1]v) and not string", keyToken)
|
|
|
|
|
|
|
|
return
|
2020-11-20 14:32:41 +00:00
|
|
|
}
|
2020-12-17 10:32:46 +00:00
|
|
|
|
2020-12-21 14:48:07 +00:00
|
|
|
switch key {
|
|
|
|
case "ReverseHosts":
|
|
|
|
decodeResultReverseHosts(dec, ent)
|
|
|
|
|
|
|
|
continue
|
|
|
|
case "IPList":
|
|
|
|
decodeResultIPList(dec, ent)
|
|
|
|
|
|
|
|
continue
|
|
|
|
case "Rules":
|
|
|
|
decodeResultRules(dec, ent)
|
|
|
|
|
|
|
|
continue
|
|
|
|
case "DNSRewriteResult":
|
|
|
|
decodeResultDNSRewriteResult(dec, ent)
|
|
|
|
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
// Go on.
|
|
|
|
}
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
handler, ok := resultHandlers[key]
|
2020-11-20 14:32:41 +00:00
|
|
|
if !ok {
|
2020-12-17 10:32:46 +00:00
|
|
|
continue
|
2020-11-20 14:32:41 +00:00
|
|
|
}
|
2020-12-17 10:32:46 +00:00
|
|
|
|
|
|
|
val, err := dec.Token()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = handler(val, ent); err != nil {
|
|
|
|
log.Debug("decodeResult handler err: %s", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-11-20 14:32:41 +00:00
|
|
|
}
|
2020-05-26 14:24:31 +01:00
|
|
|
|
2020-11-20 14:32:41 +00:00
|
|
|
func decodeLogEntry(ent *logEntry, str string) {
|
|
|
|
dec := json.NewDecoder(strings.NewReader(str))
|
2020-11-27 09:33:25 +00:00
|
|
|
dec.UseNumber()
|
|
|
|
for {
|
2020-11-20 14:32:41 +00:00
|
|
|
keyToken, err := dec.Token()
|
2020-05-26 14:24:31 +01:00
|
|
|
if err != nil {
|
2020-11-27 09:33:25 +00:00
|
|
|
if err != io.EOF {
|
|
|
|
log.Debug("decodeLogEntry err: %s", err)
|
|
|
|
}
|
2020-12-17 10:32:46 +00:00
|
|
|
|
2020-11-20 14:32:41 +00:00
|
|
|
return
|
|
|
|
}
|
2020-12-17 10:32:46 +00:00
|
|
|
|
2020-11-20 14:32:41 +00:00
|
|
|
if _, ok := keyToken.(json.Delim); ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-07 11:32:06 +00:00
|
|
|
|
|
|
|
key, ok := keyToken.(string)
|
|
|
|
if !ok {
|
2020-12-17 10:32:46 +00:00
|
|
|
log.Debug("decodeLogEntry: keyToken is %T (%[1]v) and not string", keyToken)
|
|
|
|
|
2020-12-07 11:32:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
if key == "Result" {
|
|
|
|
decodeResult(dec, ent)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-11-20 14:32:41 +00:00
|
|
|
handler, ok := logEntryHandlers[key]
|
2020-11-27 09:33:25 +00:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-07 11:32:06 +00:00
|
|
|
|
2020-11-27 09:33:25 +00:00
|
|
|
val, err := dec.Token()
|
2020-11-20 14:32:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-12-07 11:32:06 +00:00
|
|
|
|
|
|
|
if err = handler(val, ent); err != nil {
|
2020-12-17 10:32:46 +00:00
|
|
|
log.Debug("decodeLogEntry handler err: %s", err)
|
|
|
|
|
2020-11-27 09:33:25 +00:00
|
|
|
return
|
2020-05-26 14:24:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|