2021-10-27 03:06:39 -04:00
|
|
|
//nolint
|
2021-10-26 16:42:20 -04:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-11-04 18:19:27 -04:00
|
|
|
"time"
|
2021-10-26 16:42:20 -04:00
|
|
|
|
2021-11-04 18:19:27 -04:00
|
|
|
"github.com/juanfont/headscale/gen/go/headscale/v1"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"tailscale.com/tailcfg"
|
2021-10-26 16:42:20 -04:00
|
|
|
)
|
|
|
|
|
2021-11-04 18:19:27 -04:00
|
|
|
type headscaleV1APIServer struct { // v1.HeadscaleServiceServer
|
|
|
|
v1.UnimplementedHeadscaleServiceServer
|
2021-10-26 16:42:20 -04:00
|
|
|
h *Headscale
|
|
|
|
}
|
|
|
|
|
2021-11-04 18:19:27 -04:00
|
|
|
func newHeadscaleV1APIServer(h *Headscale) v1.HeadscaleServiceServer {
|
2021-10-26 16:42:20 -04:00
|
|
|
return headscaleV1APIServer{
|
|
|
|
h: h,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-04 18:19:27 -04:00
|
|
|
func (api headscaleV1APIServer) GetNamespace(
|
2021-10-26 16:42:20 -04:00
|
|
|
ctx context.Context,
|
2021-11-04 18:19:27 -04:00
|
|
|
request *v1.GetNamespaceRequest,
|
|
|
|
) (*v1.GetNamespaceResponse, error) {
|
|
|
|
namespace, err := api.h.GetNamespace(request.GetName())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-29 12:44:32 -04:00
|
|
|
|
2021-11-04 18:19:27 -04:00
|
|
|
return &v1.GetNamespaceResponse{Namespace: namespace.toProto()}, nil
|
2021-10-29 12:44:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api headscaleV1APIServer) CreateNamespace(
|
|
|
|
ctx context.Context,
|
2021-11-04 18:19:27 -04:00
|
|
|
request *v1.CreateNamespaceRequest,
|
|
|
|
) (*v1.CreateNamespaceResponse, error) {
|
|
|
|
namespace, err := api.h.CreateNamespace(request.GetName())
|
2021-10-26 16:42:20 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-04 18:19:27 -04:00
|
|
|
return &v1.CreateNamespaceResponse{Namespace: namespace.toProto()}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api headscaleV1APIServer) RenameNamespace(
|
|
|
|
ctx context.Context,
|
|
|
|
request *v1.RenameNamespaceRequest,
|
|
|
|
) (*v1.RenameNamespaceResponse, error) {
|
|
|
|
err := api.h.RenameNamespace(request.GetOldName(), request.GetNewName())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace, err := api.h.GetNamespace(request.GetNewName())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &v1.RenameNamespaceResponse{Namespace: namespace.toProto()}, nil
|
2021-10-29 12:44:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api headscaleV1APIServer) DeleteNamespace(
|
|
|
|
ctx context.Context,
|
2021-11-04 18:19:27 -04:00
|
|
|
request *v1.DeleteNamespaceRequest,
|
|
|
|
) (*v1.DeleteNamespaceResponse, error) {
|
|
|
|
err := api.h.DestroyNamespace(request.GetName())
|
2021-10-29 12:44:32 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-04 18:19:27 -04:00
|
|
|
return &v1.DeleteNamespaceResponse{}, nil
|
2021-10-29 12:44:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api headscaleV1APIServer) ListNamespaces(
|
|
|
|
ctx context.Context,
|
2021-11-04 18:19:27 -04:00
|
|
|
request *v1.ListNamespacesRequest,
|
|
|
|
) (*v1.ListNamespacesResponse, error) {
|
2021-10-29 12:44:32 -04:00
|
|
|
namespaces, err := api.h.ListNamespaces()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-04 18:19:27 -04:00
|
|
|
response := make([]*v1.Namespace, len(namespaces))
|
|
|
|
for index, namespace := range namespaces {
|
|
|
|
response[index] = namespace.toProto()
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace().Caller().Interface("namespaces", response).Msg("")
|
|
|
|
|
|
|
|
return &v1.ListNamespacesResponse{Namespaces: response}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api headscaleV1APIServer) CreatePreAuthKey(
|
|
|
|
ctx context.Context,
|
|
|
|
request *v1.CreatePreAuthKeyRequest,
|
|
|
|
) (*v1.CreatePreAuthKeyResponse, error) {
|
2021-11-08 03:02:01 -05:00
|
|
|
var expiration time.Time
|
|
|
|
if request.GetExpiration() != nil {
|
|
|
|
expiration = request.GetExpiration().AsTime()
|
|
|
|
}
|
|
|
|
|
2021-11-04 18:19:27 -04:00
|
|
|
preAuthKey, err := api.h.CreatePreAuthKey(
|
|
|
|
request.GetNamespace(),
|
2021-11-08 15:49:03 -05:00
|
|
|
request.GetReusable(),
|
2021-11-04 18:19:27 -04:00
|
|
|
request.GetEphemeral(),
|
|
|
|
&expiration,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &v1.CreatePreAuthKeyResponse{PreAuthKey: preAuthKey.toProto()}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api headscaleV1APIServer) ExpirePreAuthKey(
|
|
|
|
ctx context.Context,
|
|
|
|
request *v1.ExpirePreAuthKeyRequest,
|
|
|
|
) (*v1.ExpirePreAuthKeyResponse, error) {
|
|
|
|
preAuthKey, err := api.h.GetPreAuthKey(request.GetNamespace(), request.Key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = api.h.ExpirePreAuthKey(preAuthKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &v1.ExpirePreAuthKeyResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api headscaleV1APIServer) ListPreAuthKeys(
|
|
|
|
ctx context.Context,
|
|
|
|
request *v1.ListPreAuthKeysRequest,
|
|
|
|
) (*v1.ListPreAuthKeysResponse, error) {
|
|
|
|
preAuthKeys, err := api.h.ListPreAuthKeys(request.GetNamespace())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response := make([]*v1.PreAuthKey, len(preAuthKeys))
|
|
|
|
for index, key := range preAuthKeys {
|
|
|
|
response[index] = key.toProto()
|
|
|
|
}
|
|
|
|
|
|
|
|
return &v1.ListPreAuthKeysResponse{PreAuthKeys: response}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api headscaleV1APIServer) RegisterMachine(
|
|
|
|
ctx context.Context,
|
|
|
|
request *v1.RegisterMachineRequest,
|
|
|
|
) (*v1.RegisterMachineResponse, error) {
|
2021-11-13 03:36:45 -05:00
|
|
|
log.Trace().
|
|
|
|
Str("namespace", request.GetNamespace()).
|
|
|
|
Str("machine_key", request.GetKey()).
|
|
|
|
Msg("Registering machine")
|
2022-02-27 12:42:43 -05:00
|
|
|
|
2022-02-28 03:06:39 -05:00
|
|
|
machine, err := api.h.RegisterMachineFromAuthCallback(
|
2021-11-04 18:19:27 -04:00
|
|
|
request.GetKey(),
|
|
|
|
request.GetNamespace(),
|
2022-02-27 12:42:43 -05:00
|
|
|
RegisterMethodCLI,
|
2021-11-04 18:19:27 -04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &v1.RegisterMachineResponse{Machine: machine.toProto()}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api headscaleV1APIServer) GetMachine(
|
|
|
|
ctx context.Context,
|
|
|
|
request *v1.GetMachineRequest,
|
|
|
|
) (*v1.GetMachineResponse, error) {
|
|
|
|
machine, err := api.h.GetMachineByID(request.GetMachineId())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &v1.GetMachineResponse{Machine: machine.toProto()}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api headscaleV1APIServer) DeleteMachine(
|
|
|
|
ctx context.Context,
|
|
|
|
request *v1.DeleteMachineRequest,
|
|
|
|
) (*v1.DeleteMachineResponse, error) {
|
|
|
|
machine, err := api.h.GetMachineByID(request.GetMachineId())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = api.h.DeleteMachine(
|
|
|
|
machine,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &v1.DeleteMachineResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2021-11-21 08:40:19 -05:00
|
|
|
func (api headscaleV1APIServer) ExpireMachine(
|
|
|
|
ctx context.Context,
|
|
|
|
request *v1.ExpireMachineRequest,
|
|
|
|
) (*v1.ExpireMachineResponse, error) {
|
|
|
|
machine, err := api.h.GetMachineByID(request.GetMachineId())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
api.h.ExpireMachine(
|
|
|
|
machine,
|
|
|
|
)
|
|
|
|
|
|
|
|
log.Trace().
|
|
|
|
Str("machine", machine.Name).
|
|
|
|
Time("expiry", *machine.Expiry).
|
|
|
|
Msg("machine expired")
|
|
|
|
|
|
|
|
return &v1.ExpireMachineResponse{Machine: machine.toProto()}, nil
|
|
|
|
}
|
|
|
|
|
2021-11-04 18:19:27 -04:00
|
|
|
func (api headscaleV1APIServer) ListMachines(
|
|
|
|
ctx context.Context,
|
|
|
|
request *v1.ListMachinesRequest,
|
|
|
|
) (*v1.ListMachinesResponse, error) {
|
|
|
|
if request.GetNamespace() != "" {
|
|
|
|
machines, err := api.h.ListMachinesInNamespace(request.GetNamespace())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response := make([]*v1.Machine, len(machines))
|
|
|
|
for index, machine := range machines {
|
|
|
|
response[index] = machine.toProto()
|
|
|
|
}
|
|
|
|
|
|
|
|
return &v1.ListMachinesResponse{Machines: response}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
machines, err := api.h.ListMachines()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response := make([]*v1.Machine, len(machines))
|
|
|
|
for index, machine := range machines {
|
|
|
|
response[index] = machine.toProto()
|
|
|
|
}
|
|
|
|
|
|
|
|
return &v1.ListMachinesResponse{Machines: response}, nil
|
|
|
|
}
|
|
|
|
|
2022-05-01 09:44:34 -04:00
|
|
|
func (api headscaleV1APIServer) MoveMachine(
|
|
|
|
ctx context.Context,
|
|
|
|
request *v1.MoveMachineRequest,
|
|
|
|
) (*v1.MoveMachineResponse, error) {
|
|
|
|
machine, err := api.h.GetMachineByID(request.GetMachineId())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = api.h.SetMachineNamespace(machine, request.GetNamespace())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &v1.MoveMachineResponse{Machine: machine.toProto()}, nil
|
|
|
|
}
|
|
|
|
|
2021-11-04 18:19:27 -04:00
|
|
|
func (api headscaleV1APIServer) GetMachineRoute(
|
|
|
|
ctx context.Context,
|
|
|
|
request *v1.GetMachineRouteRequest,
|
|
|
|
) (*v1.GetMachineRouteResponse, error) {
|
|
|
|
machine, err := api.h.GetMachineByID(request.GetMachineId())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &v1.GetMachineRouteResponse{
|
2022-03-01 11:34:24 -05:00
|
|
|
Routes: machine.RoutesToProto(),
|
2021-11-04 18:19:27 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api headscaleV1APIServer) EnableMachineRoutes(
|
|
|
|
ctx context.Context,
|
|
|
|
request *v1.EnableMachineRoutesRequest,
|
|
|
|
) (*v1.EnableMachineRoutesResponse, error) {
|
|
|
|
machine, err := api.h.GetMachineByID(request.GetMachineId())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = api.h.EnableRoutes(machine, request.GetRoutes()...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &v1.EnableMachineRoutesResponse{
|
2022-03-01 11:34:24 -05:00
|
|
|
Routes: machine.RoutesToProto(),
|
2021-11-04 18:19:27 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-01-25 17:11:15 -05:00
|
|
|
func (api headscaleV1APIServer) CreateApiKey(
|
|
|
|
ctx context.Context,
|
|
|
|
request *v1.CreateApiKeyRequest,
|
|
|
|
) (*v1.CreateApiKeyResponse, error) {
|
|
|
|
var expiration time.Time
|
|
|
|
if request.GetExpiration() != nil {
|
|
|
|
expiration = request.GetExpiration().AsTime()
|
|
|
|
}
|
|
|
|
|
|
|
|
apiKey, _, err := api.h.CreateAPIKey(
|
|
|
|
&expiration,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &v1.CreateApiKeyResponse{ApiKey: apiKey}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api headscaleV1APIServer) ExpireApiKey(
|
|
|
|
ctx context.Context,
|
|
|
|
request *v1.ExpireApiKeyRequest,
|
|
|
|
) (*v1.ExpireApiKeyResponse, error) {
|
|
|
|
var apiKey *APIKey
|
|
|
|
var err error
|
|
|
|
|
|
|
|
apiKey, err = api.h.GetAPIKey(request.Prefix)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = api.h.ExpireAPIKey(apiKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &v1.ExpireApiKeyResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api headscaleV1APIServer) ListApiKeys(
|
|
|
|
ctx context.Context,
|
|
|
|
request *v1.ListApiKeysRequest,
|
|
|
|
) (*v1.ListApiKeysResponse, error) {
|
|
|
|
apiKeys, err := api.h.ListAPIKeys()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response := make([]*v1.ApiKey, len(apiKeys))
|
|
|
|
for index, key := range apiKeys {
|
|
|
|
response[index] = key.toProto()
|
|
|
|
}
|
|
|
|
|
|
|
|
return &v1.ListApiKeysResponse{ApiKeys: response}, nil
|
|
|
|
}
|
|
|
|
|
2021-11-04 18:19:27 -04:00
|
|
|
// The following service calls are for testing and debugging
|
|
|
|
func (api headscaleV1APIServer) DebugCreateMachine(
|
|
|
|
ctx context.Context,
|
|
|
|
request *v1.DebugCreateMachineRequest,
|
|
|
|
) (*v1.DebugCreateMachineResponse, error) {
|
|
|
|
namespace, err := api.h.GetNamespace(request.GetNamespace())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-15 12:24:24 -05:00
|
|
|
routes, err := stringToIPPrefix(request.GetRoutes())
|
2021-11-04 18:19:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-13 03:36:45 -05:00
|
|
|
log.Trace().
|
|
|
|
Caller().
|
|
|
|
Interface("route-prefix", routes).
|
|
|
|
Interface("route-str", request.GetRoutes()).
|
|
|
|
Msg("")
|
2021-11-04 18:19:27 -04:00
|
|
|
|
|
|
|
hostinfo := tailcfg.Hostinfo{
|
|
|
|
RoutableIPs: routes,
|
|
|
|
OS: "TestOS",
|
|
|
|
Hostname: "DebugTestMachine",
|
|
|
|
}
|
|
|
|
|
|
|
|
newMachine := Machine{
|
|
|
|
MachineKey: request.GetKey(),
|
|
|
|
Name: request.GetName(),
|
|
|
|
Namespace: *namespace,
|
|
|
|
|
|
|
|
Expiry: &time.Time{},
|
|
|
|
LastSeen: &time.Time{},
|
|
|
|
LastSuccessfulUpdate: &time.Time{},
|
|
|
|
|
2022-03-01 11:34:24 -05:00
|
|
|
HostInfo: HostInfo(hostinfo),
|
2021-11-04 18:19:27 -04:00
|
|
|
}
|
|
|
|
|
2022-02-28 11:34:50 -05:00
|
|
|
api.h.registrationCache.Set(
|
|
|
|
request.GetKey(),
|
|
|
|
newMachine,
|
2022-02-28 17:42:30 -05:00
|
|
|
registerCacheExpiration,
|
2022-02-28 11:34:50 -05:00
|
|
|
)
|
2021-10-29 12:44:32 -04:00
|
|
|
|
2021-11-04 18:19:27 -04:00
|
|
|
return &v1.DebugCreateMachineResponse{Machine: newMachine.toProto()}, nil
|
2021-10-26 16:42:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api headscaleV1APIServer) mustEmbedUnimplementedHeadscaleServiceServer() {}
|