Skip to content

Commit

Permalink
Merge branch 'master' into AG-20200-translation-script-update-auto
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Apr 3, 2023
2 parents 69749b1 + 2a0d062 commit a6ea94b
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 199 deletions.
187 changes: 90 additions & 97 deletions internal/querylog/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,86 +166,6 @@ var logEntryHandlers = map[string]logEntryHandler{
},
}

var resultHandlers = map[string]logEntryHandler{
"IsFiltered": func(t json.Token, ent *logEntry) error {
v, ok := t.(bool)
if !ok {
return nil
}
ent.Result.IsFiltered = v
return nil
},
"Rule": func(t json.Token, ent *logEntry) error {
s, ok := t.(string)
if !ok {
return nil
}

l := len(ent.Result.Rules)
if l == 0 {
ent.Result.Rules = []*filtering.ResultRule{{}}
l++
}

ent.Result.Rules[l-1].Text = s

return nil
},
"FilterID": func(t json.Token, ent *logEntry) error {
n, ok := t.(json.Number)
if !ok {
return nil
}

i, err := n.Int64()
if err != nil {
return err
}

l := len(ent.Result.Rules)
if l == 0 {
ent.Result.Rules = []*filtering.ResultRule{{}}
l++
}

ent.Result.Rules[l-1].FilterListID = i

return nil
},
"Reason": func(t json.Token, ent *logEntry) error {
v, ok := t.(json.Number)
if !ok {
return nil
}
i, err := v.Int64()
if err != nil {
return err
}
ent.Result.Reason = filtering.Reason(i)
return nil
},
"ServiceName": func(t json.Token, ent *logEntry) error {
s, ok := t.(string)
if !ok {
return nil
}

ent.Result.ServiceName = s

return nil
},
"CanonName": func(t json.Token, ent *logEntry) error {
s, ok := t.(string)
if !ok {
return nil
}

ent.Result.CanonName = s

return nil
},
}

func decodeResultRuleKey(key string, i int, dec *json.Decoder, ent *logEntry) {
var vToken json.Token
switch key {
Expand Down Expand Up @@ -582,25 +502,11 @@ func decodeResult(dec *json.Decoder, ent *logEntry) {
return
}

switch key {
case "ReverseHosts":
decodeResultReverseHosts(dec, ent)
decHandler, ok := resultDecHandlers[key]
if ok {
decHandler(dec, ent)

continue
case "IPList":
decodeResultIPList(dec, ent)

continue
case "Rules":
decodeResultRules(dec, ent)

continue
case "DNSRewriteResult":
decodeResultDNSRewriteResult(dec, ent)

continue
default:
// Go on.
}

handler, ok := resultHandlers[key]
Expand All @@ -621,6 +527,93 @@ func decodeResult(dec *json.Decoder, ent *logEntry) {
}
}

var resultHandlers = map[string]logEntryHandler{
"IsFiltered": func(t json.Token, ent *logEntry) error {
v, ok := t.(bool)
if !ok {
return nil
}
ent.Result.IsFiltered = v
return nil
},
"Rule": func(t json.Token, ent *logEntry) error {
s, ok := t.(string)
if !ok {
return nil
}

l := len(ent.Result.Rules)
if l == 0 {
ent.Result.Rules = []*filtering.ResultRule{{}}
l++
}

ent.Result.Rules[l-1].Text = s

return nil
},
"FilterID": func(t json.Token, ent *logEntry) error {
n, ok := t.(json.Number)
if !ok {
return nil
}

i, err := n.Int64()
if err != nil {
return err
}

l := len(ent.Result.Rules)
if l == 0 {
ent.Result.Rules = []*filtering.ResultRule{{}}
l++
}

ent.Result.Rules[l-1].FilterListID = i

return nil
},
"Reason": func(t json.Token, ent *logEntry) error {
v, ok := t.(json.Number)
if !ok {
return nil
}
i, err := v.Int64()
if err != nil {
return err
}
ent.Result.Reason = filtering.Reason(i)
return nil
},
"ServiceName": func(t json.Token, ent *logEntry) error {
s, ok := t.(string)
if !ok {
return nil
}

ent.Result.ServiceName = s

return nil
},
"CanonName": func(t json.Token, ent *logEntry) error {
s, ok := t.(string)
if !ok {
return nil
}

ent.Result.CanonName = s

return nil
},
}

var resultDecHandlers = map[string]func(dec *json.Decoder, ent *logEntry){
"ReverseHosts": decodeResultReverseHosts,
"IPList": decodeResultIPList,
"Rules": decodeResultRules,
"DNSRewriteResult": decodeResultDNSRewriteResult,
}

func decodeLogEntry(ent *logEntry, str string) {
dec := json.NewDecoder(strings.NewReader(str))
dec.UseNumber()
Expand Down
70 changes: 70 additions & 0 deletions internal/querylog/entry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package querylog

import (
"net"
"time"

"github.com/AdguardTeam/AdGuardHome/internal/filtering"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/miekg/dns"
)

// logEntry represents a single entry in the file.
type logEntry struct {
// client is the found client information, if any.
client *Client

Time time.Time `json:"T"`

QHost string `json:"QH"`
QType string `json:"QT"`
QClass string `json:"QC"`

ReqECS string `json:"ECS,omitempty"`

ClientID string `json:"CID,omitempty"`
ClientProto ClientProto `json:"CP"`

Upstream string `json:",omitempty"`

Answer []byte `json:",omitempty"`
OrigAnswer []byte `json:",omitempty"`

IP net.IP `json:"IP"`

Result filtering.Result

Elapsed time.Duration

Cached bool `json:",omitempty"`
AuthenticatedData bool `json:"AD,omitempty"`
}

// shallowClone returns a shallow clone of e.
func (e *logEntry) shallowClone() (clone *logEntry) {
cloneVal := *e

return &cloneVal
}

// addResponse adds data from resp to e.Answer if resp is not nil. If isOrig is
// true, addResponse sets the e.OrigAnswer field instead of e.Answer. Any
// errors are logged.
func (e *logEntry) addResponse(resp *dns.Msg, isOrig bool) {
if resp == nil {
return
}

var err error
if isOrig {
e.Answer, err = resp.Pack()
err = errors.Annotate(err, "packing answer: %w")
} else {
e.OrigAnswer, err = resp.Pack()
err = errors.Annotate(err, "packing orig answer: %w")
}
if err != nil {
log.Error("querylog: %s", err)
}
}
Loading

0 comments on commit a6ea94b

Please sign in to comment.