feat(ipv6): prefer IPv6 endpoints when IPv6 is supported

This commit is contained in:
Quentin McGaw
2026-05-04 13:21:52 +00:00
parent 3e59447e58
commit aab10f9d3f
2 changed files with 15 additions and 1 deletions
+14
View File
@@ -2,6 +2,7 @@ package utils
import ( import (
"fmt" "fmt"
"slices"
"github.com/qdm12/gluetun/internal/configuration/settings" "github.com/qdm12/gluetun/internal/configuration/settings"
"github.com/qdm12/gluetun/internal/constants/vpn" "github.com/qdm12/gluetun/internal/constants/vpn"
@@ -75,5 +76,18 @@ func GetConnection(provider string,
} }
} }
slices.SortStableFunc(connections, func(a, b models.Connection) int {
aIPv6 := a.IP.Is6()
bIPv6 := b.IP.Is6()
switch {
case aIPv6 && !bIPv6:
return -1
case !aIPv6 && bIPv6:
return 1
default:
return 0
}
})
return pickConnection(connections, selection, connPicker) return pickConnection(connections, selection, connPicker)
} }
+1 -1
View File
@@ -124,8 +124,8 @@ func Test_GetConnection(t *testing.T) {
VPN: vpn.OpenVPN, VPN: vpn.OpenVPN,
UDP: true, UDP: true,
IPs: []netip.Addr{ IPs: []netip.Addr{
netip.IPv6Unspecified(),
netip.AddrFrom4([4]byte{1, 1, 1, 1}), netip.AddrFrom4([4]byte{1, 1, 1, 1}),
netip.IPv6Unspecified(),
}, },
}, },
}, },