Pull request 1955: Upd libraries
Merge in DNS/adguard-home from upd-libs to master
Squashed commit of the following:
commit 72f4fb7d5ab8eaecb917a843dec56e3fa38b725c
Merge: 8defe106f 111005b8d
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Thu Aug 10 19:50:33 2023 +0300
Merge branch 'master' into upd-libs
commit 8defe106fc982ba916f42d5722f1cc0152042d04
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Thu Aug 10 19:38:01 2023 +0300
dnsforward: revert behavior
commit 8ccb2f675373772f8a6f829b80b317fda0c5f730
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Wed Aug 9 18:35:12 2023 +0300
all: imp code
commit a3112d3438a466bae515e56a6ee97394066ba481
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Wed Aug 9 17:26:52 2023 +0300
filtering: revert type
commit 56d2528fb4c8ee5504de0c6ec24050d63c8e6330
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Wed Aug 9 14:57:04 2023 +0300
stats: fix sort
commit 0dbb446602b7536df508a8e53c4a532455c9d064
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Wed Aug 9 14:52:28 2023 +0300
all: upd golibs & dnsproxy
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/AdguardTeam/golibs/errors"
|
||||
"github.com/AdguardTeam/golibs/log"
|
||||
@@ -99,8 +100,8 @@ func (s *server) dbStore() (err error) {
|
||||
func writeDB(path string, leases []*Lease) (err error) {
|
||||
defer func() { err = errors.Annotate(err, "writing db: %w") }()
|
||||
|
||||
slices.SortFunc(leases, func(a, b *Lease) bool {
|
||||
return a.Hostname < b.Hostname
|
||||
slices.SortFunc(leases, func(a, b *Lease) (res int) {
|
||||
return strings.Compare(a.Hostname, b.Hostname)
|
||||
})
|
||||
|
||||
dl := &dataLeases{
|
||||
|
||||
@@ -254,28 +254,38 @@ func sortNetIPAddrs(addrs []net.IP, preferIPv6 bool) {
|
||||
return
|
||||
}
|
||||
|
||||
slices.SortStableFunc(addrs, func(addrA, addrB net.IP) (sortsBefore bool) {
|
||||
slices.SortStableFunc(addrs, func(addrA, addrB net.IP) (res int) {
|
||||
switch len(addrA) {
|
||||
case net.IPv4len, net.IPv6len:
|
||||
switch len(addrB) {
|
||||
case net.IPv4len, net.IPv6len:
|
||||
// Go on.
|
||||
default:
|
||||
return true
|
||||
return -1
|
||||
}
|
||||
default:
|
||||
return false
|
||||
return 1
|
||||
}
|
||||
|
||||
if aIs4, bIs4 := addrA.To4() != nil, addrB.To4() != nil; aIs4 != bIs4 {
|
||||
if aIs4 {
|
||||
return !preferIPv6
|
||||
// Treat IPv6-mapped IPv4 addresses as IPv6 addresses.
|
||||
aIs4, bIs4 := addrA.To4() != nil, addrB.To4() != nil
|
||||
if aIs4 == bIs4 {
|
||||
return bytes.Compare(addrA, addrB)
|
||||
}
|
||||
|
||||
if aIs4 {
|
||||
if preferIPv6 {
|
||||
return 1
|
||||
}
|
||||
|
||||
return preferIPv6
|
||||
return -1
|
||||
}
|
||||
|
||||
return bytes.Compare(addrA, addrB) < 0
|
||||
if preferIPv6 {
|
||||
return -1
|
||||
}
|
||||
|
||||
return 1
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ func TestChcker_getQuestion(t *testing.T) {
|
||||
hashes := hostnameToHashes("1.2.3.sub.host.com")
|
||||
assert.Len(t, hashes, 3)
|
||||
|
||||
hash := sha256.Sum256([]byte("3.sub.host.com"))
|
||||
hash := hostnameHash(sha256.Sum256([]byte("3.sub.host.com")))
|
||||
hexPref1 := hex.EncodeToString(hash[:prefixLen])
|
||||
assert.True(t, slices.Contains(hashes, hash))
|
||||
|
||||
@@ -105,7 +105,7 @@ func TestChecker_storeInCache(t *testing.T) {
|
||||
// store in cache hashes for "3.sub.host.com" and "host.com"
|
||||
// and empty data for hash-prefix for "sub.host.com"
|
||||
hashes := []hostnameHash{}
|
||||
hash := sha256.Sum256([]byte("sub.host.com"))
|
||||
hash := hostnameHash(sha256.Sum256([]byte("sub.host.com")))
|
||||
hashes = append(hashes, hash)
|
||||
var hashesArray []hostnameHash
|
||||
hash4 := sha256.Sum256([]byte("3.sub.host.com"))
|
||||
|
||||
@@ -122,24 +122,29 @@ func matchDomainWildcard(host, wildcard string) (ok bool) {
|
||||
return isWildcard(wildcard) && strings.HasSuffix(host, wildcard[1:])
|
||||
}
|
||||
|
||||
// legacyRewriteSortsBefore sorts rewrites according to the following priority:
|
||||
// Compare is used to sort rewrites according to the following priority:
|
||||
//
|
||||
// 1. A and AAAA > CNAME;
|
||||
// 2. wildcard > exact;
|
||||
// 3. lower level wildcard > higher level wildcard;
|
||||
func legacyRewriteSortsBefore(a, b *LegacyRewrite) (sortsBefore bool) {
|
||||
if a.Type == dns.TypeCNAME && b.Type != dns.TypeCNAME {
|
||||
return true
|
||||
} else if a.Type != dns.TypeCNAME && b.Type == dns.TypeCNAME {
|
||||
return false
|
||||
func (rw *LegacyRewrite) Compare(b *LegacyRewrite) (res int) {
|
||||
if rw.Type == dns.TypeCNAME && b.Type != dns.TypeCNAME {
|
||||
return -1
|
||||
} else if rw.Type != dns.TypeCNAME && b.Type == dns.TypeCNAME {
|
||||
return 1
|
||||
}
|
||||
|
||||
if aIsWld, bIsWld := isWildcard(a.Domain), isWildcard(b.Domain); aIsWld != bIsWld {
|
||||
return bIsWld
|
||||
aIsWld, bIsWld := isWildcard(rw.Domain), isWildcard(b.Domain)
|
||||
if aIsWld == bIsWld {
|
||||
// Both are either wildcards or both aren't.
|
||||
return len(rw.Domain) - len(b.Domain)
|
||||
}
|
||||
|
||||
// Both are either wildcards or both aren't.
|
||||
return len(a.Domain) > len(b.Domain)
|
||||
if aIsWld {
|
||||
return 1
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
|
||||
// prepareRewrites normalizes and validates all legacy DNS rewrites.
|
||||
@@ -181,7 +186,7 @@ func findRewrites(
|
||||
return nil, matched
|
||||
}
|
||||
|
||||
slices.SortFunc(rewrites, legacyRewriteSortsBefore)
|
||||
slices.SortFunc(rewrites, (*LegacyRewrite).Compare)
|
||||
|
||||
for i, r := range rewrites {
|
||||
if isWildcard(r.Domain) {
|
||||
|
||||
@@ -290,8 +290,8 @@ func (clients *clientsContainer) forConfig() (objs []*clientObject) {
|
||||
// above loop can generate different orderings when writing to the config
|
||||
// file: this produces lots of diffs in config files, so sort objects by
|
||||
// name before writing.
|
||||
slices.SortStableFunc(objs, func(a, b *clientObject) (sortsBefore bool) {
|
||||
return a.Name < b.Name
|
||||
slices.SortStableFunc(objs, func(a, b *clientObject) (res int) {
|
||||
return strings.Compare(a.Name, b.Name)
|
||||
})
|
||||
|
||||
return objs
|
||||
@@ -907,7 +907,9 @@ func (clients *clientsContainer) addFromSystemARP() {
|
||||
// the persistent clients.
|
||||
func (clients *clientsContainer) close() (err error) {
|
||||
persistent := maps.Values(clients.list)
|
||||
slices.SortFunc(persistent, func(a, b *Client) (less bool) { return a.Name < b.Name })
|
||||
slices.SortFunc(persistent, func(a, b *Client) (res int) {
|
||||
return strings.Compare(a.Name, b.Name)
|
||||
})
|
||||
|
||||
var errs []error
|
||||
|
||||
|
||||
@@ -309,8 +309,8 @@ func addOption(flags *flag.FlagSet, fieldPtr any, o *commandLineOption) {
|
||||
// value hints.
|
||||
func usage(cmdName string, output io.Writer) {
|
||||
options := slices.Clone(commandLineOptions)
|
||||
slices.SortStableFunc(options, func(a, b *commandLineOption) (sortsBefore bool) {
|
||||
return a.long < b.long
|
||||
slices.SortStableFunc(options, func(a, b *commandLineOption) (res int) {
|
||||
return strings.Compare(a.long, b.long)
|
||||
})
|
||||
|
||||
b := &strings.Builder{}
|
||||
|
||||
@@ -107,8 +107,8 @@ func (l *queryLog) search(params *searchParams) (entries []*logEntry, oldest tim
|
||||
// weird on the frontend.
|
||||
//
|
||||
// See https://github.com/AdguardTeam/AdGuardHome/issues/2293.
|
||||
slices.SortStableFunc(entries, func(a, b *logEntry) (sortsBefore bool) {
|
||||
return a.Time.After(b.Time)
|
||||
slices.SortStableFunc(entries, func(a, b *logEntry) (res int) {
|
||||
return -a.Time.Compare(b.Time)
|
||||
})
|
||||
|
||||
if params.offset > 0 {
|
||||
|
||||
@@ -221,15 +221,25 @@ func unitNameToID(name []byte) (id uint32, ok bool) {
|
||||
return uint32(binary.BigEndian.Uint64(name)), true
|
||||
}
|
||||
|
||||
// compareCount used to sort countPair by Count in descending order.
|
||||
func (a countPair) compareCount(b countPair) (res int) {
|
||||
switch x, y := a.Count, b.Count; {
|
||||
case x > y:
|
||||
return -1
|
||||
case x < y:
|
||||
return +1
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func convertMapToSlice(m map[string]uint64, max int) (s []countPair) {
|
||||
s = make([]countPair, 0, len(m))
|
||||
for k, v := range m {
|
||||
s = append(s, countPair{Name: k, Count: v})
|
||||
}
|
||||
|
||||
slices.SortFunc(s, func(a, b countPair) (sortsBefore bool) {
|
||||
return a.Count > b.Count
|
||||
})
|
||||
slices.SortFunc(s, countPair.compareCount)
|
||||
if max > len(s) {
|
||||
max = len(s)
|
||||
}
|
||||
@@ -589,8 +599,15 @@ func prepareTopUpstreamsAvgTime(
|
||||
) (topUpstreamsAvgTime []topAddrsFloat) {
|
||||
keys := maps.Keys(upstreamsAvgTime)
|
||||
|
||||
slices.SortFunc(keys, func(a, b string) (sortsBefore bool) {
|
||||
return upstreamsAvgTime[a] > upstreamsAvgTime[b]
|
||||
slices.SortFunc(keys, func(a, b string) (res int) {
|
||||
switch x, y := upstreamsAvgTime[a], upstreamsAvgTime[b]; {
|
||||
case x > y:
|
||||
return -1
|
||||
case x < y:
|
||||
return +1
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
})
|
||||
|
||||
topUpstreamsAvgTime = make([]topAddrsFloat, 0, len(upstreamsAvgTime))
|
||||
|
||||
Reference in New Issue
Block a user