2021-04-28 16:55:29 +02:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
2022-05-30 15:31:06 +02:00
|
|
|
"fmt"
|
2022-09-02 00:06:19 +02:00
|
|
|
"net/netip"
|
2021-04-28 16:55:29 +02:00
|
|
|
)
|
|
|
|
|
2021-11-15 19:18:14 +00:00
|
|
|
const (
|
2022-07-29 17:35:21 +02:00
|
|
|
ErrRouteIsNotAvailable = Error("route is not available")
|
2021-11-15 19:18:14 +00:00
|
|
|
)
|
|
|
|
|
2021-11-04 22:11:38 +00:00
|
|
|
// Deprecated: use machine function instead
|
2021-08-21 14:49:46 +01:00
|
|
|
// GetAdvertisedNodeRoutes returns the subnet routes advertised by a node (identified by
|
2021-11-13 08:39:04 +00:00
|
|
|
// namespace and node name).
|
2021-11-13 08:36:45 +00:00
|
|
|
func (h *Headscale) GetAdvertisedNodeRoutes(
|
|
|
|
namespace string,
|
|
|
|
nodeName string,
|
2022-09-02 00:06:19 +02:00
|
|
|
) (*[]netip.Prefix, error) {
|
2021-11-15 16:15:50 +00:00
|
|
|
machine, err := h.GetMachine(namespace, nodeName)
|
2021-04-28 16:55:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-01 16:34:24 +00:00
|
|
|
return &machine.HostInfo.RoutableIPs, nil
|
2021-04-28 16:55:29 +02:00
|
|
|
}
|
|
|
|
|
2021-11-04 22:11:38 +00:00
|
|
|
// Deprecated: use machine function instead
|
2021-08-21 14:49:46 +01:00
|
|
|
// GetEnabledNodeRoutes returns the subnet routes enabled by a node (identified by
|
2021-11-13 08:39:04 +00:00
|
|
|
// namespace and node name).
|
2021-11-13 08:36:45 +00:00
|
|
|
func (h *Headscale) GetEnabledNodeRoutes(
|
|
|
|
namespace string,
|
|
|
|
nodeName string,
|
2022-09-02 00:06:19 +02:00
|
|
|
) ([]netip.Prefix, error) {
|
2021-11-15 16:15:50 +00:00
|
|
|
machine, err := h.GetMachine(namespace, nodeName)
|
2021-04-28 16:55:29 +02:00
|
|
|
if err != nil {
|
2021-05-08 13:59:18 +02:00
|
|
|
return nil, err
|
2021-04-28 16:55:29 +02:00
|
|
|
}
|
2021-08-21 14:49:46 +01:00
|
|
|
|
2022-03-01 16:34:24 +00:00
|
|
|
return machine.EnabledRoutes, nil
|
2021-08-21 14:49:46 +01:00
|
|
|
}
|
|
|
|
|
2021-11-04 22:11:38 +00:00
|
|
|
// Deprecated: use machine function instead
|
2021-11-13 08:39:04 +00:00
|
|
|
// IsNodeRouteEnabled checks if a certain route has been enabled.
|
2021-11-13 08:36:45 +00:00
|
|
|
func (h *Headscale) IsNodeRouteEnabled(
|
|
|
|
namespace string,
|
|
|
|
nodeName string,
|
|
|
|
routeStr string,
|
|
|
|
) bool {
|
2022-09-02 00:06:19 +02:00
|
|
|
route, err := netip.ParsePrefix(routeStr)
|
2021-08-21 14:49:46 +01:00
|
|
|
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 16:46:09 +01:00
|
|
|
|
2021-08-21 14:49:46 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-11-04 22:11:38 +00:00
|
|
|
// Deprecated: use EnableRoute in machine.go
|
2021-08-21 14:49:46 +01:00
|
|
|
// EnableNodeRoute enables a subnet route advertised by a node (identified by
|
2021-11-13 08:39:04 +00:00
|
|
|
// namespace and node name).
|
2021-11-13 08:36:45 +00:00
|
|
|
func (h *Headscale) EnableNodeRoute(
|
|
|
|
namespace string,
|
|
|
|
nodeName string,
|
|
|
|
routeStr string,
|
|
|
|
) error {
|
2021-11-15 16:15:50 +00:00
|
|
|
machine, err := h.GetMachine(namespace, nodeName)
|
2021-08-21 14:49:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-02 00:06:19 +02:00
|
|
|
route, err := netip.ParsePrefix(routeStr)
|
2021-08-21 14:49:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
availableRoutes, err := h.GetAdvertisedNodeRoutes(namespace, nodeName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-28 16:55:29 +02:00
|
|
|
|
2021-08-21 14:49:46 +01: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 16:55:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-21 14:49:46 +01:00
|
|
|
|
|
|
|
if !available {
|
2022-07-29 17:35:21 +02:00
|
|
|
return ErrRouteIsNotAvailable
|
2021-08-21 14:49:46 +01:00
|
|
|
}
|
|
|
|
|
2022-03-01 16:34:24 +00:00
|
|
|
machine.EnabledRoutes = enabledRoutes
|
2022-05-30 15:31:06 +02:00
|
|
|
|
|
|
|
if err := h.db.Save(&machine).Error; err != nil {
|
|
|
|
return fmt.Errorf("failed to update node routes in the database: %w", err)
|
|
|
|
}
|
2021-08-21 14:49:46 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|