Skip to content

Commit

Permalink
chore(all): replace net.IP with netip.Addr
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed May 20, 2023
1 parent 00ee6ff commit 0a29337
Show file tree
Hide file tree
Showing 91 changed files with 525 additions and 590 deletions.
6 changes: 3 additions & 3 deletions internal/cli/openvpnconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package cli
import (
"context"
"fmt"
"net"
"net/http"
"net/netip"
"strings"
"time"

Expand All @@ -28,11 +28,11 @@ type Unzipper interface {

type ParallelResolver interface {
Resolve(ctx context.Context, settings resolver.ParallelSettings) (
hostToIPs map[string][]net.IP, warnings []string, err error)
hostToIPs map[string][]netip.Addr, warnings []string, err error)
}

type IPFetcher interface {
FetchMultiInfo(ctx context.Context, ips []net.IP) (data []ipinfo.Response, err error)
FetchMultiInfo(ctx context.Context, ips []netip.Addr) (data []ipinfo.Response, err error)
}

type IPv6Checker interface {
Expand Down
10 changes: 5 additions & 5 deletions internal/configuration/settings/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package settings

import (
"fmt"
"net"
"net/netip"

"github.com/qdm12/gluetun/internal/configuration/settings/helpers"
"github.com/qdm12/gotree"
Expand All @@ -13,9 +13,9 @@ type DNS struct {
// ServerAddress is the DNS server to use inside
// the Go program and for the system.
// It defaults to '127.0.0.1' to be used with the
// DoT server. It cannot be nil in the internal
// DoT server. It cannot be the zero value in the internal
// state.
ServerAddress net.IP
ServerAddress netip.Addr
// KeepNameserver is true if the Docker DNS server
// found in /etc/resolv.conf should be kept.
// Note settings this to true will go around the
Expand All @@ -39,7 +39,7 @@ func (d DNS) validate() (err error) {

func (d *DNS) Copy() (copied DNS) {
return DNS{
ServerAddress: helpers.CopyIP(d.ServerAddress),
ServerAddress: d.ServerAddress,
KeepNameserver: helpers.CopyBoolPtr(d.KeepNameserver),
DoT: d.DoT.copy(),
}
Expand All @@ -63,7 +63,7 @@ func (d *DNS) overrideWith(other DNS) {
}

func (d *DNS) setDefaults() {
localhost := net.IPv4(127, 0, 0, 1) //nolint:gomnd
localhost := netip.AddrFrom4([4]byte{127, 0, 0, 1})
d.ServerAddress = helpers.DefaultIP(d.ServerAddress, localhost)
d.KeepNameserver = helpers.DefaultBool(d.KeepNameserver, false)
d.DoT.setDefaults()
Expand Down
26 changes: 1 addition & 25 deletions internal/configuration/settings/helpers/copy.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package helpers

import (
"fmt"
"net"
"net/netip"
"time"

Expand Down Expand Up @@ -81,25 +79,6 @@ func CopyLogLevelPtr(original *log.Level) (copied *log.Level) {
return copied
}

func CopyIP(original net.IP) (copied net.IP) {
if original == nil {
return nil
}
copied = make(net.IP, len(original))
copy(copied, original)
return copied
}

func CopyNetipAddress(original netip.Addr) (copied netip.Addr) {
// AsSlice creates a new byte slice so no need to copy the bytes.
bytes := original.AsSlice()
copied, ok := netip.AddrFromSlice(bytes)
if !ok {
panic(fmt.Sprintf("cannot deep copy address with bytes %#v", bytes))
}
return copied
}

func CopyStringSlice(original []string) (copied []string) {
if original == nil {
return nil
Expand Down Expand Up @@ -136,9 +115,6 @@ func CopyNetipAddressesSlice(original []netip.Addr) (copied []netip.Addr) {
}

copied = make([]netip.Addr, len(original))
for i := range original {
copied[i] = CopyNetipAddress(original[i])
}

copy(copied, original)
return copied
}
8 changes: 4 additions & 4 deletions internal/configuration/settings/helpers/default.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package helpers

import (
"net"
"net/netip"
"time"

"github.com/qdm12/log"
Expand Down Expand Up @@ -101,9 +101,9 @@ func DefaultLogLevel(existing *log.Level,
return result
}

func DefaultIP(existing net.IP, defaultValue net.IP) (
result net.IP) {
if existing != nil {
func DefaultIP(existing netip.Addr, defaultValue netip.Addr) (
result netip.Addr) {
if existing.IsValid() {
return existing
}
return defaultValue
Expand Down
17 changes: 10 additions & 7 deletions internal/configuration/settings/helpers/merge.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package helpers

import (
"net"
"fmt"
"net/http"
"net/netip"
"time"
Expand Down Expand Up @@ -96,14 +96,17 @@ func MergeWithUint32(existing, other *uint32) (result *uint32) {
return result
}

func MergeWithIP(existing, other net.IP) (result net.IP) {
if existing != nil {
func MergeWithIP(existing, other netip.Addr) (result netip.Addr) {
if existing.IsValid() {
return existing
} else if other == nil {
return nil
} else if !other.IsValid() {
return existing
}

result, ok := netip.AddrFromSlice(other.AsSlice())
if !ok {
panic(fmt.Sprintf("failed copying other address: %s", other))
}
result = make(net.IP, len(other))
copy(result, other)
return result
}

Expand Down
12 changes: 7 additions & 5 deletions internal/configuration/settings/helpers/override.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package helpers

import (
"net"
"fmt"
"net/http"
"net/netip"
"time"
Expand Down Expand Up @@ -84,12 +84,14 @@ func OverrideWithUint32(existing, other *uint32) (result *uint32) {
return result
}

func OverrideWithIP(existing, other net.IP) (result net.IP) {
if other == nil {
func OverrideWithIP(existing, other netip.Addr) (result netip.Addr) {
if !other.IsValid() {
return existing
}
result = make(net.IP, len(other))
copy(result, other)
result, ok := netip.AddrFromSlice(other.AsSlice())
if !ok {
panic(fmt.Sprintf("failed copying other address: %s", other))
}
return result
}

Expand Down
14 changes: 7 additions & 7 deletions internal/configuration/settings/serverselection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package settings
import (
"errors"
"fmt"
"net"
"net/netip"
"strings"

"github.com/qdm12/gluetun/internal/configuration/settings/helpers"
Expand All @@ -21,10 +21,10 @@ type ServerSelection struct { //nolint:maligned
VPN string
// TargetIP is the server endpoint IP address to use.
// It will override any IP address from the picked
// built-in server. It cannot be nil in the internal
// state, and can be set to an empty net.IP{} to indicate
// built-in server. It cannot be the empty value in the internal
// state, and can be set to the unspecified address to indicate
// there is not target IP address to use.
TargetIP net.IP
TargetIP netip.Addr
// Counties is the list of countries to filter VPN servers with.
Countries []string
// Regions is the list of regions to filter VPN servers with.
Expand Down Expand Up @@ -202,7 +202,7 @@ func validateServerFilters(settings ServerSelection, filterChoices models.Filter
func (ss *ServerSelection) copy() (copied ServerSelection) {
return ServerSelection{
VPN: ss.VPN,
TargetIP: helpers.CopyIP(ss.TargetIP),
TargetIP: ss.TargetIP,
Countries: helpers.CopyStringSlice(ss.Countries),
Regions: helpers.CopyStringSlice(ss.Regions),
Cities: helpers.CopyStringSlice(ss.Cities),
Expand Down Expand Up @@ -261,7 +261,7 @@ func (ss *ServerSelection) overrideWith(other ServerSelection) {

func (ss *ServerSelection) setDefaults(vpnProvider string) {
ss.VPN = helpers.DefaultString(ss.VPN, vpn.OpenVPN)
ss.TargetIP = helpers.DefaultIP(ss.TargetIP, net.IP{})
ss.TargetIP = helpers.DefaultIP(ss.TargetIP, netip.IPv4Unspecified())
ss.OwnedOnly = helpers.DefaultBool(ss.OwnedOnly, false)
ss.FreeOnly = helpers.DefaultBool(ss.FreeOnly, false)
ss.PremiumOnly = helpers.DefaultBool(ss.PremiumOnly, false)
Expand All @@ -278,7 +278,7 @@ func (ss ServerSelection) String() string {
func (ss ServerSelection) toLinesNode() (node *gotree.Node) {
node = gotree.New("Server selection settings:")
node.Appendf("VPN type: %s", ss.VPN)
if len(ss.TargetIP) > 0 {
if !ss.TargetIP.IsUnspecified() {
node.Appendf("Target IP address: %s", ss.TargetIP)
}

Expand Down
17 changes: 13 additions & 4 deletions internal/configuration/settings/unbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package settings
import (
"errors"
"fmt"
"net"
"net/netip"

"github.com/qdm12/dns/pkg/provider"
Expand Down Expand Up @@ -155,14 +154,24 @@ func (u Unbound) ToUnboundFormat() (settings unbound.Settings, err error) {
}, nil
}

func (u Unbound) GetFirstPlaintextIPv4() (ipv4 net.IP, err error) {
var (
ErrConvertingNetip = errors.New("converting net.IP to netip.Addr failed")
)

func (u Unbound) GetFirstPlaintextIPv4() (ipv4 netip.Addr, err error) {
s := u.Providers[0]
provider, err := provider.Parse(s)
if err != nil {
return nil, err
return ipv4, err
}

return provider.DNS().IPv4[0], nil
ip := provider.DNS().IPv4[0]
ipv4, ok := netip.AddrFromSlice(ip)
if !ok {
return ipv4, fmt.Errorf("%w: for ip %s (%#v)",
ErrConvertingNetip, ip, ip)
}
return ipv4.Unmap(), nil
}

func (u Unbound) String() string {
Expand Down
14 changes: 7 additions & 7 deletions internal/configuration/settings/wireguardselection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package settings

import (
"fmt"
"net"
"net/netip"

"github.com/qdm12/gluetun/internal/configuration/settings/helpers"
"github.com/qdm12/gluetun/internal/constants/providers"
Expand All @@ -15,9 +15,9 @@ type WireguardSelection struct {
// It is only used with VPN providers generating Wireguard
// configurations specific to each server and user.
// To indicate it should not be used, it should be set
// to the empty net.IP{} slice. It can never be nil
// to netaddr.IPv4Unspecified(). It can never be the zero value
// in the internal state.
EndpointIP net.IP
EndpointIP netip.Addr
// EndpointPort is a the server port to use for the VPN server.
// It is optional for VPN providers IVPN, Mullvad, Surfshark
// and Windscribe, and compulsory for the others.
Expand All @@ -40,7 +40,7 @@ func (w WireguardSelection) validate(vpnProvider string) (err error) {
providers.Surfshark, providers.Windscribe:
// endpoint IP addresses are baked in
case providers.Custom:
if len(w.EndpointIP) == 0 {
if !w.EndpointIP.IsValid() || w.EndpointIP.IsUnspecified() {
return fmt.Errorf("%w", ErrWireguardEndpointIPNotSet)
}
default: // Providers not supporting Wireguard
Expand Down Expand Up @@ -109,7 +109,7 @@ func (w WireguardSelection) validate(vpnProvider string) (err error) {

func (w *WireguardSelection) copy() (copied WireguardSelection) {
return WireguardSelection{
EndpointIP: helpers.CopyIP(w.EndpointIP),
EndpointIP: w.EndpointIP,
EndpointPort: helpers.CopyUint16Ptr(w.EndpointPort),
PublicKey: w.PublicKey,
}
Expand All @@ -128,7 +128,7 @@ func (w *WireguardSelection) overrideWith(other WireguardSelection) {
}

func (w *WireguardSelection) setDefaults() {
w.EndpointIP = helpers.DefaultIP(w.EndpointIP, net.IP{})
w.EndpointIP = helpers.DefaultIP(w.EndpointIP, netip.IPv4Unspecified())
w.EndpointPort = helpers.DefaultUint16(w.EndpointPort, 0)
}

Expand All @@ -139,7 +139,7 @@ func (w WireguardSelection) String() string {
func (w WireguardSelection) toLinesNode() (node *gotree.Node) {
node = gotree.New("Wireguard selection settings:")

if len(w.EndpointIP) > 0 {
if !w.EndpointIP.IsUnspecified() {
node.Appendf("Endpoint IP address: %s", w.EndpointIP)
}

Expand Down
14 changes: 7 additions & 7 deletions internal/configuration/sources/env/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package env

import (
"fmt"
"net"
"net/netip"

"github.com/qdm12/gluetun/internal/configuration/settings"
)
Expand All @@ -26,19 +26,19 @@ func (s *Source) readDNS() (dns settings.DNS, err error) {
return dns, nil
}

func (s *Source) readDNSServerAddress() (address net.IP, err error) {
func (s *Source) readDNSServerAddress() (address netip.Addr, err error) {
key, value := s.getEnvWithRetro("DNS_ADDRESS", "DNS_PLAINTEXT_ADDRESS")
if value == "" {
return nil, nil
return address, nil
}

address = net.ParseIP(value)
if address == nil {
return nil, fmt.Errorf("environment variable %s: %w: %s", key, ErrIPAddressParse, value)
address, err = netip.ParseAddr(value)
if err != nil {
return address, fmt.Errorf("environment variable %s: %w", key, err)
}

// TODO remove in v4
if !address.Equal(net.IPv4(127, 0, 0, 1)) { //nolint:gomnd
if address.Unmap().Compare(netip.AddrFrom4([4]byte{127, 0, 0, 1})) != 0 {
s.warner.Warn(key + " is set to " + value +
" so the DNS over TLS (DoT) server will not be used." +
" The default value changed to 127.0.0.1 so it uses the internal DoT serves." +
Expand Down
Loading

0 comments on commit 0a29337

Please sign in to comment.