2023-05-10 01:24:05 -06:00
|
|
|
package hscontrol
|
2021-04-28 08:55:29 -06:00
|
|
|
|
|
|
|
import (
|
2022-11-25 08:29:45 -07:00
|
|
|
"errors"
|
2022-05-30 07:31:06 -06:00
|
|
|
"fmt"
|
2022-09-01 16:06:19 -06:00
|
|
|
"net/netip"
|
2022-11-23 11:50:30 -07:00
|
|
|
|
2022-11-25 10:50:12 -07:00
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
2022-11-24 15:41:11 -07:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-11-25 10:50:12 -07:00
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
2022-11-23 11:50:30 -07:00
|
|
|
"gorm.io/gorm"
|
2021-04-28 08:55:29 -06:00
|
|
|
)
|
|
|
|
|
2022-11-24 09:00:40 -07:00
|
|
|
var (
|
2023-05-11 01:09:18 -06:00
|
|
|
ErrRouteIsNotAvailable = errors.New("route is not available")
|
|
|
|
ExitRouteV4 = netip.MustParsePrefix("0.0.0.0/0")
|
|
|
|
ExitRouteV6 = netip.MustParsePrefix("::/0")
|
2022-11-24 09:00:40 -07:00
|
|
|
)
|
|
|
|
|
2022-11-23 11:50:30 -07:00
|
|
|
type Route struct {
|
|
|
|
gorm.Model
|
2021-04-28 08:55:29 -06:00
|
|
|
|
2022-11-23 11:50:30 -07:00
|
|
|
MachineID uint64
|
|
|
|
Machine Machine
|
|
|
|
Prefix IPPrefix
|
2021-08-21 07:49:46 -06:00
|
|
|
|
2022-11-23 11:50:30 -07:00
|
|
|
Advertised bool
|
|
|
|
Enabled bool
|
|
|
|
IsPrimary bool
|
2021-08-21 07:49:46 -06:00
|
|
|
}
|
|
|
|
|
2022-11-23 11:50:30 -07:00
|
|
|
type Routes []Route
|
2021-08-21 07:49:46 -06:00
|
|
|
|
2022-11-23 11:50:30 -07:00
|
|
|
func (r *Route) String() string {
|
|
|
|
return fmt.Sprintf("%s:%s", r.Machine, netip.Prefix(r.Prefix).String())
|
|
|
|
}
|
2021-08-21 07:49:46 -06:00
|
|
|
|
2022-11-24 15:41:11 -07:00
|
|
|
func (r *Route) isExitRoute() bool {
|
|
|
|
return netip.Prefix(r.Prefix) == ExitRouteV4 || netip.Prefix(r.Prefix) == ExitRouteV6
|
|
|
|
}
|
|
|
|
|
2022-11-23 11:50:30 -07:00
|
|
|
func (rs Routes) toPrefixes() []netip.Prefix {
|
|
|
|
prefixes := make([]netip.Prefix, len(rs))
|
|
|
|
for i, r := range rs {
|
|
|
|
prefixes[i] = netip.Prefix(r.Prefix)
|
2021-08-21 07:49:46 -06:00
|
|
|
}
|
2022-11-25 08:29:45 -07:00
|
|
|
|
2022-11-23 11:50:30 -07:00
|
|
|
return prefixes
|
2021-08-21 07:49:46 -06:00
|
|
|
}
|
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
func (hsdb *HSDatabase) GetRoutes() ([]Route, error) {
|
2022-11-25 10:50:12 -07:00
|
|
|
var routes []Route
|
2023-05-11 01:09:18 -06:00
|
|
|
err := hsdb.db.Preload("Machine").Find(&routes).Error
|
2022-11-25 10:50:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
func (hsdb *HSDatabase) GetMachineRoutes(m *Machine) ([]Route, error) {
|
2022-11-25 10:50:12 -07:00
|
|
|
var routes []Route
|
2023-05-11 01:09:18 -06:00
|
|
|
err := hsdb.db.
|
2022-11-25 10:50:12 -07:00
|
|
|
Preload("Machine").
|
|
|
|
Where("machine_id = ?", m.ID).
|
|
|
|
Find(&routes).Error
|
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return routes, nil
|
|
|
|
}
|
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
func (hsdb *HSDatabase) GetRoute(id uint64) (*Route, error) {
|
2022-11-25 10:50:12 -07:00
|
|
|
var route Route
|
2023-05-11 01:09:18 -06:00
|
|
|
err := hsdb.db.Preload("Machine").First(&route, id).Error
|
2022-11-25 10:50:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &route, nil
|
|
|
|
}
|
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
func (hsdb *HSDatabase) EnableRoute(id uint64) error {
|
|
|
|
route, err := hsdb.GetRoute(id)
|
2022-11-25 10:50:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-22 15:39:42 -07:00
|
|
|
// Tailscale requires both IPv4 and IPv6 exit routes to
|
|
|
|
// be enabled at the same time, as per
|
|
|
|
// https://github.com/juanfont/headscale/issues/804#issuecomment-1399314002
|
|
|
|
if route.isExitRoute() {
|
2023-05-11 01:09:18 -06:00
|
|
|
return hsdb.enableRoutes(&route.Machine, ExitRouteV4.String(), ExitRouteV6.String())
|
2023-01-22 15:39:42 -07:00
|
|
|
}
|
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
return hsdb.enableRoutes(&route.Machine, netip.Prefix(route.Prefix).String())
|
2022-11-25 10:50:12 -07:00
|
|
|
}
|
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
func (hsdb *HSDatabase) DisableRoute(id uint64) error {
|
|
|
|
route, err := hsdb.GetRoute(id)
|
2022-11-25 10:50:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-07 04:17:16 -06:00
|
|
|
// Tailscale requires both IPv4 and IPv6 exit routes to
|
|
|
|
// be enabled at the same time, as per
|
|
|
|
// https://github.com/juanfont/headscale/issues/804#issuecomment-1399314002
|
|
|
|
if !route.isExitRoute() {
|
|
|
|
route.Enabled = false
|
|
|
|
route.IsPrimary = false
|
2023-05-11 01:09:18 -06:00
|
|
|
err = hsdb.db.Save(route).Error
|
2023-05-07 04:17:16 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
return hsdb.handlePrimarySubnetFailover()
|
2023-05-07 04:17:16 -06:00
|
|
|
}
|
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
routes, err := hsdb.GetMachineRoutes(&route.Machine)
|
2022-11-25 10:50:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-07 04:17:16 -06:00
|
|
|
for i := range routes {
|
|
|
|
if routes[i].isExitRoute() {
|
|
|
|
routes[i].Enabled = false
|
|
|
|
routes[i].IsPrimary = false
|
2023-05-11 01:09:18 -06:00
|
|
|
err = hsdb.db.Save(&routes[i]).Error
|
2023-05-07 04:17:16 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
return hsdb.handlePrimarySubnetFailover()
|
2022-11-25 10:50:12 -07:00
|
|
|
}
|
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
func (hsdb *HSDatabase) DeleteRoute(id uint64) error {
|
|
|
|
route, err := hsdb.GetRoute(id)
|
2023-03-06 01:05:40 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-07 04:17:16 -06:00
|
|
|
// Tailscale requires both IPv4 and IPv6 exit routes to
|
|
|
|
// be enabled at the same time, as per
|
|
|
|
// https://github.com/juanfont/headscale/issues/804#issuecomment-1399314002
|
|
|
|
if !route.isExitRoute() {
|
2023-05-11 01:09:18 -06:00
|
|
|
if err := hsdb.db.Unscoped().Delete(&route).Error; err != nil {
|
2023-05-07 04:17:16 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
return hsdb.handlePrimarySubnetFailover()
|
2023-05-07 04:17:16 -06:00
|
|
|
}
|
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
routes, err := hsdb.GetMachineRoutes(&route.Machine)
|
2023-05-07 04:17:16 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
routesToDelete := []Route{}
|
|
|
|
for _, r := range routes {
|
|
|
|
if r.isExitRoute() {
|
|
|
|
routesToDelete = append(routesToDelete, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
if err := hsdb.db.Unscoped().Delete(&routesToDelete).Error; err != nil {
|
2023-03-06 01:05:40 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
return hsdb.handlePrimarySubnetFailover()
|
2023-03-06 01:05:40 -07:00
|
|
|
}
|
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
func (hsdb *HSDatabase) DeleteMachineRoutes(m *Machine) error {
|
|
|
|
routes, err := hsdb.GetMachineRoutes(m)
|
2023-03-06 01:05:40 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range routes {
|
2023-05-11 01:09:18 -06:00
|
|
|
if err := hsdb.db.Unscoped().Delete(&routes[i]).Error; err != nil {
|
2023-03-06 01:05:40 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
return hsdb.handlePrimarySubnetFailover()
|
2023-03-06 01:05:40 -07:00
|
|
|
}
|
|
|
|
|
2022-11-25 08:29:45 -07:00
|
|
|
// isUniquePrefix returns if there is another machine providing the same route already.
|
2023-05-11 01:09:18 -06:00
|
|
|
func (hsdb *HSDatabase) isUniquePrefix(route Route) bool {
|
2022-11-24 09:00:40 -07:00
|
|
|
var count int64
|
2023-05-11 01:09:18 -06:00
|
|
|
hsdb.db.
|
2022-11-24 09:00:40 -07:00
|
|
|
Model(&Route{}).
|
|
|
|
Where("prefix = ? AND machine_id != ? AND advertised = ? AND enabled = ?",
|
|
|
|
route.Prefix,
|
|
|
|
route.MachineID,
|
|
|
|
true, true).Count(&count)
|
2022-11-25 08:29:45 -07:00
|
|
|
|
2022-11-24 09:00:40 -07:00
|
|
|
return count == 0
|
|
|
|
}
|
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
func (hsdb *HSDatabase) getPrimaryRoute(prefix netip.Prefix) (*Route, error) {
|
2022-11-24 15:41:11 -07:00
|
|
|
var route Route
|
2023-05-11 01:09:18 -06:00
|
|
|
err := hsdb.db.
|
2022-11-24 15:41:11 -07:00
|
|
|
Preload("Machine").
|
|
|
|
Where("prefix = ? AND advertised = ? AND enabled = ? AND is_primary = ?", IPPrefix(prefix), true, true, true).
|
|
|
|
First(&route).Error
|
2022-11-25 08:29:45 -07:00
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
2022-11-24 15:41:11 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-25 08:29:45 -07:00
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
2022-11-24 15:41:11 -07:00
|
|
|
return nil, gorm.ErrRecordNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return &route, nil
|
|
|
|
}
|
|
|
|
|
2022-11-23 11:50:30 -07:00
|
|
|
// getMachinePrimaryRoutes returns the routes that are enabled and marked as primary (for subnet failover)
|
2022-11-25 08:29:45 -07:00
|
|
|
// Exit nodes are not considered for this, as they are never marked as Primary.
|
2023-05-11 01:09:18 -06:00
|
|
|
func (hsdb *HSDatabase) getMachinePrimaryRoutes(m *Machine) ([]Route, error) {
|
2022-11-23 11:50:30 -07:00
|
|
|
var routes []Route
|
2023-05-11 01:09:18 -06:00
|
|
|
err := hsdb.db.
|
2022-11-23 11:50:30 -07:00
|
|
|
Preload("Machine").
|
|
|
|
Where("machine_id = ? AND advertised = ? AND enabled = ? AND is_primary = ?", m.ID, true, true, true).
|
|
|
|
Find(&routes).Error
|
2021-08-21 07:49:46 -06:00
|
|
|
if err != nil {
|
2022-11-23 11:50:30 -07:00
|
|
|
return nil, err
|
2021-08-21 07:49:46 -06:00
|
|
|
}
|
|
|
|
|
2022-11-23 11:50:30 -07:00
|
|
|
return routes, nil
|
|
|
|
}
|
2021-08-21 07:49:46 -06:00
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
func (hsdb *HSDatabase) processMachineRoutes(machine *Machine) error {
|
2022-11-23 11:50:30 -07:00
|
|
|
currentRoutes := []Route{}
|
2023-05-11 01:09:18 -06:00
|
|
|
err := hsdb.db.Where("machine_id = ?", machine.ID).Find(¤tRoutes).Error
|
2021-08-21 07:49:46 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-28 08:55:29 -06:00
|
|
|
|
2022-11-23 11:50:30 -07:00
|
|
|
advertisedRoutes := map[netip.Prefix]bool{}
|
|
|
|
for _, prefix := range machine.HostInfo.RoutableIPs {
|
|
|
|
advertisedRoutes[prefix] = false
|
2021-08-21 07:49:46 -06:00
|
|
|
}
|
|
|
|
|
2022-11-25 08:29:45 -07:00
|
|
|
for pos, route := range currentRoutes {
|
2022-11-23 11:50:30 -07:00
|
|
|
if _, ok := advertisedRoutes[netip.Prefix(route.Prefix)]; ok {
|
|
|
|
if !route.Advertised {
|
2022-11-25 08:29:45 -07:00
|
|
|
currentRoutes[pos].Advertised = true
|
2023-05-11 01:09:18 -06:00
|
|
|
err := hsdb.db.Save(¤tRoutes[pos]).Error
|
2022-11-23 11:50:30 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
advertisedRoutes[netip.Prefix(route.Prefix)] = true
|
2022-11-25 08:29:45 -07:00
|
|
|
} else if route.Advertised {
|
|
|
|
currentRoutes[pos].Advertised = false
|
|
|
|
currentRoutes[pos].Enabled = false
|
2023-05-11 01:09:18 -06:00
|
|
|
err := hsdb.db.Save(¤tRoutes[pos]).Error
|
2022-11-25 08:29:45 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-04-28 08:55:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-21 07:49:46 -06:00
|
|
|
|
2022-11-23 11:50:30 -07:00
|
|
|
for prefix, exists := range advertisedRoutes {
|
|
|
|
if !exists {
|
|
|
|
route := Route{
|
|
|
|
MachineID: machine.ID,
|
|
|
|
Prefix: IPPrefix(prefix),
|
|
|
|
Advertised: true,
|
|
|
|
Enabled: false,
|
|
|
|
}
|
2023-05-11 01:09:18 -06:00
|
|
|
err := hsdb.db.Create(&route).Error
|
2022-11-23 11:50:30 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-05-30 07:31:06 -06:00
|
|
|
}
|
2021-08-21 07:49:46 -06:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-24 15:41:11 -07:00
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
func (hsdb *HSDatabase) handlePrimarySubnetFailover() error {
|
2022-11-24 15:41:11 -07:00
|
|
|
// first, get all the enabled routes
|
|
|
|
var routes []Route
|
2023-05-11 01:09:18 -06:00
|
|
|
err := hsdb.db.
|
2022-11-24 15:41:11 -07:00
|
|
|
Preload("Machine").
|
|
|
|
Where("advertised = ? AND enabled = ?", true, true).
|
|
|
|
Find(&routes).Error
|
2022-11-25 08:29:45 -07:00
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
2022-11-24 15:41:11 -07:00
|
|
|
log.Error().Err(err).Msg("error getting routes")
|
|
|
|
}
|
|
|
|
|
2022-12-20 02:08:32 -07:00
|
|
|
routesChanged := false
|
2022-11-25 08:29:45 -07:00
|
|
|
for pos, route := range routes {
|
2022-11-24 15:41:11 -07:00
|
|
|
if route.isExitRoute() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !route.IsPrimary {
|
2023-05-11 01:09:18 -06:00
|
|
|
_, err := hsdb.getPrimaryRoute(netip.Prefix(route.Prefix))
|
|
|
|
if hsdb.isUniquePrefix(route) || errors.Is(err, gorm.ErrRecordNotFound) {
|
2022-11-25 10:50:12 -07:00
|
|
|
log.Info().
|
|
|
|
Str("prefix", netip.Prefix(route.Prefix).String()).
|
|
|
|
Str("machine", route.Machine.GivenName).
|
|
|
|
Msg("Setting primary route")
|
2022-11-25 08:29:45 -07:00
|
|
|
routes[pos].IsPrimary = true
|
2023-05-11 01:09:18 -06:00
|
|
|
err := hsdb.db.Save(&routes[pos]).Error
|
2022-11-24 15:41:11 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("error marking route as primary")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2022-11-25 08:29:45 -07:00
|
|
|
|
2022-12-20 02:08:32 -07:00
|
|
|
routesChanged = true
|
2022-12-20 14:03:15 -07:00
|
|
|
|
2022-11-24 15:41:11 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if route.IsPrimary {
|
|
|
|
if route.Machine.isOnline() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// machine offline, find a new primary
|
|
|
|
log.Info().
|
|
|
|
Str("machine", route.Machine.Hostname).
|
|
|
|
Str("prefix", netip.Prefix(route.Prefix).String()).
|
|
|
|
Msgf("machine offline, finding a new primary subnet")
|
|
|
|
|
|
|
|
// find a new primary route
|
|
|
|
var newPrimaryRoutes []Route
|
2023-05-11 01:09:18 -06:00
|
|
|
err := hsdb.db.
|
2022-11-24 15:41:11 -07:00
|
|
|
Preload("Machine").
|
|
|
|
Where("prefix = ? AND machine_id != ? AND advertised = ? AND enabled = ?",
|
|
|
|
route.Prefix,
|
|
|
|
route.MachineID,
|
|
|
|
true, true).
|
|
|
|
Find(&newPrimaryRoutes).Error
|
2022-11-25 08:29:45 -07:00
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
2022-11-24 15:41:11 -07:00
|
|
|
log.Error().Err(err).Msg("error finding new primary route")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var newPrimaryRoute *Route
|
2022-11-25 08:29:45 -07:00
|
|
|
for pos, r := range newPrimaryRoutes {
|
2022-11-24 15:41:11 -07:00
|
|
|
if r.Machine.isOnline() {
|
2022-11-25 08:29:45 -07:00
|
|
|
newPrimaryRoute = &newPrimaryRoutes[pos]
|
|
|
|
|
2022-11-24 15:41:11 -07:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if newPrimaryRoute == nil {
|
|
|
|
log.Warn().
|
|
|
|
Str("machine", route.Machine.Hostname).
|
|
|
|
Str("prefix", netip.Prefix(route.Prefix).String()).
|
|
|
|
Msgf("no alternative primary route found")
|
2022-11-25 08:29:45 -07:00
|
|
|
|
2022-11-24 15:41:11 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info().
|
|
|
|
Str("old_machine", route.Machine.Hostname).
|
|
|
|
Str("prefix", netip.Prefix(route.Prefix).String()).
|
|
|
|
Str("new_machine", newPrimaryRoute.Machine.Hostname).
|
|
|
|
Msgf("found new primary route")
|
|
|
|
|
|
|
|
// disable the old primary route
|
2022-11-25 08:29:45 -07:00
|
|
|
routes[pos].IsPrimary = false
|
2023-05-11 01:09:18 -06:00
|
|
|
err = hsdb.db.Save(&routes[pos]).Error
|
2022-11-24 15:41:11 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("error disabling old primary route")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// enable the new primary route
|
|
|
|
newPrimaryRoute.IsPrimary = true
|
2023-05-11 01:09:18 -06:00
|
|
|
err = hsdb.db.Save(&newPrimaryRoute).Error
|
2022-11-24 15:41:11 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("error enabling new primary route")
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2022-12-20 02:08:32 -07:00
|
|
|
|
|
|
|
routesChanged = true
|
2022-11-24 15:41:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 02:08:32 -07:00
|
|
|
if routesChanged {
|
2023-05-11 01:09:18 -06:00
|
|
|
hsdb.notifyStateChange()
|
2022-12-20 02:08:32 -07:00
|
|
|
}
|
2022-12-20 14:03:15 -07:00
|
|
|
|
2022-11-24 15:41:11 -07:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-25 10:50:12 -07:00
|
|
|
|
|
|
|
func (rs Routes) toProto() []*v1.Route {
|
|
|
|
protoRoutes := []*v1.Route{}
|
|
|
|
|
|
|
|
for _, route := range rs {
|
|
|
|
protoRoute := v1.Route{
|
|
|
|
Id: uint64(route.ID),
|
|
|
|
Machine: route.Machine.toProto(),
|
|
|
|
Prefix: netip.Prefix(route.Prefix).String(),
|
|
|
|
Advertised: route.Advertised,
|
|
|
|
Enabled: route.Enabled,
|
|
|
|
IsPrimary: route.IsPrimary,
|
|
|
|
CreatedAt: timestamppb.New(route.CreatedAt),
|
|
|
|
UpdatedAt: timestamppb.New(route.UpdatedAt),
|
|
|
|
}
|
|
|
|
|
|
|
|
if route.DeletedAt.Valid {
|
|
|
|
protoRoute.DeletedAt = timestamppb.New(route.DeletedAt.Time)
|
|
|
|
}
|
|
|
|
|
|
|
|
protoRoutes = append(protoRoutes, &protoRoute)
|
|
|
|
}
|
|
|
|
|
|
|
|
return protoRoutes
|
|
|
|
}
|