headscale/routes.go

115 lines
2.5 KiB
Go
Raw Normal View History

2021-04-28 22:55:29 +08:00
package headscale
import (
"inet.af/netaddr"
)
2021-11-16 03:18:14 +08:00
const (
errRouteIsNotAvailable = Error("route is not available")
)
// Deprecated: use machine function instead
// GetAdvertisedNodeRoutes returns the subnet routes advertised by a node (identified by
2021-11-13 16:39:04 +08:00
// namespace and node name).
2021-11-13 16:36:45 +08:00
func (h *Headscale) GetAdvertisedNodeRoutes(
namespace string,
nodeName string,
) (*[]netaddr.IPPrefix, error) {
2021-11-16 00:15:50 +08:00
machine, err := h.GetMachine(namespace, nodeName)
2021-04-28 22:55:29 +08:00
if err != nil {
return nil, err
}
2022-03-02 00:34:24 +08:00
return &machine.HostInfo.RoutableIPs, nil
2021-04-28 22:55:29 +08:00
}
// Deprecated: use machine function instead
// GetEnabledNodeRoutes returns the subnet routes enabled by a node (identified by
2021-11-13 16:39:04 +08:00
// namespace and node name).
2021-11-13 16:36:45 +08:00
func (h *Headscale) GetEnabledNodeRoutes(
namespace string,
nodeName string,
) ([]netaddr.IPPrefix, error) {
2021-11-16 00:15:50 +08:00
machine, err := h.GetMachine(namespace, nodeName)
2021-04-28 22:55:29 +08:00
if err != nil {
return nil, err
2021-04-28 22:55:29 +08:00
}
2022-03-02 00:34:24 +08:00
return machine.EnabledRoutes, nil
}
// Deprecated: use machine function instead
2021-11-13 16:39:04 +08:00
// IsNodeRouteEnabled checks if a certain route has been enabled.
2021-11-13 16:36:45 +08:00
func (h *Headscale) IsNodeRouteEnabled(
namespace string,
nodeName string,
routeStr string,
) bool {
route, err := netaddr.ParseIPPrefix(routeStr)
if err != nil {
return false
}
enabledRoutes, err := h.GetEnabledNodeRoutes(namespace, nodeName)
if err != nil {
return false
}
for _, enabledRoute := range enabledRoutes {
if route == enabledRoute {
return true
}
}
2021-11-14 23:46:09 +08:00
return false
}
// Deprecated: use EnableRoute in machine.go
// EnableNodeRoute enables a subnet route advertised by a node (identified by
2021-11-13 16:39:04 +08:00
// namespace and node name).
2021-11-13 16:36:45 +08:00
func (h *Headscale) EnableNodeRoute(
namespace string,
nodeName string,
routeStr string,
) error {
2021-11-16 00:15:50 +08:00
machine, err := h.GetMachine(namespace, nodeName)
if err != nil {
return err
}
route, err := netaddr.ParseIPPrefix(routeStr)
if err != nil {
return err
}
availableRoutes, err := h.GetAdvertisedNodeRoutes(namespace, nodeName)
if err != nil {
return err
}
2021-04-28 22:55:29 +08:00
enabledRoutes, err := h.GetEnabledNodeRoutes(namespace, nodeName)
if err != nil {
return err
}
available := false
for _, availableRoute := range *availableRoutes {
// If the route is available, and not yet enabled, add it to the new routing table
if route == availableRoute {
available = true
if !h.IsNodeRouteEnabled(namespace, nodeName, routeStr) {
enabledRoutes = append(enabledRoutes, route)
2021-04-28 22:55:29 +08:00
}
}
}
if !available {
2021-11-16 03:18:14 +08:00
return errRouteIsNotAvailable
}
2022-03-02 00:34:24 +08:00
machine.EnabledRoutes = enabledRoutes
2021-11-16 00:15:50 +08:00
h.db.Save(&machine)
return nil
}