mirror of
https://github.com/juanfont/headscale.git
synced 2024-11-27 12:05:26 +00:00
Fix IP Address Order Bug
This commit is contained in:
parent
23a3adf8d2
commit
6567af7730
@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -73,7 +74,23 @@ type (
|
|||||||
|
|
||||||
type MachineAddresses []netip.Addr
|
type MachineAddresses []netip.Addr
|
||||||
|
|
||||||
|
func (ma MachineAddresses) Sort() {
|
||||||
|
sort.Slice(ma, func(index1, index2 int) bool {
|
||||||
|
if ma[index1].Is4() && ma[index2].Is6() {
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if ma[index1].Is6() && ma[index2].Is4() {
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return ma[index1].Compare(ma[index2]) < 0
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func (ma MachineAddresses) StringSlice() []string {
|
func (ma MachineAddresses) StringSlice() []string {
|
||||||
|
ma.Sort()
|
||||||
strSlice := make([]string, 0, len(ma))
|
strSlice := make([]string, 0, len(ma))
|
||||||
for _, addr := range ma {
|
for _, addr := range ma {
|
||||||
strSlice = append(strSlice, addr.String())
|
strSlice = append(strSlice, addr.String())
|
||||||
|
@ -113,3 +113,29 @@ func Test_MachineCanAccess(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMachineAddressesOrder(t *testing.T) {
|
||||||
|
machineAddresses := MachineAddresses{
|
||||||
|
netip.MustParseAddr("2001:db8::2"),
|
||||||
|
netip.MustParseAddr("100.64.0.2"),
|
||||||
|
netip.MustParseAddr("2001:db8::1"),
|
||||||
|
netip.MustParseAddr("100.64.0.1"),
|
||||||
|
}
|
||||||
|
|
||||||
|
strSlice := machineAddresses.StringSlice()
|
||||||
|
expected := []string{
|
||||||
|
"100.64.0.1",
|
||||||
|
"100.64.0.2",
|
||||||
|
"2001:db8::1",
|
||||||
|
"2001:db8::2",
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(strSlice) != len(expected) {
|
||||||
|
t.Fatalf("unexpected slice length: got %v, want %v", len(strSlice), len(expected))
|
||||||
|
}
|
||||||
|
for i, addr := range strSlice {
|
||||||
|
if addr != expected[i] {
|
||||||
|
t.Errorf("unexpected address at index %v: got %v, want %v", i, addr, expected[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user