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

@@ -48,7 +48,7 @@ func GetConnection(provider string,
connections := make([]models.Connection, 0, len(servers))
for _, server := range servers {
for _, ip := range server.IPs {
if !ipv6Supported && ip.To4() == nil {
if !ipv6Supported && ip.Is6() {
continue
}

View File

@@ -3,7 +3,7 @@ package utils
import (
"errors"
"math/rand"
"net"
"net/netip"
"testing"
"github.com/golang/mock/gomock"
@@ -58,7 +58,7 @@ func Test_GetConnection(t *testing.T) {
{
VPN: vpn.OpenVPN,
UDP: true,
IPs: []net.IP{net.IPv4(1, 1, 1, 1)},
IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1})},
Hostname: "name",
},
},
@@ -68,7 +68,7 @@ func Test_GetConnection(t *testing.T) {
randSource: rand.NewSource(0),
connection: models.Connection{
Type: vpn.OpenVPN,
IP: net.IPv4(1, 1, 1, 1),
IP: netip.AddrFrom4([4]byte{1, 1, 1, 1}),
Protocol: constants.UDP,
Port: 1194,
Hostname: "name",
@@ -79,7 +79,7 @@ func Test_GetConnection(t *testing.T) {
{
VPN: vpn.OpenVPN,
UDP: true,
IPs: []net.IP{net.IPv4(1, 1, 1, 1)},
IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1})},
Hostname: "hostname",
OvpnX509: "x509",
},
@@ -90,7 +90,7 @@ func Test_GetConnection(t *testing.T) {
randSource: rand.NewSource(0),
connection: models.Connection{
Type: vpn.OpenVPN,
IP: net.IPv4(1, 1, 1, 1),
IP: netip.AddrFrom4([4]byte{1, 1, 1, 1}),
Protocol: constants.UDP,
Port: 1194,
Hostname: "x509",
@@ -101,14 +101,14 @@ func Test_GetConnection(t *testing.T) {
{
VPN: vpn.OpenVPN,
UDP: true,
IPs: []net.IP{
net.IPv4(1, 1, 1, 1),
IPs: []netip.Addr{
netip.AddrFrom4([4]byte{1, 1, 1, 1}),
// All IPv6 is ignored
net.IPv6zero,
net.IPv6zero,
net.IPv6zero,
net.IPv6zero,
net.IPv6zero,
netip.IPv6Unspecified(),
netip.IPv6Unspecified(),
netip.IPv6Unspecified(),
netip.IPv6Unspecified(),
netip.IPv6Unspecified(),
},
},
},
@@ -118,7 +118,7 @@ func Test_GetConnection(t *testing.T) {
randSource: rand.NewSource(0),
connection: models.Connection{
Type: vpn.OpenVPN,
IP: net.IPv4(1, 1, 1, 1),
IP: netip.AddrFrom4([4]byte{1, 1, 1, 1}),
Protocol: constants.UDP,
Port: 1194,
},
@@ -128,9 +128,9 @@ func Test_GetConnection(t *testing.T) {
{
VPN: vpn.OpenVPN,
UDP: true,
IPs: []net.IP{
net.IPv6zero,
net.IPv4(1, 1, 1, 1),
IPs: []netip.Addr{
netip.IPv6Unspecified(),
netip.AddrFrom4([4]byte{1, 1, 1, 1}),
},
},
},
@@ -141,7 +141,7 @@ func Test_GetConnection(t *testing.T) {
randSource: rand.NewSource(0),
connection: models.Connection{
Type: vpn.OpenVPN,
IP: net.IPv6zero,
IP: netip.IPv6Unspecified(),
Protocol: constants.UDP,
Port: 1194,
},
@@ -151,21 +151,21 @@ func Test_GetConnection(t *testing.T) {
{
VPN: vpn.OpenVPN,
UDP: true,
IPs: []net.IP{net.IPv4(1, 1, 1, 1)},
IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1})},
OvpnX509: "ovpnx509",
},
{
VPN: vpn.Wireguard,
UDP: true,
IPs: []net.IP{net.IPv4(2, 2, 2, 2)},
IPs: []netip.Addr{netip.AddrFrom4([4]byte{2, 2, 2, 2})},
OvpnX509: "ovpnx509",
},
{
VPN: vpn.OpenVPN,
UDP: true,
IPs: []net.IP{
net.IPv4(3, 3, 3, 3),
{1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, // ipv6 ignored
IPs: []netip.Addr{
netip.AddrFrom4([4]byte{3, 3, 3, 3}),
netip.AddrFrom16([16]byte{1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}), // ipv6 ignored
},
Hostname: "hostname",
},
@@ -176,7 +176,7 @@ func Test_GetConnection(t *testing.T) {
randSource: rand.NewSource(0),
connection: models.Connection{
Type: vpn.OpenVPN,
IP: net.IPv4(1, 1, 1, 1),
IP: netip.AddrFrom4([4]byte{1, 1, 1, 1}),
Protocol: constants.UDP,
Port: 1194,
Hostname: "ovpnx509",

View File

@@ -4,15 +4,15 @@ import (
"context"
"errors"
"fmt"
"net"
"net/http"
"net/netip"
)
type NoPortForwarder interface {
PortForward(ctx context.Context, client *http.Client,
logger Logger, gateway net.IP, serverName string) (
logger Logger, gateway netip.Addr, serverName string) (
port uint16, err error)
KeepPortForward(ctx context.Context, gateway net.IP,
KeepPortForward(ctx context.Context, gateway netip.Addr,
serverName string) (err error)
}
@@ -29,10 +29,10 @@ func NewNoPortForwarding(providerName string) *NoPortForwarding {
var ErrPortForwardingNotSupported = errors.New("custom port forwarding obtention is not supported")
func (n *NoPortForwarding) PortForward(context.Context, *http.Client,
Logger, net.IP, string) (port uint16, err error) {
Logger, netip.Addr, string) (port uint16, err error) {
return 0, fmt.Errorf("%w: for %s", ErrPortForwardingNotSupported, n.providerName)
}
func (n *NoPortForwarding) KeepPortForward(context.Context, net.IP, string) (err error) {
func (n *NoPortForwarding) KeepPortForward(context.Context, netip.Addr, string) (err error) {
return fmt.Errorf("%w: for %s", ErrPortForwardingNotSupported, n.providerName)
}

View File

@@ -4,7 +4,7 @@ import (
"errors"
"fmt"
"math/rand"
"net"
"net/netip"
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/constants/vpn"
@@ -25,13 +25,15 @@ func pickConnection(connections []models.Connection,
return connection, ErrNoConnectionToPickFrom
}
if len(selection.TargetIP) > 0 && selection.VPN == vpn.Wireguard {
targetIPSet := selection.TargetIP.IsValid() && !selection.TargetIP.IsUnspecified()
if targetIPSet && selection.VPN == vpn.Wireguard {
// we need the right public key
return getTargetIPConnection(connections, selection.TargetIP)
}
connection = pickRandomConnection(connections, randSource)
if len(selection.TargetIP) > 0 {
if targetIPSet {
connection.IP = selection.TargetIP
}
@@ -46,9 +48,9 @@ func pickRandomConnection(connections []models.Connection,
var errTargetIPNotFound = errors.New("target IP address not found")
func getTargetIPConnection(connections []models.Connection,
targetIP net.IP) (connection models.Connection, err error) {
targetIP netip.Addr) (connection models.Connection, err error) {
for _, connection := range connections {
if targetIP.Equal(connection.IP) {
if targetIP == connection.IP {
return connection, nil
}
}

View File

@@ -22,8 +22,7 @@ func BuildWireguardSettings(connection models.Connection,
settings.RulePriority = rulePriority
settings.Endpoint = new(net.UDPAddr)
settings.Endpoint.IP = make(net.IP, len(connection.IP))
copy(settings.Endpoint.IP, connection.IP)
settings.Endpoint.IP = connection.IP.AsSlice()
settings.Endpoint.Port = int(connection.Port)
settings.Addresses = make([]netip.Prefix, 0, len(userSettings.Addresses))

View File

@@ -24,7 +24,7 @@ func Test_BuildWireguardSettings(t *testing.T) {
}{
"some settings": {
connection: models.Connection{
IP: net.IPv4(1, 2, 3, 4),
IP: netip.AddrFrom4([4]byte{1, 2, 3, 4}),
Port: 51821,
PubKey: "public",
},
@@ -44,7 +44,7 @@ func Test_BuildWireguardSettings(t *testing.T) {
PublicKey: "public",
PreSharedKey: "pre-shared",
Endpoint: &net.UDPAddr{
IP: net.IPv4(1, 2, 3, 4),
IP: net.IP{1, 2, 3, 4},
Port: 51821,
},
Addresses: []netip.Prefix{