2021-04-28 08:55:29 -06:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
2021-05-14 16:05:41 -06:00
|
|
|
"gorm.io/datatypes"
|
2021-04-28 08:55:29 -06:00
|
|
|
"inet.af/netaddr"
|
|
|
|
)
|
|
|
|
|
2021-11-15 12:18:14 -07:00
|
|
|
const (
|
|
|
|
errRouteIsNotAvailable = Error("route is not available")
|
|
|
|
)
|
|
|
|
|
2021-11-04 16:11:38 -06:00
|
|
|
// Deprecated: use machine function instead
|
2021-08-21 07:49:46 -06:00
|
|
|
// GetAdvertisedNodeRoutes returns the subnet routes advertised by a node (identified by
|
2021-11-13 01:39:04 -07:00
|
|
|
// namespace and node name).
|
2021-11-13 01:36:45 -07:00
|
|
|
func (h *Headscale) GetAdvertisedNodeRoutes(
|
|
|
|
namespace string,
|
|
|
|
nodeName string,
|
|
|
|
) (*[]netaddr.IPPrefix, error) {
|
2021-11-15 09:15:50 -07:00
|
|
|
machine, err := h.GetMachine(namespace, nodeName)
|
2021-04-28 08:55:29 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-15 09:15:50 -07:00
|
|
|
hostInfo, err := machine.GetHostInfo()
|
2021-04-28 08:55:29 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-14 08:46:09 -07:00
|
|
|
|
2021-08-21 07:49:46 -06:00
|
|
|
return &hostInfo.RoutableIPs, nil
|
2021-04-28 08:55:29 -06:00
|
|
|
}
|
|
|
|
|
2021-11-04 16:11:38 -06:00
|
|
|
// Deprecated: use machine function instead
|
2021-08-21 07:49:46 -06:00
|
|
|
// GetEnabledNodeRoutes returns the subnet routes enabled by a node (identified by
|
2021-11-13 01:39:04 -07:00
|
|
|
// namespace and node name).
|
2021-11-13 01:36:45 -07:00
|
|
|
func (h *Headscale) GetEnabledNodeRoutes(
|
|
|
|
namespace string,
|
|
|
|
nodeName string,
|
|
|
|
) ([]netaddr.IPPrefix, error) {
|
2021-11-15 09:15:50 -07:00
|
|
|
machine, err := h.GetMachine(namespace, nodeName)
|
2021-04-28 08:55:29 -06:00
|
|
|
if err != nil {
|
2021-05-08 05:59:18 -06:00
|
|
|
return nil, err
|
2021-04-28 08:55:29 -06:00
|
|
|
}
|
2021-08-21 07:49:46 -06:00
|
|
|
|
2021-11-15 09:15:50 -07:00
|
|
|
data, err := machine.EnabledRoutes.MarshalJSON()
|
2021-04-28 08:55:29 -06:00
|
|
|
if err != nil {
|
2021-05-08 05:59:18 -06:00
|
|
|
return nil, err
|
2021-04-28 08:55:29 -06:00
|
|
|
}
|
2021-08-21 07:49:46 -06:00
|
|
|
|
|
|
|
routesStr := []string{}
|
|
|
|
err = json.Unmarshal(data, &routesStr)
|
2021-04-28 08:55:29 -06:00
|
|
|
if err != nil {
|
2021-05-08 05:59:18 -06:00
|
|
|
return nil, err
|
2021-04-28 08:55:29 -06:00
|
|
|
}
|
|
|
|
|
2021-08-21 07:49:46 -06:00
|
|
|
routes := make([]netaddr.IPPrefix, len(routesStr))
|
|
|
|
for index, routeStr := range routesStr {
|
|
|
|
route, err := netaddr.ParseIPPrefix(routeStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
routes[index] = route
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
2021-11-04 16:11:38 -06:00
|
|
|
// Deprecated: use machine function instead
|
2021-11-13 01:39:04 -07:00
|
|
|
// IsNodeRouteEnabled checks if a certain route has been enabled.
|
2021-11-13 01:36:45 -07:00
|
|
|
func (h *Headscale) IsNodeRouteEnabled(
|
|
|
|
namespace string,
|
|
|
|
nodeName string,
|
|
|
|
routeStr string,
|
|
|
|
) bool {
|
2021-08-21 07:49:46 -06:00
|
|
|
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 08:46:09 -07:00
|
|
|
|
2021-08-21 07:49:46 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-11-04 16:11:38 -06:00
|
|
|
// Deprecated: use EnableRoute in machine.go
|
2021-08-21 07:49:46 -06:00
|
|
|
// EnableNodeRoute enables a subnet route advertised by a node (identified by
|
2021-11-13 01:39:04 -07:00
|
|
|
// namespace and node name).
|
2021-11-13 01:36:45 -07:00
|
|
|
func (h *Headscale) EnableNodeRoute(
|
|
|
|
namespace string,
|
|
|
|
nodeName string,
|
|
|
|
routeStr string,
|
|
|
|
) error {
|
2021-11-15 09:15:50 -07:00
|
|
|
machine, err := h.GetMachine(namespace, nodeName)
|
2021-08-21 07:49:46 -06:00
|
|
|
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 08:55:29 -06:00
|
|
|
|
2021-08-21 07:49:46 -06: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 08:55:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-21 07:49:46 -06:00
|
|
|
|
|
|
|
if !available {
|
2021-11-15 12:18:14 -07:00
|
|
|
return errRouteIsNotAvailable
|
2021-08-21 07:49:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
routes, err := json.Marshal(enabledRoutes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-15 09:15:50 -07:00
|
|
|
machine.EnabledRoutes = datatypes.JSON(routes)
|
|
|
|
h.db.Save(&machine)
|
2021-08-21 07:49:46 -06:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|