2023-05-21 16:37:59 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/netip"
|
|
|
|
|
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
"gorm.io/gorm"
|
2024-10-02 07:06:09 +00:00
|
|
|
"tailscale.com/net/tsaddr"
|
2023-05-21 16:37:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Route struct {
|
|
|
|
gorm.Model
|
|
|
|
|
2023-09-24 11:42:05 +00:00
|
|
|
NodeID uint64
|
|
|
|
Node Node
|
2023-12-09 17:09:24 +00:00
|
|
|
|
|
|
|
// TODO(kradalby): change this custom type to netip.Prefix
|
2024-10-02 09:41:58 +00:00
|
|
|
Prefix netip.Prefix `gorm:"serializer:text"`
|
2023-05-21 16:37:59 +00:00
|
|
|
|
|
|
|
Advertised bool
|
|
|
|
Enabled bool
|
|
|
|
IsPrimary bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type Routes []Route
|
|
|
|
|
|
|
|
func (r *Route) String() string {
|
2023-12-09 17:09:24 +00:00
|
|
|
return fmt.Sprintf("%s:%s", r.Node.Hostname, netip.Prefix(r.Prefix).String())
|
2023-05-21 16:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Route) IsExitRoute() bool {
|
2024-10-02 09:41:58 +00:00
|
|
|
return tsaddr.IsExitRoute(r.Prefix)
|
2023-05-21 16:37:59 +00:00
|
|
|
}
|
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
func (r *Route) IsAnnouncable() bool {
|
|
|
|
return r.Advertised && r.Enabled
|
|
|
|
}
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
func (rs Routes) Prefixes() []netip.Prefix {
|
|
|
|
prefixes := make([]netip.Prefix, len(rs))
|
|
|
|
for i, r := range rs {
|
|
|
|
prefixes[i] = netip.Prefix(r.Prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
return prefixes
|
|
|
|
}
|
|
|
|
|
2023-12-09 17:09:24 +00:00
|
|
|
// Primaries returns Primary routes from a list of routes.
|
|
|
|
func (rs Routes) Primaries() Routes {
|
|
|
|
res := make(Routes, 0)
|
|
|
|
for _, route := range rs {
|
|
|
|
if route.IsPrimary {
|
|
|
|
res = append(res, route)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2024-10-02 09:41:58 +00:00
|
|
|
func (rs Routes) PrefixMap() map[netip.Prefix][]Route {
|
|
|
|
res := map[netip.Prefix][]Route{}
|
2023-12-09 17:09:24 +00:00
|
|
|
|
|
|
|
for _, route := range rs {
|
|
|
|
if _, ok := res[route.Prefix]; ok {
|
|
|
|
res[route.Prefix] = append(res[route.Prefix], route)
|
|
|
|
} else {
|
|
|
|
res[route.Prefix] = []Route{route}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2023-05-21 16:37:59 +00:00
|
|
|
func (rs Routes) Proto() []*v1.Route {
|
|
|
|
protoRoutes := []*v1.Route{}
|
|
|
|
|
|
|
|
for _, route := range rs {
|
|
|
|
protoRoute := v1.Route{
|
|
|
|
Id: uint64(route.ID),
|
2023-09-24 11:42:05 +00:00
|
|
|
Node: route.Node.Proto(),
|
2024-10-02 09:41:58 +00:00
|
|
|
Prefix: route.Prefix.String(),
|
2023-05-21 16:37:59 +00:00
|
|
|
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
|
|
|
|
}
|