mirror of
https://github.com/juanfont/headscale.git
synced 2024-12-02 03:33:05 +00:00
Add support methods for the API
This commit is contained in:
parent
1d9cd88053
commit
347d4d0ca5
1 changed files with 85 additions and 0 deletions
85
routes.go
85
routes.go
|
@ -5,7 +5,9 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
|
||||||
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
"google.golang.org/protobuf/types/known/timestamppb"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -49,6 +51,64 @@ func (rs Routes) toPrefixes() []netip.Prefix {
|
||||||
return prefixes
|
return prefixes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Headscale) GetRoutes() ([]Route, error) {
|
||||||
|
var routes []Route
|
||||||
|
err := h.db.Preload("Machine").Find(&routes).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return routes, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Headscale) GetMachineRoutes(m *Machine) ([]Route, error) {
|
||||||
|
var routes []Route
|
||||||
|
err := h.db.
|
||||||
|
Preload("Machine").
|
||||||
|
Where("machine_id = ?", m.ID).
|
||||||
|
Find(&routes).Error
|
||||||
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return routes, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Headscale) GetRoute(id uint64) (*Route, error) {
|
||||||
|
var route Route
|
||||||
|
err := h.db.Preload("Machine").First(&route, id).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &route, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Headscale) EnableRoute(id uint64) error {
|
||||||
|
route, err := h.GetRoute(id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return h.EnableRoutes(&route.Machine, netip.Prefix(route.Prefix).String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Headscale) DisableRoute(id uint64) error {
|
||||||
|
route, err := h.GetRoute(id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
route.Enabled = false
|
||||||
|
route.IsPrimary = false
|
||||||
|
err = h.db.Save(route).Error
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return h.handlePrimarySubnetFailover()
|
||||||
|
}
|
||||||
|
|
||||||
// isUniquePrefix returns if there is another machine providing the same route already.
|
// isUniquePrefix returns if there is another machine providing the same route already.
|
||||||
func (h *Headscale) isUniquePrefix(route Route) bool {
|
func (h *Headscale) isUniquePrefix(route Route) bool {
|
||||||
var count int64
|
var count int64
|
||||||
|
@ -247,3 +307,28 @@ func (h *Headscale) handlePrimarySubnetFailover() error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue