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"
|
2021-12-06 17:26:43 +03:00
|
|
|
"github.com/AdguardTeam/golibs/netutil"
|
2020-05-26 15:37:37 +03:00
|
|
|
"github.com/miekg/dns"
|
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.
|
|
|
|
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 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-06 17:26:43 +03:00
|
|
|
func (l *queryLog) entryToJSON(entry *logEntry, anonFunc aghnet.IPMutFunc) (jsonEntry jobject) {
|
2020-05-26 15:37:37 +03:00
|
|
|
var msg *dns.Msg
|
|
|
|
|
|
|
|
if len(entry.Answer) > 0 {
|
|
|
|
msg = new(dns.Msg)
|
|
|
|
if err := msg.Unpack(entry.Answer); err != nil {
|
|
|
|
log.Debug("Failed to unpack dns message answer: %s: %s", err, string(entry.Answer))
|
|
|
|
msg = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 13:36:52 +03:00
|
|
|
hostname := entry.QHost
|
|
|
|
question := jobject{
|
|
|
|
"type": entry.QType,
|
|
|
|
"class": entry.QClass,
|
|
|
|
"name": hostname,
|
|
|
|
}
|
|
|
|
if qhost, err := idna.ToUnicode(hostname); err == nil {
|
|
|
|
if qhost != hostname && qhost != "" {
|
|
|
|
question["unicode_name"] = qhost
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Debug("translating %q into unicode: %s", hostname, err)
|
|
|
|
}
|
|
|
|
|
2021-12-06 17:26:43 +03:00
|
|
|
eip := netutil.CloneIP(entry.IP)
|
|
|
|
anonFunc(eip)
|
|
|
|
|
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),
|
2021-12-06 17:26:43 +03:00
|
|
|
"client": eip,
|
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,
|
2020-05-26 15:37:37 +03:00
|
|
|
}
|
2021-12-06 17:26:43 +03:00
|
|
|
if eip.Equal(entry.IP) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-26 15:37:37 +03:00
|
|
|
if msg != nil {
|
|
|
|
jsonEntry["status"] = dns.RcodeToString[msg.Rcode]
|
|
|
|
|
|
|
|
opt := msg.IsEdns0()
|
|
|
|
dnssecOk := false
|
|
|
|
if opt != nil {
|
|
|
|
dnssecOk = opt.Do()
|
|
|
|
}
|
2020-12-17 13:32:46 +03:00
|
|
|
|
2020-05-26 15:37:37 +03:00
|
|
|
jsonEntry["answer_dnssec"] = dnssecOk
|
|
|
|
}
|
|
|
|
|
2020-12-17 13:32:46 +03:00
|
|
|
jsonEntry["rules"] = resultRulesToJSONRules(entry.Result.Rules)
|
|
|
|
|
|
|
|
if len(entry.Result.Rules) > 0 && len(entry.Result.Rules[0].Text) > 0 {
|
|
|
|
jsonEntry["rule"] = entry.Result.Rules[0].Text
|
|
|
|
jsonEntry["filterId"] = entry.Result.Rules[0].FilterListID
|
2020-05-26 15:37:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(entry.Result.ServiceName) != 0 {
|
|
|
|
jsonEntry["service_name"] = entry.Result.ServiceName
|
|
|
|
}
|
|
|
|
|
|
|
|
answers := answerToMap(msg)
|
|
|
|
if answers != nil {
|
|
|
|
jsonEntry["answer"] = answers
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(entry.OrigAnswer) != 0 {
|
|
|
|
a := new(dns.Msg)
|
|
|
|
err := a.Unpack(entry.OrigAnswer)
|
|
|
|
if err == nil {
|
|
|
|
answers = answerToMap(a)
|
|
|
|
if answers != nil {
|
|
|
|
jsonEntry["original_answer"] = answers
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Debug("Querylog: msg.Unpack(entry.OrigAnswer): %s: %s", err, string(entry.OrigAnswer))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return jsonEntry
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|