- Preparing for demo
This commit is contained in:
Tim Beatham 2024-01-17 13:50:18 +00:00
parent 3f82ef9cd7
commit 41d41694a6
3 changed files with 26 additions and 24 deletions

View File

@ -141,7 +141,14 @@ func (m *ConnectionManagerImpl) HasConnection(endPoint string) bool {
// RemoveConnection removes the given connection if it exists // RemoveConnection removes the given connection if it exists
func (m *ConnectionManagerImpl) RemoveConnection(endPoint string) error { func (m *ConnectionManagerImpl) RemoveConnection(endPoint string) error {
m.conLoc.Lock() m.conLoc.Lock()
err := m.clientConnections[endPoint].Close() connection, ok := m.clientConnections[endPoint]
if !ok {
// nothing to do if no connection
return nil
}
err := connection.Close()
delete(m.clientConnections, endPoint) delete(m.clientConnections, endPoint)
m.conLoc.Unlock() m.conLoc.Unlock()

View File

@ -17,7 +17,7 @@ func HashString(value string) int {
} }
// ConsistentHash implementation. Traverse the values until we find a key // ConsistentHash implementation. Traverse the values until we find a key
// less than ours. // greater than ours.
func ConsistentHash[V any, K any](values []V, client K, bucketFunc func(V) int, keyFunc func(K) int) V { func ConsistentHash[V any, K any](values []V, client K, bucketFunc func(V) int, keyFunc func(K) int) V {
if len(values) == 0 { if len(values) == 0 {
panic("values is empty") panic("values is empty")

View File

@ -35,12 +35,11 @@ type routeNode struct {
} }
type convertMeshNodeParams struct { type convertMeshNodeParams struct {
node MeshNode node MeshNode
correspondingPeer MeshNode mesh MeshProvider
mesh MeshProvider device *wgtypes.Device
device *wgtypes.Device peerToClients map[string][]net.IPNet
peerToClients map[string][]net.IPNet routes map[string][]routeNode
routes map[string][]routeNode
} }
func (m *WgMeshConfigApplyer) convertMeshNode(params convertMeshNodeParams) (*wgtypes.PeerConfig, error) { func (m *WgMeshConfigApplyer) convertMeshNode(params convertMeshNodeParams) (*wgtypes.PeerConfig, error) {
@ -59,8 +58,7 @@ func (m *WgMeshConfigApplyer) convertMeshNode(params convertMeshNodeParams) (*wg
allowedips = append(allowedips, clients...) allowedips = append(allowedips, clients...)
} }
for _, route := range params.node.GetRoutes() { for _, bestRoutes := range lib.MapValues(params.routes) {
bestRoutes := params.routes[route.GetDestination().String()]
var pickedRoute routeNode var pickedRoute routeNode
if len(bestRoutes) == 1 { if len(bestRoutes) == 1 {
@ -70,8 +68,7 @@ func (m *WgMeshConfigApplyer) convertMeshNode(params convertMeshNodeParams) (*wg
return lib.HashString(rn.gateway) return lib.HashString(rn.gateway)
} }
// Else there is more than one candidate so consistently hash pickedRoute = lib.ConsistentHash(bestRoutes, params.node, bucketFunc, m.hashFunc)
pickedRoute = lib.ConsistentHash(bestRoutes, params.correspondingPeer, bucketFunc, m.hashFunc)
} }
if pickedRoute.gateway == pubKey.String() { if pickedRoute.gateway == pubKey.String() {
@ -346,12 +343,11 @@ func (m *WgMeshConfigApplyer) getPeerConfig(params *GetConfigParams) (*wgtypes.C
peerToClients[pubKey.String()] = append(clients, *n.GetWgHost()) peerToClients[pubKey.String()] = append(clients, *n.GetWgHost())
cfg, err := m.convertMeshNode(convertMeshNodeParams{ cfg, err := m.convertMeshNode(convertMeshNodeParams{
node: n, node: n,
correspondingPeer: peer, mesh: params.mesh,
mesh: params.mesh, device: params.dev,
device: params.dev, peerToClients: peerToClients,
peerToClients: peerToClients, routes: params.routes,
routes: params.routes,
}) })
if err != nil { if err != nil {
@ -372,12 +368,11 @@ func (m *WgMeshConfigApplyer) getPeerConfig(params *GetConfigParams) (*wgtypes.C
} }
peer, err := m.convertMeshNode(convertMeshNodeParams{ peer, err := m.convertMeshNode(convertMeshNodeParams{
node: n, node: n,
correspondingPeer: self, mesh: params.mesh,
mesh: params.mesh, peerToClients: peerToClients,
peerToClients: peerToClients, routes: params.routes,
routes: params.routes, device: params.dev,
device: params.dev,
}) })
if err != nil { if err != nil {