chore(all): replace net.IP with netip.Addr

This commit is contained in:
Quentin McGaw
2023-05-20 19:58:18 +00:00
parent 00ee6ff9a7
commit 0a29337c3b
91 changed files with 525 additions and 590 deletions

View File

@@ -3,7 +3,7 @@ package openvpn
import (
"errors"
"fmt"
"net"
"net/netip"
"sort"
"strings"
)
@@ -54,7 +54,7 @@ func ExtractHost(b []byte) (host, warning string, err error) {
return hosts[0], warning, nil
}
func ExtractIPs(b []byte) (ips []net.IP, err error) {
func ExtractIPs(b []byte) (ips []netip.Addr, err error) {
const rejectIP, rejectDomain = false, true
ipStrings := extractRemoteHosts(b, rejectIP, rejectDomain)
if len(ipStrings) == 0 {
@@ -65,9 +65,12 @@ func ExtractIPs(b []byte) (ips []net.IP, err error) {
return ipStrings[i] < ipStrings[j]
})
ips = make([]net.IP, len(ipStrings))
ips = make([]netip.Addr, len(ipStrings))
for i := range ipStrings {
ips[i] = net.ParseIP(ipStrings[i])
ips[i], err = netip.ParseAddr(ipStrings[i])
if err != nil {
return nil, fmt.Errorf("parsing IP address: %w", err)
}
}
return ips, nil
@@ -85,9 +88,9 @@ func extractRemoteHosts(content []byte, rejectIP, rejectDomain bool) (hosts []st
continue
}
host := fields[1]
parsedIP := net.ParseIP(host)
if (rejectIP && parsedIP != nil) ||
(rejectDomain && parsedIP == nil) {
_, err := netip.ParseAddr(host)
if (rejectIP && err == nil) ||
(rejectDomain && err != nil) {
continue
}
hosts = append(hosts, host)

View File

@@ -1,15 +1,20 @@
package resolver
import "net"
import (
"net/netip"
)
func uniqueIPsToSlice(uniqueIPs map[string]struct{}) (ips []net.IP) {
ips = make([]net.IP, 0, len(uniqueIPs))
func uniqueIPsToSlice(uniqueIPs map[string]struct{}) (ips []netip.Addr) {
ips = make([]netip.Addr, 0, len(uniqueIPs))
for key := range uniqueIPs {
IP := net.ParseIP(key)
if IPv4 := IP.To4(); IPv4 != nil {
IP = IPv4
ip, err := netip.ParseAddr(key)
if err != nil {
panic(err)
}
ips = append(ips, IP)
if ip.Is4In6() {
ip = netip.AddrFrom4(ip.As4())
}
ips = append(ips, ip)
}
return ips
}

View File

@@ -1,7 +1,7 @@
package resolver
import (
"net"
"net/netip"
"testing"
"github.com/stretchr/testify/assert"
@@ -11,23 +11,23 @@ func Test_uniqueIPsToSlice(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
inputIPs map[string]struct{}
outputIPs []net.IP
outputIPs []netip.Addr
}{
"nil": {
inputIPs: nil,
outputIPs: []net.IP{},
outputIPs: []netip.Addr{},
},
"empty": {
inputIPs: map[string]struct{}{},
outputIPs: []net.IP{},
outputIPs: []netip.Addr{},
},
"single IPv4": {
inputIPs: map[string]struct{}{"1.1.1.1": {}},
outputIPs: []net.IP{{1, 1, 1, 1}},
outputIPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1})},
},
"two IPv4s": {
inputIPs: map[string]struct{}{"1.1.1.1": {}, "1.1.2.1": {}},
outputIPs: []net.IP{{1, 1, 1, 1}, {1, 1, 2, 1}},
outputIPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1}), netip.AddrFrom4([4]byte{1, 1, 2, 1})},
},
}
for name, testCase := range testCases {

View File

@@ -4,7 +4,7 @@ import (
"context"
"errors"
"fmt"
"net"
"net/netip"
)
type Parallel struct {
@@ -31,7 +31,7 @@ type ParallelSettings struct {
type parallelResult struct {
host string
IPs []net.IP
IPs []netip.Addr
}
var (
@@ -40,7 +40,7 @@ var (
)
func (pr *Parallel) Resolve(ctx context.Context, settings ParallelSettings) (
hostToIPs map[string][]net.IP, warnings []string, err error) {
hostToIPs map[string][]netip.Addr, warnings []string, err error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
@@ -53,7 +53,7 @@ func (pr *Parallel) Resolve(ctx context.Context, settings ParallelSettings) (
go pr.resolveAsync(ctx, host, settings.Repeat, results, errors)
}
hostToIPs = make(map[string][]net.IP, len(settings.Hosts))
hostToIPs = make(map[string][]netip.Addr, len(settings.Hosts))
maxFails := int(settings.MaxFailRatio * float64(len(settings.Hosts)))
for range settings.Hosts {

View File

@@ -1,11 +1,11 @@
package resolver
import (
"bytes"
"context"
"errors"
"fmt"
"net"
"net/netip"
"sort"
"time"
)
@@ -31,7 +31,7 @@ type RepeatSettings struct {
}
func (r *Repeat) Resolve(ctx context.Context, host string, settings RepeatSettings) (
ips []net.IP, err error) {
ips []netip.Addr, err error) {
timedCtx, cancel := context.WithTimeout(ctx, settings.MaxDuration)
defer cancel()
@@ -54,7 +54,7 @@ func (r *Repeat) Resolve(ctx context.Context, host string, settings RepeatSettin
if settings.SortIPs {
sort.Slice(ips, func(i, j int) bool {
return bytes.Compare(ips[i], ips[j]) < 1
return ips[i].Compare(ips[j]) < 1
})
}
@@ -121,15 +121,15 @@ func (r *Repeat) resolveOnce(ctx, timedCtx context.Context, host string,
}
}
func (r *Repeat) lookupIPs(ctx context.Context, host string) (ips []net.IP, err error) {
func (r *Repeat) lookupIPs(ctx context.Context, host string) (ips []netip.Addr, err error) {
addresses, err := r.resolver.LookupIPAddr(ctx, host)
if err != nil {
return nil, err
}
ips = make([]net.IP, 0, len(addresses))
ips = make([]netip.Addr, 0, len(addresses))
for i := range addresses {
ip := addresses[i].IP
if ip == nil {
ip, ok := netip.AddrFromSlice(addresses[i].IP)
if !ok {
continue
}
ips = append(ips, ip)