diff --git a/internal/provider/utils/wireguard.go b/internal/provider/utils/wireguard.go index bbe93eef..88b949ef 100644 --- a/internal/provider/utils/wireguard.go +++ b/internal/provider/utils/wireguard.go @@ -1,7 +1,6 @@ package utils import ( - "net" "net/netip" "github.com/qdm12/gluetun/internal/configuration/settings" @@ -21,9 +20,7 @@ func BuildWireguardSettings(connection models.Connection, const rulePriority = 101 // 100 is to receive external connections settings.RulePriority = rulePriority - settings.Endpoint = new(net.UDPAddr) - settings.Endpoint.IP = connection.IP.AsSlice() - settings.Endpoint.Port = int(connection.Port) + settings.Endpoint = netip.AddrPortFrom(connection.IP, connection.Port) settings.Addresses = make([]netip.Prefix, 0, len(userSettings.Addresses)) for _, address := range userSettings.Addresses { diff --git a/internal/provider/utils/wireguard_test.go b/internal/provider/utils/wireguard_test.go index f28f9ae8..b6dfb8de 100644 --- a/internal/provider/utils/wireguard_test.go +++ b/internal/provider/utils/wireguard_test.go @@ -1,7 +1,6 @@ package utils import ( - "net" "net/netip" "testing" @@ -43,10 +42,7 @@ func Test_BuildWireguardSettings(t *testing.T) { PrivateKey: "private", PublicKey: "public", PreSharedKey: "pre-shared", - Endpoint: &net.UDPAddr{ - IP: net.IP{1, 2, 3, 4}, - Port: 51821, - }, + Endpoint: netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 51821), Addresses: []netip.Prefix{ netip.PrefixFrom(netip.AddrFrom4([4]byte{1, 1, 1, 1}), 32), }, diff --git a/internal/wireguard/config.go b/internal/wireguard/config.go index f1c3fa71..0e10554a 100644 --- a/internal/wireguard/config.go +++ b/internal/wireguard/config.go @@ -57,7 +57,10 @@ func makeDeviceConfig(settings Settings) (config wgtypes.Config, err error) { *allIPv6(), }, ReplaceAllowedIPs: true, - Endpoint: settings.Endpoint, + Endpoint: &net.UDPAddr{ + IP: settings.Endpoint.Addr().AsSlice(), + Port: int(settings.Endpoint.Port()), + }, }, }, } diff --git a/internal/wireguard/config_test.go b/internal/wireguard/config_test.go index e3a65557..c50ba9ef 100644 --- a/internal/wireguard/config_test.go +++ b/internal/wireguard/config_test.go @@ -3,6 +3,7 @@ package wireguard import ( "errors" "net" + "net/netip" "testing" "github.com/stretchr/testify/assert" @@ -60,10 +61,7 @@ func Test_makeDeviceConfig(t *testing.T) { PublicKey: validKey2, PreSharedKey: validKey3, FirewallMark: 9876, - Endpoint: &net.UDPAddr{ - IP: net.IPv4(99, 99, 99, 99), - Port: 51820, - }, + Endpoint: netip.AddrPortFrom(netip.AddrFrom4([4]byte{99, 99, 99, 99}), 51820), }, config: wgtypes.Config{ PrivateKey: parseKey(t, validKey1), @@ -85,7 +83,7 @@ func Test_makeDeviceConfig(t *testing.T) { }, ReplaceAllowedIPs: true, Endpoint: &net.UDPAddr{ - IP: net.IPv4(99, 99, 99, 99), + IP: net.IP{99, 99, 99, 99}, Port: 51820, }, }, diff --git a/internal/wireguard/constructor_test.go b/internal/wireguard/constructor_test.go index 572f999c..ff87e4db 100644 --- a/internal/wireguard/constructor_test.go +++ b/internal/wireguard/constructor_test.go @@ -1,7 +1,6 @@ package wireguard import ( - "net" "net/netip" "testing" @@ -31,9 +30,7 @@ func Test_New(t *testing.T) { settings: Settings{ PrivateKey: validKeyString, PublicKey: validKeyString, - Endpoint: &net.UDPAddr{ - IP: net.IPv4(1, 2, 3, 4), - }, + Endpoint: netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 0), Addresses: []netip.Prefix{ netip.PrefixFrom(netip.AddrFrom4([4]byte{5, 6, 7, 8}), 32), }, @@ -46,10 +43,7 @@ func Test_New(t *testing.T) { InterfaceName: "wg0", PrivateKey: validKeyString, PublicKey: validKeyString, - Endpoint: &net.UDPAddr{ - IP: net.IPv4(1, 2, 3, 4), - Port: 51820, - }, + Endpoint: netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 51820), Addresses: []netip.Prefix{ netip.PrefixFrom(netip.AddrFrom4([4]byte{5, 6, 7, 8}), 32), }, diff --git a/internal/wireguard/settings.go b/internal/wireguard/settings.go index d33b0053..986e719d 100644 --- a/internal/wireguard/settings.go +++ b/internal/wireguard/settings.go @@ -3,7 +3,6 @@ package wireguard import ( "errors" "fmt" - "net" "net/netip" "regexp" "strings" @@ -22,7 +21,7 @@ type Settings struct { // Pre shared key in base 64 format PreSharedKey string // Wireguard server endpoint to connect to. - Endpoint *net.UDPAddr + Endpoint netip.AddrPort // Addresses assigned to the client. // Note IPv6 addresses are ignored if IPv6 is not supported. Addresses []netip.Prefix @@ -46,9 +45,9 @@ func (s *Settings) SetDefaults() { s.InterfaceName = defaultInterfaceName } - if s.Endpoint != nil && s.Endpoint.Port == 0 { + if s.Endpoint.IsValid() && s.Endpoint.Port() == 0 { const defaultPort = 51820 - s.Endpoint.Port = defaultPort + s.Endpoint = netip.AddrPortFrom(s.Endpoint.Addr(), defaultPort) } if s.FirewallMark == 0 { @@ -74,8 +73,7 @@ var ( ErrPublicKeyMissing = errors.New("public key is missing") ErrPublicKeyInvalid = errors.New("cannot parse public key") ErrPreSharedKeyInvalid = errors.New("cannot parse pre-shared key") - ErrEndpointMissing = errors.New("endpoint is missing") - ErrEndpointIPMissing = errors.New("endpoint IP is missing") + ErrEndpointAddrMissing = errors.New("endpoint address is missing") ErrEndpointPortMissing = errors.New("endpoint port is missing") ErrAddressMissing = errors.New("interface address is missing") ErrAddressNotValid = errors.New("interface address is not valid") @@ -109,11 +107,9 @@ func (s *Settings) Check() (err error) { } switch { - case s.Endpoint == nil: - return fmt.Errorf("%w", ErrEndpointMissing) - case len(s.Endpoint.IP) == 0: - return fmt.Errorf("%w", ErrEndpointIPMissing) - case s.Endpoint.Port == 0: + case !s.Endpoint.Addr().IsValid(): + return fmt.Errorf("%w", ErrEndpointAddrMissing) + case s.Endpoint.Port() == 0: return fmt.Errorf("%w", ErrEndpointPortMissing) } @@ -198,7 +194,7 @@ func (s Settings) ToLines(settings ToLinesSettings) (lines []string) { lines = append(lines, fieldPrefix+"Pre shared key: "+isSet) endpointStr := notSet - if s.Endpoint != nil { + if s.Endpoint.Addr().IsValid() { endpointStr = s.Endpoint.String() } lines = append(lines, fieldPrefix+"Endpoint: "+endpointStr) diff --git a/internal/wireguard/settings_test.go b/internal/wireguard/settings_test.go index b2ec904e..61939ebd 100644 --- a/internal/wireguard/settings_test.go +++ b/internal/wireguard/settings_test.go @@ -2,7 +2,6 @@ package wireguard import ( "errors" - "net" "net/netip" "testing" @@ -29,39 +28,28 @@ func Test_Settings_SetDefaults(t *testing.T) { }, "default endpoint port": { original: Settings{ - Endpoint: &net.UDPAddr{ - IP: net.IPv4(1, 2, 3, 4), - }, + Endpoint: netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 0), }, expected: Settings{ - InterfaceName: "wg0", - FirewallMark: 51820, - Endpoint: &net.UDPAddr{ - IP: net.IPv4(1, 2, 3, 4), - Port: 51820, - }, + InterfaceName: "wg0", + FirewallMark: 51820, + Endpoint: netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 51820), IPv6: ptr(false), Implementation: "auto", }, }, "not empty settings": { original: Settings{ - InterfaceName: "wg1", - FirewallMark: 999, - Endpoint: &net.UDPAddr{ - IP: net.IPv4(1, 2, 3, 4), - Port: 9999, - }, + InterfaceName: "wg1", + FirewallMark: 999, + Endpoint: netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 9999), IPv6: ptr(true), Implementation: "userspace", }, expected: Settings{ - InterfaceName: "wg1", - FirewallMark: 999, - Endpoint: &net.UDPAddr{ - IP: net.IPv4(1, 2, 3, 4), - Port: 9999, - }, + InterfaceName: "wg1", + FirewallMark: 999, + Endpoint: netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 9999), IPv6: ptr(true), Implementation: "userspace", }, @@ -138,31 +126,20 @@ func Test_Settings_Check(t *testing.T) { }, err: errors.New("cannot parse pre-shared key"), }, - "empty endpoint": { + "invalid endpoint address": { settings: Settings{ InterfaceName: "wg0", PrivateKey: validKey1, PublicKey: validKey2, }, - err: ErrEndpointMissing, + err: ErrEndpointAddrMissing, }, - "nil endpoint IP": { + "zero endpoint port": { settings: Settings{ InterfaceName: "wg0", PrivateKey: validKey1, PublicKey: validKey2, - Endpoint: &net.UDPAddr{}, - }, - err: ErrEndpointIPMissing, - }, - "nil endpoint port": { - settings: Settings{ - InterfaceName: "wg0", - PrivateKey: validKey1, - PublicKey: validKey2, - Endpoint: &net.UDPAddr{ - IP: net.IPv4(1, 2, 3, 4), - }, + Endpoint: netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 0), }, err: ErrEndpointPortMissing, }, @@ -171,10 +148,7 @@ func Test_Settings_Check(t *testing.T) { InterfaceName: "wg0", PrivateKey: validKey1, PublicKey: validKey2, - Endpoint: &net.UDPAddr{ - IP: net.IPv4(1, 2, 3, 4), - Port: 51820, - }, + Endpoint: netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 51820), }, err: ErrAddressMissing, }, @@ -183,11 +157,8 @@ func Test_Settings_Check(t *testing.T) { InterfaceName: "wg0", PrivateKey: validKey1, PublicKey: validKey2, - Endpoint: &net.UDPAddr{ - IP: net.IPv4(1, 2, 3, 4), - Port: 51820, - }, - Addresses: []netip.Prefix{{}}, + Endpoint: netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 51820), + Addresses: []netip.Prefix{{}}, }, err: errors.New("interface address is not valid: for address 1 of 1"), }, @@ -196,10 +167,7 @@ func Test_Settings_Check(t *testing.T) { InterfaceName: "wg0", PrivateKey: validKey1, PublicKey: validKey2, - Endpoint: &net.UDPAddr{ - IP: net.IPv4(1, 2, 3, 4), - Port: 51820, - }, + Endpoint: netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 51820), Addresses: []netip.Prefix{ netip.PrefixFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 24), }, @@ -211,10 +179,7 @@ func Test_Settings_Check(t *testing.T) { InterfaceName: "wg0", PrivateKey: validKey1, PublicKey: validKey2, - Endpoint: &net.UDPAddr{ - IP: net.IPv4(1, 2, 3, 4), - Port: 51820, - }, + Endpoint: netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 51820), Addresses: []netip.Prefix{ netip.PrefixFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 24), }, @@ -228,10 +193,7 @@ func Test_Settings_Check(t *testing.T) { InterfaceName: "wg0", PrivateKey: validKey1, PublicKey: validKey2, - Endpoint: &net.UDPAddr{ - IP: net.IPv4(1, 2, 3, 4), - Port: 51820, - }, + Endpoint: netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 51820), Addresses: []netip.Prefix{ netip.PrefixFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 24), }, @@ -331,12 +293,9 @@ func Test_Settings_Lines(t *testing.T) { PrivateKey: "private key", PublicKey: "public key", PreSharedKey: "pre-shared key", - Endpoint: &net.UDPAddr{ - IP: net.IPv4(1, 2, 3, 4), - Port: 51820, - }, - FirewallMark: 999, - RulePriority: 888, + Endpoint: netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 2, 3, 4}), 51820), + FirewallMark: 999, + RulePriority: 888, Addresses: []netip.Prefix{ netip.PrefixFrom(netip.AddrFrom4([4]byte{1, 1, 1, 1}), 24), netip.PrefixFrom(netip.AddrFrom4([4]byte{2, 2, 2, 2}), 32),