2022-11-05 02:07:22 -06:00
|
|
|
// nolint
|
2023-05-10 01:24:05 -06:00
|
|
|
package hscontrol
|
2021-10-26 14:42:20 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-07-25 03:25:20 -06:00
|
|
|
"fmt"
|
2022-05-13 01:47:34 -06:00
|
|
|
"strings"
|
2021-11-04 16:19:27 -06:00
|
|
|
"time"
|
2021-10-26 14:42:20 -06:00
|
|
|
|
2022-04-15 05:11:41 -06:00
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
2024-02-08 09:28:19 -07:00
|
|
|
"github.com/juanfont/headscale/hscontrol/db"
|
2023-05-21 10:37:59 -06:00
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
2023-05-11 01:09:18 -06:00
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
2021-11-04 16:19:27 -06:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-05-13 02:17:52 -06:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2024-02-08 09:28:19 -07:00
|
|
|
"gorm.io/gorm"
|
2021-11-04 16:19:27 -06:00
|
|
|
"tailscale.com/tailcfg"
|
2022-11-05 02:07:22 -06:00
|
|
|
"tailscale.com/types/key"
|
2021-10-26 14:42:20 -06:00
|
|
|
)
|
|
|
|
|
2021-11-04 16:19:27 -06:00
|
|
|
type headscaleV1APIServer struct { // v1.HeadscaleServiceServer
|
|
|
|
v1.UnimplementedHeadscaleServiceServer
|
2021-10-26 14:42:20 -06:00
|
|
|
h *Headscale
|
|
|
|
}
|
|
|
|
|
2021-11-04 16:19:27 -06:00
|
|
|
func newHeadscaleV1APIServer(h *Headscale) v1.HeadscaleServiceServer {
|
2021-10-26 14:42:20 -06:00
|
|
|
return headscaleV1APIServer{
|
|
|
|
h: h,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-17 09:43:44 -07:00
|
|
|
func (api headscaleV1APIServer) GetUser(
|
2021-10-26 14:42:20 -06:00
|
|
|
ctx context.Context,
|
2023-01-17 09:43:44 -07:00
|
|
|
request *v1.GetUserRequest,
|
|
|
|
) (*v1.GetUserResponse, error) {
|
2023-05-11 01:09:18 -06:00
|
|
|
user, err := api.h.db.GetUser(request.GetName())
|
2021-11-04 16:19:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-29 10:44:32 -06:00
|
|
|
|
2023-05-21 10:37:59 -06:00
|
|
|
return &v1.GetUserResponse{User: user.Proto()}, nil
|
2021-10-29 10:44:32 -06:00
|
|
|
}
|
|
|
|
|
2023-01-17 09:43:44 -07:00
|
|
|
func (api headscaleV1APIServer) CreateUser(
|
2021-10-29 10:44:32 -06:00
|
|
|
ctx context.Context,
|
2023-01-17 09:43:44 -07:00
|
|
|
request *v1.CreateUserRequest,
|
|
|
|
) (*v1.CreateUserResponse, error) {
|
2023-05-11 01:09:18 -06:00
|
|
|
user, err := api.h.db.CreateUser(request.GetName())
|
2021-10-26 14:42:20 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-21 10:37:59 -06:00
|
|
|
return &v1.CreateUserResponse{User: user.Proto()}, nil
|
2021-11-04 16:19:27 -06:00
|
|
|
}
|
|
|
|
|
2023-01-17 09:43:44 -07:00
|
|
|
func (api headscaleV1APIServer) RenameUser(
|
2021-11-04 16:19:27 -06:00
|
|
|
ctx context.Context,
|
2023-01-17 09:43:44 -07:00
|
|
|
request *v1.RenameUserRequest,
|
|
|
|
) (*v1.RenameUserResponse, error) {
|
2023-05-11 01:09:18 -06:00
|
|
|
err := api.h.db.RenameUser(request.GetOldName(), request.GetNewName())
|
2021-11-04 16:19:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
user, err := api.h.db.GetUser(request.GetNewName())
|
2021-11-04 16:19:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-21 10:37:59 -06:00
|
|
|
return &v1.RenameUserResponse{User: user.Proto()}, nil
|
2021-10-29 10:44:32 -06:00
|
|
|
}
|
|
|
|
|
2023-01-17 09:43:44 -07:00
|
|
|
func (api headscaleV1APIServer) DeleteUser(
|
2021-10-29 10:44:32 -06:00
|
|
|
ctx context.Context,
|
2023-01-17 09:43:44 -07:00
|
|
|
request *v1.DeleteUserRequest,
|
|
|
|
) (*v1.DeleteUserResponse, error) {
|
2023-05-11 01:09:18 -06:00
|
|
|
err := api.h.db.DestroyUser(request.GetName())
|
2021-10-29 10:44:32 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-17 09:43:44 -07:00
|
|
|
return &v1.DeleteUserResponse{}, nil
|
2021-10-29 10:44:32 -06:00
|
|
|
}
|
|
|
|
|
2023-01-17 09:43:44 -07:00
|
|
|
func (api headscaleV1APIServer) ListUsers(
|
2021-10-29 10:44:32 -06:00
|
|
|
ctx context.Context,
|
2023-01-17 09:43:44 -07:00
|
|
|
request *v1.ListUsersRequest,
|
|
|
|
) (*v1.ListUsersResponse, error) {
|
2023-05-11 01:09:18 -06:00
|
|
|
users, err := api.h.db.ListUsers()
|
2021-10-29 10:44:32 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-17 09:43:44 -07:00
|
|
|
response := make([]*v1.User, len(users))
|
|
|
|
for index, user := range users {
|
2023-05-21 10:37:59 -06:00
|
|
|
response[index] = user.Proto()
|
2021-11-04 16:19:27 -06:00
|
|
|
}
|
|
|
|
|
2023-01-17 09:43:44 -07:00
|
|
|
log.Trace().Caller().Interface("users", response).Msg("")
|
2021-11-04 16:19:27 -06:00
|
|
|
|
2023-01-17 09:43:44 -07:00
|
|
|
return &v1.ListUsersResponse{Users: response}, nil
|
2021-11-04 16:19:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api headscaleV1APIServer) CreatePreAuthKey(
|
|
|
|
ctx context.Context,
|
|
|
|
request *v1.CreatePreAuthKeyRequest,
|
|
|
|
) (*v1.CreatePreAuthKeyResponse, error) {
|
2021-11-08 01:02:01 -07:00
|
|
|
var expiration time.Time
|
|
|
|
if request.GetExpiration() != nil {
|
|
|
|
expiration = request.GetExpiration().AsTime()
|
|
|
|
}
|
|
|
|
|
2022-09-23 01:58:06 -06:00
|
|
|
for _, tag := range request.AclTags {
|
|
|
|
err := validateTag(tag)
|
|
|
|
if err != nil {
|
|
|
|
return &v1.CreatePreAuthKeyResponse{
|
|
|
|
PreAuthKey: nil,
|
|
|
|
}, status.Error(codes.InvalidArgument, err.Error())
|
2022-08-25 04:43:15 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
preAuthKey, err := api.h.db.CreatePreAuthKey(
|
2023-01-17 09:43:44 -07:00
|
|
|
request.GetUser(),
|
2021-11-08 13:49:03 -07:00
|
|
|
request.GetReusable(),
|
2021-11-04 16:19:27 -06:00
|
|
|
request.GetEphemeral(),
|
|
|
|
&expiration,
|
2022-08-25 04:03:38 -06:00
|
|
|
request.AclTags,
|
2021-11-04 16:19:27 -06:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-21 10:37:59 -06:00
|
|
|
return &v1.CreatePreAuthKeyResponse{PreAuthKey: preAuthKey.Proto()}, nil
|
2021-11-04 16:19:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api headscaleV1APIServer) ExpirePreAuthKey(
|
|
|
|
ctx context.Context,
|
|
|
|
request *v1.ExpirePreAuthKeyRequest,
|
|
|
|
) (*v1.ExpirePreAuthKeyResponse, error) {
|
2024-02-08 09:28:19 -07:00
|
|
|
err := api.h.db.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
|
preAuthKey, err := db.GetPreAuthKey(tx, request.GetUser(), request.Key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-04 16:19:27 -06:00
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
return db.ExpirePreAuthKey(tx, preAuthKey)
|
|
|
|
})
|
2021-11-04 16:19:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &v1.ExpirePreAuthKeyResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api headscaleV1APIServer) ListPreAuthKeys(
|
|
|
|
ctx context.Context,
|
|
|
|
request *v1.ListPreAuthKeysRequest,
|
|
|
|
) (*v1.ListPreAuthKeysResponse, error) {
|
2023-05-11 01:09:18 -06:00
|
|
|
preAuthKeys, err := api.h.db.ListPreAuthKeys(request.GetUser())
|
2021-11-04 16:19:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response := make([]*v1.PreAuthKey, len(preAuthKeys))
|
|
|
|
for index, key := range preAuthKeys {
|
2023-05-21 10:37:59 -06:00
|
|
|
response[index] = key.Proto()
|
2021-11-04 16:19:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return &v1.ListPreAuthKeysResponse{PreAuthKeys: response}, nil
|
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
func (api headscaleV1APIServer) RegisterNode(
|
2021-11-04 16:19:27 -06:00
|
|
|
ctx context.Context,
|
2023-09-24 05:42:05 -06:00
|
|
|
request *v1.RegisterNodeRequest,
|
|
|
|
) (*v1.RegisterNodeResponse, error) {
|
2021-11-13 01:36:45 -07:00
|
|
|
log.Trace().
|
2023-01-17 09:43:44 -07:00
|
|
|
Str("user", request.GetUser()).
|
2023-11-19 14:37:04 -07:00
|
|
|
Str("machine_key", request.GetKey()).
|
2023-09-24 05:42:05 -06:00
|
|
|
Msg("Registering node")
|
2022-02-27 10:42:43 -07:00
|
|
|
|
2023-11-19 14:37:04 -07:00
|
|
|
var mkey key.MachinePublic
|
|
|
|
err := mkey.UnmarshalText([]byte(request.GetKey()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
node, err := db.Write(api.h.db.DB, func(tx *gorm.DB) (*types.Node, error) {
|
|
|
|
return db.RegisterNodeFromAuthCallback(
|
|
|
|
tx,
|
|
|
|
api.h.registrationCache,
|
|
|
|
mkey,
|
|
|
|
request.GetUser(),
|
|
|
|
nil,
|
|
|
|
util.RegisterMethodCLI,
|
|
|
|
api.h.cfg.IPPrefixes,
|
|
|
|
)
|
|
|
|
})
|
2021-11-04 16:19:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
return &v1.RegisterNodeResponse{Node: node.Proto()}, nil
|
2021-11-04 16:19:27 -06:00
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
func (api headscaleV1APIServer) GetNode(
|
2021-11-04 16:19:27 -06:00
|
|
|
ctx context.Context,
|
2023-09-24 05:42:05 -06:00
|
|
|
request *v1.GetNodeRequest,
|
|
|
|
) (*v1.GetNodeResponse, error) {
|
|
|
|
node, err := api.h.db.GetNodeByID(request.GetNodeId())
|
2021-11-04 16:19:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-09 10:09:24 -07:00
|
|
|
resp := node.Proto()
|
|
|
|
|
|
|
|
// Populate the online field based on
|
|
|
|
// currently connected nodes.
|
|
|
|
resp.Online = api.h.nodeNotifier.IsConnected(node.MachineKey)
|
|
|
|
|
|
|
|
return &v1.GetNodeResponse{Node: resp}, nil
|
2021-11-04 16:19:27 -06:00
|
|
|
}
|
|
|
|
|
2022-04-25 13:16:14 -06:00
|
|
|
func (api headscaleV1APIServer) SetTags(
|
2022-04-15 05:11:41 -06:00
|
|
|
ctx context.Context,
|
2022-04-25 13:16:14 -06:00
|
|
|
request *v1.SetTagsRequest,
|
|
|
|
) (*v1.SetTagsResponse, error) {
|
2022-05-13 01:47:34 -06:00
|
|
|
for _, tag := range request.GetTags() {
|
2022-07-25 03:25:20 -06:00
|
|
|
err := validateTag(tag)
|
|
|
|
if err != nil {
|
2024-02-08 09:28:19 -07:00
|
|
|
return nil, err
|
2022-05-13 01:47:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
node, err := db.Write(api.h.db.DB, func(tx *gorm.DB) (*types.Node, error) {
|
|
|
|
err := db.SetTags(tx, request.GetNodeId(), request.GetTags())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.GetNodeByID(tx, request.GetNodeId())
|
|
|
|
})
|
2022-05-13 03:09:28 -06:00
|
|
|
if err != nil {
|
|
|
|
return &v1.SetTagsResponse{
|
2023-09-24 05:42:05 -06:00
|
|
|
Node: nil,
|
2024-02-08 09:28:19 -07:00
|
|
|
}, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
stateUpdate := types.StateUpdate{
|
|
|
|
Type: types.StatePeerChanged,
|
|
|
|
ChangeNodes: types.Nodes{node},
|
|
|
|
Message: "called from api.SetTags",
|
|
|
|
}
|
|
|
|
if stateUpdate.Valid() {
|
|
|
|
ctx := types.NotifyCtx(ctx, "cli-settags", node.Hostname)
|
|
|
|
api.h.nodeNotifier.NotifyWithIgnore(ctx, stateUpdate, node.MachineKey.String())
|
2022-05-13 03:09:28 -06:00
|
|
|
}
|
2022-04-15 05:11:41 -06:00
|
|
|
|
2022-04-25 13:16:14 -06:00
|
|
|
log.Trace().
|
2023-09-24 05:42:05 -06:00
|
|
|
Str("node", node.Hostname).
|
2022-04-25 13:16:14 -06:00
|
|
|
Strs("tags", request.GetTags()).
|
2023-09-24 05:42:05 -06:00
|
|
|
Msg("Changing tags of node")
|
2022-04-15 05:11:41 -06:00
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
return &v1.SetTagsResponse{Node: node.Proto()}, nil
|
2022-04-15 05:11:41 -06:00
|
|
|
}
|
|
|
|
|
2022-07-25 03:25:20 -06:00
|
|
|
func validateTag(tag string) error {
|
|
|
|
if strings.Index(tag, "tag:") != 0 {
|
|
|
|
return fmt.Errorf("tag must start with the string 'tag:'")
|
|
|
|
}
|
|
|
|
if strings.ToLower(tag) != tag {
|
|
|
|
return fmt.Errorf("tag should be lowercase")
|
|
|
|
}
|
|
|
|
if len(strings.Fields(tag)) > 1 {
|
|
|
|
return fmt.Errorf("tag should not contains space")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
func (api headscaleV1APIServer) DeleteNode(
|
2021-11-04 16:19:27 -06:00
|
|
|
ctx context.Context,
|
2023-09-24 05:42:05 -06:00
|
|
|
request *v1.DeleteNodeRequest,
|
|
|
|
) (*v1.DeleteNodeResponse, error) {
|
|
|
|
node, err := api.h.db.GetNodeByID(request.GetNodeId())
|
2021-11-04 16:19:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
err = api.h.db.DeleteNode(
|
|
|
|
node,
|
2024-02-08 09:28:19 -07:00
|
|
|
api.h.nodeNotifier.ConnectedMap(),
|
2021-11-04 16:19:27 -06:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
stateUpdate := types.StateUpdate{
|
|
|
|
Type: types.StatePeerRemoved,
|
|
|
|
Removed: []tailcfg.NodeID{tailcfg.NodeID(node.ID)},
|
|
|
|
}
|
|
|
|
if stateUpdate.Valid() {
|
|
|
|
ctx := types.NotifyCtx(ctx, "cli-deletenode", node.Hostname)
|
|
|
|
api.h.nodeNotifier.NotifyAll(ctx, stateUpdate)
|
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
return &v1.DeleteNodeResponse{}, nil
|
2021-11-04 16:19:27 -06:00
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
func (api headscaleV1APIServer) ExpireNode(
|
2021-11-21 06:40:19 -07:00
|
|
|
ctx context.Context,
|
2023-09-24 05:42:05 -06:00
|
|
|
request *v1.ExpireNodeRequest,
|
|
|
|
) (*v1.ExpireNodeResponse, error) {
|
2024-02-08 09:28:19 -07:00
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
node, err := db.Write(api.h.db.DB, func(tx *gorm.DB) (*types.Node, error) {
|
|
|
|
db.NodeSetExpiry(
|
|
|
|
tx,
|
|
|
|
request.GetNodeId(),
|
|
|
|
now,
|
|
|
|
)
|
|
|
|
|
|
|
|
return db.GetNodeByID(tx, request.GetNodeId())
|
|
|
|
})
|
2021-11-21 06:40:19 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
selfUpdate := types.StateUpdate{
|
|
|
|
Type: types.StateSelfUpdate,
|
|
|
|
ChangeNodes: types.Nodes{node},
|
|
|
|
}
|
|
|
|
if selfUpdate.Valid() {
|
|
|
|
ctx := types.NotifyCtx(ctx, "cli-expirenode-self", node.Hostname)
|
|
|
|
api.h.nodeNotifier.NotifyByMachineKey(
|
|
|
|
ctx,
|
|
|
|
selfUpdate,
|
|
|
|
node.MachineKey)
|
|
|
|
}
|
2023-07-17 05:35:05 -06:00
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
stateUpdate := types.StateUpdateExpire(node.ID, now)
|
|
|
|
if stateUpdate.Valid() {
|
|
|
|
ctx := types.NotifyCtx(ctx, "cli-expirenode-peers", node.Hostname)
|
|
|
|
api.h.nodeNotifier.NotifyWithIgnore(ctx, stateUpdate, node.MachineKey.String())
|
|
|
|
}
|
2021-11-21 06:40:19 -07:00
|
|
|
|
|
|
|
log.Trace().
|
2023-09-24 05:42:05 -06:00
|
|
|
Str("node", node.Hostname).
|
|
|
|
Time("expiry", *node.Expiry).
|
|
|
|
Msg("node expired")
|
2021-11-21 06:40:19 -07:00
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
return &v1.ExpireNodeResponse{Node: node.Proto()}, nil
|
2021-11-21 06:40:19 -07:00
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
func (api headscaleV1APIServer) RenameNode(
|
2022-03-13 15:03:20 -06:00
|
|
|
ctx context.Context,
|
2023-09-24 05:42:05 -06:00
|
|
|
request *v1.RenameNodeRequest,
|
|
|
|
) (*v1.RenameNodeResponse, error) {
|
2024-02-08 09:28:19 -07:00
|
|
|
node, err := db.Write(api.h.db.DB, func(tx *gorm.DB) (*types.Node, error) {
|
|
|
|
err := db.RenameNode(
|
|
|
|
tx,
|
|
|
|
request.GetNodeId(),
|
|
|
|
request.GetNewName(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.GetNodeByID(tx, request.GetNodeId())
|
|
|
|
})
|
2022-03-13 15:03:20 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
stateUpdate := types.StateUpdate{
|
|
|
|
Type: types.StatePeerChanged,
|
|
|
|
ChangeNodes: types.Nodes{node},
|
|
|
|
Message: "called from api.RenameNode",
|
|
|
|
}
|
|
|
|
if stateUpdate.Valid() {
|
|
|
|
ctx := types.NotifyCtx(ctx, "cli-renamenode", node.Hostname)
|
|
|
|
api.h.nodeNotifier.NotifyWithIgnore(ctx, stateUpdate, node.MachineKey.String())
|
2022-04-24 13:56:28 -06:00
|
|
|
}
|
2022-03-13 15:03:20 -06:00
|
|
|
|
|
|
|
log.Trace().
|
2023-09-24 05:42:05 -06:00
|
|
|
Str("node", node.Hostname).
|
2022-05-16 12:35:35 -06:00
|
|
|
Str("new_name", request.GetNewName()).
|
2023-09-24 05:42:05 -06:00
|
|
|
Msg("node renamed")
|
2022-03-13 15:03:20 -06:00
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
return &v1.RenameNodeResponse{Node: node.Proto()}, nil
|
2022-03-13 15:03:20 -06:00
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
func (api headscaleV1APIServer) ListNodes(
|
2021-11-04 16:19:27 -06:00
|
|
|
ctx context.Context,
|
2023-09-24 05:42:05 -06:00
|
|
|
request *v1.ListNodesRequest,
|
|
|
|
) (*v1.ListNodesResponse, error) {
|
2024-02-08 09:28:19 -07:00
|
|
|
isConnected := api.h.nodeNotifier.ConnectedMap()
|
2023-01-17 09:43:44 -07:00
|
|
|
if request.GetUser() != "" {
|
2024-02-08 09:28:19 -07:00
|
|
|
nodes, err := db.Read(api.h.db.DB, func(rx *gorm.DB) (types.Nodes, error) {
|
|
|
|
return db.ListNodesByUser(rx, request.GetUser())
|
|
|
|
})
|
2021-11-04 16:19:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
response := make([]*v1.Node, len(nodes))
|
|
|
|
for index, node := range nodes {
|
2023-12-09 10:09:24 -07:00
|
|
|
resp := node.Proto()
|
|
|
|
|
|
|
|
// Populate the online field based on
|
|
|
|
// currently connected nodes.
|
2024-02-08 09:28:19 -07:00
|
|
|
resp.Online = isConnected[node.MachineKey]
|
2023-12-09 10:09:24 -07:00
|
|
|
|
|
|
|
response[index] = resp
|
2021-11-04 16:19:27 -06:00
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
return &v1.ListNodesResponse{Nodes: response}, nil
|
2021-11-04 16:19:27 -06:00
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
nodes, err := api.h.db.ListNodes()
|
2021-11-04 16:19:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
response := make([]*v1.Node, len(nodes))
|
|
|
|
for index, node := range nodes {
|
2023-12-09 10:09:24 -07:00
|
|
|
resp := node.Proto()
|
|
|
|
|
|
|
|
// Populate the online field based on
|
|
|
|
// currently connected nodes.
|
2024-02-08 09:28:19 -07:00
|
|
|
resp.Online = isConnected[node.MachineKey]
|
2023-12-09 10:09:24 -07:00
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
validTags, invalidTags := api.h.ACLPolicy.TagsOfNode(
|
2024-02-08 09:28:19 -07:00
|
|
|
node,
|
2022-04-16 05:15:18 -06:00
|
|
|
)
|
2023-12-09 10:09:24 -07:00
|
|
|
resp.InvalidTags = invalidTags
|
|
|
|
resp.ValidTags = validTags
|
|
|
|
response[index] = resp
|
2021-11-04 16:19:27 -06:00
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
return &v1.ListNodesResponse{Nodes: response}, nil
|
2021-11-04 16:19:27 -06:00
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
func (api headscaleV1APIServer) MoveNode(
|
2022-05-01 07:44:34 -06:00
|
|
|
ctx context.Context,
|
2023-09-24 05:42:05 -06:00
|
|
|
request *v1.MoveNodeRequest,
|
|
|
|
) (*v1.MoveNodeResponse, error) {
|
|
|
|
node, err := api.h.db.GetNodeByID(request.GetNodeId())
|
2022-05-01 07:44:34 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
err = api.h.db.AssignNodeToUser(node, request.GetUser())
|
2022-05-01 07:44:34 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
return &v1.MoveNodeResponse{Node: node.Proto()}, nil
|
2022-05-01 07:44:34 -06:00
|
|
|
}
|
|
|
|
|
2022-11-25 17:03:39 -07:00
|
|
|
func (api headscaleV1APIServer) GetRoutes(
|
2021-11-04 16:19:27 -06:00
|
|
|
ctx context.Context,
|
2022-11-25 17:03:39 -07:00
|
|
|
request *v1.GetRoutesRequest,
|
|
|
|
) (*v1.GetRoutesResponse, error) {
|
2024-02-08 09:28:19 -07:00
|
|
|
routes, err := db.Read(api.h.db.DB, func(rx *gorm.DB) (types.Routes, error) {
|
|
|
|
return db.GetRoutes(rx)
|
|
|
|
})
|
2021-11-04 16:19:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-25 17:03:39 -07:00
|
|
|
return &v1.GetRoutesResponse{
|
2023-05-21 10:37:59 -06:00
|
|
|
Routes: types.Routes(routes).Proto(),
|
2021-11-04 16:19:27 -06:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-11-25 17:03:39 -07:00
|
|
|
func (api headscaleV1APIServer) EnableRoute(
|
|
|
|
ctx context.Context,
|
|
|
|
request *v1.EnableRouteRequest,
|
|
|
|
) (*v1.EnableRouteResponse, error) {
|
2024-02-08 09:28:19 -07:00
|
|
|
update, err := db.Write(api.h.db.DB, func(tx *gorm.DB) (*types.StateUpdate, error) {
|
|
|
|
return db.EnableRoute(tx, request.GetRouteId())
|
|
|
|
})
|
2022-11-25 17:03:39 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
if update != nil && update.Valid() {
|
|
|
|
ctx := types.NotifyCtx(ctx, "cli-enableroute", "unknown")
|
|
|
|
api.h.nodeNotifier.NotifyAll(
|
|
|
|
ctx, *update)
|
|
|
|
}
|
|
|
|
|
2022-11-25 17:03:39 -07:00
|
|
|
return &v1.EnableRouteResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api headscaleV1APIServer) DisableRoute(
|
|
|
|
ctx context.Context,
|
|
|
|
request *v1.DisableRouteRequest,
|
|
|
|
) (*v1.DisableRouteResponse, error) {
|
2024-02-08 09:28:19 -07:00
|
|
|
isConnected := api.h.nodeNotifier.ConnectedMap()
|
|
|
|
update, err := db.Write(api.h.db.DB, func(tx *gorm.DB) (*types.StateUpdate, error) {
|
|
|
|
return db.DisableRoute(tx, request.GetRouteId(), isConnected)
|
|
|
|
})
|
2022-11-25 17:03:39 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
if update != nil && update.Valid() {
|
|
|
|
ctx := types.NotifyCtx(ctx, "cli-disableroute", "unknown")
|
|
|
|
api.h.nodeNotifier.NotifyAll(ctx, *update)
|
|
|
|
}
|
|
|
|
|
2022-11-25 17:03:39 -07:00
|
|
|
return &v1.DisableRouteResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
func (api headscaleV1APIServer) GetNodeRoutes(
|
2021-11-04 16:19:27 -06:00
|
|
|
ctx context.Context,
|
2023-09-24 05:42:05 -06:00
|
|
|
request *v1.GetNodeRoutesRequest,
|
|
|
|
) (*v1.GetNodeRoutesResponse, error) {
|
|
|
|
node, err := api.h.db.GetNodeByID(request.GetNodeId())
|
2021-11-04 16:19:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
routes, err := api.h.db.GetNodeRoutes(node)
|
2021-11-04 16:19:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
return &v1.GetNodeRoutesResponse{
|
2023-05-21 10:37:59 -06:00
|
|
|
Routes: types.Routes(routes).Proto(),
|
2021-11-04 16:19:27 -06:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-03-06 01:05:40 -07:00
|
|
|
func (api headscaleV1APIServer) DeleteRoute(
|
|
|
|
ctx context.Context,
|
|
|
|
request *v1.DeleteRouteRequest,
|
|
|
|
) (*v1.DeleteRouteResponse, error) {
|
2024-02-08 09:28:19 -07:00
|
|
|
isConnected := api.h.nodeNotifier.ConnectedMap()
|
|
|
|
update, err := db.Write(api.h.db.DB, func(tx *gorm.DB) (*types.StateUpdate, error) {
|
|
|
|
return db.DeleteRoute(tx, request.GetRouteId(), isConnected)
|
|
|
|
})
|
2023-03-06 01:05:40 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:28:19 -07:00
|
|
|
if update != nil && update.Valid() {
|
|
|
|
ctx := types.NotifyCtx(ctx, "cli-deleteroute", "unknown")
|
|
|
|
api.h.nodeNotifier.NotifyWithIgnore(ctx, *update)
|
|
|
|
}
|
|
|
|
|
2023-03-06 01:05:40 -07:00
|
|
|
return &v1.DeleteRouteResponse{}, nil
|
|
|
|
}
|
|
|
|
|
2022-01-25 15:11:15 -07: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()
|
|
|
|
}
|
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
apiKey, _, err := api.h.db.CreateAPIKey(
|
2022-01-25 15:11:15 -07:00
|
|
|
&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) {
|
2023-05-21 10:37:59 -06:00
|
|
|
var apiKey *types.APIKey
|
2022-01-25 15:11:15 -07:00
|
|
|
var err error
|
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
apiKey, err = api.h.db.GetAPIKey(request.Prefix)
|
2022-01-25 15:11:15 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
err = api.h.db.ExpireAPIKey(apiKey)
|
2022-01-25 15:11:15 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &v1.ExpireApiKeyResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api headscaleV1APIServer) ListApiKeys(
|
|
|
|
ctx context.Context,
|
|
|
|
request *v1.ListApiKeysRequest,
|
|
|
|
) (*v1.ListApiKeysResponse, error) {
|
2023-05-11 01:09:18 -06:00
|
|
|
apiKeys, err := api.h.db.ListAPIKeys()
|
2022-01-25 15:11:15 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response := make([]*v1.ApiKey, len(apiKeys))
|
|
|
|
for index, key := range apiKeys {
|
2023-05-21 10:37:59 -06:00
|
|
|
response[index] = key.Proto()
|
2022-01-25 15:11:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return &v1.ListApiKeysResponse{ApiKeys: response}, nil
|
|
|
|
}
|
|
|
|
|
2021-11-04 16:19:27 -06:00
|
|
|
// The following service calls are for testing and debugging
|
2023-09-24 05:42:05 -06:00
|
|
|
func (api headscaleV1APIServer) DebugCreateNode(
|
2021-11-04 16:19:27 -06:00
|
|
|
ctx context.Context,
|
2023-09-24 05:42:05 -06:00
|
|
|
request *v1.DebugCreateNodeRequest,
|
|
|
|
) (*v1.DebugCreateNodeResponse, error) {
|
2023-05-11 01:09:18 -06:00
|
|
|
user, err := api.h.db.GetUser(request.GetUser())
|
2021-11-04 16:19:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-11 01:09:18 -06:00
|
|
|
routes, err := util.StringToIPPrefix(request.GetRoutes())
|
2021-11-04 16:19:27 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-11-13 01:36:45 -07:00
|
|
|
log.Trace().
|
|
|
|
Caller().
|
|
|
|
Interface("route-prefix", routes).
|
|
|
|
Interface("route-str", request.GetRoutes()).
|
|
|
|
Msg("")
|
2021-11-04 16:19:27 -06:00
|
|
|
|
|
|
|
hostinfo := tailcfg.Hostinfo{
|
|
|
|
RoutableIPs: routes,
|
|
|
|
OS: "TestOS",
|
2023-09-24 05:42:05 -06:00
|
|
|
Hostname: "DebugTestNode",
|
2021-11-04 16:19:27 -06:00
|
|
|
}
|
|
|
|
|
2023-11-19 14:37:04 -07:00
|
|
|
var mkey key.MachinePublic
|
|
|
|
err = mkey.UnmarshalText([]byte(request.GetKey()))
|
2022-05-16 12:32:37 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-19 14:37:04 -07:00
|
|
|
givenName, err := api.h.db.GenerateGivenName(mkey, request.GetName())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeKey := key.NewNode()
|
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
newNode := types.Node{
|
2023-11-19 14:37:04 -07:00
|
|
|
MachineKey: mkey,
|
|
|
|
NodeKey: nodeKey.Public(),
|
2022-04-24 13:56:28 -06:00
|
|
|
Hostname: request.GetName(),
|
2022-05-16 12:32:37 -06:00
|
|
|
GivenName: givenName,
|
2023-01-17 09:43:44 -07:00
|
|
|
User: *user,
|
2021-11-04 16:19:27 -06:00
|
|
|
|
2023-09-11 05:23:29 -06:00
|
|
|
Expiry: &time.Time{},
|
|
|
|
LastSeen: &time.Time{},
|
2021-11-04 16:19:27 -06:00
|
|
|
|
2023-11-21 10:20:06 -07:00
|
|
|
Hostinfo: &hostinfo,
|
2021-11-04 16:19:27 -06:00
|
|
|
}
|
2022-11-28 08:54:23 -07:00
|
|
|
|
2023-11-19 14:37:04 -07:00
|
|
|
log.Debug().
|
|
|
|
Str("machine_key", mkey.ShortString()).
|
|
|
|
Msg("adding debug machine via CLI, appending to registration cache")
|
2021-11-04 16:19:27 -06:00
|
|
|
|
2022-02-28 09:34:50 -07:00
|
|
|
api.h.registrationCache.Set(
|
2023-11-19 14:37:04 -07:00
|
|
|
mkey.String(),
|
2023-09-24 05:42:05 -06:00
|
|
|
newNode,
|
2022-02-28 15:42:30 -07:00
|
|
|
registerCacheExpiration,
|
2022-02-28 09:34:50 -07:00
|
|
|
)
|
2021-10-29 10:44:32 -06:00
|
|
|
|
2023-09-24 05:42:05 -06:00
|
|
|
return &v1.DebugCreateNodeResponse{Node: newNode.Proto()}, nil
|
2021-10-26 14:42:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api headscaleV1APIServer) mustEmbedUnimplementedHeadscaleServiceServer() {}
|