2020-05-26 15:37:37 +03:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
|
|
|
|
2023-02-27 17:18:56 +03:00
|
|
|
if a := answerToJSON(msg); a != nil {
|
2021-12-13 18:06:01 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-02-27 17:18:56 +03:00
|
|
|
if a := answerToJSON(orig); a != nil {
|
2021-12-13 18:06:01 +03:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2023-02-27 17:18:56 +03:00
|
|
|
// answerToJSON converts the answer records of msg, if any, to their JSON form.
|
|
|
|
func answerToJSON(msg *dns.Msg) (answers []*dnsAnswer) {
|
|
|
|
if msg == nil || len(msg.Answer) == 0 {
|
2020-05-26 15:37:37 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-27 17:18:56 +03:00
|
|
|
answers = make([]*dnsAnswer, 0, len(msg.Answer))
|
|
|
|
for _, rr := range msg.Answer {
|
|
|
|
header := rr.Header()
|
|
|
|
a := &dnsAnswer{
|
2021-01-28 16:00:29 +03:00
|
|
|
Type: dns.TypeToString[header.Rrtype],
|
2023-02-27 17:18:56 +03:00
|
|
|
// Remove the header string from the answer value since it's mostly
|
|
|
|
// unnecessary in the log.
|
|
|
|
Value: strings.TrimPrefix(rr.String(), header.String()),
|
|
|
|
TTL: header.Ttl,
|
2020-05-26 15:37:37 +03:00
|
|
|
}
|
2021-01-28 16:00:29 +03:00
|
|
|
|
2023-02-27 17:18:56 +03:00
|
|
|
answers = append(answers, a)
|
2020-05-26 15:37:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return answers
|
|
|
|
}
|