2020-05-26 15:37:37 +03:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2021-01-28 16:00:29 +03:00
|
|
|
"strings"
|
2020-05-26 15:37:37 +03:00
|
|
|
"time"
|
|
|
|
|
2021-12-06 17:26:43 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2021-05-21 16:15:47 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2020-05-26 15:37:37 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
"github.com/miekg/dns"
|
2022-10-21 20:42:00 +03:00
|
|
|
"golang.org/x/exp/slices"
|
2021-06-29 13:36:52 +03:00
|
|
|
"golang.org/x/net/idna"
|
2020-05-26 15:37:37 +03:00
|
|
|
)
|
|
|
|
|
2020-12-17 13:32:46 +03:00
|
|
|
// TODO(a.garipov): Use a proper structured approach here.
|
|
|
|
|
|
|
|
// jobject is a JSON object alias.
|
2022-08-03 14:36:18 +03:00
|
|
|
type jobject = map[string]any
|
2020-12-17 13:32:46 +03:00
|
|
|
|
|
|
|
// entriesToJSON converts query log entries to JSON.
|
|
|
|
func (l *queryLog) entriesToJSON(entries []*logEntry, oldest time.Time) (res jobject) {
|
2021-12-06 17:26:43 +03:00
|
|
|
data := make([]jobject, 0, len(entries))
|
2020-05-26 15:37:37 +03:00
|
|
|
|
2021-12-06 17:26:43 +03:00
|
|
|
// The elements order is already reversed to be from newer to older.
|
|
|
|
for _, entry := range entries {
|
|
|
|
jsonEntry := l.entryToJSON(entry, l.anonymizer.Load())
|
2020-05-26 15:37:37 +03:00
|
|
|
data = append(data, jsonEntry)
|
|
|
|
}
|
|
|
|
|
2020-12-17 13:32:46 +03:00
|
|
|
res = jobject{
|
|
|
|
"data": data,
|
|
|
|
"oldest": "",
|
|
|
|
}
|
2020-05-26 15:37:37 +03:00
|
|
|
if !oldest.IsZero() {
|
2020-12-17 13:32:46 +03:00
|
|
|
res["oldest"] = oldest.Format(time.RFC3339Nano)
|
2020-05-26 15:37:37 +03:00
|
|
|
}
|
|
|
|
|
2020-12-17 13:32:46 +03:00
|
|
|
return res
|
2020-05-26 15:37:37 +03:00
|
|
|
}
|
|
|
|
|
2021-12-13 18:06:01 +03:00
|
|
|
// entryToJSON converts a log entry's data into an entry for the JSON API.
|
2021-12-06 17:26:43 +03:00
|
|
|
func (l *queryLog) entryToJSON(entry *logEntry, anonFunc aghnet.IPMutFunc) (jsonEntry jobject) {
|
2021-06-29 13:36:52 +03:00
|
|
|
hostname := entry.QHost
|
|
|
|
question := jobject{
|
|
|
|
"type": entry.QType,
|
|
|
|
"class": entry.QClass,
|
|
|
|
"name": hostname,
|
|
|
|
}
|
2021-12-13 18:06:01 +03:00
|
|
|
|
|
|
|
if qhost, err := idna.ToUnicode(hostname); err != nil {
|
|
|
|
log.Debug("querylog: translating %q into unicode: %s", hostname, err)
|
|
|
|
} else if qhost != hostname && qhost != "" {
|
|
|
|
question["unicode_name"] = qhost
|
2021-06-29 13:36:52 +03:00
|
|
|
}
|
|
|
|
|
2022-10-21 20:42:00 +03:00
|
|
|
entIP := slices.Clone(entry.IP)
|
|
|
|
anonFunc(entIP)
|
2021-12-06 17:26:43 +03:00
|
|
|
|
2020-12-17 13:32:46 +03:00
|
|
|
jsonEntry = jobject{
|
2020-05-29 11:15:22 +03:00
|
|
|
"reason": entry.Result.Reason.String(),
|
|
|
|
"elapsedMs": strconv.FormatFloat(entry.Elapsed.Seconds()*1000, 'f', -1, 64),
|
|
|
|
"time": entry.Time.Format(time.RFC3339Nano),
|
2022-10-21 20:42:00 +03:00
|
|
|
"client": entIP,
|
2020-05-29 11:15:22 +03:00
|
|
|
"client_proto": entry.ClientProto,
|
2021-12-07 17:43:51 +03:00
|
|
|
"cached": entry.Cached,
|
2020-12-17 13:32:46 +03:00
|
|
|
"upstream": entry.Upstream,
|
2021-06-29 13:36:52 +03:00
|
|
|
"question": question,
|
2021-12-13 18:06:01 +03:00
|
|
|
"rules": resultRulesToJSONRules(entry.Result.Rules),
|
2020-05-26 15:37:37 +03:00
|
|
|
}
|
2021-12-13 18:06:01 +03:00
|
|
|
|
2022-10-21 20:42:00 +03:00
|
|
|
if entIP.Equal(entry.IP) {
|
2021-12-06 17:26:43 +03:00
|
|
|
jsonEntry["client_info"] = entry.client
|
|
|
|
}
|
2020-05-26 15:37:37 +03:00
|
|
|
|
2021-01-27 18:32:13 +03:00
|
|
|
if entry.ClientID != "" {
|
|
|
|
jsonEntry["client_id"] = entry.ClientID
|
|
|
|
}
|
|
|
|
|
2022-03-03 17:52:11 +03:00
|
|
|
if entry.ReqECS != "" {
|
|
|
|
jsonEntry["ecs"] = entry.ReqECS
|
|
|
|
}
|
|
|
|
|
2021-12-13 18:06:01 +03:00
|
|
|
if len(entry.Result.Rules) > 0 {
|
|
|
|
if r := entry.Result.Rules[0]; len(r.Text) > 0 {
|
|
|
|
jsonEntry["rule"] = r.Text
|
|
|
|
jsonEntry["filterId"] = r.FilterListID
|
2020-05-26 15:37:37 +03:00
|
|
|
}
|
2021-12-13 18:06:01 +03:00
|
|
|
}
|
2020-12-17 13:32:46 +03:00
|
|
|
|
2021-12-13 18:06:01 +03:00
|
|
|
if len(entry.Result.ServiceName) != 0 {
|
|
|
|
jsonEntry["service_name"] = entry.Result.ServiceName
|
2020-05-26 15:37:37 +03:00
|
|
|
}
|
|
|
|
|
2021-12-13 18:06:01 +03:00
|
|
|
l.setMsgData(entry, jsonEntry)
|
|
|
|
l.setOrigAns(entry, jsonEntry)
|
2020-12-17 13:32:46 +03:00
|
|
|
|
2021-12-13 18:06:01 +03:00
|
|
|
return jsonEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
// setMsgData sets the message data in jsonEntry.
|
|
|
|
func (l *queryLog) setMsgData(entry *logEntry, jsonEntry jobject) {
|
|
|
|
if len(entry.Answer) == 0 {
|
|
|
|
return
|
2020-05-26 15:37:37 +03:00
|
|
|
}
|
|
|
|
|
2021-12-13 18:06:01 +03:00
|
|
|
msg := &dns.Msg{}
|
|
|
|
if err := msg.Unpack(entry.Answer); err != nil {
|
|
|
|
log.Debug("querylog: failed to unpack dns msg answer: %v: %s", entry.Answer, err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonEntry["status"] = dns.RcodeToString[msg.Rcode]
|
|
|
|
// Old query logs may still keep AD flag value in the message. Try to get
|
|
|
|
// it from there as well.
|
|
|
|
jsonEntry["answer_dnssec"] = entry.AuthenticatedData || msg.AuthenticatedData
|
|
|
|
|
|
|
|
if a := answerToMap(msg); a != nil {
|
|
|
|
jsonEntry["answer"] = a
|
2020-05-26 15:37:37 +03:00
|
|
|
}
|
2021-12-13 18:06:01 +03:00
|
|
|
}
|
2020-05-26 15:37:37 +03:00
|
|
|
|
2021-12-13 18:06:01 +03:00
|
|
|
// setOrigAns sets the original answer data in jsonEntry.
|
|
|
|
func (l *queryLog) setOrigAns(entry *logEntry, jsonEntry jobject) {
|
|
|
|
if len(entry.OrigAnswer) == 0 {
|
|
|
|
return
|
2020-05-26 15:37:37 +03:00
|
|
|
}
|
|
|
|
|
2021-12-13 18:06:01 +03:00
|
|
|
orig := &dns.Msg{}
|
|
|
|
err := orig.Unpack(entry.OrigAnswer)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug("querylog: orig.Unpack(entry.OrigAnswer): %v: %s", entry.OrigAnswer, err)
|
|
|
|
|
|
|
|
return
|
2020-05-26 15:37:37 +03:00
|
|
|
}
|
|
|
|
|
2021-12-13 18:06:01 +03:00
|
|
|
if a := answerToMap(orig); a != nil {
|
|
|
|
jsonEntry["original_answer"] = a
|
|
|
|
}
|
2020-05-26 15:37:37 +03:00
|
|
|
}
|
|
|
|
|
2021-05-21 16:15:47 +03:00
|
|
|
func resultRulesToJSONRules(rules []*filtering.ResultRule) (jsonRules []jobject) {
|
2020-12-17 13:32:46 +03:00
|
|
|
jsonRules = make([]jobject, len(rules))
|
|
|
|
for i, r := range rules {
|
|
|
|
jsonRules[i] = jobject{
|
|
|
|
"filter_list_id": r.FilterListID,
|
|
|
|
"text": r.Text,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return jsonRules
|
|
|
|
}
|
|
|
|
|
2021-01-28 16:00:29 +03:00
|
|
|
type dnsAnswer struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
TTL uint32 `json:"ttl"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func answerToMap(a *dns.Msg) (answers []*dnsAnswer) {
|
2020-05-26 15:37:37 +03:00
|
|
|
if a == nil || len(a.Answer) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-28 16:00:29 +03:00
|
|
|
answers = make([]*dnsAnswer, 0, len(a.Answer))
|
2020-05-26 15:37:37 +03:00
|
|
|
for _, k := range a.Answer {
|
|
|
|
header := k.Header()
|
2021-01-28 16:00:29 +03:00
|
|
|
answer := &dnsAnswer{
|
|
|
|
Type: dns.TypeToString[header.Rrtype],
|
|
|
|
TTL: header.Ttl,
|
2020-05-26 15:37:37 +03:00
|
|
|
}
|
2021-01-28 16:00:29 +03:00
|
|
|
|
|
|
|
// Some special treatment for some well-known types.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Consider just calling String() for everyone
|
|
|
|
// instead.
|
2020-05-26 15:37:37 +03:00
|
|
|
switch v := k.(type) {
|
2021-01-28 16:00:29 +03:00
|
|
|
case nil:
|
|
|
|
// Probably unlikely, but go on.
|
2020-05-26 15:37:37 +03:00
|
|
|
case *dns.A:
|
2021-01-28 16:00:29 +03:00
|
|
|
answer.Value = v.A.String()
|
2020-05-26 15:37:37 +03:00
|
|
|
case *dns.AAAA:
|
2021-01-28 16:00:29 +03:00
|
|
|
answer.Value = v.AAAA.String()
|
2020-05-26 15:37:37 +03:00
|
|
|
case *dns.MX:
|
2021-01-28 16:00:29 +03:00
|
|
|
answer.Value = fmt.Sprintf("%v %v", v.Preference, v.Mx)
|
2020-05-26 15:37:37 +03:00
|
|
|
case *dns.CNAME:
|
2021-01-28 16:00:29 +03:00
|
|
|
answer.Value = v.Target
|
2020-05-26 15:37:37 +03:00
|
|
|
case *dns.NS:
|
2021-01-28 16:00:29 +03:00
|
|
|
answer.Value = v.Ns
|
2020-05-26 15:37:37 +03:00
|
|
|
case *dns.SPF:
|
2021-01-28 16:00:29 +03:00
|
|
|
answer.Value = strings.Join(v.Txt, "\n")
|
2020-05-26 15:37:37 +03:00
|
|
|
case *dns.TXT:
|
2021-01-28 16:00:29 +03:00
|
|
|
answer.Value = strings.Join(v.Txt, "\n")
|
2020-05-26 15:37:37 +03:00
|
|
|
case *dns.PTR:
|
2021-01-28 16:00:29 +03:00
|
|
|
answer.Value = v.Ptr
|
2020-05-26 15:37:37 +03:00
|
|
|
case *dns.SOA:
|
2021-01-28 16:00:29 +03:00
|
|
|
answer.Value = fmt.Sprintf("%v %v %v %v %v %v %v", v.Ns, v.Mbox, v.Serial, v.Refresh, v.Retry, v.Expire, v.Minttl)
|
2020-05-26 15:37:37 +03:00
|
|
|
case *dns.CAA:
|
2021-01-28 16:00:29 +03:00
|
|
|
answer.Value = fmt.Sprintf("%v %v \"%v\"", v.Flag, v.Tag, v.Value)
|
2020-05-26 15:37:37 +03:00
|
|
|
case *dns.HINFO:
|
2021-01-28 16:00:29 +03:00
|
|
|
answer.Value = fmt.Sprintf("\"%v\" \"%v\"", v.Cpu, v.Os)
|
2020-05-26 15:37:37 +03:00
|
|
|
case *dns.RRSIG:
|
2021-01-28 16:00:29 +03:00
|
|
|
answer.Value = fmt.Sprintf("%v %v %v %v %v %v %v %v %v", dns.TypeToString[v.TypeCovered], v.Algorithm, v.Labels, v.OrigTtl, v.Expiration, v.Inception, v.KeyTag, v.SignerName, v.Signature)
|
2020-05-26 15:37:37 +03:00
|
|
|
default:
|
2021-01-28 16:00:29 +03:00
|
|
|
answer.Value = v.String()
|
2020-05-26 15:37:37 +03:00
|
|
|
}
|
2021-01-28 16:00:29 +03:00
|
|
|
|
2020-05-26 15:37:37 +03:00
|
|
|
answers = append(answers, answer)
|
|
|
|
}
|
|
|
|
|
|
|
|
return answers
|
|
|
|
}
|