2021-04-28 10:55:29 -04:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
|
2021-05-14 18:05:41 -04:00
|
|
|
"gorm.io/datatypes"
|
2021-04-28 10:55:29 -04:00
|
|
|
"inet.af/netaddr"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetNodeRoutes returns the subnet routes advertised by a node (identified by
|
|
|
|
// namespace and node name)
|
|
|
|
func (h *Headscale) GetNodeRoutes(namespace string, nodeName string) (*[]netaddr.IPPrefix, error) {
|
|
|
|
m, err := h.GetMachine(namespace, nodeName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hi, err := m.GetHostInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &hi.RoutableIPs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnableNodeRoute enables a subnet route advertised by a node (identified by
|
|
|
|
// namespace and node name)
|
2021-05-08 07:59:18 -04:00
|
|
|
func (h *Headscale) EnableNodeRoute(namespace string, nodeName string, routeStr 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
|
|
|
}
|
|
|
|
hi, err := m.GetHostInfo()
|
|
|
|
if err != nil {
|
2021-05-08 07:59:18 -04:00
|
|
|
return nil, err
|
2021-04-28 10:55:29 -04:00
|
|
|
}
|
|
|
|
route, err := netaddr.ParseIPPrefix(routeStr)
|
|
|
|
if err != nil {
|
2021-05-08 07:59:18 -04:00
|
|
|
return nil, err
|
2021-04-28 10:55:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, rIP := range hi.RoutableIPs {
|
|
|
|
if rIP == route {
|
|
|
|
routes, _ := json.Marshal([]string{routeStr}) // TODO: only one for the time being, so overwriting the rest
|
2021-05-14 18:05:41 -04:00
|
|
|
m.EnabledRoutes = datatypes.JSON(routes)
|
2021-07-04 15:40:46 -04:00
|
|
|
h.db.Save(&m)
|
2021-04-28 10:55:29 -04:00
|
|
|
|
2021-05-08 07:59:18 -04:00
|
|
|
// THIS IS COMPLETELY USELESS.
|
|
|
|
// The peers map is stored in memory in the server process.
|
2021-08-07 13:52:01 -04:00
|
|
|
// Definitely not accessible from the CLI tool.
|
2021-05-08 07:59:18 -04:00
|
|
|
// We need RPC to the server - or some kind of 'needsUpdate' field in the DB
|
2021-04-28 10:55:29 -04:00
|
|
|
peers, _ := h.getPeers(*m)
|
|
|
|
for _, p := range *peers {
|
2021-08-05 17:14:37 -04:00
|
|
|
if pUp, ok := h.clientsPolling.Load(uint64(p.ID)); ok {
|
|
|
|
pUp.(chan []byte) <- []byte{}
|
2021-04-28 10:55:29 -04:00
|
|
|
}
|
|
|
|
}
|
2021-05-08 07:59:18 -04:00
|
|
|
return &rIP, nil
|
2021-04-28 10:55:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-08 07:59:18 -04:00
|
|
|
return nil, errors.New("could not find routable range")
|
2021-04-28 10:55:29 -04:00
|
|
|
}
|