2020-05-26 06:24:31 -07:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
2020-11-20 07:32:41 -07:00
|
|
|
"encoding/json"
|
2020-11-27 02:33:25 -07:00
|
|
|
"io"
|
2020-05-26 06:24:31 -07:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-10-30 03:32:02 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/dnsfilter"
|
2020-05-26 06:24:31 -07:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
2020-12-07 04:32:06 -07:00
|
|
|
type logEntryHandler (func(t json.Token, ent *logEntry) error)
|
|
|
|
|
|
|
|
var logEntryHandlers = map[string]logEntryHandler{
|
2020-11-20 07:32:41 -07: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
|
|
|
|
},
|
|
|
|
"IsFiltered": func(t json.Token, ent *logEntry) error {
|
2020-11-27 02:33:25 -07:00
|
|
|
v, ok := t.(bool)
|
2020-11-20 07:32:41 -07:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-27 02:33:25 -07:00
|
|
|
ent.Result.IsFiltered = v
|
2020-11-20 07:32:41 -07:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
"Rule": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ent.Result.Rule = v
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
"FilterID": func(t json.Token, ent *logEntry) error {
|
2020-11-27 02:33:25 -07:00
|
|
|
v, ok := t.(json.Number)
|
2020-11-20 07:32:41 -07:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-27 02:33:25 -07:00
|
|
|
i, err := v.Int64()
|
2020-11-20 07:32:41 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-27 02:33:25 -07:00
|
|
|
ent.Result.FilterID = i
|
2020-11-20 07:32:41 -07:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
"Reason": func(t json.Token, ent *logEntry) error {
|
2020-11-27 02:33:25 -07:00
|
|
|
v, ok := t.(json.Number)
|
2020-11-20 07:32:41 -07:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-27 02:33:25 -07:00
|
|
|
i, err := v.Int64()
|
2020-11-20 07:32:41 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ent.Result.Reason = dnsfilter.Reason(i)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
"ServiceName": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ent.Result.ServiceName = v
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
"Upstream": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
2020-05-26 06:24:31 -07:00
|
|
|
}
|
2020-11-20 07:32:41 -07:00
|
|
|
ent.Upstream = v
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
"Elapsed": func(t json.Token, ent *logEntry) error {
|
2020-11-27 02:33:25 -07:00
|
|
|
v, ok := t.(json.Number)
|
2020-11-20 07:32:41 -07:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-27 02:33:25 -07:00
|
|
|
i, err := v.Int64()
|
2020-11-20 07:32:41 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ent.Elapsed = time.Duration(i)
|
|
|
|
return nil
|
|
|
|
},
|
2020-11-27 02:33:25 -07:00
|
|
|
"Result": func(json.Token, *logEntry) error {
|
|
|
|
return nil
|
|
|
|
},
|
2020-11-20 07:32:41 -07:00
|
|
|
"Question": func(t json.Token, ent *logEntry) error {
|
|
|
|
v, ok := t.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var qstr []byte
|
|
|
|
qstr, err := base64.StdEncoding.DecodeString(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
q := new(dns.Msg)
|
|
|
|
err = q.Unpack(qstr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ent.QHost = q.Question[0].Name
|
|
|
|
if len(ent.QHost) == 0 {
|
|
|
|
return nil // nil???
|
|
|
|
}
|
|
|
|
ent.QHost = ent.QHost[:len(ent.QHost)-1]
|
|
|
|
ent.QType = dns.TypeToString[q.Question[0].Qtype]
|
|
|
|
ent.QClass = dns.ClassToString[q.Question[0].Qclass]
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
"Time": 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
|
|
|
|
},
|
|
|
|
}
|
2020-05-26 06:24:31 -07:00
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
func decodeLogEntry(ent *logEntry, str string) {
|
|
|
|
dec := json.NewDecoder(strings.NewReader(str))
|
2020-11-27 02:33:25 -07:00
|
|
|
dec.UseNumber()
|
|
|
|
for {
|
2020-11-20 07:32:41 -07:00
|
|
|
keyToken, err := dec.Token()
|
2020-05-26 06:24:31 -07:00
|
|
|
if err != nil {
|
2020-11-27 02:33:25 -07:00
|
|
|
if err != io.EOF {
|
|
|
|
log.Debug("decodeLogEntry err: %s", err)
|
|
|
|
}
|
2020-11-20 07:32:41 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := keyToken.(json.Delim); ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-07 04:32:06 -07:00
|
|
|
|
|
|
|
key, ok := keyToken.(string)
|
|
|
|
if !ok {
|
|
|
|
log.Debug("decodeLogEntry: keyToken is %T and not string", keyToken)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-20 07:32:41 -07:00
|
|
|
handler, ok := logEntryHandlers[key]
|
2020-11-27 02:33:25 -07:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-07 04:32:06 -07:00
|
|
|
|
2020-11-27 02:33:25 -07:00
|
|
|
val, err := dec.Token()
|
2020-11-20 07:32:41 -07:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-12-07 04:32:06 -07:00
|
|
|
|
|
|
|
if err = handler(val, ent); err != nil {
|
2020-11-27 02:33:25 -07:00
|
|
|
log.Debug("decodeLogEntry err: %s", err)
|
|
|
|
return
|
2020-05-26 06:24:31 -07: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]
|
|
|
|
}
|