mirror of
https://github.com/tim-beatham/smegmesh.git
synced 2025-02-10 14:29:22 +01:00
47-default-routing
Implementing default routing so that all traffic goes out of an exit point.
This commit is contained in:
parent
92c0805275
commit
0058c9f4c9
@ -47,6 +47,8 @@ type WgMeshConfiguration struct {
|
|||||||
IPDiscovery IPDiscovery `yaml:"ipDiscovery"`
|
IPDiscovery IPDiscovery `yaml:"ipDiscovery"`
|
||||||
// AdvertiseRoutes advertises other meshes if the node is in multiple meshes
|
// AdvertiseRoutes advertises other meshes if the node is in multiple meshes
|
||||||
AdvertiseRoutes bool `yaml:"advertiseRoutes"`
|
AdvertiseRoutes bool `yaml:"advertiseRoutes"`
|
||||||
|
// AdvertiseDefaultRoute advertises a default route out of the mesh.
|
||||||
|
AdvertiseDefaultRoute bool `yaml:"advertiseDefaults"`
|
||||||
// Endpoint is the IP in which this computer is publicly reachable.
|
// Endpoint is the IP in which this computer is publicly reachable.
|
||||||
// usecase is when the node has multiple IP addresses
|
// usecase is when the node has multiple IP addresses
|
||||||
Endpoint string `yaml:"publicEndpoint"`
|
Endpoint string `yaml:"publicEndpoint"`
|
||||||
|
@ -116,7 +116,6 @@ func (m *WgMeshConfigApplyer) getRoutes(meshProvider MeshProvider) map[string][]
|
|||||||
meshPrefixes := lib.Map(lib.MapValues(m.meshManager.GetMeshes()), func(mesh MeshProvider) *net.IPNet {
|
meshPrefixes := lib.Map(lib.MapValues(m.meshManager.GetMeshes()), func(mesh MeshProvider) *net.IPNet {
|
||||||
ula := &ip.ULABuilder{}
|
ula := &ip.ULABuilder{}
|
||||||
ipNet, _ := ula.GetIPNet(mesh.GetMeshId())
|
ipNet, _ := ula.GetIPNet(mesh.GetMeshId())
|
||||||
|
|
||||||
return ipNet
|
return ipNet
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -125,6 +124,12 @@ func (m *WgMeshConfigApplyer) getRoutes(meshProvider MeshProvider) map[string][]
|
|||||||
|
|
||||||
for _, route := range node.GetRoutes() {
|
for _, route := range node.GetRoutes() {
|
||||||
if lib.Contains(meshPrefixes, func(prefix *net.IPNet) bool {
|
if lib.Contains(meshPrefixes, func(prefix *net.IPNet) bool {
|
||||||
|
defaultRoute, _, _ := net.ParseCIDR("::/0")
|
||||||
|
|
||||||
|
if prefix.IP.Equal(defaultRoute) && m.config.AdvertiseDefaultRoute {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
return prefix.Contains(route.GetDestination().IP)
|
return prefix.Contains(route.GetDestination().IP)
|
||||||
}) {
|
}) {
|
||||||
continue
|
continue
|
||||||
@ -168,6 +173,10 @@ func (m *WgMeshConfigApplyer) getCorrespondingPeer(peers []MeshNode, client Mesh
|
|||||||
|
|
||||||
func (m *WgMeshConfigApplyer) getClientConfig(mesh MeshProvider, peers []MeshNode, clients []MeshNode) (*wgtypes.Config, error) {
|
func (m *WgMeshConfigApplyer) getClientConfig(mesh MeshProvider, peers []MeshNode, clients []MeshNode) (*wgtypes.Config, error) {
|
||||||
self, err := m.meshManager.GetSelf(mesh.GetMeshId())
|
self, err := m.meshManager.GetSelf(mesh.GetMeshId())
|
||||||
|
routes := lib.Map(lib.MapKeys(m.getRoutes(mesh)), func(destination string) net.IPNet {
|
||||||
|
_, ipNet, _ := net.ParseCIDR(destination)
|
||||||
|
return *ipNet
|
||||||
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -184,17 +193,13 @@ func (m *WgMeshConfigApplyer) getClientConfig(mesh MeshProvider, peers []MeshNod
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
allowedips := make([]net.IPNet, 1)
|
|
||||||
_, ipnet, _ := net.ParseCIDR("::/0")
|
|
||||||
allowedips[0] = *ipnet
|
|
||||||
|
|
||||||
peerCfgs := make([]wgtypes.PeerConfig, 1)
|
peerCfgs := make([]wgtypes.PeerConfig, 1)
|
||||||
|
|
||||||
peerCfgs[0] = wgtypes.PeerConfig{
|
peerCfgs[0] = wgtypes.PeerConfig{
|
||||||
PublicKey: pubKey,
|
PublicKey: pubKey,
|
||||||
Endpoint: endpoint,
|
Endpoint: endpoint,
|
||||||
PersistentKeepaliveInterval: &keepAlive,
|
PersistentKeepaliveInterval: &keepAlive,
|
||||||
AllowedIPs: allowedips,
|
AllowedIPs: routes,
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg := wgtypes.Config{
|
cfg := wgtypes.Config{
|
||||||
|
@ -471,7 +471,7 @@ func NewMeshManager(params *NewMeshManagerParams) MeshManager {
|
|||||||
m.RouteManager = params.RouteManager
|
m.RouteManager = params.RouteManager
|
||||||
|
|
||||||
if m.RouteManager == nil {
|
if m.RouteManager == nil {
|
||||||
m.RouteManager = NewRouteManager(m)
|
m.RouteManager = NewRouteManager(m, ¶ms.Conf)
|
||||||
}
|
}
|
||||||
|
|
||||||
m.idGenerator = params.IdGenerator
|
m.idGenerator = params.IdGenerator
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package mesh
|
package mesh
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/tim-beatham/wgmesh/pkg/conf"
|
||||||
"github.com/tim-beatham/wgmesh/pkg/ip"
|
"github.com/tim-beatham/wgmesh/pkg/ip"
|
||||||
"github.com/tim-beatham/wgmesh/pkg/lib"
|
"github.com/tim-beatham/wgmesh/pkg/lib"
|
||||||
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
||||||
@ -13,6 +16,7 @@ type RouteManager interface {
|
|||||||
|
|
||||||
type RouteManagerImpl struct {
|
type RouteManagerImpl struct {
|
||||||
meshManager MeshManager
|
meshManager MeshManager
|
||||||
|
conf *conf.WgMeshConfiguration
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RouteManagerImpl) UpdateRoutes() error {
|
func (r *RouteManagerImpl) UpdateRoutes() error {
|
||||||
@ -32,12 +36,22 @@ func (r *RouteManagerImpl) UpdateRoutes() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
routes, err := mesh1.GetRoutes(pubKey.String())
|
routeMap, err := mesh1.GetRoutes(pubKey.String())
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if r.conf.AdvertiseDefaultRoute {
|
||||||
|
_, defaultRoute, _ := net.ParseCIDR("::/0")
|
||||||
|
|
||||||
|
mesh1.AddRoutes(NodeID(self), &RouteStub{
|
||||||
|
Destination: defaultRoute,
|
||||||
|
HopCount: 0,
|
||||||
|
Path: make([]string, 0),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
for _, mesh2 := range meshes {
|
for _, mesh2 := range meshes {
|
||||||
if mesh1 == mesh2 {
|
if mesh1 == mesh2 {
|
||||||
continue
|
continue
|
||||||
@ -50,7 +64,9 @@ func (r *RouteManagerImpl) UpdateRoutes() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = mesh2.AddRoutes(NodeID(self), append(lib.MapValues(routes), &RouteStub{
|
routes := lib.MapValues(routeMap)
|
||||||
|
|
||||||
|
err = mesh2.AddRoutes(NodeID(self), append(routes, &RouteStub{
|
||||||
Destination: ipNet,
|
Destination: ipNet,
|
||||||
HopCount: 0,
|
HopCount: 0,
|
||||||
Path: make([]string, 0),
|
Path: make([]string, 0),
|
||||||
@ -88,6 +104,6 @@ func (r *RouteManagerImpl) RemoveRoutes(meshId string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRouteManager(m MeshManager) RouteManager {
|
func NewRouteManager(m MeshManager, conf *conf.WgMeshConfiguration) RouteManager {
|
||||||
return &RouteManagerImpl{meshManager: m}
|
return &RouteManagerImpl{meshManager: m, conf: conf}
|
||||||
}
|
}
|
||||||
|
@ -173,7 +173,7 @@ type MeshProviderFactory interface {
|
|||||||
// MeshNodeFactoryParams are the parameters required to construct
|
// MeshNodeFactoryParams are the parameters required to construct
|
||||||
// a mesh node
|
// a mesh node
|
||||||
type MeshNodeFactoryParams struct {
|
type MeshNodeFactoryParams struct {
|
||||||
PublicKey *wgtypes.Key
|
PublicKey *wgtypes.Key
|
||||||
NodeIP net.IP
|
NodeIP net.IP
|
||||||
WgPort int
|
WgPort int
|
||||||
Endpoint string
|
Endpoint string
|
||||||
|
Loading…
Reference in New Issue
Block a user