2020-05-26 12:37:37 +00:00
|
|
|
package querylog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2021-01-28 13:00:29 +00:00
|
|
|
"strings"
|
2020-05-26 12:37:37 +00:00
|
|
|
"time"
|
|
|
|
|
2021-12-06 14:26:43 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
|
2021-05-21 13:15:47 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
|
2020-05-26 12:37:37 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-12-06 14:26:43 +00:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2020-05-26 12:37:37 +00:00
|
|
|
"github.com/miekg/dns"
|
2021-06-29 10:36:52 +00:00
|
|
|
"golang.org/x/net/idna"
|
2020-05-26 12:37:37 +00:00
|
|
|
)
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
// TODO(a.garipov): Use a proper structured approach here.
|
|
|
|
|
|
|
|
// jobject is a JSON object alias.
|
|
|
|
type jobject = map[string]interface{}
|
|
|
|
|
|
|
|
// entriesToJSON converts query log entries to JSON.
|
|
|
|
func (l *queryLog) entriesToJSON(entries []*logEntry, oldest time.Time) (res jobject) {
|
2021-12-06 14:26:43 +00:00
|
|
|
data := make([]jobject, 0, len(entries))
|
2020-05-26 12:37:37 +00: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 {
|
|
|
|
jsonEntry := l.entryToJSON(entry, l.anonymizer.Load())
|
2020-05-26 12:37:37 +00:00
|
|
|
data = append(data, jsonEntry)
|
|
|
|
}
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
res = jobject{
|
|
|
|
"data": data,
|
|
|
|
"oldest": "",
|
|
|
|
}
|
2020-05-26 12:37:37 +00:00
|
|
|
if !oldest.IsZero() {
|
2020-12-17 10:32:46 +00:00
|
|
|
res["oldest"] = oldest.Format(time.RFC3339Nano)
|
2020-05-26 12:37:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
return res
|
2020-05-26 12:37:37 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 15:06:01 +00:00
|
|
|
// entryToJSON converts a log entry's data into an entry for the JSON API.
|
2021-12-06 14:26:43 +00:00
|
|
|
func (l *queryLog) entryToJSON(entry *logEntry, anonFunc aghnet.IPMutFunc) (jsonEntry jobject) {
|
2021-06-29 10:36:52 +00: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 {
|
|
|
|
log.Debug("querylog: translating %q into unicode: %s", hostname, err)
|
|
|
|
} else if qhost != hostname && qhost != "" {
|
|
|
|
question["unicode_name"] = qhost
|
2021-06-29 10:36:52 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 14:26:43 +00:00
|
|
|
eip := netutil.CloneIP(entry.IP)
|
|
|
|
anonFunc(eip)
|
|
|
|
|
2020-12-17 10:32:46 +00:00
|
|
|
jsonEntry = jobject{
|
2020-05-29 08:15:22 +00:00
|
|
|
"reason": entry.Result.Reason.String(),
|
|
|
|
"elapsedMs": strconv.FormatFloat(entry.Elapsed.Seconds()*1000, 'f', -1, 64),
|
|
|
|
"time": entry.Time.Format(time.RFC3339Nano),
|
2021-12-06 14:26:43 +00:00
|
|
|
"client": eip,
|
2020-05-29 08:15:22 +00: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 10:36:52 +00:00
|
|
|
"question": question,
|
2021-12-13 15:06:01 +00:00
|
|
|
"rules": resultRulesToJSONRules(entry.Result.Rules),
|
2020-05-26 12:37:37 +00:00
|
|
|
}
|
2021-12-13 15:06:01 +00:00
|
|
|
|
2021-12-06 14:26:43 +00:00
|
|
|
if eip.Equal(entry.IP) {
|
|
|
|
jsonEntry["client_info"] = entry.client
|
|
|
|
}
|
2020-05-26 12:37:37 +00: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 12:37:37 +00: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 12:37:37 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 15:06:01 +00:00
|
|
|
l.setMsgData(entry, jsonEntry)
|
|
|
|
l.setOrigAns(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.
|
|
|
|
func (l *queryLog) setMsgData(entry *logEntry, jsonEntry jobject) {
|
|
|
|
if len(entry.Answer) == 0 {
|
|
|
|
return
|
2020-05-26 12:37:37 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 15:06:01 +00: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 12:37:37 +00:00
|
|
|
}
|
2021-12-13 15:06:01 +00:00
|
|
|
}
|
2020-05-26 12:37:37 +00:00
|
|
|
|
2021-12-13 15:06:01 +00: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 12:37:37 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 15:06:01 +00: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 12:37:37 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 15:06:01 +00:00
|
|
|
if a := answerToMap(orig); a != nil {
|
|
|
|
jsonEntry["original_answer"] = a
|
|
|
|
}
|
2020-05-26 12:37:37 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 13:15:47 +00: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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func answerToMap(a *dns.Msg) (answers []*dnsAnswer) {
|
2020-05-26 12:37:37 +00:00
|
|
|
if a == nil || len(a.Answer) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-28 13:00:29 +00:00
|
|
|
answers = make([]*dnsAnswer, 0, len(a.Answer))
|
2020-05-26 12:37:37 +00:00
|
|
|
for _, k := range a.Answer {
|
|
|
|
header := k.Header()
|
2021-01-28 13:00:29 +00:00
|
|
|
answer := &dnsAnswer{
|
|
|
|
Type: dns.TypeToString[header.Rrtype],
|
|
|
|
TTL: header.Ttl,
|
2020-05-26 12:37:37 +00:00
|
|
|
}
|
2021-01-28 13:00:29 +00:00
|
|
|
|
|
|
|
// Some special treatment for some well-known types.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Consider just calling String() for everyone
|
|
|
|
// instead.
|
2020-05-26 12:37:37 +00:00
|
|
|
switch v := k.(type) {
|
2021-01-28 13:00:29 +00:00
|
|
|
case nil:
|
|
|
|
// Probably unlikely, but go on.
|
2020-05-26 12:37:37 +00:00
|
|
|
case *dns.A:
|
2021-01-28 13:00:29 +00:00
|
|
|
answer.Value = v.A.String()
|
2020-05-26 12:37:37 +00:00
|
|
|
case *dns.AAAA:
|
2021-01-28 13:00:29 +00:00
|
|
|
answer.Value = v.AAAA.String()
|
2020-05-26 12:37:37 +00:00
|
|
|
case *dns.MX:
|
2021-01-28 13:00:29 +00:00
|
|
|
answer.Value = fmt.Sprintf("%v %v", v.Preference, v.Mx)
|
2020-05-26 12:37:37 +00:00
|
|
|
case *dns.CNAME:
|
2021-01-28 13:00:29 +00:00
|
|
|
answer.Value = v.Target
|
2020-05-26 12:37:37 +00:00
|
|
|
case *dns.NS:
|
2021-01-28 13:00:29 +00:00
|
|
|
answer.Value = v.Ns
|
2020-05-26 12:37:37 +00:00
|
|
|
case *dns.SPF:
|
2021-01-28 13:00:29 +00:00
|
|
|
answer.Value = strings.Join(v.Txt, "\n")
|
2020-05-26 12:37:37 +00:00
|
|
|
case *dns.TXT:
|
2021-01-28 13:00:29 +00:00
|
|
|
answer.Value = strings.Join(v.Txt, "\n")
|
2020-05-26 12:37:37 +00:00
|
|
|
case *dns.PTR:
|
2021-01-28 13:00:29 +00:00
|
|
|
answer.Value = v.Ptr
|
2020-05-26 12:37:37 +00:00
|
|
|
case *dns.SOA:
|
2021-01-28 13:00:29 +00: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 12:37:37 +00:00
|
|
|
case *dns.CAA:
|
2021-01-28 13:00:29 +00:00
|
|
|
answer.Value = fmt.Sprintf("%v %v \"%v\"", v.Flag, v.Tag, v.Value)
|
2020-05-26 12:37:37 +00:00
|
|
|
case *dns.HINFO:
|
2021-01-28 13:00:29 +00:00
|
|
|
answer.Value = fmt.Sprintf("\"%v\" \"%v\"", v.Cpu, v.Os)
|
2020-05-26 12:37:37 +00:00
|
|
|
case *dns.RRSIG:
|
2021-01-28 13:00:29 +00: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 12:37:37 +00:00
|
|
|
default:
|
2021-01-28 13:00:29 +00:00
|
|
|
answer.Value = v.String()
|
2020-05-26 12:37:37 +00:00
|
|
|
}
|
2021-01-28 13:00:29 +00:00
|
|
|
|
2020-05-26 12:37:37 +00:00
|
|
|
answers = append(answers, answer)
|
|
|
|
}
|
|
|
|
|
|
|
|
return answers
|
|
|
|
}
|