Skip to content

Commit

Permalink
Pull request 330: AGDNS-1982 Improve code vol.1
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 16a9ed7
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Mar 21 19:39:39 2024 +0300

    scripts: rm todo

commit 32a8a66
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Mar 21 19:36:51 2024 +0300

    all: imp alignment, docs

commit a5e79f7
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Mar 21 17:31:30 2024 +0300

    all: revert go upd, imp code, docs

commit 538834c
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Mar 21 16:41:32 2024 +0300

    all: upd ci specs

commit 97b7083
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Mar 21 16:39:44 2024 +0300

    proxy: fix before req cond, imp docs

commit afc19fe
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Mar 21 15:55:33 2024 +0300

    all: refactor, imp docs
  • Loading branch information
EugeneOne1 committed Mar 22, 2024
1 parent 9f2b21c commit 1b67700
Show file tree
Hide file tree
Showing 19 changed files with 364 additions and 563 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Build

'env':
'GO_VERSION': '1.21.7'
'GO_VERSION': '1.21.8'

'on':
'push':
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'name': Docker

'env':
'GO_VERSION': '1.21.7'
'GO_VERSION': '1.21.8'

'on':
'push':
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'name': 'lint'

'env':
'GO_VERSION': '1.21.7'
'GO_VERSION': '1.21.8'

'on':
'push':
Expand Down
2 changes: 1 addition & 1 deletion bamboo-specs/bamboo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
- |
set -e -f -u -x
make VERBOSE=1 go-tools go-lint
make VERBOSE=1 GOMAXPROCS=1 go-tools go-lint
'Test':
'docker':
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/AdguardTeam/dnsproxy
go 1.21.8

require (
github.com/AdguardTeam/golibs v0.20.1
github.com/AdguardTeam/golibs v0.20.4
github.com/ameshkov/dnscrypt/v2 v2.2.7
github.com/ameshkov/dnsstamps v1.0.3
github.com/beefsack/go-rate v0.0.0-20220214233405-116f4ca011a0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/AdguardTeam/golibs v0.20.1 h1:ol8qLjWGZhU9paMMwN+OLWVTUigGsXa29iVTyd62VKY=
github.com/AdguardTeam/golibs v0.20.1/go.mod h1:bgcMgRviCKyU6mkrX+RtT/OsKPFzyppelfRsksMG3KU=
github.com/AdguardTeam/golibs v0.20.4 h1:grcXP/Fm1zSeAfQVehZ/l8IinSTQt3UPbKKOGpUq7p0=
github.com/AdguardTeam/golibs v0.20.4/go.mod h1:/votX6WK1PdcZ3T2kBOPjPCGmfhlKixhI6ljYrFRPvI=
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da h1:KjTM2ks9d14ZYCvmHS9iAKVt9AyzRSqNU1qabPih5BY=
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da/go.mod h1:eHEWzANqSiWQsof+nXEI9bUVUyV6F53Fp89EuCh2EAA=
github.com/aead/poly1305 v0.0.0-20180717145839-3fee0db0b635 h1:52m0LGchQBBVqJRyYYufQuIbVqRawmubW3OFGqK1ekw=
Expand Down
5 changes: 2 additions & 3 deletions internal/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"slices"
"time"

proxynetutil "github.com/AdguardTeam/dnsproxy/internal/netutil"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/netutil"
Expand Down Expand Up @@ -78,9 +77,9 @@ func ResolveDialContext(
}

if preferV6 {
slices.SortStableFunc(ips, proxynetutil.PreferIPv6)
slices.SortStableFunc(ips, netutil.PreferIPv6)
} else {
slices.SortStableFunc(ips, proxynetutil.PreferIPv4)
slices.SortStableFunc(ips, netutil.PreferIPv4)
}

addrs := make([]string, 0, len(ips))
Expand Down
75 changes: 1 addition & 74 deletions internal/netutil/netutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,86 +7,13 @@ package netutil

import (
"net/netip"
"slices"
"strings"
)

// PreferIPv4 compares two addresses, preferring IPv4 addresses over IPv6 ones.
// Invalid addresses are sorted near the end.
func PreferIPv4(a, b netip.Addr) (res int) {
if !a.IsValid() {
return 1
} else if !b.IsValid() {
return -1
}

if aIs4 := a.Is4(); aIs4 == b.Is4() {
return a.Compare(b)
} else if aIs4 {
return -1
}

return 1
}

// PreferIPv6 compares two addresses, preferring IPv6 addresses over IPv4 ones.
// Invalid addresses are sorted near the end.
func PreferIPv6(a, b netip.Addr) (res int) {
if !a.IsValid() {
return 1
} else if !b.IsValid() {
return -1
}

if aIs6 := a.Is6(); aIs6 == b.Is6() {
return a.Compare(b)
} else if aIs6 {
return -1
}

return 1
}

// SortNetIPAddrs sorts addrs in accordance with the protocol preferences.
// Invalid addresses are sorted near the end. Zones are ignored.
func SortNetIPAddrs(addrs []netip.Addr, preferIPv6 bool) {
l := len(addrs)
if l <= 1 {
return
}

slices.SortStableFunc(addrs, func(addrA, addrB netip.Addr) (res int) {
if !addrA.IsValid() {
return 1
} else if !addrB.IsValid() {
return -1
}

aIs4, bIs4 := addrA.Is4(), addrB.Is4()
if aIs4 == bIs4 {
return addrA.Compare(addrB)
}

if aIs4 {
if preferIPv6 {
return 1
}

return -1
}

if preferIPv6 {
return -1
}

return 1
})
}

// ParseSubnet parses s either as a CIDR prefix itself, or as an IP address,
// returning the corresponding single-IP CIDR prefix.
//
// TODO(e.burkov): Move to golibs.
// TODO(e.burkov): Replace usages with [netutil.Prefix].
func ParseSubnet(s string) (p netip.Prefix, err error) {
if strings.Contains(s, "/") {
p, err = netip.ParsePrefix(s)
Expand Down
48 changes: 0 additions & 48 deletions internal/netutil/netutil_example_test.go

This file was deleted.

75 changes: 0 additions & 75 deletions internal/netutil/netutil_test.go

This file was deleted.

Loading

0 comments on commit 1b67700

Please sign in to comment.