mirror of
https://github.com/slackhq/nebula.git
synced 2025-11-12 20:43:57 +01:00
deduplicate relays (#1265)
This commit is contained in:
parent
8adba3960b
commit
028d31c011
@ -70,7 +70,7 @@ func (c *Control) InjectRelays(vpnIp netip.Addr, relayVpnIps []netip.Addr) {
|
|||||||
defer remoteList.Unlock()
|
defer remoteList.Unlock()
|
||||||
c.f.lightHouse.Unlock()
|
c.f.lightHouse.Unlock()
|
||||||
|
|
||||||
remoteList.unlockedSetRelay(vpnIp, vpnIp, relayVpnIps)
|
remoteList.unlockedSetRelay(vpnIp, relayVpnIps)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetFromTun will pull a packet off the tun side of nebula
|
// GetFromTun will pull a packet off the tun side of nebula
|
||||||
|
|||||||
@ -1222,7 +1222,7 @@ func (lhh *LightHouseHandler) handleHostQueryReply(n *NebulaMeta, fromVpnAddrs [
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
am.unlockedSetRelay(fromVpnAddrs[0], certVpnAddr, relays)
|
am.unlockedSetRelay(fromVpnAddrs[0], relays)
|
||||||
am.Unlock()
|
am.Unlock()
|
||||||
|
|
||||||
// Non-blocking attempt to trigger, skip if it would block
|
// Non-blocking attempt to trigger, skip if it would block
|
||||||
@ -1286,7 +1286,7 @@ func (lhh *LightHouseHandler) handleHostUpdateNotification(n *NebulaMeta, fromVp
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
am.unlockedSetRelay(fromVpnAddrs[0], detailsVpnAddr, relays)
|
am.unlockedSetRelay(fromVpnAddrs[0], relays)
|
||||||
am.Unlock()
|
am.Unlock()
|
||||||
|
|
||||||
n = lhh.resetMeta()
|
n = lhh.resetMeta()
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
@ -429,7 +430,7 @@ func (r *RemoteList) unlockedSetV4(ownerVpnIp, vpnIp netip.Addr, to []*V4AddrPor
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RemoteList) unlockedSetRelay(ownerVpnIp, vpnIp netip.Addr, to []netip.Addr) {
|
func (r *RemoteList) unlockedSetRelay(ownerVpnIp netip.Addr, to []netip.Addr) {
|
||||||
r.shouldRebuild = true
|
r.shouldRebuild = true
|
||||||
c := r.unlockedGetOrMakeRelay(ownerVpnIp)
|
c := r.unlockedGetOrMakeRelay(ownerVpnIp)
|
||||||
|
|
||||||
@ -595,6 +596,21 @@ func (r *RemoteList) unlockedCollect() {
|
|||||||
|
|
||||||
// unlockedSort assumes you have the write lock and performs the deduping and sorting of the address list
|
// unlockedSort assumes you have the write lock and performs the deduping and sorting of the address list
|
||||||
func (r *RemoteList) unlockedSort(preferredRanges []netip.Prefix) {
|
func (r *RemoteList) unlockedSort(preferredRanges []netip.Prefix) {
|
||||||
|
// Use a map to deduplicate any relay addresses
|
||||||
|
dedupedRelays := map[netip.Addr]struct{}{}
|
||||||
|
for _, relay := range r.relays {
|
||||||
|
dedupedRelays[relay] = struct{}{}
|
||||||
|
}
|
||||||
|
r.relays = r.relays[:0]
|
||||||
|
for relay := range dedupedRelays {
|
||||||
|
r.relays = append(r.relays, relay)
|
||||||
|
}
|
||||||
|
// Put them in a somewhat consistent order after de-duplication
|
||||||
|
slices.SortFunc(r.relays, func(a, b netip.Addr) int {
|
||||||
|
return a.Compare(b)
|
||||||
|
})
|
||||||
|
|
||||||
|
// Now the addrs
|
||||||
n := len(r.addrs)
|
n := len(r.addrs)
|
||||||
if n < 2 {
|
if n < 2 {
|
||||||
return
|
return
|
||||||
|
|||||||
@ -41,6 +41,16 @@ func TestRemoteList_Rebuild(t *testing.T) {
|
|||||||
func(netip.Addr, *V6AddrPort) bool { return true },
|
func(netip.Addr, *V6AddrPort) bool { return true },
|
||||||
)
|
)
|
||||||
|
|
||||||
|
rl.unlockedSetRelay(
|
||||||
|
netip.MustParseAddr("0.0.0.1"),
|
||||||
|
[]netip.Addr{
|
||||||
|
netip.MustParseAddr("1::1"),
|
||||||
|
netip.MustParseAddr("1.2.3.4"),
|
||||||
|
netip.MustParseAddr("1.2.3.4"),
|
||||||
|
netip.MustParseAddr("1::1"),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
rl.Rebuild([]netip.Prefix{})
|
rl.Rebuild([]netip.Prefix{})
|
||||||
assert.Len(t, rl.addrs, 10, "addrs contains too many entries")
|
assert.Len(t, rl.addrs, 10, "addrs contains too many entries")
|
||||||
|
|
||||||
@ -76,6 +86,11 @@ func TestRemoteList_Rebuild(t *testing.T) {
|
|||||||
assert.Equal(t, "[1::1]:2", rl.addrs[8].String())
|
assert.Equal(t, "[1::1]:2", rl.addrs[8].String())
|
||||||
assert.Equal(t, "[1:100::1]:1", rl.addrs[9].String())
|
assert.Equal(t, "[1:100::1]:1", rl.addrs[9].String())
|
||||||
|
|
||||||
|
// assert relay deduplicated
|
||||||
|
assert.Len(t, rl.relays, 2)
|
||||||
|
assert.Equal(t, "1.2.3.4", rl.relays[0].String())
|
||||||
|
assert.Equal(t, "1::1", rl.relays[1].String())
|
||||||
|
|
||||||
// Ensure we can hoist a specific ipv4 range over anything else
|
// Ensure we can hoist a specific ipv4 range over anything else
|
||||||
rl.Rebuild([]netip.Prefix{netip.MustParsePrefix("172.17.0.0/16")})
|
rl.Rebuild([]netip.Prefix{netip.MustParsePrefix("172.17.0.0/16")})
|
||||||
assert.Len(t, rl.addrs, 10, "addrs contains too many entries")
|
assert.Len(t, rl.addrs, 10, "addrs contains too many entries")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user