wgtunnel

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddClientHandler

func AddClientHandler(im *InterfaceManager, smdClient smdclient.SMDClientInterface) http.HandlerFunc

AddClientHandler godoc

@Summary		Add a WireGuard client
@Description	Initiate a WireGuard tunnel from a client using its public key
@Description	and peer name (IP address).
@Description
@Description	The source IP of the request is read and is used as the peer
@Description	name along with the public key to authenticate unless the
@Description	`X-Forward-For` header is set. In that case, the value of the
@Description	header is used as the peer name. If the peer exists in the
@Description	internal tunnel manager, the IP presented is the one used.
@Description	Otherwise, the next available IP in range is assigned.
@Accept			json
@Produce		json
@Success		200				{object}	WGResponse
@Failure		400				{object}	nil
@Failure		500				{object}	nil
@Param			pubkey			body		PublicKeyRequest	true	"WireGuard public key of client"
@Param			X-Forwarded-For	header		string				false	"Override source IP"
@Router			/cloud-init/wg-init [post]

func AddWireGuardPeer

func AddWireGuardPeer(interfaceID, publicKey, vpnIP, clientIP string) error

AddWireGuardPeer adds a peer to the WireGuard configuration.

func GetUsableIP

func GetUsableIP(network *net.IPNet) (net.IP, error)

GetUsableIP checks if the given IP in a net.IPNet is usable. If not, it returns the first usable IP in the subnet.

Types

type IPAllocator

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

IPAllocator manages IP address allocation within a network range.

func NewIPAllocator

func NewIPAllocator(cidr string) (*IPAllocator, error)

NewIPAllocator initializes a new IPAllocator for a given network.

func (*IPAllocator) IsAllocated

func (a *IPAllocator) IsAllocated(ipAddr net.IPAddr) bool

IsAllocated checks if an IP address is currently allocated.

func (*IPAllocator) NextAvailable

func (a *IPAllocator) NextAvailable() (net.IPAddr, error)

NextAvailable returns the next available IP address in the range.

func (*IPAllocator) Release

func (a *IPAllocator) Release(ipAddr net.IPAddr) error

Release releases an IP address back to the pool.

func (*IPAllocator) Reserve

func (a *IPAllocator) Reserve(ipAddr net.IPAddr) error

Reserve reserves a specific IP address.

type InterfaceManager

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

func NewInterfaceManager

func NewInterfaceManager(name string, localIp net.IP, network *net.IPNet) *InterfaceManager

func (*InterfaceManager) AddPeer added in v1.0.4

func (m *InterfaceManager) AddPeer(peerName, publicKey, vpnIP, clientIP string) error

func (*InterfaceManager) GetInterfaceName

func (m *InterfaceManager) GetInterfaceName() string

func (*InterfaceManager) GetPeers

func (m *InterfaceManager) GetPeers() map[string]PeerConfig

func (*InterfaceManager) GetServerConfig

func (m *InterfaceManager) GetServerConfig() (ServerConfig, error)

func (*InterfaceManager) IpForPeer

func (m *InterfaceManager) IpForPeer(peerName string, publicKey string) string

IpForPeer allocates an IP address for a given peer based on its name and public key. If the peer already exists, it returns the existing IP address. Otherwise, it allocates a new IP address for the peer and stores the peer configuration.

func (*InterfaceManager) PublicKey

func (m *InterfaceManager) PublicKey() (string, error)

func (*InterfaceManager) RemovePeer

func (m *InterfaceManager) RemovePeer(peerName string) error

func (*InterfaceManager) StartServer

func (m *InterfaceManager) StartServer() error

func (*InterfaceManager) StopServer added in v1.0.4

func (m *InterfaceManager) StopServer() error

type PeerConfig

type PeerConfig struct {
	PublicKey string     `json:"public_key" yaml:"public_key"`
	IP        net.IPAddr `json:"ip" yaml:"ip"`
}

type PublicKeyRequest

type PublicKeyRequest struct {
	PublicKey string `` /* 133-byte string literal not displayed */
}

PublicKeyRequest represents the JSON payload for a WireGuard public key.

type ServerConfig

type ServerConfig struct {
	PublicKey string `json:"public_key" yaml:"public_key"`
	IP        string `json:"ip" yaml:"ip"`
	Port      string `json:"port" yaml:"port"`
}

type Store

type Store interface {
	IpForPeer(peerName, publicKey string) string
	GetInterfaceName() string
	GetServerConfig() (ServerConfig, error)
}

type WGResponse added in v1.1.0

type WGResponse struct {
	Message      string `json:"message" yaml:"message" example:"WireGuard tunnel created successfully"`
	ClientVPNIP  string `json:"client-vpn-ip" yaml:"client-vpn-ip" example:"10.89.0.7" description:"Assigned WireGuard VPN IP address"`
	ServerPubKey string `json:"server-public-key" yaml:"server-public-key" example:"dHMOGL8vTGhTgqXyYdu6cLGXEPmTcWm+vS18GcQseyg="`
	ServerIP     string `json:"server-ip" yaml:"server-ip" example:"10.87.0.1" description:"WireGuard server IP"`
	ServerPort   string `json:"server-port" yaml:"server-port" example:"51820" description:"WireGuard server port"`
}

WGResponse represents the JSON payload for a response from the WireGuard server.

Jump to

Keyboard shortcuts

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