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

@@ -4,8 +4,8 @@ import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"net/netip"
"github.com/qdm12/gluetun/internal/provider/common"
)
@@ -15,20 +15,20 @@ type apiData struct {
}
type apiServer struct {
PublicName string `json:"public_name"`
CountryName string `json:"country_name"`
CountryCode string `json:"country_code"`
Location string `json:"location"`
Continent string `json:"continent"`
IPv4In1 net.IP `json:"ip_v4_in1"`
IPv4In2 net.IP `json:"ip_v4_in2"`
IPv4In3 net.IP `json:"ip_v4_in3"`
IPv4In4 net.IP `json:"ip_v4_in4"`
IPv6In1 net.IP `json:"ip_v6_in1"`
IPv6In2 net.IP `json:"ip_v6_in2"`
IPv6In3 net.IP `json:"ip_v6_in3"`
IPv6In4 net.IP `json:"ip_v6_in4"`
Health string `json:"health"`
PublicName string `json:"public_name"`
CountryName string `json:"country_name"`
CountryCode string `json:"country_code"`
Location string `json:"location"`
Continent string `json:"continent"`
IPv4In1 netip.Addr `json:"ip_v4_in1"`
IPv4In2 netip.Addr `json:"ip_v4_in2"`
IPv4In3 netip.Addr `json:"ip_v4_in3"`
IPv4In4 netip.Addr `json:"ip_v4_in4"`
IPv6In1 netip.Addr `json:"ip_v6_in1"`
IPv6In2 netip.Addr `json:"ip_v6_in2"`
IPv6In3 netip.Addr `json:"ip_v6_in3"`
IPv6In4 netip.Addr `json:"ip_v6_in4"`
Health string `json:"health"`
}
func fetchAPI(ctx context.Context, client *http.Client) (

View File

@@ -3,7 +3,7 @@ package updater
import (
"context"
"fmt"
"net"
"net/netip"
"sort"
"strings"
@@ -57,12 +57,12 @@ func (u *Updater) FetchServers(ctx context.Context, minServers int) (
baseWireguardServer.WgPubKey = "PyLCXAQT8KkM4T+dUsOQfn+Ub3pGxfGlxkIApuig+hk="
ipv4WireguadServer := baseWireguardServer
ipv4WireguadServer.IPs = []net.IP{apiServer.IPv4In1}
ipv4WireguadServer.IPs = []netip.Addr{apiServer.IPv4In1}
ipv4WireguadServer.Hostname = apiServer.CountryCode + ".vpn.airdns.org"
servers = append(servers, ipv4WireguadServer)
ipv6WireguadServer := baseWireguardServer
ipv6WireguadServer.IPs = []net.IP{apiServer.IPv6In1}
ipv6WireguadServer.IPs = []netip.Addr{apiServer.IPv6In1}
ipv6WireguadServer.Hostname = apiServer.CountryCode + ".ipv6.vpn.airdns.org"
servers = append(servers, ipv6WireguadServer)
@@ -74,22 +74,22 @@ func (u *Updater) FetchServers(ctx context.Context, minServers int) (
// Ignore IPs 1 and 2 since tls-crypt is superior to tls-auth really.
ipv4In3OpenVPNServer := baseOpenVPNServer
ipv4In3OpenVPNServer.IPs = []net.IP{apiServer.IPv4In3}
ipv4In3OpenVPNServer.IPs = []netip.Addr{apiServer.IPv4In3}
ipv4In3OpenVPNServer.Hostname = apiServer.CountryCode + "3.vpn.airdns.org"
servers = append(servers, ipv4In3OpenVPNServer)
ipv6In3OpenVPNServer := baseOpenVPNServer
ipv6In3OpenVPNServer.IPs = []net.IP{apiServer.IPv6In3}
ipv6In3OpenVPNServer.IPs = []netip.Addr{apiServer.IPv6In3}
ipv6In3OpenVPNServer.Hostname = apiServer.CountryCode + "3.ipv6.vpn.airdns.org"
servers = append(servers, ipv6In3OpenVPNServer)
ipv4In4OpenVPNServer := baseOpenVPNServer
ipv4In4OpenVPNServer.IPs = []net.IP{apiServer.IPv4In4}
ipv4In4OpenVPNServer.IPs = []netip.Addr{apiServer.IPv4In4}
ipv4In4OpenVPNServer.Hostname = apiServer.CountryCode + "4.vpn.airdns.org"
servers = append(servers, ipv4In4OpenVPNServer)
ipv6In4OpenVPNServer := baseOpenVPNServer
ipv6In4OpenVPNServer.IPs = []net.IP{apiServer.IPv6In4}
ipv6In4OpenVPNServer.IPs = []netip.Addr{apiServer.IPv6In4}
ipv6In4OpenVPNServer.Hostname = apiServer.CountryCode + "4.ipv6.vpn.airdns.org"
servers = append(servers, ipv6In4OpenVPNServer)
}

View File

@@ -6,7 +6,7 @@ package common
import (
context "context"
net "net"
netip "net/netip"
reflect "reflect"
gomock "github.com/golang/mock/gomock"
@@ -39,10 +39,10 @@ func (m *MockParallelResolver) EXPECT() *MockParallelResolverMockRecorder {
}
// Resolve mocks base method.
func (m *MockParallelResolver) Resolve(arg0 context.Context, arg1 resolver.ParallelSettings) (map[string][]net.IP, []string, error) {
func (m *MockParallelResolver) Resolve(arg0 context.Context, arg1 resolver.ParallelSettings) (map[string][]netip.Addr, []string, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Resolve", arg0, arg1)
ret0, _ := ret[0].(map[string][]net.IP)
ret0, _ := ret[0].(map[string][]netip.Addr)
ret1, _ := ret[1].([]string)
ret2, _ := ret[2].(error)
return ret0, ret1, ret2

View File

@@ -2,4 +2,5 @@ package common
// Exceptionally, these mocks are exported since they are used by all
// provider subpackages tests, and it reduces test code duplication a lot.
// Note mocks.go might need to be removed before re-generating it.
//go:generate mockgen -destination=mocks.go -package $GOPACKAGE . ParallelResolver,Storage,Unzipper,Warner

View File

@@ -3,7 +3,7 @@ package common
import (
"context"
"errors"
"net"
"net/netip"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/publicip/ipinfo"
@@ -21,7 +21,7 @@ type Fetcher 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 Unzipper interface {
@@ -34,5 +34,5 @@ type Warner interface {
}
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)
}

View File

@@ -1,7 +1,7 @@
package custom
import (
"net"
"net/netip"
"testing"
"github.com/qdm12/gluetun/internal/configuration/settings"
@@ -46,7 +46,7 @@ func Test_modifyConfig(t *testing.T) {
Verbosity: intPtr(0),
}.WithDefaults(providers.Custom),
connection: models.Connection{
IP: net.IPv4(1, 2, 3, 4),
IP: netip.AddrFrom4([4]byte{1, 2, 3, 4}),
Port: 1194,
Protocol: constants.UDP,
},

View File

@@ -1,7 +1,7 @@
package updater
import (
"net"
"net/netip"
"github.com/qdm12/gluetun/internal/constants"
"github.com/qdm12/gluetun/internal/constants/vpn"
@@ -47,7 +47,7 @@ func (hts hostToServer) hostsSlice() (hosts []string) {
return hosts
}
func (hts hostToServer) adaptWithIPs(hostToIPs map[string][]net.IP) {
func (hts hostToServer) adaptWithIPs(hostToIPs map[string][]netip.Addr) {
for host, IPs := range hostToIPs {
server := hts[host]
server.IPs = IPs

View File

@@ -3,7 +3,7 @@ package expressvpn
import (
"errors"
"math/rand"
"net"
"net/netip"
"testing"
"github.com/golang/mock/gomock"
@@ -41,7 +41,7 @@ func Test_Provider_GetConnection(t *testing.T) {
},
"default OpenVPN TCP port": {
filteredServers: []models.Server{
{IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
{IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1})}},
},
selection: settings.ServerSelection{
OpenVPN: settings.OpenVPNSelection{
@@ -52,7 +52,7 @@ func Test_Provider_GetConnection(t *testing.T) {
},
"default OpenVPN UDP port": {
filteredServers: []models.Server{
{IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
{IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1})}},
},
selection: settings.ServerSelection{
OpenVPN: settings.OpenVPNSelection{
@@ -61,14 +61,14 @@ func Test_Provider_GetConnection(t *testing.T) {
}.WithDefaults(provider),
connection: models.Connection{
Type: vpn.OpenVPN,
IP: net.IPv4(1, 1, 1, 1),
IP: netip.AddrFrom4([4]byte{1, 1, 1, 1}),
Port: 1195,
Protocol: constants.UDP,
},
},
"default Wireguard port": {
filteredServers: []models.Server{
{IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
{IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1})}},
},
selection: settings.ServerSelection{
VPN: vpn.Wireguard,

View File

@@ -1,7 +1,7 @@
package updater
import (
"net"
"net/netip"
"github.com/qdm12/gluetun/internal/constants/vpn"
"github.com/qdm12/gluetun/internal/models"
@@ -33,7 +33,7 @@ func (hts hostToServer) toHostsSlice() (hosts []string) {
return hosts
}
func (hts hostToServer) adaptWithIPs(hostToIPs map[string][]net.IP) {
func (hts hostToServer) adaptWithIPs(hostToIPs map[string][]netip.Addr) {
for host, IPs := range hostToIPs {
server := hts[host]
server.IPs = IPs

View File

@@ -1,7 +1,7 @@
package updater
import (
"net"
"net/netip"
"sort"
"github.com/qdm12/gluetun/internal/constants/vpn"
@@ -38,7 +38,7 @@ func (hts hostToServer) toHostsSlice() (hosts []string) {
return hosts
}
func (hts hostToServer) adaptWithIPs(hostToIPs map[string][]net.IP) {
func (hts hostToServer) adaptWithIPs(hostToIPs map[string][]netip.Addr) {
for host, IPs := range hostToIPs {
server := hts[host]
server.IPs = IPs

View File

@@ -1,7 +1,7 @@
package updater
import (
"net"
"net/netip"
"testing"
"github.com/qdm12/gluetun/internal/constants/vpn"
@@ -134,17 +134,17 @@ func Test_hostToServer_adaptWithIPs(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
initialHTS hostToServer
hostToIPs map[string][]net.IP
hostToIPs map[string][]netip.Addr
expectedHTS hostToServer
}{
"create server": {
initialHTS: hostToServer{},
hostToIPs: map[string][]net.IP{
"A": {{1, 2, 3, 4}},
hostToIPs: map[string][]netip.Addr{
"A": {netip.AddrFrom4([4]byte{1, 2, 3, 4})},
},
expectedHTS: hostToServer{
"A": models.Server{
IPs: []net.IP{{1, 2, 3, 4}},
IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 2, 3, 4})},
},
},
},
@@ -154,13 +154,13 @@ func Test_hostToServer_adaptWithIPs(t *testing.T) {
Country: "country",
},
},
hostToIPs: map[string][]net.IP{
"A": {{1, 2, 3, 4}},
hostToIPs: map[string][]netip.Addr{
"A": {netip.AddrFrom4([4]byte{1, 2, 3, 4})},
},
expectedHTS: hostToServer{
"A": models.Server{
Country: "country",
IPs: []net.IP{{1, 2, 3, 4}},
IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 2, 3, 4})},
},
},
},
@@ -170,7 +170,7 @@ func Test_hostToServer_adaptWithIPs(t *testing.T) {
Country: "country",
},
},
hostToIPs: map[string][]net.IP{},
hostToIPs: map[string][]netip.Addr{},
expectedHTS: hostToServer{},
},
}

View File

@@ -3,7 +3,7 @@ package updater
import (
"context"
"errors"
"net"
"net/netip"
"testing"
"time"
@@ -32,7 +32,7 @@ func Test_Updater_GetServers(t *testing.T) {
// Resolution
expectResolve bool
resolverSettings resolver.ParallelSettings
hostToIPs map[string][]net.IP
hostToIPs map[string][]netip.Addr
resolveWarnings []string
resolveErr error
@@ -161,9 +161,9 @@ func Test_Updater_GetServers(t *testing.T) {
SortIPs: true,
},
},
hostToIPs: map[string][]net.IP{
"hosta": {{1, 1, 1, 1}, {2, 2, 2, 2}},
"hostb": {{3, 3, 3, 3}, {4, 4, 4, 4}},
hostToIPs: map[string][]netip.Addr{
"hosta": {netip.AddrFrom4([4]byte{1, 1, 1, 1}), netip.AddrFrom4([4]byte{2, 2, 2, 2})},
"hostb": {netip.AddrFrom4([4]byte{3, 3, 3, 3}), netip.AddrFrom4([4]byte{4, 4, 4, 4})},
},
resolveWarnings: []string{"resolve warning"},
servers: []models.Server{
@@ -173,7 +173,7 @@ func Test_Updater_GetServers(t *testing.T) {
City: "City A",
Hostname: "hosta",
UDP: true,
IPs: []net.IP{{1, 1, 1, 1}, {2, 2, 2, 2}},
IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1}), netip.AddrFrom4([4]byte{2, 2, 2, 2})},
},
{
VPN: vpn.OpenVPN,
@@ -181,7 +181,7 @@ func Test_Updater_GetServers(t *testing.T) {
City: "City B",
Hostname: "hostb",
UDP: true,
IPs: []net.IP{{3, 3, 3, 3}, {4, 4, 4, 4}},
IPs: []netip.Addr{netip.AddrFrom4([4]byte{3, 3, 3, 3}), netip.AddrFrom4([4]byte{4, 4, 4, 4})},
},
},
},

View File

@@ -3,8 +3,8 @@ package ivpn
import (
"errors"
"math/rand"
"net"
"net/http"
"net/netip"
"testing"
"github.com/golang/mock/gomock"
@@ -41,7 +41,7 @@ func Test_Provider_GetConnection(t *testing.T) {
},
"default OpenVPN TCP port": {
filteredServers: []models.Server{
{IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
{IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1})}},
},
selection: settings.ServerSelection{
OpenVPN: settings.OpenVPNSelection{
@@ -50,14 +50,14 @@ func Test_Provider_GetConnection(t *testing.T) {
}.WithDefaults(provider),
connection: models.Connection{
Type: vpn.OpenVPN,
IP: net.IPv4(1, 1, 1, 1),
IP: netip.AddrFrom4([4]byte{1, 1, 1, 1}),
Port: 443,
Protocol: constants.TCP,
},
},
"default OpenVPN UDP port": {
filteredServers: []models.Server{
{IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
{IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1})}},
},
selection: settings.ServerSelection{
OpenVPN: settings.OpenVPNSelection{
@@ -66,21 +66,21 @@ func Test_Provider_GetConnection(t *testing.T) {
}.WithDefaults(provider),
connection: models.Connection{
Type: vpn.OpenVPN,
IP: net.IPv4(1, 1, 1, 1),
IP: netip.AddrFrom4([4]byte{1, 1, 1, 1}),
Port: 1194,
Protocol: constants.UDP,
},
},
"default Wireguard port": {
filteredServers: []models.Server{
{IPs: []net.IP{net.IPv4(1, 1, 1, 1)}, WgPubKey: "x"},
{IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1})}, WgPubKey: "x"},
},
selection: settings.ServerSelection{
VPN: vpn.Wireguard,
}.WithDefaults(provider),
connection: models.Connection{
Type: vpn.Wireguard,
IP: net.IPv4(1, 1, 1, 1),
IP: netip.AddrFrom4([4]byte{1, 1, 1, 1}),
Port: 58237,
Protocol: constants.UDP,
PubKey: "x",

View File

@@ -4,8 +4,8 @@ import (
"context"
"errors"
"io"
"net"
"net/http"
"net/netip"
"strings"
"testing"
"time"
@@ -36,7 +36,7 @@ func Test_Updater_GetServers(t *testing.T) {
// Resolution
expectResolve bool
resolveSettings resolver.ParallelSettings
hostToIPs map[string][]net.IP
hostToIPs map[string][]netip.Addr
resolveWarnings []string
resolveErr error
@@ -109,24 +109,24 @@ func Test_Updater_GetServers(t *testing.T) {
SortIPs: true,
},
},
hostToIPs: map[string][]net.IP{
"hosta": {{1, 1, 1, 1}, {2, 2, 2, 2}},
"hostb": {{3, 3, 3, 3}, {4, 4, 4, 4}},
"hostc": {{5, 5, 5, 5}, {6, 6, 6, 6}},
hostToIPs: map[string][]netip.Addr{
"hosta": {netip.AddrFrom4([4]byte{1, 1, 1, 1}), netip.AddrFrom4([4]byte{2, 2, 2, 2})},
"hostb": {netip.AddrFrom4([4]byte{3, 3, 3, 3}), netip.AddrFrom4([4]byte{4, 4, 4, 4})},
"hostc": {netip.AddrFrom4([4]byte{5, 5, 5, 5}), netip.AddrFrom4([4]byte{6, 6, 6, 6})},
},
resolveWarnings: []string{"resolve warning"},
servers: []models.Server{
{VPN: vpn.OpenVPN, Country: "Country1",
City: "City A", Hostname: "hosta", TCP: true, UDP: true,
IPs: []net.IP{{1, 1, 1, 1}, {2, 2, 2, 2}}},
IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1}), netip.AddrFrom4([4]byte{2, 2, 2, 2})}},
{VPN: vpn.OpenVPN, Country: "Country2",
City: "City B", Hostname: "hostb", TCP: true, UDP: true,
IPs: []net.IP{{3, 3, 3, 3}, {4, 4, 4, 4}}},
IPs: []netip.Addr{netip.AddrFrom4([4]byte{3, 3, 3, 3}), netip.AddrFrom4([4]byte{4, 4, 4, 4})}},
{VPN: vpn.Wireguard,
Country: "Country3", City: "City C",
Hostname: "hostc",
WgPubKey: "xyz",
IPs: []net.IP{{5, 5, 5, 5}, {6, 6, 6, 6}}},
IPs: []netip.Addr{netip.AddrFrom4([4]byte{5, 5, 5, 5}), netip.AddrFrom4([4]byte{6, 6, 6, 6})}},
},
},
}

View File

@@ -3,8 +3,8 @@ package mullvad
import (
"errors"
"math/rand"
"net"
"net/http"
"net/netip"
"testing"
"github.com/golang/mock/gomock"
@@ -41,7 +41,7 @@ func Test_Provider_GetConnection(t *testing.T) {
},
"default OpenVPN TCP port": {
filteredServers: []models.Server{
{IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
{IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1})}},
},
selection: settings.ServerSelection{
OpenVPN: settings.OpenVPNSelection{
@@ -50,14 +50,14 @@ func Test_Provider_GetConnection(t *testing.T) {
}.WithDefaults(provider),
connection: models.Connection{
Type: vpn.OpenVPN,
IP: net.IPv4(1, 1, 1, 1),
IP: netip.AddrFrom4([4]byte{1, 1, 1, 1}),
Port: 443,
Protocol: constants.TCP,
},
},
"default OpenVPN UDP port": {
filteredServers: []models.Server{
{IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
{IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1})}},
},
selection: settings.ServerSelection{
OpenVPN: settings.OpenVPNSelection{
@@ -66,21 +66,21 @@ func Test_Provider_GetConnection(t *testing.T) {
}.WithDefaults(provider),
connection: models.Connection{
Type: vpn.OpenVPN,
IP: net.IPv4(1, 1, 1, 1),
IP: netip.AddrFrom4([4]byte{1, 1, 1, 1}),
Port: 1194,
Protocol: constants.UDP,
},
},
"default Wireguard port": {
filteredServers: []models.Server{
{IPs: []net.IP{net.IPv4(1, 1, 1, 1)}, WgPubKey: "x"},
{IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1})}, WgPubKey: "x"},
},
selection: settings.ServerSelection{
VPN: vpn.Wireguard,
}.WithDefaults(provider),
connection: models.Connection{
Type: vpn.Wireguard,
IP: net.IPv4(1, 1, 1, 1),
IP: netip.AddrFrom4([4]byte{1, 1, 1, 1}),
Port: 51820,
Protocol: constants.UDP,
PubKey: "x",

View File

@@ -3,7 +3,7 @@ package updater
import (
"errors"
"fmt"
"net"
"net/netip"
"strings"
"github.com/qdm12/gluetun/internal/constants/vpn"
@@ -14,8 +14,8 @@ type hostToServer map[string]models.Server
var (
ErrNoIP = errors.New("no IP address for VPN server")
ErrParseIPv4 = errors.New("cannot parse IPv4 address")
ErrParseIPv6 = errors.New("cannot parse IPv6 address")
ErrIPIsNotV4 = errors.New("IP address is not IPv4")
ErrIPIsNotV6 = errors.New("IP address is not IPv6")
ErrVPNTypeNotSupported = errors.New("VPN type not supported")
)
@@ -48,17 +48,21 @@ func (hts hostToServer) add(data serverData) (err error) {
}
if data.IPv4 != "" {
ipv4 := net.ParseIP(data.IPv4)
if ipv4 == nil || ipv4.To4() == nil {
return fmt.Errorf("%w: %s", ErrParseIPv4, data.IPv4)
ipv4, err := netip.ParseAddr(data.IPv4)
if err != nil {
return fmt.Errorf("parsing IPv4 address: %w", err)
} else if !ipv4.Is4() {
return fmt.Errorf("%w: %s", ErrIPIsNotV4, data.IPv4)
}
server.IPs = append(server.IPs, ipv4)
}
if data.IPv6 != "" {
ipv6 := net.ParseIP(data.IPv6)
if ipv6 == nil || ipv6.To4() != nil {
return fmt.Errorf("%w: %s", ErrParseIPv6, data.IPv6)
ipv6, err := netip.ParseAddr(data.IPv6)
if err != nil {
return fmt.Errorf("parsing IPv6 address: %w", err)
} else if !ipv6.Is6() {
return fmt.Errorf("%w: %s", ErrIPIsNotV6, data.IPv6)
}
server.IPs = append(server.IPs, ipv6)
}

View File

@@ -1,29 +1,31 @@
package updater
import (
"bytes"
"net"
"net/netip"
"sort"
)
func uniqueSortedIPs(ips []net.IP) []net.IP {
func uniqueSortedIPs(ips []netip.Addr) []netip.Addr {
uniqueIPs := make(map[string]struct{}, len(ips))
for _, ip := range ips {
key := ip.String()
uniqueIPs[key] = struct{}{}
}
ips = make([]net.IP, 0, len(uniqueIPs))
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)
}
if ip.Is4In6() {
ip = netip.AddrFrom4(ip.As4())
}
ips = append(ips, ip)
}
sort.Slice(ips, func(i, j int) bool {
return bytes.Compare(ips[i], ips[j]) < 0
return ips[i].Compare(ips[j]) < 0
})
return ips

View File

@@ -1,7 +1,7 @@
package updater
import (
"net"
"net/netip"
"testing"
"github.com/stretchr/testify/assert"
@@ -10,24 +10,24 @@ import (
func Test_uniqueSortedIPs(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
inputIPs []net.IP
outputIPs []net.IP
inputIPs []netip.Addr
outputIPs []netip.Addr
}{
"nil": {
inputIPs: nil,
outputIPs: []net.IP{},
outputIPs: []netip.Addr{},
},
"empty": {
inputIPs: []net.IP{},
outputIPs: []net.IP{},
inputIPs: []netip.Addr{},
outputIPs: []netip.Addr{},
},
"single IPv4": {
inputIPs: []net.IP{{1, 1, 1, 1}},
outputIPs: []net.IP{{1, 1, 1, 1}},
inputIPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1})},
outputIPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1})},
},
"two IPv4s": {
inputIPs: []net.IP{{1, 1, 2, 1}, {1, 1, 1, 1}},
outputIPs: []net.IP{{1, 1, 1, 1}, {1, 1, 2, 1}},
inputIPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 2, 1}), netip.AddrFrom4([4]byte{1, 1, 1, 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

@@ -2,15 +2,16 @@ package updater
import (
"fmt"
"net"
"net/netip"
)
func parseIPv4(s string) (ipv4 net.IP, err error) {
ip := net.ParseIP(s)
if ip == nil {
return nil, fmt.Errorf("%w: %q", ErrParseIP, s)
} else if ip.To4() == nil {
return nil, fmt.Errorf("%w: %s", ErrNotIPv4, ip)
func parseIPv4(s string) (ipv4 netip.Addr, err error) {
ipv4, err = netip.ParseAddr(s)
if err != nil {
return ipv4, err
}
return ip, nil
if !ipv4.Is4() {
return ipv4, fmt.Errorf("%w: %s", ErrNotIPv4, ipv4)
}
return ipv4, nil
}

View File

@@ -4,7 +4,7 @@ import (
"context"
"errors"
"fmt"
"net"
"net/netip"
"sort"
"github.com/qdm12/gluetun/internal/constants/vpn"
@@ -13,7 +13,6 @@ import (
)
var (
ErrParseIP = errors.New("cannot parse IP address")
ErrNotIPv4 = errors.New("IP address is not IPv4")
)
@@ -47,7 +46,7 @@ func (u *Updater) FetchServers(ctx context.Context, minServers int) (
Region: jsonServer.Country,
Hostname: jsonServer.Domain,
Number: number,
IPs: []net.IP{ip},
IPs: []netip.Addr{ip},
TCP: jsonServer.Features.TCP,
UDP: jsonServer.Features.UDP,
}

View File

@@ -1,7 +1,7 @@
package updater
import (
"net"
"net/netip"
"github.com/qdm12/gluetun/internal/constants/vpn"
"github.com/qdm12/gluetun/internal/models"
@@ -9,7 +9,7 @@ import (
type cityToServer map[string]models.Server
func (cts cityToServer) add(city string, ips []net.IP) {
func (cts cityToServer) add(city string, ips []netip.Addr) {
server, ok := cts[city]
if !ok {
server.VPN = vpn.OpenVPN

View File

@@ -1,7 +1,7 @@
package updater
import (
"net"
"net/netip"
"github.com/qdm12/gluetun/internal/constants/vpn"
"github.com/qdm12/gluetun/internal/models"
@@ -28,7 +28,7 @@ func (hts hostToServer) toHostsSlice() (hosts []string) {
return hosts
}
func (hts hostToServer) adaptWithIPs(hostToIPs map[string][]net.IP) {
func (hts hostToServer) adaptWithIPs(hostToIPs map[string][]netip.Addr) {
for host, IPs := range hostToIPs {
server := hts[host]
server.IPs = IPs

View File

@@ -2,7 +2,7 @@ package updater
import (
"context"
"net"
"net/netip"
"github.com/qdm12/gluetun/internal/models"
"github.com/qdm12/gluetun/internal/provider/common"
@@ -10,7 +10,7 @@ import (
func setLocationInfo(ctx context.Context, fetcher common.IPFetcher, servers []models.Server) (err error) {
// Get public IP address information
ipsToGetInfo := make([]net.IP, 0, len(servers))
ipsToGetInfo := make([]netip.Addr, 0, len(servers))
for _, server := range servers {
ipsToGetInfo = append(ipsToGetInfo, server.IPs...)
}

View File

@@ -9,6 +9,7 @@ import (
"io"
"net"
"net/http"
"net/netip"
"net/url"
"os"
"strconv"
@@ -21,14 +22,14 @@ import (
)
var (
ErrServerNameNotFound = errors.New("server name not found in servers")
ErrGatewayIPIsNil = errors.New("gateway IP address is nil")
ErrServerNameEmpty = errors.New("server name is empty")
ErrServerNameNotFound = errors.New("server name not found in servers")
ErrGatewayIPIsNotValid = errors.New("gateway IP address is not valid")
ErrServerNameEmpty = errors.New("server name is empty")
)
// PortForward obtains a VPN server side port forwarded from PIA.
func (p *Provider) PortForward(ctx context.Context, client *http.Client,
logger utils.Logger, gateway net.IP, serverName string) (
logger utils.Logger, gateway netip.Addr, serverName string) (
port uint16, err error) {
server, ok := p.storage.GetServerByName(providers.PrivateInternetAccess, serverName)
if !ok {
@@ -40,8 +41,8 @@ func (p *Provider) PortForward(ctx context.Context, client *http.Client,
" (region " + server.Region + ") does not support port forwarding")
return 0, nil
}
if gateway == nil {
return 0, ErrGatewayIPIsNil
if !gateway.IsValid() {
return 0, fmt.Errorf("%w: %s", ErrGatewayIPIsNotValid, gateway)
} else if serverName == "" {
return 0, ErrServerNameEmpty
}
@@ -91,7 +92,7 @@ var (
)
func (p *Provider) KeepPortForward(ctx context.Context,
gateway net.IP, serverName string) (err error) {
gateway netip.Addr, serverName string) (err error) {
privateIPClient, err := newHTTPClient(serverName)
if err != nil {
return fmt.Errorf("creating custom HTTP client: %w", err)
@@ -132,7 +133,7 @@ func (p *Provider) KeepPortForward(ctx context.Context,
}
func refreshPIAPortForwardData(ctx context.Context, client, privateIPClient *http.Client,
gateway net.IP, portForwardPath, authFilePath string) (data piaPortForwardData, err error) {
gateway netip.Addr, portForwardPath, authFilePath string) (data piaPortForwardData, err error) {
data.Token, err = fetchToken(ctx, client, authFilePath)
if err != nil {
return data, fmt.Errorf("fetching token: %w", err)
@@ -314,7 +315,7 @@ func getOpenvpnCredentials(authFilePath string) (
return username, password, nil
}
func fetchPortForwardData(ctx context.Context, client *http.Client, gateway net.IP, token string) (
func fetchPortForwardData(ctx context.Context, client *http.Client, gateway netip.Addr, token string) (
port uint16, signature string, expiration time.Time, err error) {
errSubstitutions := map[string]string{url.QueryEscape(token): "<token>"}
@@ -368,7 +369,7 @@ var (
ErrBadResponse = errors.New("bad response received")
)
func bindPort(ctx context.Context, client *http.Client, gateway net.IP, data piaPortForwardData) (err error) {
func bindPort(ctx context.Context, client *http.Client, gateway netip.Addr, data piaPortForwardData) (err error) {
payload, err := packPayload(data.Port, data.Token, data.Expiration)
if err != nil {
return fmt.Errorf("serializing payload: %w", err)

View File

@@ -7,8 +7,8 @@ import (
"errors"
"fmt"
"io"
"net"
"net/http"
"net/netip"
)
var (
@@ -31,8 +31,8 @@ type regionData struct {
}
type serverData struct {
IP net.IP `json:"ip"`
CN string `json:"cn"`
IP netip.Addr `json:"ip"`
CN string `json:"cn"`
}
func fetchAPI(ctx context.Context, client *http.Client) (

View File

@@ -1,7 +1,7 @@
package updater
import (
"net"
"net/netip"
"github.com/qdm12/gluetun/internal/constants/vpn"
"github.com/qdm12/gluetun/internal/models"
@@ -10,7 +10,7 @@ import (
type nameToServer map[string]models.Server
func (nts nameToServer) add(name, hostname, region string,
tcp, udp, portForward bool, ip net.IP) (change bool) {
tcp, udp, portForward bool, ip netip.Addr) (change bool) {
server, ok := nts[name]
if !ok {
change = true
@@ -32,7 +32,7 @@ func (nts nameToServer) add(name, hostname, region string,
ipFound := false
for _, existingIP := range server.IPs {
if ip.Equal(existingIP) {
if ip == existingIP {
ipFound = true
break
}

View File

@@ -1,7 +1,7 @@
package updater
import (
"net"
"net/netip"
"github.com/qdm12/gluetun/internal/constants/vpn"
"github.com/qdm12/gluetun/internal/models"
@@ -32,7 +32,7 @@ func (hts hostToServer) toHostsSlice() (hosts []string) {
return hosts
}
func (hts hostToServer) adaptWithIPs(hostToIPs map[string][]net.IP) {
func (hts hostToServer) adaptWithIPs(hostToIPs map[string][]netip.Addr) {
for host, IPs := range hostToIPs {
server := hts[host]
server.IPs = IPs

View File

@@ -5,8 +5,8 @@ import (
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"net/netip"
)
var (
@@ -26,8 +26,8 @@ type logicalServer struct {
}
type physicalServer struct {
EntryIP net.IP
ExitIP net.IP
EntryIP netip.Addr
ExitIP netip.Addr
Domain string
Status uint8
}

View File

@@ -1,7 +1,7 @@
package updater
import (
"net"
"net/netip"
"github.com/qdm12/gluetun/internal/constants/vpn"
"github.com/qdm12/gluetun/internal/models"
@@ -10,7 +10,7 @@ import (
type ipToServer map[string]models.Server
func (its ipToServer) add(country, region, city, name, hostname string,
free bool, entryIP net.IP) {
free bool, entryIP netip.Addr) {
key := entryIP.String()
server, ok := its[key]
@@ -27,7 +27,7 @@ func (its ipToServer) add(country, region, city, name, hostname string,
server.Free = free
server.UDP = true
server.TCP = true
server.IPs = []net.IP{entryIP}
server.IPs = []netip.Addr{entryIP}
its[key] = server
}

View File

@@ -2,8 +2,8 @@ package provider
import (
"context"
"net"
"net/http"
"net/netip"
"github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/models"
@@ -22,8 +22,8 @@ type Provider interface {
type PortForwarder interface {
PortForward(ctx context.Context, client *http.Client,
logger utils.Logger, gateway net.IP, serverName string) (
logger utils.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)
}

View File

@@ -1,7 +1,7 @@
package updater
import (
"net"
"net/netip"
"github.com/qdm12/gluetun/internal/constants/vpn"
"github.com/qdm12/gluetun/internal/models"
@@ -32,7 +32,7 @@ func (hts hostToServer) toHostsSlice() (hosts []string) {
return hosts
}
func (hts hostToServer) adaptWithIPs(hostToIPs map[string][]net.IP) {
func (hts hostToServer) adaptWithIPs(hostToIPs map[string][]netip.Addr) {
for host, IPs := range hostToIPs {
server := hts[host]
server.IPs = IPs

View File

@@ -3,7 +3,7 @@ package updater
import (
"context"
"fmt"
"net"
"net/netip"
"sort"
"strings"
@@ -76,7 +76,7 @@ func (u *Updater) FetchServers(ctx context.Context, minServers int) (
servers = hts.toServersSlice()
// Get public IP address information
ipsToGetInfo := make([]net.IP, len(servers))
ipsToGetInfo := make([]netip.Addr, len(servers))
for i := range servers {
ipsToGetInfo[i] = servers[i].IPs[0]
}

View File

@@ -1,7 +1,7 @@
package updater
import (
"net"
"net/netip"
"github.com/qdm12/gluetun/internal/constants/vpn"
"github.com/qdm12/gluetun/internal/models"
@@ -75,7 +75,7 @@ func (hts hostToServers) toHostsSlice() (hosts []string) {
return hosts
}
func (hts hostToServers) adaptWithIPs(hostToIPs map[string][]net.IP) {
func (hts hostToServers) adaptWithIPs(hostToIPs map[string][]netip.Addr) {
for host, IPs := range hostToIPs {
servers := hts[host]
for i := range servers {

View File

@@ -1,7 +1,7 @@
package updater
import (
"net"
"net/netip"
"github.com/qdm12/gluetun/internal/constants/vpn"
"github.com/qdm12/gluetun/internal/models"
@@ -10,7 +10,7 @@ import (
type hostToServer map[string]models.Server
func (hts hostToServer) add(host, country, city string,
tcp, udp bool, ips []net.IP) {
tcp, udp bool, ips []netip.Addr) {
server, ok := hts[host]
if !ok {
server.VPN = vpn.OpenVPN
@@ -39,7 +39,7 @@ func (hts hostToServer) toHostsSlice() (hosts []string) {
return hosts
}
func (hts hostToServer) adaptWithIPs(hostToIPs map[string][]net.IP) {
func (hts hostToServer) adaptWithIPs(hostToIPs map[string][]netip.Addr) {
for host, IPs := range hostToIPs {
server := hts[host]
server.IPs = IPs

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{

View File

@@ -1,7 +1,7 @@
package updater
import (
"net"
"net/netip"
"github.com/qdm12/gluetun/internal/models"
)
@@ -16,7 +16,7 @@ func (hts hostToServer) toHostsSlice() (hosts []string) {
return hosts
}
func (hts hostToServer) adaptWithIPs(hostToIPs map[string][]net.IP) {
func (hts hostToServer) adaptWithIPs(hostToIPs map[string][]netip.Addr) {
for host, IPs := range hostToIPs {
server := hts[host]
server.IPs = IPs

View File

@@ -1,7 +1,7 @@
package updater
import (
"net"
"net/netip"
"github.com/qdm12/gluetun/internal/models"
)
@@ -16,7 +16,7 @@ func (hts hostToServer) toHostsSlice() (hosts []string) {
return hosts
}
func (hts hostToServer) adaptWithIPs(hostToIPs map[string][]net.IP) {
func (hts hostToServer) adaptWithIPs(hostToIPs map[string][]netip.Addr) {
for host, IPs := range hostToIPs {
server := hts[host]
server.IPs = IPs

View File

@@ -1,7 +1,7 @@
package updater
import (
"net"
"net/netip"
"github.com/qdm12/gluetun/internal/constants/vpn"
"github.com/qdm12/gluetun/internal/models"
@@ -33,7 +33,7 @@ func (hts hostToServer) toHostsSlice() (hosts []string) {
return hosts
}
func (hts hostToServer) adaptWithIPs(hostToIPs map[string][]net.IP) {
func (hts hostToServer) adaptWithIPs(hostToIPs map[string][]netip.Addr) {
for host, IPs := range hostToIPs {
server := hts[host]
server.IPs = IPs

View File

@@ -3,7 +3,7 @@ package wevpn
import (
"errors"
"math/rand"
"net"
"net/netip"
"testing"
"github.com/golang/mock/gomock"
@@ -41,7 +41,7 @@ func Test_Provider_GetConnection(t *testing.T) {
},
"default OpenVPN TCP port": {
filteredServers: []models.Server{
{IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
{IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1})}},
},
selection: settings.ServerSelection{
OpenVPN: settings.OpenVPNSelection{
@@ -50,14 +50,14 @@ func Test_Provider_GetConnection(t *testing.T) {
}.WithDefaults(provider),
connection: models.Connection{
Type: vpn.OpenVPN,
IP: net.IPv4(1, 1, 1, 1),
IP: netip.AddrFrom4([4]byte{1, 1, 1, 1}),
Port: 1195,
Protocol: constants.TCP,
},
},
"default OpenVPN UDP port": {
filteredServers: []models.Server{
{IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
{IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1})}},
},
selection: settings.ServerSelection{
OpenVPN: settings.OpenVPNSelection{
@@ -66,14 +66,14 @@ func Test_Provider_GetConnection(t *testing.T) {
}.WithDefaults(provider),
connection: models.Connection{
Type: vpn.OpenVPN,
IP: net.IPv4(1, 1, 1, 1),
IP: netip.AddrFrom4([4]byte{1, 1, 1, 1}),
Port: 1194,
Protocol: constants.UDP,
},
},
"default Wireguard port": {
filteredServers: []models.Server{
{IPs: []net.IP{net.IPv4(1, 1, 1, 1)}, WgPubKey: "x"},
{IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1})}, WgPubKey: "x"},
},
selection: settings.ServerSelection{
VPN: vpn.Wireguard,

View File

@@ -3,8 +3,8 @@ package windscribe
import (
"errors"
"math/rand"
"net"
"net/http"
"net/netip"
"testing"
"github.com/golang/mock/gomock"
@@ -42,7 +42,7 @@ func Test_Provider_GetConnection(t *testing.T) {
},
"default OpenVPN TCP port": {
filteredServers: []models.Server{
{IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
{IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1})}},
},
selection: settings.ServerSelection{
OpenVPN: settings.OpenVPNSelection{
@@ -51,14 +51,14 @@ func Test_Provider_GetConnection(t *testing.T) {
}.WithDefaults(provider),
connection: models.Connection{
Type: vpn.OpenVPN,
IP: net.IPv4(1, 1, 1, 1),
IP: netip.AddrFrom4([4]byte{1, 1, 1, 1}),
Port: 443,
Protocol: constants.TCP,
},
},
"default OpenVPN UDP port": {
filteredServers: []models.Server{
{IPs: []net.IP{net.IPv4(1, 1, 1, 1)}},
{IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1})}},
},
selection: settings.ServerSelection{
OpenVPN: settings.OpenVPNSelection{
@@ -67,21 +67,21 @@ func Test_Provider_GetConnection(t *testing.T) {
}.WithDefaults(provider),
connection: models.Connection{
Type: vpn.OpenVPN,
IP: net.IPv4(1, 1, 1, 1),
IP: netip.AddrFrom4([4]byte{1, 1, 1, 1}),
Port: 1194,
Protocol: constants.UDP,
},
},
"default Wireguard port": {
filteredServers: []models.Server{
{IPs: []net.IP{net.IPv4(1, 1, 1, 1)}, WgPubKey: "x"},
{IPs: []netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1})}, WgPubKey: "x"},
},
selection: settings.ServerSelection{
VPN: vpn.Wireguard,
}.WithDefaults(provider),
connection: models.Connection{
Type: vpn.Wireguard,
IP: net.IPv4(1, 1, 1, 1),
IP: netip.AddrFrom4([4]byte{1, 1, 1, 1}),
Port: 1194,
Protocol: constants.UDP,
PubKey: "x",

View File

@@ -5,8 +5,8 @@ import (
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"net/netip"
"strconv"
"time"
)
@@ -32,10 +32,10 @@ type groupData struct {
}
type serverData struct {
Hostname string `json:"hostname"`
IP net.IP `json:"ip"`
IP2 net.IP `json:"ip2"`
IP3 net.IP `json:"ip3"`
Hostname string `json:"hostname"`
IP netip.Addr `json:"ip"`
IP2 netip.Addr `json:"ip2"`
IP3 netip.Addr `json:"ip3"`
}
func fetchAPI(ctx context.Context, client *http.Client) (

View File

@@ -4,7 +4,7 @@ import (
"context"
"errors"
"fmt"
"net"
"net/netip"
"sort"
"github.com/qdm12/gluetun/internal/constants/vpn"
@@ -30,11 +30,11 @@ func (u *Updater) FetchServers(ctx context.Context, minServers int) (
x5090Name := group.OvpnX509
wgPubKey := group.WgPubKey
for _, node := range group.Nodes {
ips := make([]net.IP, 0, 2) //nolint:gomnd
if node.IP != nil {
ips := make([]netip.Addr, 0, 2) //nolint:gomnd
if node.IP.IsValid() {
ips = append(ips, node.IP)
}
if node.IP2 != nil {
if node.IP2.IsValid() {
ips = append(ips, node.IP2)
}
server := models.Server{
@@ -49,7 +49,7 @@ func (u *Updater) FetchServers(ctx context.Context, minServers int) (
}
servers = append(servers, server)
if node.IP3 == nil { // Wireguard + Stealth
if !node.IP3.IsValid() { // Wireguard + Stealth
continue
} else if wgPubKey == "" {
return nil, fmt.Errorf("%w: for node %s", ErrNoWireguardKey, node.Hostname)
@@ -60,7 +60,7 @@ func (u *Updater) FetchServers(ctx context.Context, minServers int) (
server.TCP = false
server.OvpnX509 = ""
server.WgPubKey = wgPubKey
server.IPs = []net.IP{node.IP3}
server.IPs = []netip.Addr{node.IP3}
servers = append(servers, server)
}
}