config

package
v0.5.61 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2024 License: Apache-2.0 Imports: 44 Imported by: 0

Documentation

Overview

Package config defines, loads and parses project wide configuration settings from various sources

Index

Constants

View Source
const (
	DefaultSocketPath = "/var/run/cunicu.sock"

	// Ephemeral Port Range (RFC6056 Sect. 2.1)
	EphemeralPortMin = (1 << 15) + (1 << 14)
	EphemeralPortMax = (1 << 16) - 1
)
View Source
const (
	DefaultRouteTable = 254 // main table
)

Variables

View Source
var (
	DefaultPrefixes = []string{"fc2f:9a4d::/32", "10.237.0.0/16"}

	DefaultBackends = []url.URL{
		{
			Scheme: "grpc",
			Host:   "signal.cunicu.li:443",
		},
	}

	DefaultICEURLs = []url.URL{
		{
			Scheme: "grpc",
			Host:   "relay.cunicu.li:443",
		},
	}

	DefaultSettings = Settings{
		Backends: DefaultBackends,
		RPC: RPCSettings{
			Socket: DefaultSocketPath,
			Wait:   false,
		},
		Log: LogSettings{
			Banner: true,
		},
		WatchInterval: 1 * time.Second,
		DefaultInterfaceSettings: InterfaceSettings{
			DiscoverPeers:     true,
			DiscoverEndpoints: true,
			SyncConfig:        true,
			SyncHosts:         true,
			SyncRoutes:        true,
			WatchRoutes:       true,

			PortForwarding: true,

			ICE: ICESettings{
				URLs:                DefaultICEURLs,
				CheckInterval:       200 * time.Millisecond,
				DisconnectedTimeout: 5 * time.Second,
				FailedTimeout:       25 * time.Second,
				RestartTimeout:      10 * time.Second,
				InterfaceFilter:     "*",
				KeepaliveInterval:   2 * time.Second,
				MaxBindingRequests:  7,
				PortRange: PortRangeSettings{
					Min: EphemeralPortMin,
					Max: EphemeralPortMax,
				},
				CandidateTypes: []ice.CandidateType{
					ice.CandidateTypeHost,
					ice.CandidateTypeServerReflexive,
					ice.CandidateTypePeerReflexive,
					ice.CandidateTypeRelay,
				},
				NetworkTypes: []ice.NetworkType{
					ice.NetworkTypeUDP4,
					ice.NetworkTypeUDP6,
					ice.NetworkTypeTCP4,
					ice.NetworkTypeTCP6,
				},
			},

			RoutingTable: DefaultRouteTable,

			ListenPortRange: &PortRangeSettings{
				Min: wg.DefaultPort,
				Max: EphemeralPortMax,
			},
		},
	}
)
View Source
var RuntimeConfigFile = "/var/lib/cunicu.runtime.yaml"

Functions

func DecoderConfig

func DecoderConfig(result any) *mapstructure.DecoderConfig

DecoderConfig returns the mapstructure DecoderConfig which is used by cunicu

func ExtractInterfaceOrder

func ExtractInterfaceOrder(buf []byte) ([]string, error)

func InitDefaults

func InitDefaults() error

func Map

func Map(v any, tagName string) map[string]any

Types

type BaseHookSetting

type BaseHookSetting struct {
	Type string `koanf:"type"`
}

type ChangedHandler

type ChangedHandler interface {
	OnConfigChanged(key string, oldValue, newValue any) error
}

type Config

type Config struct {
	*Settings
	*Meta
	*koanf.Koanf
	Runtime *runtimeSource
	Sources []Source

	// Settings which are not configurable via configuration file
	Files   []string
	Domains []string
	Watch   bool

	Providers         []koanf.Provider
	InterfaceOrder    []string
	InterfaceOrderCLI []string
	// contains filtered or unexported fields
}

func New

func New(flags *pflag.FlagSet) *Config

New creates a new configuration instance.

func (*Config) AddProvider

func (c *Config) AddProvider(provider koanf.Provider) error

func (*Config) AddSource

func (c *Config) AddSource(source Source) error

func (*Config) Init

func (c *Config) Init(args []string) (err error)

func (*Config) InterfaceFilter

func (c *Config) InterfaceFilter(name string) bool

InterfaceFilter checks if the provided interface name is matched by any configuration.

func (*Config) InterfaceOrderByName

func (c *Config) InterfaceOrderByName(name string) []string

InterfaceOrderByName returns a list of interface config sections which are used by a given interface

func (*Config) InterfaceSettings

func (c *Config) InterfaceSettings(name string) (cfg *InterfaceSettings)

InterfaceSettings returns interface specific settings These settings are constructed by merging the settings of each interface section which matches the name. This behavior is quite similar to the OpenSSH client configuration file.

func (*Config) InvokeChangedHandlers

func (c *Config) InvokeChangedHandlers(key string, change types.Change) error

func (*Config) Marshal

func (c *Config) Marshal(wr io.Writer) error

Marshal writes the configuration in YAML format to the provided writer.

func (*Config) ReloadAllSources

func (c *Config) ReloadAllSources() (map[string]types.Change, error)

ReloadAllSources reloads all configuration sources

func (*Config) Update

func (c *Config) Update(sets map[string]any) (map[string]types.Change, error)

Update sets multiple settings in the provided map.

type ExecHookSetting

type ExecHookSetting struct {
	BaseHookSetting `koanf:",squash"`
	Command         string            `koanf:"command"`
	Args            []string          `koanf:"args"`
	Env             map[string]string `koanf:"env"`
	Stdin           bool              `koanf:"stdin"`
}

type HookSetting

type HookSetting any

type ICESettings

type ICESettings struct {
	URLs           []url.URL           `koanf:"urls,omitempty"`
	CandidateTypes []ice.CandidateType `koanf:"candidate_types,omitempty"`
	NetworkTypes   []ice.NetworkType   `koanf:"network_types,omitempty"`
	NAT1to1IPs     []string            `koanf:"nat_1to1_ips,omitempty"`

	RelayTCP *bool `koanf:"relay_tcp,omitempty"`
	RelayTLS *bool `koanf:"relay_tls,omitempty"`

	PortRange PortRangeSettings `koanf:"port_range,omitempty"`

	Lite               bool `koanf:"lite,omitempty"`
	MDNS               bool `koanf:"mdns,omitempty"`
	MaxBindingRequests int  `koanf:"max_binding_requests,omitempty"`
	InsecureSkipVerify bool `koanf:"insecure_skip_verify,omitempty"`

	InterfaceFilter string `koanf:"interface_filter,omitempty"`

	DisconnectedTimeout time.Duration `koanf:"disconnected_timeout,omitempty"`
	FailedTimeout       time.Duration `koanf:"failed_timeout,omitempty"`

	// KeepaliveInterval used to keep candidates alive
	KeepaliveInterval time.Duration `koanf:"keepalive_interval,omitempty"`

	// CheckInterval is the interval at which the agent performs candidate checks in the connecting phase
	CheckInterval  time.Duration `koanf:"check_interval,omitempty"`
	RestartTimeout time.Duration `koanf:"restart_timeout,omitempty"`

	Username string `koanf:"username,omitempty"`
	Password string `koanf:"password,omitempty"`
}

func (*ICESettings) HasCandidateType

func (s *ICESettings) HasCandidateType(ct ice.CandidateType) bool

func (*ICESettings) HasNetworkType

func (s *ICESettings) HasNetworkType(nt ice.NetworkType) bool

type InterfaceSettings

type InterfaceSettings struct {
	HostName string `koanf:"hostname,omitempty"`
	Domain   string `koanf:"domain,omitempty"`

	ExtraHosts map[string][]net.IPAddr `koanf:"extra_hosts,omitempty"`

	MTU       int          `koanf:"mtu,omitempty"`
	DNS       []net.IPAddr `koanf:"dns,omitempty"`
	Addresses []net.IPNet  `koanf:"addresses,omitempty"`
	Prefixes  []net.IPNet  `koanf:"prefixes"`
	Networks  []net.IPNet  `koanf:"networks,omitempty"`

	// Peer discovery
	Community crypto.KeyPassphrase `koanf:"community,omitempty"`
	Whitelist []crypto.Key         `koanf:"whitelist,omitempty"`
	Blacklist []crypto.Key         `koanf:"blacklist,omitempty"`

	// Endpoint discovery
	ICE            ICESettings `koanf:"ice,omitempty"`
	PortForwarding bool        `koanf:"port_forwarding,omitempty"`

	// Route sync
	RoutingTable int `koanf:"routing_table,omitempty"`

	// Hooks
	Hooks []HookSetting `koanf:"hooks,omitempty"`

	// WireGuard
	UserSpace       bool                    `koanf:"userspace,omitempty"`
	PrivateKey      crypto.Key              `koanf:"private_key,omitempty"`
	ListenPort      *int                    `koanf:"listen_port,omitempty"`
	ListenPortRange *PortRangeSettings      `koanf:"listen_port_range,omitempty"`
	FirewallMark    int                     `koanf:"fwmark,omitempty"`
	Peers           map[string]PeerSettings `koanf:"peers,omitempty"`

	// Feature flags
	DiscoverEndpoints bool `koanf:"discover_endpoints,omitempty"`
	DiscoverPeers     bool `koanf:"discover_peers,omitempty"`
	SyncConfig        bool `koanf:"sync_config,omitempty"`
	SyncRoutes        bool `koanf:"sync_routes,omitempty"`
	SyncHosts         bool `koanf:"sync_hosts,omitempty"`

	WatchConfig bool `koanf:"watch_config,omitempty"`
	WatchRoutes bool `koanf:"watch_routes,omitempty"`
}

func NewInterfaceSettingsFromConfig

func NewInterfaceSettingsFromConfig(c *wg.Config) (*InterfaceSettings, error)

func (*InterfaceSettings) AgentConfig

func (c *InterfaceSettings) AgentConfig(ctx context.Context, peer *crypto.Key) (*ice.AgentConfig, error)

func (*InterfaceSettings) AgentURLs

func (c *InterfaceSettings) AgentURLs(ctx context.Context, pk *crypto.Key) ([]*stun.URI, error)

func (*InterfaceSettings) Check

func (c *InterfaceSettings) Check() error

type LocalFileProvider

type LocalFileProvider struct {
	*file.File
	// contains filtered or unexported fields
}

func NewLocalFileProvider

func NewLocalFileProvider(path string) *LocalFileProvider

func (*LocalFileProvider) Order

func (p *LocalFileProvider) Order() []string

func (*LocalFileProvider) ReadBytes

func (p *LocalFileProvider) ReadBytes() ([]byte, error)

type LogSettings

type LogSettings struct {
	Level  string   `koanf:"level,omitempty"`
	Rules  []string `koanf:"rules,omitempty"`
	File   string   `koanf:"file,omitempty"`
	Color  string   `koanf:"color,omitempty"`
	Banner bool     `koanf:"banner,omitempty"`
}

type LookupProvider

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

func NewLookupProvider

func NewLookupProvider(domain string) *LookupProvider

func (*LookupProvider) Read

func (p *LookupProvider) Read() (map[string]any, error)

func (*LookupProvider) ReadBytes

func (p *LookupProvider) ReadBytes() ([]byte, error)

func (*LookupProvider) SubProviders

func (p *LookupProvider) SubProviders() []koanf.Provider

func (*LookupProvider) Version

func (p *LookupProvider) Version() any

func (*LookupProvider) Watch

func (p *LookupProvider) Watch(cb func(event any, err error)) error

type Meta

type Meta struct {
	Fields map[string]*Meta
	Parent *Meta
	Type   reflect.Type
	// contains filtered or unexported fields
}

func Metadata

func Metadata() *Meta

func (*Meta) AddChangedHandler

func (m *Meta) AddChangedHandler(key string, h ChangedHandler)

func (*Meta) CompletionOptions

func (m *Meta) CompletionOptions() []string

func (*Meta) InvokeChangedHandlers

func (m *Meta) InvokeChangedHandlers(key string, change types.Change) error

func (*Meta) Keys

func (m *Meta) Keys() []string

func (*Meta) Lookup

func (m *Meta) Lookup(key string) *Meta

func (*Meta) Parse

func (m *Meta) Parse(str string) (any, error)

type Orderable

type Orderable interface {
	Order() []string
}

type OutputFormat

type OutputFormat string
const (
	OutputFormatJSON   OutputFormat = "json"
	OutputFormatLogger OutputFormat = "logger"
	OutputFormatHuman  OutputFormat = "human"
)

func (OutputFormat) MarshalText

func (f OutputFormat) MarshalText() ([]byte, error)

func (*OutputFormat) Set

func (f *OutputFormat) Set(str string) error

func (OutputFormat) String

func (f OutputFormat) String() string

func (*OutputFormat) Type

func (f *OutputFormat) Type() string

func (*OutputFormat) UnmarshalText

func (f *OutputFormat) UnmarshalText(text []byte) error

type PeerSettings

type PeerSettings struct {
	PublicKey                   crypto.Key           `koanf:"public_key,omitempty"`
	PresharedKey                crypto.Key           `koanf:"preshared_key,omitempty"`
	PresharedKeyPassphrase      crypto.KeyPassphrase `koanf:"preshared_key_passphrase,omitempty"`
	Endpoint                    string               `koanf:"endpoint,omitempty"`
	PersistentKeepaliveInterval time.Duration        `koanf:"persistent_keepalive,omitempty"`
	AllowedIPs                  []net.IPNet          `koanf:"allowed_ips,omitempty"`
}

type PortRangeSettings

type PortRangeSettings struct {
	Min int `koanf:"min,omitempty"`
	Max int `koanf:"max,omitempty"`
}

type RPCSettings

type RPCSettings struct {
	Socket string `koanf:"socket,omitempty"`
	Wait   bool   `koanf:"wait,omitempty"`
}

type RemoteFileProvider

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

func NewRemoteFileProvider

func NewRemoteFileProvider(u *url.URL) *RemoteFileProvider

func (*RemoteFileProvider) Order

func (p *RemoteFileProvider) Order() []string

func (*RemoteFileProvider) Read

func (p *RemoteFileProvider) Read() (map[string]interface{}, error)

func (*RemoteFileProvider) ReadBytes

func (p *RemoteFileProvider) ReadBytes() ([]byte, error)

func (*RemoteFileProvider) Version

func (p *RemoteFileProvider) Version() any

type Settings

type Settings struct {
	Experimental bool `koanf:"experimental,omitempty"`

	WatchInterval time.Duration `koanf:"watch_interval,omitempty"`
	Backends      []url.URL     `koanf:"backends,omitempty"`

	Log LogSettings `koanf:"log,omitempty"`
	RPC RPCSettings `koanf:"rpc,omitempty"`

	DefaultInterfaceSettings InterfaceSettings            `koanf:",squash"`
	Interfaces               map[string]InterfaceSettings `koanf:"interfaces"`
}

func (*Settings) Check

func (s *Settings) Check() error

Check performs plausibility checks on the provided configuration.

type Source

type Source interface {
	Load() error
	Config() *koanf.Koanf
	Order() []string
}

type StructsProvider

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

func NewStructsProvider

func NewStructsProvider(v any, t string) *StructsProvider

StructsProvider is very similar koanf's struct provider but slightly adjusted to our needs.

func (*StructsProvider) Read

func (p *StructsProvider) Read() (map[string]any, error)

func (*StructsProvider) ReadBytes

func (p *StructsProvider) ReadBytes() ([]byte, error)

type SubProvidable

type SubProvidable interface {
	SubProviders() []koanf.Provider
}

type Versioned

type Versioned interface {
	Version() any
}

type Watchable

type Watchable interface {
	Watch(cb func(event any, err error)) error
}

type WebHookSetting

type WebHookSetting struct {
	BaseHookSetting `koanf:",squash"`
	URL             url.URL           `koanf:"url"`
	Method          string            `koanf:"method"`
	Headers         map[string]string `koanf:"headers"`
}

type WireGuardProvider

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

func NewWireGuardProvider

func NewWireGuardProvider() *WireGuardProvider

func (*WireGuardProvider) Order

func (p *WireGuardProvider) Order() []string

func (*WireGuardProvider) Read

func (p *WireGuardProvider) Read() (map[string]interface{}, error)

func (*WireGuardProvider) ReadBytes

func (p *WireGuardProvider) ReadBytes() ([]byte, error)

Jump to

Keyboard shortcuts

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