2021-04-28 10:55:29 -04:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-08-21 09:49:46 -04:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2021-04-28 10:55:29 -04:00
|
|
|
|
2021-08-21 09:49:46 -04:00
|
|
|
"github.com/pterm/pterm"
|
2021-05-14 18:05:41 -04:00
|
|
|
"gorm.io/datatypes"
|
2021-04-28 10:55:29 -04:00
|
|
|
"inet.af/netaddr"
|
|
|
|
)
|
|
|
|
|
2021-08-21 09:49:46 -04:00
|
|
|
// GetAdvertisedNodeRoutes returns the subnet routes advertised by a node (identified by
|
2021-04-28 10:55:29 -04:00
|
|
|
// namespace and node name)
|
2021-08-21 09:49:46 -04:00
|
|
|
func (h *Headscale) GetAdvertisedNodeRoutes(namespace string, nodeName string) (*[]netaddr.IPPrefix, error) {
|
2021-04-28 10:55:29 -04:00
|
|
|
m, err := h.GetMachine(namespace, nodeName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-21 09:49:46 -04:00
|
|
|
hostInfo, err := m.GetHostInfo()
|
2021-04-28 10:55:29 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-21 09:49:46 -04:00
|
|
|
return &hostInfo.RoutableIPs, nil
|
2021-04-28 10:55:29 -04:00
|
|
|
}
|
|
|
|
|
2021-08-21 09:49:46 -04:00
|
|
|
// GetEnabledNodeRoutes returns the subnet routes enabled by a node (identified by
|
2021-04-28 10:55:29 -04:00
|
|
|
// namespace and node name)
|
2021-08-21 09:49:46 -04:00
|
|
|
func (h *Headscale) GetEnabledNodeRoutes(namespace string, nodeName string) ([]netaddr.IPPrefix, error) {
|
2021-04-28 10:55:29 -04:00
|
|
|
m, err := h.GetMachine(namespace, nodeName)
|
|
|
|
if err != nil {
|
2021-05-08 07:59:18 -04:00
|
|
|
return nil, err
|
2021-04-28 10:55:29 -04:00
|
|
|
}
|
2021-08-21 09:49:46 -04:00
|
|
|
|
|
|
|
data, err := m.EnabledRoutes.MarshalJSON()
|
2021-04-28 10:55:29 -04:00
|
|
|
if err != nil {
|
2021-05-08 07:59:18 -04:00
|
|
|
return nil, err
|
2021-04-28 10:55:29 -04:00
|
|
|
}
|
2021-08-21 09:49:46 -04:00
|
|
|
|
|
|
|
routesStr := []string{}
|
|
|
|
err = json.Unmarshal(data, &routesStr)
|
2021-04-28 10:55:29 -04:00
|
|
|
if err != nil {
|
2021-05-08 07:59:18 -04:00
|
|
|
return nil, err
|
2021-04-28 10:55:29 -04:00
|
|
|
}
|
|
|
|
|
2021-08-21 09:49:46 -04: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-09-02 11:08:39 -04:00
|
|
|
// IsNodeRouteEnabled checks if a certain route has been enabled
|
2021-08-21 09:49:46 -04: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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnableNodeRoute enables a subnet route advertised by a node (identified by
|
|
|
|
// namespace and node name)
|
|
|
|
func (h *Headscale) EnableNodeRoute(namespace string, nodeName string, routeStr string) error {
|
|
|
|
m, 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 10:55:29 -04:00
|
|
|
|
2021-08-21 09:49:46 -04: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 10:55:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-21 09:49:46 -04:00
|
|
|
|
|
|
|
if !available {
|
|
|
|
return fmt.Errorf("route (%s) is not available on node %s", nodeName, routeStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
routes, err := json.Marshal(enabledRoutes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.EnabledRoutes = datatypes.JSON(routes)
|
|
|
|
h.db.Save(&m)
|
|
|
|
|
|
|
|
err = h.RequestMapUpdates(m.NamespaceID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-02 11:08:39 -04:00
|
|
|
// RoutesToPtables converts the list of routes to a nice table
|
2021-08-21 09:49:46 -04:00
|
|
|
func (h *Headscale) RoutesToPtables(namespace string, nodeName string, availableRoutes []netaddr.IPPrefix) pterm.TableData {
|
|
|
|
d := pterm.TableData{{"Route", "Enabled"}}
|
|
|
|
|
|
|
|
for _, route := range availableRoutes {
|
|
|
|
enabled := h.IsNodeRouteEnabled(namespace, nodeName, route.String())
|
|
|
|
|
|
|
|
d = append(d, []string{route.String(), strconv.FormatBool(enabled)})
|
|
|
|
}
|
|
|
|
return d
|
2021-04-28 10:55:29 -04:00
|
|
|
}
|