wgipam

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2019 License: Apache-2.0 Imports: 21 Imported by: 0

README

wgipam builds.sr.ht status GoDoc Go Report Card

Command wgipamd implements an IP Address Management (IPAM) daemon for dynamic IP address assignment to WireGuard peers, using the wg-dynamic protocol.

For more information about wg-dynamic, please see: https://git.zx2c4.com/wg-dynamic/about/.

This project is not affiliated with the WireGuard or wg-dynamic projects.

Apache 2.0 Licensed.

Overview

This is an experimental project to build an extensible and robust IP address allocation and assignment system for WireGuard peers, that operates using the wg-dynamic protocol. No guarantees are made about the project's stability or reliability.

Documentation

Overview

Package wgipam implements an IP Address Management (IPAM) system for dynamic IP address assignment to WireGuard peers, using the wg-dynamic protocol.

For more information about wg-dynamic, please see: https://git.zx2c4.com/wg-dynamic/about/.

This project is not affiliated with the WireGuard or wg-dynamic projects.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Family

type Family int

A Family specifies one or more IP address families, such as IPv4, IPv6, or DualStack.

const (
	IPv4 Family
	IPv6
	DualStack Family = IPv4 | IPv6
)

List of possible Family values.

func (Family) String

func (i Family) String() string

type HTTPHandler

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

A HTTPHandler provides the HTTP debug API handler for wgipamd.

func NewHTTPHandler

func NewHTTPHandler(
	usePrometheus, usePProf bool,
	reg *prometheus.Registry,
	stores map[string]Store,
) *HTTPHandler

NewHTTPHandler creates a HTTPHandler with the specified configuration.

func (*HTTPHandler) ServeHTTP

func (h *HTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Handler

type Handler struct {
	// Leases specifies a Store for Lease storage. Leases must be non-nil or
	// the Handler will panic.
	Leases LeaseStore

	// IPs specifies an IPAllocator for allocating and freeing client addresses.
	// IPs must be non-nil or the Handler will panic.
	IPs IPAllocator

	// LeaseDuration specifies the amount of time leases may exist before they
	// expire and are purged.
	LeaseDuration time.Duration

	// Log specifies a logger for the Server. If nil, all logs are discarded.
	Log *log.Logger

	// Metrics specifies Prometheus metrics for the Handler. If nil, metrics
	// are not collected.
	Metrics *HandlerMetrics

	// NewRequest specifies an optional hook which will be invoked when a new
	// request is received. The source address src of the remote client is
	// passed to NewRequest. If NewRequest is nil, it is a no-op.
	NewRequest func(src net.Addr)
}

A Handler handles IP allocation requests using the wg-dynamic protocol.

func (*Handler) RequestIP

func (h *Handler) RequestIP(src net.Addr, req *wgdynamic.RequestIP) (*wgdynamic.RequestIP, error)

RequestIP implements the wg-dynamic request_ip command.

type HandlerMetrics

type HandlerMetrics struct {
	RequestsTotal *prometheus.CounterVec
	ErrorsTotal   *prometheus.CounterVec
}

HandlerMetrics contains metrics related to Handler operations.

func NewHandlerMetrics

func NewHandlerMetrics(reg *prometheus.Registry, ifi string) *HandlerMetrics

NewHandlerMetrics produces a HandlerMetrics structure which registers its metrics with reg and adds an interface label ifi.

type IPAllocator

type IPAllocator interface {
	// Allocate allocates an available IP address from each underlying subnet
	// which matches the input Family. It returns false if one or more of the
	// subnets ran out of IP addresses during allocation.
	Allocate(family Family) (ips []*net.IPNet, ok bool, err error)

	// Free returns an allocated IP address to the IPAllocator. Free operations
	// should be idempotent; that is, an error should only be returned if the
	// free operation fails. Attempting to free an IP that did not already exist
	// should not return an error.
	Free(ip *net.IPNet) error
}

An IPAllocator can allocate IP addresses from one or more subnets.

func DualStackIPAllocator

func DualStackIPAllocator(store IPStore, subnets []Subnet) (IPAllocator, error)

DualStackIPAllocator returns an IPAllocator which can allocate both IPv4 and IPv6 addresses. It is a convenience wrapper around other IPAllocators that automatically allocates addresses from the input subnets as appropriate.

func SimpleIPAllocator

func SimpleIPAllocator(store IPStore, subnet Subnet) (IPAllocator, error)

SimpleIPAllocator returns an IPAllocator which allocates IP addresses in order by iterating through addresses in a subnet.

type IPStore

type IPStore interface {
	// Subnets returns all existing subnets. Note that the order of the subnets
	// is unspecified. The caller must sort the leases for deterministic output.
	Subnets() (subnets []*net.IPNet, err error)

	// SaveSubnet creates or updates a Subnet by its CIDR value.
	SaveSubnet(subnet *net.IPNet) error

	// AllocatedIPs returns all of the IP addresses allocated within a specific
	// subnet. Note that the order of the addresses is unspecified. The caller
	// must sort the addresses for deterministic output.
	AllocatedIPs(subnet *net.IPNet) (ips []*net.IPNet, err error)

	// AllocateIP allocates an IP address from the specified subnet. It returns
	// false if the address is already allocated from the subnet, and returns an
	// error if the subnet does not exist.
	AllocateIP(subnet, ip *net.IPNet) (ok bool, err error)

	// FreeIP frees an allocated IP address in the specified subnet. FreeIP
	// operations should be idempotent; the same rules apply as with DeleteLease.
	// It returns an error if the subnet does not exist.
	FreeIP(subnet, ip *net.IPNet) error
}

An IPStore manages IP address allocation and storage.

type Lease

type Lease struct {
	IPs    []*net.IPNet
	Start  time.Time
	Length time.Duration
}

A Lease is a record of allocated IP addresses assigned to a client.

func (*Lease) Expired

func (l *Lease) Expired(t time.Time) bool

Expired determines if the Lease is expired as of time t.

func (*Lease) String

func (l *Lease) String() string

String returns a string suitable for logging.

type LeaseStore

type LeaseStore interface {
	// Leases returns all existing Leases. Note that the order of the Leases
	// is unspecified. The caller must sort the leases for deterministic output.
	Leases() (leases []*Lease, err error)

	// Lease returns the Lease identified by key. It returns false if no
	// Lease exists for key.
	Lease(key uint64) (lease *Lease, ok bool, err error)

	// SaveLease creates or updates a Lease by key.
	SaveLease(key uint64, lease *Lease) error

	// DeleteLease deletes a Lease by key. Delete operations should be idempotent;
	// that is, an error should only be returned if the delete operation fails.
	// Attempting to delete an item that did not already exist should not
	// return an error.
	DeleteLease(key uint64) error
}

A LeaseStore manages Lease storage.

type PurgeStats

type PurgeStats struct {
	// FreedIPs contains a map of subnet CIDRs to the number of IP addresses
	// that were freed within that subnet.
	FreedIPs map[string]int
}

PurgeStats contains statistics returned from a Store's Purge operation.

type Store

type Store interface {
	// Close syncs and closes the Store's internal state.
	io.Closer

	// Purge purge Leases which expire on or before the specified point in time,
	// and frees the IP addresses associated with the leases.
	// Purge operations that specify the same point in time should be
	// idempotent; the same rules apply as with DeleteLease.
	Purge(t time.Time) (*PurgeStats, error)

	LeaseStore
	IPStore
}

A Store manages Leases and IP allocations. To ensure compliance with the expected behaviors of the Store interface, use the wgipamtest.TestStore function.

func FileStore

func FileStore(file string) (Store, error)

FileStore returns a Store which stores Leases in a file on disk.

func IPStoreMetrics

func IPStoreMetrics(reg *prometheus.Registry, ifi string, subnets []Subnet, ips Store) (Store, error)

IPStoreMetrics produces a Store which gathers Prometheus metrics for IPStore operations.

func MemoryStore

func MemoryStore() Store

MemoryStore returns a Store which stores data in memory.

type Subnet

type Subnet struct {
	Subnet     net.IPNet
	Start, End net.IP
	Reserved   []net.IP
}

A Subnet is an IP address subnet which can be used for IP address allocations. It also contains parameters which can be used to prevent certain addresses from being allocated.

func (*Subnet) Validate

func (s *Subnet) Validate() error

Validate verifies the correctness of a Subnet's configuration.

Directories

Path Synopsis
cmd
wgipamd
Command wgipamd implements an IP Address Management (IPAM) daemon for dynamic IP address assignment to WireGuard peers, using the wg-dynamic protocol.
Command wgipamd implements an IP Address Management (IPAM) daemon for dynamic IP address assignment to WireGuard peers, using the wg-dynamic protocol.
internal
config
Package config provides configuration file support for the wgipamd server.
Package config provides configuration file support for the wgipamd server.
wgipamd
Package wgipamd implements package main logic for the wgipamd server.
Package wgipamd implements package main logic for the wgipamd server.
Package wgipamtest provides testing facilities for wgipamd types.
Package wgipamtest provides testing facilities for wgipamd types.

Jump to

Keyboard shortcuts

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