2023-10-26 17:53:12 +02:00
|
|
|
package mesh
|
|
|
|
|
|
|
|
import (
|
2023-10-27 18:49:18 +02:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/ip"
|
2023-11-07 20:48:53 +01:00
|
|
|
"github.com/tim-beatham/wgmesh/pkg/lib"
|
2023-10-27 18:49:18 +02:00
|
|
|
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
2023-10-26 17:53:12 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type RouteManager interface {
|
|
|
|
UpdateRoutes() error
|
2023-11-07 20:48:53 +01:00
|
|
|
RemoveRoutes(meshId string) error
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type RouteManagerImpl struct {
|
2023-11-25 04:15:58 +01:00
|
|
|
meshManager MeshManager
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RouteManagerImpl) UpdateRoutes() error {
|
2023-11-05 19:03:58 +01:00
|
|
|
meshes := r.meshManager.GetMeshes()
|
2023-10-27 18:49:18 +02:00
|
|
|
ulaBuilder := new(ip.ULABuilder)
|
2023-10-26 17:53:12 +02:00
|
|
|
|
2023-10-27 18:49:18 +02:00
|
|
|
for _, mesh1 := range meshes {
|
2023-11-25 04:15:58 +01:00
|
|
|
self, err := r.meshManager.GetSelf(mesh1.GetMeshId())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pubKey, err := self.GetPublicKey()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
routes, err := mesh1.GetRoutes(pubKey.String())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-27 18:49:18 +02:00
|
|
|
for _, mesh2 := range meshes {
|
|
|
|
if mesh1 == mesh2 {
|
|
|
|
continue
|
|
|
|
}
|
2023-10-26 17:53:12 +02:00
|
|
|
|
2023-10-27 18:49:18 +02:00
|
|
|
ipNet, err := ulaBuilder.GetIPNet(mesh2.GetMeshId())
|
2023-10-26 17:53:12 +02:00
|
|
|
|
2023-10-27 18:49:18 +02:00
|
|
|
if err != nil {
|
|
|
|
logging.Log.WriteErrorf(err.Error())
|
|
|
|
return err
|
|
|
|
}
|
2023-10-26 17:53:12 +02:00
|
|
|
|
2023-11-25 04:15:58 +01:00
|
|
|
err = mesh2.AddRoutes(NodeID(self), append(lib.MapValues(routes), &RouteStub{
|
|
|
|
Destination: ipNet,
|
|
|
|
HopCount: 0,
|
2023-11-27 19:55:41 +01:00
|
|
|
Path: make([]string, 0),
|
2023-11-25 04:15:58 +01:00
|
|
|
})...)
|
2023-10-26 17:53:12 +02:00
|
|
|
|
2023-10-27 18:49:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-26 17:53:12 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-07 20:48:53 +01:00
|
|
|
// removeRoutes: removes all meshes we are no longer a part of
|
|
|
|
func (r *RouteManagerImpl) RemoveRoutes(meshId string) error {
|
|
|
|
ulaBuilder := new(ip.ULABuilder)
|
|
|
|
meshes := r.meshManager.GetMeshes()
|
|
|
|
|
|
|
|
ipNet, err := ulaBuilder.GetIPNet(meshId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mesh1 := range meshes {
|
|
|
|
self, err := r.meshManager.GetSelf(meshId)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-24 18:49:06 +01:00
|
|
|
mesh1.RemoveRoutes(NodeID(self), ipNet.String())
|
2023-11-07 20:48:53 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-05 19:03:58 +01:00
|
|
|
func NewRouteManager(m MeshManager) RouteManager {
|
2023-11-25 04:15:58 +01:00
|
|
|
return &RouteManagerImpl{meshManager: m}
|
2023-10-26 17:53:12 +02:00
|
|
|
}
|