2020-05-26 13:24:31 +00: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-05-26 13:24:31 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-10-30 10:32:02 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsfilter"
|
2020-05-26 13:24:31 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
)
|
|
|
|
|
2020-12-07 11:32:06 +00:00
|
|
|
type logEntryHandler (func(t json.Token, ent *logEntry) error)
|
|
|
|
|
|
|
|
var logEntryHandlers = map[string]logEntryHandler{
|
2020-11-20 14:32:41 +00:00
|
|
|
"IP": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(ent.IP) == 0 {
|
|
|
|
ent.IP = v
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
"T": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
ent.Time, err = time.Parse(time.RFC3339, v)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
ent.QClass = v
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
"CP": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
ent.ClientProto, err = NewClientProto(v)
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
"Answer": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
ent.Answer, err = base64.StdEncoding.DecodeString(v)
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
"OrigAnswer": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
ent.OrigAnswer, err = base64.StdEncoding.DecodeString(v)
|
|
|
|
return err
|
|
|
|
},
|
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
|
|
|
|
}
|
2020-12-17 10:32:46 +00:00
|
|
|
ent.Upstream = v
|
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
|
|
|
|
}
|
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
|
|
|
|
}
|
2020-12-17 10:32:46 +00:00
|
|
|
ent.Elapsed = time.Duration(i)
|
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 {
|
|
|
|
ent.Result.Rules = []*dnsfilter.ResultRule{{}}
|
|
|
|
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 13:24:31 +00: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 {
|
|
|
|
ent.Result.Rules = []*dnsfilter.ResultRule{{}}
|
|
|
|
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
|
|
|
|
}
|
2020-12-17 10:32:46 +00:00
|
|
|
ent.Result.Reason = dnsfilter.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-11-20 14:32:41 +00:00
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-17 10:32:46 +00:00
|
|
|
ent.Result.ServiceName = v
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeResult(dec *json.Decoder, ent *logEntry) {
|
|
|
|
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
|
|
|
|
|
|
|
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 13:24:31 +00: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 13:24:31 +00: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 13:24:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get value from "key":"value"
|
|
|
|
func readJSONValue(s, name string) string {
|
|
|
|
i := strings.Index(s, "\""+name+"\":\"")
|
|
|
|
if i == -1 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
start := i + 1 + len(name) + 3
|
|
|
|
i = strings.IndexByte(s[start:], '"')
|
|
|
|
if i == -1 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
end := start + i
|
|
|
|
return s[start:end]
|
|
|
|
}
|