chore(settings): inet.af/netaddr -> net/netip

This commit is contained in:
Quentin McGaw
2023-04-23 11:43:50 +00:00
parent 1693c59e0d
commit 4e2e46014d
8 changed files with 92 additions and 59 deletions

View File

@@ -3,12 +3,12 @@ package settings
import ( import (
"errors" "errors"
"fmt" "fmt"
"net/netip"
"regexp" "regexp"
"github.com/qdm12/dns/pkg/blacklist" "github.com/qdm12/dns/pkg/blacklist"
"github.com/qdm12/gluetun/internal/configuration/settings/helpers" "github.com/qdm12/gluetun/internal/configuration/settings/helpers"
"github.com/qdm12/gotree" "github.com/qdm12/gotree"
"inet.af/netaddr"
) )
// DNSBlacklist is settings for the DNS blacklist building. // DNSBlacklist is settings for the DNS blacklist building.
@@ -18,8 +18,8 @@ type DNSBlacklist struct {
BlockSurveillance *bool BlockSurveillance *bool
AllowedHosts []string AllowedHosts []string
AddBlockedHosts []string AddBlockedHosts []string
AddBlockedIPs []netaddr.IP AddBlockedIPs []netip.Addr
AddBlockedIPPrefixes []netaddr.IPPrefix AddBlockedIPPrefixes []netip.Prefix
} }
func (b *DNSBlacklist) setDefaults() { func (b *DNSBlacklist) setDefaults() {
@@ -58,8 +58,8 @@ func (b DNSBlacklist) copy() (copied DNSBlacklist) {
BlockSurveillance: helpers.CopyBoolPtr(b.BlockSurveillance), BlockSurveillance: helpers.CopyBoolPtr(b.BlockSurveillance),
AllowedHosts: helpers.CopyStringSlice(b.AllowedHosts), AllowedHosts: helpers.CopyStringSlice(b.AllowedHosts),
AddBlockedHosts: helpers.CopyStringSlice(b.AddBlockedHosts), AddBlockedHosts: helpers.CopyStringSlice(b.AddBlockedHosts),
AddBlockedIPs: helpers.CopyNetaddrIPsSlice(b.AddBlockedIPs), AddBlockedIPs: helpers.CopyNetipAddressesSlice(b.AddBlockedIPs),
AddBlockedIPPrefixes: helpers.CopyIPPrefixSlice(b.AddBlockedIPPrefixes), AddBlockedIPPrefixes: helpers.CopyNetipPrefixesSlice(b.AddBlockedIPPrefixes),
} }
} }
@@ -69,8 +69,8 @@ func (b *DNSBlacklist) mergeWith(other DNSBlacklist) {
b.BlockSurveillance = helpers.MergeWithBool(b.BlockSurveillance, other.BlockSurveillance) b.BlockSurveillance = helpers.MergeWithBool(b.BlockSurveillance, other.BlockSurveillance)
b.AllowedHosts = helpers.MergeStringSlices(b.AllowedHosts, other.AllowedHosts) b.AllowedHosts = helpers.MergeStringSlices(b.AllowedHosts, other.AllowedHosts)
b.AddBlockedHosts = helpers.MergeStringSlices(b.AddBlockedHosts, other.AddBlockedHosts) b.AddBlockedHosts = helpers.MergeStringSlices(b.AddBlockedHosts, other.AddBlockedHosts)
b.AddBlockedIPs = helpers.MergeNetaddrIPsSlices(b.AddBlockedIPs, other.AddBlockedIPs) b.AddBlockedIPs = helpers.MergeNetipAddressesSlices(b.AddBlockedIPs, other.AddBlockedIPs)
b.AddBlockedIPPrefixes = helpers.MergeIPPrefixesSlices(b.AddBlockedIPPrefixes, other.AddBlockedIPPrefixes) b.AddBlockedIPPrefixes = helpers.MergeNetipPrefixesSlices(b.AddBlockedIPPrefixes, other.AddBlockedIPPrefixes)
} }
func (b *DNSBlacklist) overrideWith(other DNSBlacklist) { func (b *DNSBlacklist) overrideWith(other DNSBlacklist) {
@@ -79,8 +79,8 @@ func (b *DNSBlacklist) overrideWith(other DNSBlacklist) {
b.BlockSurveillance = helpers.OverrideWithBool(b.BlockSurveillance, other.BlockSurveillance) b.BlockSurveillance = helpers.OverrideWithBool(b.BlockSurveillance, other.BlockSurveillance)
b.AllowedHosts = helpers.OverrideWithStringSlice(b.AllowedHosts, other.AllowedHosts) b.AllowedHosts = helpers.OverrideWithStringSlice(b.AllowedHosts, other.AllowedHosts)
b.AddBlockedHosts = helpers.OverrideWithStringSlice(b.AddBlockedHosts, other.AddBlockedHosts) b.AddBlockedHosts = helpers.OverrideWithStringSlice(b.AddBlockedHosts, other.AddBlockedHosts)
b.AddBlockedIPs = helpers.OverrideWithNetaddrIPsSlice(b.AddBlockedIPs, other.AddBlockedIPs) b.AddBlockedIPs = helpers.OverrideWithNetipAddressesSlice(b.AddBlockedIPs, other.AddBlockedIPs)
b.AddBlockedIPPrefixes = helpers.OverrideWithIPPrefixesSlice(b.AddBlockedIPPrefixes, other.AddBlockedIPPrefixes) b.AddBlockedIPPrefixes = helpers.OverrideWithNetipPrefixesSlice(b.AddBlockedIPPrefixes, other.AddBlockedIPPrefixes)
} }
func (b DNSBlacklist) ToBlacklistFormat() (settings blacklist.BuilderSettings, err error) { func (b DNSBlacklist) ToBlacklistFormat() (settings blacklist.BuilderSettings, err error) {
@@ -90,8 +90,8 @@ func (b DNSBlacklist) ToBlacklistFormat() (settings blacklist.BuilderSettings, e
BlockSurveillance: *b.BlockSurveillance, BlockSurveillance: *b.BlockSurveillance,
AllowedHosts: b.AllowedHosts, AllowedHosts: b.AllowedHosts,
AddBlockedHosts: b.AddBlockedHosts, AddBlockedHosts: b.AddBlockedHosts,
AddBlockedIPs: b.AddBlockedIPs, AddBlockedIPs: netipAddressesToNetaddrIPs(b.AddBlockedIPs),
AddBlockedIPPrefixes: b.AddBlockedIPPrefixes, AddBlockedIPPrefixes: netipPrefixesToNetaddrIPPrefixes(b.AddBlockedIPPrefixes),
}, nil }, nil
} }

View File

@@ -1,11 +1,12 @@
package helpers package helpers
import ( import (
"fmt"
"net" "net"
"net/netip"
"time" "time"
"github.com/qdm12/log" "github.com/qdm12/log"
"inet.af/netaddr"
) )
func CopyStringPtr(original *string) (copied *string) { func CopyStringPtr(original *string) (copied *string) {
@@ -113,21 +114,17 @@ func CopyIPNetPtr(original *net.IPNet) (copied *net.IPNet) {
return copied return copied
} }
func CopyNetaddrIP(original netaddr.IP) (copied netaddr.IP) { func CopyNetipAddress(original netip.Addr) (copied netip.Addr) {
b, err := original.MarshalBinary() // AsSlice creates a new byte slice so no need to copy the bytes.
if err != nil { bytes := original.AsSlice()
panic(err) copied, ok := netip.AddrFromSlice(bytes)
if !ok {
panic(fmt.Sprintf("cannot deep copy address with bytes %#v", bytes))
} }
err = copied.UnmarshalBinary(b)
if err != nil {
panic(err)
}
return copied return copied
} }
func CopyIPPrefix(original netaddr.IPPrefix) (copied netaddr.IPPrefix) { func CopyNetipPrefix(original netip.Prefix) (copied netip.Prefix) {
b, err := original.MarshalText() b, err := original.MarshalText()
if err != nil { if err != nil {
panic(err) panic(err)
@@ -173,26 +170,26 @@ func CopyIPNetSlice(original []net.IPNet) (copied []net.IPNet) {
return copied return copied
} }
func CopyIPPrefixSlice(original []netaddr.IPPrefix) (copied []netaddr.IPPrefix) { func CopyNetipPrefixesSlice(original []netip.Prefix) (copied []netip.Prefix) {
if original == nil { if original == nil {
return nil return nil
} }
copied = make([]netaddr.IPPrefix, len(original)) copied = make([]netip.Prefix, len(original))
for i := range original { for i := range original {
copied[i] = CopyIPPrefix(original[i]) copied[i] = CopyNetipPrefix(original[i])
} }
return copied return copied
} }
func CopyNetaddrIPsSlice(original []netaddr.IP) (copied []netaddr.IP) { func CopyNetipAddressesSlice(original []netip.Addr) (copied []netip.Addr) {
if original == nil { if original == nil {
return nil return nil
} }
copied = make([]netaddr.IP, len(original)) copied = make([]netip.Addr, len(original))
for i := range original { for i := range original {
copied[i] = CopyNetaddrIP(original[i]) copied[i] = CopyNetipAddress(original[i])
} }
return copied return copied

View File

@@ -3,10 +3,10 @@ package helpers
import ( import (
"net" "net"
"net/http" "net/http"
"net/netip"
"time" "time"
"github.com/qdm12/log" "github.com/qdm12/log"
"inet.af/netaddr"
) )
func MergeWithBool(existing, other *bool) (result *bool) { func MergeWithBool(existing, other *bool) (result *bool) {
@@ -213,13 +213,13 @@ func MergeIPNetsSlices(a, b []net.IPNet) (result []net.IPNet) {
return result return result
} }
func MergeNetaddrIPsSlices(a, b []netaddr.IP) (result []netaddr.IP) { func MergeNetipAddressesSlices(a, b []netip.Addr) (result []netip.Addr) {
if a == nil && b == nil { if a == nil && b == nil {
return nil return nil
} }
seen := make(map[string]struct{}, len(a)+len(b)) seen := make(map[string]struct{}, len(a)+len(b))
result = make([]netaddr.IP, 0, len(a)+len(b)) result = make([]netip.Addr, 0, len(a)+len(b))
for _, ip := range a { for _, ip := range a {
key := ip.String() key := ip.String()
if _, ok := seen[key]; ok { if _, ok := seen[key]; ok {
@@ -239,13 +239,13 @@ func MergeNetaddrIPsSlices(a, b []netaddr.IP) (result []netaddr.IP) {
return result return result
} }
func MergeIPPrefixesSlices(a, b []netaddr.IPPrefix) (result []netaddr.IPPrefix) { func MergeNetipPrefixesSlices(a, b []netip.Prefix) (result []netip.Prefix) {
if a == nil && b == nil { if a == nil && b == nil {
return nil return nil
} }
seen := make(map[string]struct{}, len(a)+len(b)) seen := make(map[string]struct{}, len(a)+len(b))
result = make([]netaddr.IPPrefix, 0, len(a)+len(b)) result = make([]netip.Prefix, 0, len(a)+len(b))
for _, ipPrefix := range a { for _, ipPrefix := range a {
key := ipPrefix.String() key := ipPrefix.String()
if _, ok := seen[key]; ok { if _, ok := seen[key]; ok {

View File

@@ -3,10 +3,10 @@ package helpers
import ( import (
"net" "net"
"net/http" "net/http"
"net/netip"
"time" "time"
"github.com/qdm12/log" "github.com/qdm12/log"
"inet.af/netaddr"
) )
func OverrideWithBool(existing, other *bool) (result *bool) { func OverrideWithBool(existing, other *bool) (result *bool) {
@@ -154,20 +154,20 @@ func OverrideWithIPNetsSlice(existing, other []net.IPNet) (result []net.IPNet) {
return result return result
} }
func OverrideWithNetaddrIPsSlice(existing, other []netaddr.IP) (result []netaddr.IP) { func OverrideWithNetipAddressesSlice(existing, other []netip.Addr) (result []netip.Addr) {
if other == nil { if other == nil {
return existing return existing
} }
result = make([]netaddr.IP, len(other)) result = make([]netip.Addr, len(other))
copy(result, other) copy(result, other)
return result return result
} }
func OverrideWithIPPrefixesSlice(existing, other []netaddr.IPPrefix) (result []netaddr.IPPrefix) { func OverrideWithNetipPrefixesSlice(existing, other []netip.Prefix) (result []netip.Prefix) {
if other == nil { if other == nil {
return existing return existing
} }
result = make([]netaddr.IPPrefix, len(other)) result = make([]netip.Prefix, len(other))
copy(result, other) copy(result, other)
return result return result
} }

View File

@@ -0,0 +1,36 @@
package settings
import (
"net/netip"
"inet.af/netaddr"
)
func netipAddressToNetaddrIP(address netip.Addr) (ip netaddr.IP) {
if address.Is4() {
return netaddr.IPFrom4(address.As4())
}
return netaddr.IPFrom16(address.As16())
}
func netipAddressesToNetaddrIPs(addresses []netip.Addr) (ips []netaddr.IP) {
ips = make([]netaddr.IP, len(addresses))
for i := range addresses {
ips[i] = netipAddressToNetaddrIP(addresses[i])
}
return ips
}
func netipPrefixToNetaddrIPPrefix(prefix netip.Prefix) (ipPrefix netaddr.IPPrefix) {
netaddrIP := netipAddressToNetaddrIP(prefix.Addr())
bits := prefix.Bits()
return netaddr.IPPrefixFrom(netaddrIP, uint8(bits))
}
func netipPrefixesToNetaddrIPPrefixes(prefixes []netip.Prefix) (ipPrefixes []netaddr.IPPrefix) {
ipPrefixes = make([]netaddr.IPPrefix, len(prefixes))
for i := range ipPrefixes {
ipPrefixes[i] = netipPrefixToNetaddrIPPrefix(prefixes[i])
}
return ipPrefixes
}

View File

@@ -4,12 +4,12 @@ import (
"errors" "errors"
"fmt" "fmt"
"net" "net"
"net/netip"
"github.com/qdm12/dns/pkg/provider" "github.com/qdm12/dns/pkg/provider"
"github.com/qdm12/dns/pkg/unbound" "github.com/qdm12/dns/pkg/unbound"
"github.com/qdm12/gluetun/internal/configuration/settings/helpers" "github.com/qdm12/gluetun/internal/configuration/settings/helpers"
"github.com/qdm12/gotree" "github.com/qdm12/gotree"
"inet.af/netaddr"
) )
// Unbound is settings for the Unbound program. // Unbound is settings for the Unbound program.
@@ -21,7 +21,7 @@ type Unbound struct {
VerbosityDetailsLevel *uint8 VerbosityDetailsLevel *uint8
ValidationLogLevel *uint8 ValidationLogLevel *uint8
Username string Username string
Allowed []netaddr.IPPrefix Allowed []netip.Prefix
} }
func (u *Unbound) setDefaults() { func (u *Unbound) setDefaults() {
@@ -44,9 +44,9 @@ func (u *Unbound) setDefaults() {
u.ValidationLogLevel = helpers.DefaultUint8(u.ValidationLogLevel, defaultValidationLogLevel) u.ValidationLogLevel = helpers.DefaultUint8(u.ValidationLogLevel, defaultValidationLogLevel)
if u.Allowed == nil { if u.Allowed == nil {
u.Allowed = []netaddr.IPPrefix{ u.Allowed = []netip.Prefix{
netaddr.IPPrefixFrom(netaddr.IPv4(0, 0, 0, 0), 0), netip.PrefixFrom(netip.AddrFrom4([4]byte{}), 0),
netaddr.IPPrefixFrom(netaddr.IPv6Raw([16]byte{}), 0), netip.PrefixFrom(netip.AddrFrom16([16]byte{}), 0),
} }
} }
@@ -102,7 +102,7 @@ func (u Unbound) copy() (copied Unbound) {
VerbosityDetailsLevel: helpers.CopyUint8Ptr(u.VerbosityDetailsLevel), VerbosityDetailsLevel: helpers.CopyUint8Ptr(u.VerbosityDetailsLevel),
ValidationLogLevel: helpers.CopyUint8Ptr(u.ValidationLogLevel), ValidationLogLevel: helpers.CopyUint8Ptr(u.ValidationLogLevel),
Username: u.Username, Username: u.Username,
Allowed: helpers.CopyIPPrefixSlice(u.Allowed), Allowed: helpers.CopyNetipPrefixesSlice(u.Allowed),
} }
} }
@@ -114,7 +114,7 @@ func (u *Unbound) mergeWith(other Unbound) {
u.VerbosityDetailsLevel = helpers.MergeWithUint8(u.VerbosityDetailsLevel, other.VerbosityDetailsLevel) u.VerbosityDetailsLevel = helpers.MergeWithUint8(u.VerbosityDetailsLevel, other.VerbosityDetailsLevel)
u.ValidationLogLevel = helpers.MergeWithUint8(u.ValidationLogLevel, other.ValidationLogLevel) u.ValidationLogLevel = helpers.MergeWithUint8(u.ValidationLogLevel, other.ValidationLogLevel)
u.Username = helpers.MergeWithString(u.Username, other.Username) u.Username = helpers.MergeWithString(u.Username, other.Username)
u.Allowed = helpers.MergeIPPrefixesSlices(u.Allowed, other.Allowed) u.Allowed = helpers.MergeNetipPrefixesSlices(u.Allowed, other.Allowed)
} }
func (u *Unbound) overrideWith(other Unbound) { func (u *Unbound) overrideWith(other Unbound) {
@@ -125,7 +125,7 @@ func (u *Unbound) overrideWith(other Unbound) {
u.VerbosityDetailsLevel = helpers.OverrideWithUint8(u.VerbosityDetailsLevel, other.VerbosityDetailsLevel) u.VerbosityDetailsLevel = helpers.OverrideWithUint8(u.VerbosityDetailsLevel, other.VerbosityDetailsLevel)
u.ValidationLogLevel = helpers.OverrideWithUint8(u.ValidationLogLevel, other.ValidationLogLevel) u.ValidationLogLevel = helpers.OverrideWithUint8(u.ValidationLogLevel, other.ValidationLogLevel)
u.Username = helpers.OverrideWithString(u.Username, other.Username) u.Username = helpers.OverrideWithString(u.Username, other.Username)
u.Allowed = helpers.OverrideWithIPPrefixesSlice(u.Allowed, other.Allowed) u.Allowed = helpers.OverrideWithNetipPrefixesSlice(u.Allowed, other.Allowed)
} }
func (u Unbound) ToUnboundFormat() (settings unbound.Settings, err error) { func (u Unbound) ToUnboundFormat() (settings unbound.Settings, err error) {
@@ -149,7 +149,7 @@ func (u Unbound) ToUnboundFormat() (settings unbound.Settings, err error) {
VerbosityDetailsLevel: *u.VerbosityDetailsLevel, VerbosityDetailsLevel: *u.VerbosityDetailsLevel,
ValidationLogLevel: *u.ValidationLogLevel, ValidationLogLevel: *u.ValidationLogLevel,
AccessControl: unbound.AccessControlSettings{ AccessControl: unbound.AccessControlSettings{
Allowed: u.Allowed, Allowed: netipPrefixesToNetaddrIPPrefixes(u.Allowed),
}, },
Username: u.Username, Username: u.Username,
}, nil }, nil

View File

@@ -2,11 +2,11 @@ package settings
import ( import (
"encoding/json" "encoding/json"
"net/netip"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"inet.af/netaddr"
) )
func Test_Unbound_JSON(t *testing.T) { func Test_Unbound_JSON(t *testing.T) {
@@ -20,9 +20,9 @@ func Test_Unbound_JSON(t *testing.T) {
VerbosityDetailsLevel: nil, VerbosityDetailsLevel: nil,
ValidationLogLevel: uint8Ptr(0), ValidationLogLevel: uint8Ptr(0),
Username: "user", Username: "user",
Allowed: []netaddr.IPPrefix{ Allowed: []netip.Prefix{
netaddr.IPPrefixFrom(netaddr.IPv4(0, 0, 0, 0), 0), netip.PrefixFrom(netip.AddrFrom4([4]byte{}), 0),
netaddr.IPPrefixFrom(netaddr.IPv6Raw([16]byte{}), 0), netip.PrefixFrom(netip.AddrFrom16([16]byte{}), 0),
}, },
} }

View File

@@ -3,10 +3,10 @@ package env
import ( import (
"errors" "errors"
"fmt" "fmt"
"net/netip"
"github.com/qdm12/gluetun/internal/configuration/settings" "github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/govalid/binary" "github.com/qdm12/govalid/binary"
"inet.af/netaddr"
) )
func (s *Source) readDNSBlacklist() (blacklist settings.DNSBlacklist, err error) { func (s *Source) readDNSBlacklist() (blacklist settings.DNSBlacklist, err error) {
@@ -55,24 +55,24 @@ var (
ErrPrivateAddressNotValid = errors.New("private address is not a valid IP or CIDR range") ErrPrivateAddressNotValid = errors.New("private address is not a valid IP or CIDR range")
) )
func readDoTPrivateAddresses() (ips []netaddr.IP, func readDoTPrivateAddresses() (ips []netip.Addr,
ipPrefixes []netaddr.IPPrefix, err error) { ipPrefixes []netip.Prefix, err error) {
privateAddresses := envToCSV("DOT_PRIVATE_ADDRESS") privateAddresses := envToCSV("DOT_PRIVATE_ADDRESS")
if len(privateAddresses) == 0 { if len(privateAddresses) == 0 {
return nil, nil, nil return nil, nil, nil
} }
ips = make([]netaddr.IP, 0, len(privateAddresses)) ips = make([]netip.Addr, 0, len(privateAddresses))
ipPrefixes = make([]netaddr.IPPrefix, 0, len(privateAddresses)) ipPrefixes = make([]netip.Prefix, 0, len(privateAddresses))
for _, privateAddress := range privateAddresses { for _, privateAddress := range privateAddresses {
ip, err := netaddr.ParseIP(privateAddress) ip, err := netip.ParseAddr(privateAddress)
if err == nil { if err == nil {
ips = append(ips, ip) ips = append(ips, ip)
continue continue
} }
ipPrefix, err := netaddr.ParseIPPrefix(privateAddress) ipPrefix, err := netip.ParsePrefix(privateAddress)
if err == nil { if err == nil {
ipPrefixes = append(ipPrefixes, ipPrefix) ipPrefixes = append(ipPrefixes, ipPrefix)
continue continue