2023-05-21 10:37:59 -06: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"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ExitRouteV4 = netip.MustParsePrefix("0.0.0.0/0")
|
|
|
|
ExitRouteV6 = netip.MustParsePrefix("::/0")
|
|
|
|
)
|
|
|
|
|
|
|
|
type Route struct {
|
|
|
|
gorm.Model
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
NodeID uint64
|
|
|
|
Node Node
|
|
|
|
Prefix IPPrefix
|
2023-05-21 10:37:59 -06:00
|
|
|
|
|
|
|
Advertised bool
|
|
|
|
Enabled bool
|
|
|
|
IsPrimary bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type Routes []Route
|
|
|
|
|
|
|
|
func (r *Route) String() string {
|
2023-09-24 05:42:05 -06:00
|
|
|
return fmt.Sprintf("%s:%s", r.Node, netip.Prefix(r.Prefix).String())
|
2023-05-21 10:37:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Route) IsExitRoute() bool {
|
|
|
|
return netip.Prefix(r.Prefix) == ExitRouteV4 || netip.Prefix(r.Prefix) == ExitRouteV6
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs Routes) Proto() []*v1.Route {
|
|
|
|
protoRoutes := []*v1.Route{}
|
|
|
|
|
|
|
|
for _, route := range rs {
|
|
|
|
protoRoute := v1.Route{
|
|
|
|
Id: uint64(route.ID),
|
2023-09-24 05:42:05 -06:00
|
|
|
Node: route.Node.Proto(),
|
2023-05-21 10:37:59 -06:00
|
|
|
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
|
|
|
|
}
|