daemoncmd

package
v0.17.1 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2023 License: Apache-2.0 Imports: 47 Imported by: 0

Documentation

Overview

Package daemoncmd contains the entrypoint for webmesh nodes running as an application daemon.

Index

Constants

View Source
const DaemonNamespaceHeader = "x-webmesh-namespace"

DaemonNamespaceHeader is the header used to set the namespace for daemon requests. When set, the daemon will only return connections that are in the same namespace. If unset, the daemon will use a default "global" namespace.

View Source
const DefaultNamespace = "global"

DefaultNamespace is the default namespace.

Variables

View Source
var (
	// ErrNoPortsAvailable is returned when no ports are available.
	ErrNoPortsAvailable = status.Errorf(codes.FailedPrecondition, "no ports available")
	// ErrNoIndexAvailable is returned when no utun index is available.
	ErrNoIndexAvailable = status.Errorf(codes.FailedPrecondition, "no utun index available")
	// ErrNotConnected is returned when the node is not connected to the mesh.
	ErrNotConnected = status.Errorf(codes.FailedPrecondition, "not connected to the specified network")
	// ErrAlreadyConnected is returned when the node is already connected to the mesh.
	ErrAlreadyConnected = status.Errorf(codes.FailedPrecondition, "already connected to the specified network")
	// ErrConnected is returned when the node is connected to the mesh.
	ErrConnected = status.Errorf(codes.FailedPrecondition, "connected to the specified network")
)
View Source
var ProfilesPrefix = []byte("/profiles")

ProfilesPrefix is the prefix for profiles.

Functions

func DefaultDaemonSocket

func DefaultDaemonSocket() string

DefaultDaemonSocket returns the default daemon socket path.

func NamespaceFromContext added in v0.17.0

func NamespaceFromContext(ctx context.Context) string

func NamespacedPrefixFromContext added in v0.17.0

func NamespacedPrefixFromContext(ctx context.Context) []byte

NamespacedPrefixFromContext returns the namespaced profiles prefix for the given context.

func Run

func Run(ctx context.Context, conf Config) error

Run runs the app daemon with the given configuration. The context can be used to shutdown the server, otherwise it will wait for a SIGINT or SIGTERM.

Types

type AppDaemon

type AppDaemon struct {
	v1.UnimplementedAppDaemonServer
	// contains filtered or unexported fields
}

AppDaemon is the app daemon RPC server.

func NewServer

func NewServer(conf Config) (*AppDaemon, error)

NewServer returns a new AppDaemon server.

func (*AppDaemon) Close

func (app *AppDaemon) Close() error

func (*AppDaemon) Connect

func (app *AppDaemon) Connect(ctx context.Context, req *v1.ConnectRequest) (*v1.ConnectResponse, error)

func (*AppDaemon) Disconnect

func (app *AppDaemon) Disconnect(ctx context.Context, req *v1.DisconnectRequest) (*v1.DisconnectResponse, error)

func (*AppDaemon) DropConnection added in v0.17.0

func (app *AppDaemon) DropConnection(ctx context.Context, req *v1.DropConnectionRequest) (*v1.DropConnectionResponse, error)

func (*AppDaemon) GetConnection added in v0.17.0

func (app *AppDaemon) GetConnection(ctx context.Context, req *v1.GetConnectionRequest) (*v1.GetConnectionResponse, error)

func (*AppDaemon) ListConnections added in v0.17.0

func (app *AppDaemon) ListConnections(ctx context.Context, req *v1.ListConnectionsRequest) (*v1.ListConnectionsResponse, error)

func (*AppDaemon) Metrics

func (app *AppDaemon) Metrics(ctx context.Context, req *v1.MetricsRequest) (*v1.MetricsResponse, error)

func (*AppDaemon) PutConnection added in v0.17.0

func (app *AppDaemon) PutConnection(ctx context.Context, req *v1.PutConnectionRequest) (*v1.PutConnectionResponse, error)

func (*AppDaemon) Query

func (app *AppDaemon) Query(ctx context.Context, req *v1.AppQueryRequest) (*v1.QueryResponse, error)

func (*AppDaemon) Status

func (app *AppDaemon) Status(ctx context.Context, _ *v1.StatusRequest) (*v1.DaemonStatus, error)

type CORS added in v0.15.11

type CORS struct {
	// Enabled is true if CORS is enabled.
	Enabled bool `koanf:"enabled"`
	// AllowedOrigins is a list of allowed origins.
	AllowedOrigins []string `koanf:"allowed-origins"`
}

CORS are options for configuring CORS. These are only applicable when grpc-web is enabled.

func (*CORS) BindFlags added in v0.15.11

func (conf *CORS) BindFlags(prefix string, flagset *pflag.FlagSet)

BindFlags binds the CORS flags to the given flagset.

type Config

type Config struct {
	// Enabled is true if the daemon is enabled.
	Enabled bool `koanf:"enabled"`
	// NodeID is the ID to use for mesh connections from this server.
	// If not provided, one will be generated from the key.
	NodeID string `koanf:"node-id"`
	// KeyFile is the path to the WireGuard private key for the node.
	// If set and it does not exist it will be created, otherwise one
	// will be generated.
	KeyFile string `koanf:"key-file,omitempty"`
	// KeyRotation is the duration between key rotations.
	KeyRotation time.Duration `koanf:"key-rotation"`
	// Bind is the bind address for the daemon.
	Bind string `koanf:"bind"`
	// InsecureSocket uses an insecure socket when binding to a unix socket.
	InsecureSocket bool `koanf:"insecure-socket"`
	// GRPCWeb enables gRPC-Web support.
	GRPCWeb bool `koanf:"grpc-web"`
	// CORS are options for configuring CORS. These are only applicable when
	// grpc-web is enabled.
	CORS CORS `koanf:"cors"`
	// UI are options for exposing a gRPC UI.
	UI WebUI `koanf:"ui"`
	// Persistence are options for persisting mesh data.
	Persistence Persistence `koanf:"persistence"`
	// WireGuardStartPort is the starting port for WireGuard connections.
	WireGuardStartPort uint16 `koanf:"wireguard-start-port"`
	// LogLevel is the log level for the daemon.
	LogLevel string `koanf:"log-level"`
	// LogFormat is the log format for the daemon.
	LogFormat string `koanf:"log-format"`

	// Logger is a pre-configured logger.
	Logger *slog.Logger `koanf:"-"`
}

Config is the configuration for the applicaton daeemon.

func NewDefaultConfig

func NewDefaultConfig() *Config

NewDefaultConfig returns the default configuration.

func (*Config) BindFlags

func (conf *Config) BindFlags(prefix string, flagset *pflag.FlagSet) *Config

BindFlags binds the flags to the given flagset.

func (*Config) LoadKey

func (conf *Config) LoadKey(log *slog.Logger) (crypto.PrivateKey, error)

LoadKey loads the wireguard key from the configuration.

func (*Config) NewLogger

func (conf *Config) NewLogger() *slog.Logger

NewLogger returns a logger with the given configuration.

func (*Config) Validate

func (conf *Config) Validate() error

Validate validates the configuration.

type ConnManager added in v0.15.8

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

ConnManager manages the connections for the daemon.

func NewConnManager added in v0.15.8

func NewConnManager(conf Config) (*ConnManager, error)

NewConnManager creates a new connection manager.

func (*ConnManager) Close added in v0.15.8

func (m *ConnManager) Close() error

Close closes the connection manager and all connections. It is not safe to use the connection manager after calling Close.

func (*ConnManager) ConnIDs added in v0.15.8

func (m *ConnManager) ConnIDs(ctx context.Context) []string

ConnIDs returns the IDs of all currently active connections.

func (*ConnManager) DataDir added in v0.15.13

func (m *ConnManager) DataDir(connID string) string

DataDir returns the data directory for the given connection ID.

func (*ConnManager) Disconnect added in v0.15.8

func (m *ConnManager) Disconnect(ctx context.Context, connID string) error

Disconnect disconnects the connection for the given ID.

func (*ConnManager) DropStorage added in v0.16.0

func (m *ConnManager) DropStorage(ctx context.Context, connID string) error

DropStorage drops storage for the connection with the given ID.

func (*ConnManager) Get added in v0.15.8

func (m *ConnManager) Get(ctx context.Context, connID string) (embed.Node, bool)

Get gets the connection for the given ID.

func (*ConnManager) GetMeshNode added in v0.17.0

func (m *ConnManager) GetMeshNode(ctx context.Context, connID string) (types.MeshNode, error)

GetMeshNode returns the full mesh node for the given ID.

func (*ConnManager) GetStatus added in v0.17.0

func (m *ConnManager) GetStatus(ctx context.Context, connID string) v1.DaemonConnStatus

GetStatus returns the status of the connection for the given ID.

func (*ConnManager) NewConn added in v0.15.8

func (m *ConnManager) NewConn(ctx context.Context, req *v1.ConnectRequest) (id string, node embed.Node, err error)

NewConn creates a new connection for the given request. Start must be called on the returned node to start the connection.

func (*ConnManager) NodeID added in v0.16.0

func (m *ConnManager) NodeID() string

NodeID returns the node ID used for connections.

func (*ConnManager) Profiles added in v0.17.0

func (m *ConnManager) Profiles() ProfileStore

Profiles returns the profiles store.

func (*ConnManager) PublicKey added in v0.16.0

func (m *ConnManager) PublicKey() string

PublicKey returns the encoded public key used for connections.

func (*ConnManager) RemoveConn added in v0.16.0

func (m *ConnManager) RemoveConn(ctx context.Context, connID string)

RemoveConn removes the connection for the given ID.

type NamespacedConn added in v0.17.0

type NamespacedConn struct {
	ConnID    string
	Namespace string
}

NamespacedConn is a namespaced connection.

func NamespacedConnFromContext added in v0.17.0

func NamespacedConnFromContext(ctx context.Context, connID string) NamespacedConn

NamespacedConnFromContext returns a namespaced connection from the given context and connection ID.

type Persistence added in v0.15.4

type Persistence struct {
	// Path is the root path to store mesh connection data.
	// Each connection will receive its own subdirectory.
	Path string `koanf:"path"`
}

Persistence is configuration for persistence of mesh connection storage.

func (*Persistence) BindFlags added in v0.15.4

func (conf *Persistence) BindFlags(prefix string, flagset *pflag.FlagSet)

BindFlags binds the persistence flags to the given flagset.

type Profile added in v0.17.0

type Profile struct {
	*v1.PutConnectionRequest
}

Profile contains the details of a connection profile. It is semantically equivalent to a PutConnectionRequest at the moment and is therefore functionally an alias.

func (Profile) MarshalJSON added in v0.17.0

func (p Profile) MarshalJSON() ([]byte, error)

MarshalJSON marshals the profile to JSON.

func (Profile) MarshalProto added in v0.17.0

func (p Profile) MarshalProto() ([]byte, error)

MarshalProto marshals the profile to proto.

func (*Profile) UnmarshalJSON added in v0.17.0

func (p *Profile) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the profile from JSON.

func (*Profile) UnmarshalProto added in v0.17.0

func (p *Profile) UnmarshalProto(data []byte) error

UnmarshalProto unmarshals the profile from proto.

type ProfileID added in v0.17.0

type ProfileID string

ProfileID is a profile ID.

func ProfileIDFromKey added in v0.17.0

func ProfileIDFromKey(key []byte) ProfileID

ProfileIDFromKey returns the profile ID from the storage key.

func (ProfileID) Bytes added in v0.17.0

func (id ProfileID) Bytes() []byte

Bytes returns the byte representation of the profile ID.

func (ProfileID) IsEmpty added in v0.17.0

func (id ProfileID) IsEmpty() bool

IsEmpty returns whether the profile ID is empty.

func (ProfileID) StorageKey added in v0.17.0

func (id ProfileID) StorageKey(ctx context.Context) []byte

StorageKey returns the storage key for the profile ID.

func (ProfileID) String added in v0.17.0

func (id ProfileID) String() string

String returns the string representation of the profile ID.

type ProfileIDs added in v0.17.0

type ProfileIDs []ProfileID

ProfileIDs is a list of profile IDs.

func (ProfileIDs) Strings added in v0.17.0

func (ids ProfileIDs) Strings() []string

Strings returns the string representations of the profile IDs.

type ProfileStore added in v0.17.0

type ProfileStore interface {
	// Put stores or updates a profile.
	Put(ctx context.Context, id ProfileID, profile Profile) error
	// Get retrieves a profile.
	Get(ctx context.Context, id ProfileID) (Profile, error)
	// List lists all profiles.
	List(ctx context.Context) (Profiles, error)
	// ListProfileIDs lists all profile IDs.
	ListProfileIDs(ctx context.Context) (ProfileIDs, error)
	// Delete deletes a profile.
	Delete(ctx context.Context, id ProfileID) error
	// Close closes the store.
	Close() error
}

ProfileStore manages the storage of connection profiles.

func NewProfileStore added in v0.17.0

func NewProfileStore(diskPath string) (ProfileStore, error)

NewProfileStore returns a new ProfileStore. If diskPath is an empty string, an in-memory store is returned.

type Profiles added in v0.17.0

type Profiles map[ProfileID]Profile

Profiles is a map of profile IDs to connection parameters.

func (Profiles) IDs added in v0.17.0

func (p Profiles) IDs() []ProfileID

IDs returns the IDs of the profiles.

type WebUI

type WebUI struct {
	// Enabled is true if the gRPC UI is enabled.
	Enabled bool `koanf:"enabled"`
	// ListenAddress is the address to listen on.
	ListenAddress string `koanf:"listen-address"`
}

WebUI are options for exposing a gRPC UI.

func (*WebUI) BindFlags

func (conf *WebUI) BindFlags(prefix string, flagset *pflag.FlagSet)

BindFlags binds the UI flags to the given flagset.

Jump to

Keyboard shortcuts

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