headscale

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2024 License: MIT Imports: 10 Imported by: 0

README

Headscale Client Go

Go Reference Go Report Card

A client implementation for the Headscale HTTP API.

Example (Using API Key)

package main

import (
	"context"
	"fmt"
	"log/slog"
	"os"

	hsClient "github.com/hibare/headscale-client-go"
)

func main() {
	slog.SetLogLoggerLevel(slog.LevelDebug) // Optional

	serverUrl := os.Getenv("HS_SERVER_URL")
	apiToken := os.Getenv("HS_API_TOKEN")

	client, err := hsClient.NewClient(serverUrl, apiToken, hsClient.HeadscaleClientOptions{})
	if err != nil {
		panic(err)
	}

	nodes, err := client.Nodes().List(context.Background())
	if err != nil {
		panic(err)
	}

	for _, node := range nodes.Nodes {
		fmt.Printf("Node: %s\n", node.Name)
	}

}

Documentation

Index

Constants

View Source
const DefaultHTTPClientTimeout = time.Minute
View Source
const DefaultUserAgent = "headscale-client-go"

Variables

This section is empty.

Functions

This section is empty.

Types

type APIKey

type APIKey struct {
	ID         string    `json:"id"`
	Prefix     string    `json:"prefix"`
	Expiration time.Time `json:"expiration"`
	CreatedAt  time.Time `json:"createdAt"`
	LastSeen   time.Time `json:"lastSeen"`
}

type APIKeyResource

type APIKeyResource struct {
	Client HeadscaleClientInterface
}

func (*APIKeyResource) Create

func (a *APIKeyResource) Create(ctx context.Context, expiration time.Time) (APIKey, error)

func (*APIKeyResource) Delete

func (a *APIKeyResource) Delete(ctx context.Context, prefix string) error

func (*APIKeyResource) Expire

func (a *APIKeyResource) Expire(ctx context.Context, prefix string) error

func (*APIKeyResource) List

type APIKeysResponse

type APIKeysResponse struct {
	APIKeys []APIKey `json:"apiKeys"`
}

type AddAPIKeyRequest

type AddAPIKeyRequest struct {
	Expiration time.Time `json:"expiration"`
}

type AddTagsRequest

type AddTagsRequest struct {
	Tags []string `json:"tags"`
}

type ChangesResponse

type ChangesResponse struct {
	Changes []string `json:"changes"`
}

type Client

type Client struct {
	BaseURL   *url.URL
	UserAgent string
	APIKey    string
	HTTP      *http.Client
	Logger    Logger
	// contains filtered or unexported fields
}

func (*Client) APIKeys

func (c *Client) APIKeys() *APIKeyResource

func (*Client) Nodes

func (c *Client) Nodes() *NodeResource

func (*Client) Policy

func (c *Client) Policy() *PolicyResource

func (*Client) Routes

func (c *Client) Routes() *RoutesResource

func (*Client) Users

func (c *Client) Users() *UserResource

type CreatePreAuthKeyRequest

type CreatePreAuthKeyRequest struct {
	User       string    `json:"user"`
	Reusable   bool      `json:"reusable"`
	Ephemeral  bool      `json:"ephemeral"`
	Expiration time.Time `json:"expiration"`
	AclTags    []string  `json:"aclTags"`
}

type CreateUserRequest

type CreateUserRequest struct {
	Name string `json:"name"`
}

type DefaultLogger

type DefaultLogger struct {
	// contains filtered or unexported fields
}

DefaultLogger is a simple implementation of the Logger interface.

func NewDefaultLogger

func NewDefaultLogger(level LogLevel) *DefaultLogger

NewDefaultLogger creates a new DefaultLogger.

func (*DefaultLogger) Debug

func (l *DefaultLogger) Debug(ctx context.Context, msg string, keysAndValues ...interface{})

Debug logs a debug message.

func (*DefaultLogger) Error

func (l *DefaultLogger) Error(ctx context.Context, msg string, keysAndValues ...interface{})

Error logs an error message.

func (*DefaultLogger) Info

func (l *DefaultLogger) Info(ctx context.Context, msg string, keysAndValues ...interface{})

Info logs an informational message.

func (*DefaultLogger) Warn

func (l *DefaultLogger) Warn(ctx context.Context, msg string, keysAndValues ...interface{})

Warn logs a warning message.

type ExpireAPIKeyRequest

type ExpireAPIKeyRequest struct {
	Prefix string `json:"prefix"`
}

type ExpirePreAuthKeyRequest

type ExpirePreAuthKeyRequest struct {
	User string `json:"user"`
	Key  string `json:"key"`
}

type HeadscaleClientInterface

type HeadscaleClientInterface interface {
	APIKeys() *APIKeyResource
	Nodes() *NodeResource
	Policy() *PolicyResource
	Routes() *RoutesResource
	Users() *UserResource
	// contains filtered or unexported methods
}

func NewClient

func NewClient(baseURL, apiKey string, opt HeadscaleClientOptions) (HeadscaleClientInterface, error)

func NewMockClient

func NewMockClient() HeadscaleClientInterface

type HeadscaleClientOptions

type HeadscaleClientOptions struct {
	Http      *http.Client
	UserAgent string
	Logger    Logger
}

type LogLevel

type LogLevel int
const (
	LevelDebug LogLevel = iota
	LevelInfo
	LevelWarn
	LevelError
	LevelCritical
)

type Logger

type Logger interface {
	Info(ctx context.Context, msg string, keysAndValues ...interface{})
	Error(ctx context.Context, msg string, keysAndValues ...interface{})
	Warn(ctx context.Context, msg string, keysAndValues ...interface{})
	Debug(ctx context.Context, msg string, keysAndValues ...interface{})
}

Logger is an interface for logging messages.

type MockClient

type MockClient struct {
	mock.Mock
}

MockClient is a mock implementation of the HeadscaleClientInterface.

func (*MockClient) APIKeys

func (m *MockClient) APIKeys() *APIKeyResource

Implementing the HeadscaleClientInterface methods

func (*MockClient) Nodes

func (m *MockClient) Nodes() *NodeResource

func (*MockClient) Policy

func (m *MockClient) Policy() *PolicyResource

func (*MockClient) Routes

func (m *MockClient) Routes() *RoutesResource

func (*MockClient) Users

func (m *MockClient) Users() *UserResource

type Node

type Node struct {
	ID             string     `json:"id"`
	MachineKey     string     `json:"machineKey"`
	NodeKey        string     `json:"nodeKey"`
	DiscoKey       string     `json:"discoKey"`
	IPAddresses    []string   `json:"ipAddresses"`
	Name           string     `json:"name"`
	User           User       `json:"user"`
	LastSeen       time.Time  `json:"lastSeen"`
	Expiry         time.Time  `json:"expiry"`
	PreAuthKey     PreAuthKey `json:"preAuthKey"`
	CreatedAt      time.Time  `json:"createdAt"`
	RegisterMethod string     `json:"registerMethod"`
	ForcedTags     []string   `json:"forcedTags"`
	InvalidTags    []string   `json:"invalidTags"`
	ValidTags      []string   `json:"validTags"`
	GivenName      string     `json:"givenName"`
	Online         bool       `json:"online"`
}

type NodeResource

type NodeResource struct {
	Client HeadscaleClientInterface
}

func (*NodeResource) AddTags

func (n *NodeResource) AddTags(ctx context.Context, id string, tags []string) (NodeResponse, error)

func (*NodeResource) Delete

func (n *NodeResource) Delete(ctx context.Context, id string) error

func (*NodeResource) Expire

func (n *NodeResource) Expire(ctx context.Context, id string) error

func (*NodeResource) Get

func (n *NodeResource) Get(ctx context.Context, id string) (NodeResponse, error)

func (*NodeResource) GetRoutes

func (n *NodeResource) GetRoutes(ctx context.Context, id string) (RoutesResponse, error)

func (*NodeResource) List

func (*NodeResource) Register

func (n *NodeResource) Register(ctx context.Context, user, key string) (NodeResponse, error)

func (*NodeResource) Rename

func (n *NodeResource) Rename(ctx context.Context, id, name string) (NodeResponse, error)

func (*NodeResource) UpdateUser

func (n *NodeResource) UpdateUser(ctx context.Context, id, user string) (NodeResponse, error)

type NodeResponse

type NodeResponse struct {
	Node Node `json:"node"`
}

type NodesResponse

type NodesResponse struct {
	Nodes []Node `json:"nodes"`
}

type Policy

type Policy struct {
	Policy    string `json:"policy"`
	UpdatedAt string `json:"updated_at"`
}

type PolicyResource

type PolicyResource struct {
	Client HeadscaleClientInterface
}

func (*PolicyResource) Get

func (p *PolicyResource) Get(ctx context.Context) (Policy, error)

func (*PolicyResource) Put

func (p *PolicyResource) Put(ctx context.Context, policy string) error

type PreAuthKey

type PreAuthKey struct {
	User       string    `json:"user"`
	ID         string    `json:"id"`
	Key        string    `json:"key"`
	Reusable   bool      `json:"reusable"`
	Ephemeral  bool      `json:"ephemeral"`
	Used       bool      `json:"used"`
	Expiration time.Time `json:"expiration"`
	CreatedAt  time.Time `json:"createdAt"`
	AclTags    []string  `json:"aclTags"`
}

type PreAuthKeyResource

type PreAuthKeyResource struct {
	Client HeadscaleClientInterface
}

func (*PreAuthKeyResource) Create

func (p *PreAuthKeyResource) Create(ctx context.Context, user string, reusable bool, ephemeral bool, expiration time.Time, aclTags []string) (PreAuthKeyResponse, error)

func (*PreAuthKeyResource) Expire

func (p *PreAuthKeyResource) Expire(ctx context.Context, user string, key string) error

func (*PreAuthKeyResource) List

type PreAuthKeyResponse

type PreAuthKeyResponse struct {
	PreAuthKey []PreAuthKey `json:"preAuthKey"`
}

type PreAuthKeysResponse

type PreAuthKeysResponse struct {
	PreAuthKeys []PreAuthKey `json:"preAuthKeys"`
}

type Route

type Route struct {
	ID         string       `json:"id"`
	Node       NodeResponse `json:"node"`
	Prefix     string       `json:"prefix"`
	Advertised bool         `json:"advertised"`
	Enabled    bool         `json:"enabled"`
	IsPrimary  bool         `json:"isPrimary"`
	CreatedAt  time.Time    `json:"createdAt"`
	UpdatedAt  time.Time    `json:"updatedAt"`
	DeletedAt  time.Time    `json:"deletedAt"`
}

type RoutesResource

type RoutesResource struct {
	Client HeadscaleClientInterface
}

func (*RoutesResource) Delete

func (r *RoutesResource) Delete(ctx context.Context, id string) error

func (*RoutesResource) Disable

func (r *RoutesResource) Disable(ctx context.Context, id string) error

func (*RoutesResource) Enable

func (r *RoutesResource) Enable(ctx context.Context, id string) error

func (*RoutesResource) List

type RoutesResponse

type RoutesResponse struct {
	Routes []Route `json:"routes"`
}

type UpdatePolicyRequest

type UpdatePolicyRequest struct {
	Policy string `json:"policy"`
}

type User

type User struct {
	ID            string    `json:"id"`
	Name          string    `json:"name"`
	CreatedAt     time.Time `json:"createdAt"`
	DisplayName   string    `json:"displayName"`
	Email         string    `json:"email"`
	ProviderID    string    `json:"providerId"`
	Provider      string    `json:"provider"`
	ProfilePicURL string    `json:"profilePicUrl"`
}

type UserResource

type UserResource struct {
	Client HeadscaleClientInterface
}

func (*UserResource) Create

func (u *UserResource) Create(ctx context.Context, name string) (User, error)

func (*UserResource) Delete

func (u *UserResource) Delete(ctx context.Context, name string) error

func (*UserResource) Get

func (u *UserResource) Get(ctx context.Context, name string) (UserResponse, error)

func (*UserResource) List

func (*UserResource) Rename

func (u *UserResource) Rename(ctx context.Context, name, newName string) error

type UserResponse

type UserResponse struct {
	User User `json:"user"`
}

type UsersResponse

type UsersResponse struct {
	Users []User `json:"user"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL