2020-05-26 13:37:37 +01:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
2024-11-21 17:19:39 +00:00
|
|
|
"context"
|
2024-02-08 17:39:18 +00:00
|
|
|
"slices"
|
2020-05-26 13:37:37 +01:00
|
|
|
"strconv"
|
2021-01-28 13:00:29 +00:00
|
|
|
"strings"
|
2020-05-26 13:37:37 +01:00
|
|
|
"time"
|
|
|
|
|
2021-12-06 14:26:43 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2021-05-21 14:15:47 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2024-11-21 17:19:39 +00:00
|
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
2020-05-26 13:37:37 +01:00
|
|
|
"github.com/miekg/dns"
|
2021-06-29 11:36:52 +01:00
|
|
|
"golang.org/x/net/idna"
|
2020-05-26 13:37:37 +01:00
|
|
|
)
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
// TODO(a.garipov): Use a proper structured approach here.
|
|
|
|
|
|
|
|
// jobject is a JSON object alias.
|
2022-08-03 12:36:18 +01:00
|
|
|
type jobject = map[string]any
|
2020-12-17 10:32:46 +00:00
|
|
|
|
|
|
|
// entriesToJSON converts query log entries to JSON.
|
2024-11-21 17:19:39 +00:00
|
|
|
func (l *queryLog) entriesToJSON(
|
|
|
|
ctx context.Context,
|
2023-03-31 16:44:51 +01:00
|
|
|
entries []*logEntry,
|
|
|
|
oldest time.Time,
|
|
|
|
anonFunc aghnet.IPMutFunc,
|
|
|
|
) (res jobject) {
|
2021-12-06 14:26:43 +00:00
|
|
|
data := make([]jobject, 0, len(entries))
|
2020-05-26 13:37:37 +01:00
|
|
|
|
2021-12-06 14:26:43 +00:00
|
|
|
// The elements order is already reversed to be from newer to older.
|
|
|
|
for _, entry := range entries {
|
2024-11-21 17:19:39 +00:00
|
|
|
jsonEntry := l.entryToJSON(ctx, entry, anonFunc)
|
2020-05-26 13:37:37 +01:00
|
|
|
data = append(data, jsonEntry)
|
|
|
|
}
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
res = jobject{
|
|
|
|
"data": data,
|
|
|
|
"oldest": "",
|
|
|
|
}
|
2020-05-26 13:37:37 +01:00
|
|
|
if !oldest.IsZero() {
|
2020-12-17 10:32:46 +00:00
|
|
|
res["oldest"] = oldest.Format(time.RFC3339Nano)
|
2020-05-26 13:37:37 +01:00
|
|
|
}
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
return res
|
2020-05-26 13:37:37 +01:00
|
|
|
}
|
|
|
|
|
2021-12-13 15:06:01 +00:00
|
|
|
// entryToJSON converts a log entry's data into an entry for the JSON API.
|
2024-11-21 17:19:39 +00:00
|
|
|
func (l *queryLog) entryToJSON(
|
|
|
|
ctx context.Context,
|
|
|
|
entry *logEntry,
|
|
|
|
anonFunc aghnet.IPMutFunc,
|
|
|
|
) (jsonEntry jobject) {
|
2021-06-29 11:36:52 +01:00
|
|
|
hostname := entry.QHost
|
|
|
|
question := jobject{
|
|
|
|
"type": entry.QType,
|
|
|
|
"class": entry.QClass,
|
|
|
|
"name": hostname,
|
|
|
|
}
|
2021-12-13 15:06:01 +00:00
|
|
|
|
|
|
|
if qhost, err := idna.ToUnicode(hostname); err != nil {
|
2024-11-21 17:19:39 +00:00
|
|
|
l.logger.DebugContext(
|
|
|
|
ctx,
|
|
|
|
"translating into unicode",
|
|
|
|
"hostname", hostname,
|
|
|
|
slogutil.KeyError, err,
|
|
|
|
)
|
2021-12-13 15:06:01 +00:00
|
|
|
} else if qhost != hostname && qhost != "" {
|
|
|
|
question["unicode_name"] = qhost
|
2021-06-29 11:36:52 +01:00
|
|
|
}
|
|
|
|
|
2022-10-21 18:42:00 +01:00
|
|
|
entIP := slices.Clone(entry.IP)
|
|
|
|
anonFunc(entIP)
|
2021-12-06 14:26:43 +00:00
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
jsonEntry = jobject{
|
2020-05-29 09:15:22 +01: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 18:42:00 +01:00
|
|
|
"client": entIP,
|
2020-05-29 09:15:22 +01:00
|
|
|
"client_proto": entry.ClientProto,
|
2021-12-07 14:43:51 +00:00
|
|
|
"cached": entry.Cached,
|
2020-12-17 10:32:46 +00:00
|
|
|
"upstream": entry.Upstream,
|
2021-06-29 11:36:52 +01:00
|
|
|
"question": question,
|
2021-12-13 15:06:01 +00:00
|
|
|
"rules": resultRulesToJSONRules(entry.Result.Rules),
|
2020-05-26 13:37:37 +01:00
|
|
|
}
|
2021-12-13 15:06:01 +00:00
|
|
|
|
2022-10-21 18:42:00 +01:00
|
|
|
if entIP.Equal(entry.IP) {
|
2021-12-06 14:26:43 +00:00
|
|
|
jsonEntry["client_info"] = entry.client
|
|
|
|
}
|
2020-05-26 13:37:37 +01:00
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
if entry.ClientID != "" {
|
|
|
|
jsonEntry["client_id"] = entry.ClientID
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:52:11 +00:00
|
|
|
if entry.ReqECS != "" {
|
|
|
|
jsonEntry["ecs"] = entry.ReqECS
|
|
|
|
}
|
|
|
|
|
2021-12-13 15:06:01 +00: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 13:37:37 +01:00
|
|
|
}
|
2021-12-13 15:06:01 +00:00
|
|
|
}
|
2020-12-17 10:32:46 +00:00
|
|
|
|
2021-12-13 15:06:01 +00:00
|
|
|
if len(entry.Result.ServiceName) != 0 {
|
|
|
|
jsonEntry["service_name"] = entry.Result.ServiceName
|
2020-05-26 13:37:37 +01:00
|
|
|
}
|
|
|
|
|
2024-11-21 17:19:39 +00:00
|
|
|
l.setMsgData(ctx, entry, jsonEntry)
|
|
|
|
l.setOrigAns(ctx, entry, jsonEntry)
|
2020-12-17 10:32:46 +00:00
|
|
|
|
2021-12-13 15:06:01 +00:00
|
|
|
return jsonEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
// setMsgData sets the message data in jsonEntry.
|
2024-11-21 17:19:39 +00:00
|
|
|
func (l *queryLog) setMsgData(ctx context.Context, entry *logEntry, jsonEntry jobject) {
|
2021-12-13 15:06:01 +00:00
|
|
|
if len(entry.Answer) == 0 {
|
|
|
|
return
|
2020-05-26 13:37:37 +01:00
|
|
|
}
|
|
|
|
|
2021-12-13 15:06:01 +00:00
|
|
|
msg := &dns.Msg{}
|
|
|
|
if err := msg.Unpack(entry.Answer); err != nil {
|
2024-11-21 17:19:39 +00:00
|
|
|
l.logger.DebugContext(
|
|
|
|
ctx,
|
|
|
|
"unpacking dns message",
|
|
|
|
"answer", entry.Answer,
|
|
|
|
slogutil.KeyError, err,
|
|
|
|
)
|
2021-12-13 15:06:01 +00:00
|
|
|
|
|
|
|
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 14:18:56 +00:00
|
|
|
if a := answerToJSON(msg); a != nil {
|
2021-12-13 15:06:01 +00:00
|
|
|
jsonEntry["answer"] = a
|
2020-05-26 13:37:37 +01:00
|
|
|
}
|
2021-12-13 15:06:01 +00:00
|
|
|
}
|
2020-05-26 13:37:37 +01:00
|
|
|
|
2021-12-13 15:06:01 +00:00
|
|
|
// setOrigAns sets the original answer data in jsonEntry.
|
2024-11-21 17:19:39 +00:00
|
|
|
func (l *queryLog) setOrigAns(ctx context.Context, entry *logEntry, jsonEntry jobject) {
|
2021-12-13 15:06:01 +00:00
|
|
|
if len(entry.OrigAnswer) == 0 {
|
|
|
|
return
|
2020-05-26 13:37:37 +01:00
|
|
|
}
|
|
|
|
|
2021-12-13 15:06:01 +00:00
|
|
|
orig := &dns.Msg{}
|
|
|
|
err := orig.Unpack(entry.OrigAnswer)
|
|
|
|
if err != nil {
|
2024-11-21 17:19:39 +00:00
|
|
|
l.logger.DebugContext(
|
|
|
|
ctx,
|
|
|
|
"setting original answer",
|
|
|
|
"answer", entry.OrigAnswer,
|
|
|
|
slogutil.KeyError, err,
|
|
|
|
)
|
2021-12-13 15:06:01 +00:00
|
|
|
|
|
|
|
return
|
2020-05-26 13:37:37 +01:00
|
|
|
}
|
|
|
|
|
2023-02-27 14:18:56 +00:00
|
|
|
if a := answerToJSON(orig); a != nil {
|
2021-12-13 15:06:01 +00:00
|
|
|
jsonEntry["original_answer"] = a
|
|
|
|
}
|
2020-05-26 13:37:37 +01:00
|
|
|
}
|
|
|
|
|
2021-05-21 14:15:47 +01:00
|
|
|
func resultRulesToJSONRules(rules []*filtering.ResultRule) (jsonRules []jobject) {
|
2020-12-17 10:32:46 +00: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 13:00:29 +00:00
|
|
|
type dnsAnswer struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
TTL uint32 `json:"ttl"`
|
|
|
|
}
|
|
|
|
|
2023-02-27 14:18:56 +00: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 13:37:37 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-27 14:18:56 +00:00
|
|
|
answers = make([]*dnsAnswer, 0, len(msg.Answer))
|
|
|
|
for _, rr := range msg.Answer {
|
|
|
|
header := rr.Header()
|
|
|
|
a := &dnsAnswer{
|
2021-01-28 13:00:29 +00:00
|
|
|
Type: dns.TypeToString[header.Rrtype],
|
2023-02-27 14:18:56 +00: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 13:37:37 +01:00
|
|
|
}
|
2021-01-28 13:00:29 +00:00
|
|
|
|
2023-02-27 14:18:56 +00:00
|
|
|
answers = append(answers, a)
|
2020-05-26 13:37:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return answers
|
|
|
|
}
|