vpnlib

package
v0.0.0-...-9758eb9 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package vpnlib provides utilities for managing VPN connections over TLS sockets, Including IP address allocation, route management, and client to server communication over the VPN tunnel.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddIpToIface

func AddIpToIface(iface, localIp, remoteIp string, subnetSize uint8) error

AddIpToIface adds an ip address to a network interface.

func AddRoutesToIface

func AddRoutesToIface(iface string, routes []string) error

AddRoutesToIface adds routes to a network interface.

func AddRoutesViaGateway

func AddRoutesViaGateway(gateway string, routes []string) error

AddRoutesViaGateway adds routes through a specified gateway IP.

func AddServerIp

func AddServerIp(iface, localIp string, subnetSize uint8) error

func AddServerIpDarwin

func AddServerIpDarwin(iface, localIp string, subnetSize uint8) error

func AddServerIpLinux

func AddServerIpLinux(iface, localIp string, subnetSize uint8) error

func CheckIPForwardingEnabled

func CheckIPForwardingEnabled() (bool, error)

func ConnToTunCopy

func ConnToTunCopy(ctx context.Context, logger *zap.Logger, conn net.Conn, peerAddress string, tun io.Writer) error

func CreateTun

func CreateTun() (ifce *water.Interface, err error)

CreateTun creates a TUN interface for Linux

func DeleteRoutesViaGateway

func DeleteRoutesViaGateway(gateway string, routes []string) error

DeleteRoutesViaGateway removes routes that go through a specified gateway IP.

func GetDefaultGateway

func GetDefaultGateway(addressFamily int) (net.IP, string, error)

func GetDnsByPassRoutes

func GetDnsByPassRoutes(vpnIfaceName string, routes []string, addressFamily int) (map[string]bool, error)

func GetDnsServers

func GetDnsServers() ([]string, error)

GetDnsServers returns a list of all active resolvers used by the system

func GetLocalInterfacesForIp

func GetLocalInterfacesForIp(ipAddress string) ([]string, error)

Returns a list of interfaces on which this IP network was found

func IsIPInCIDR

func IsIPInCIDR(ipStr, cidrStr string) (bool, error)

IsIPInCIDR checks if an IP address is in a CIDR range.

func RunServer

func RunServer(
	ctx context.Context,
	logger *zap.Logger,
	vpnClientListener net.Listener,
	dhcpPoolSubnet string,
	advertisedRoutes []string,
	opts ...ServerOption,
) error

RunServer runs the VPN "server"

func TunToConnCopy

func TunToConnCopy(
	ctx context.Context,
	logger *zap.Logger,
	source io.Reader,
	conn net.Conn,
) error

TunToConnCopy reads packets and fowards them to the given connection. This function is used by the VPN "clients" and must *not* be used by the server. This function is *not* resilient to errors and will return upon encountering a read/write error or if the context is cancelled.

Types

type ConnectionMap

type ConnectionMap struct {
	sync.RWMutex

	Connections map[string]net.Conn
}

ConnectionMap is a concurrent-safe map for managing connections. The key is the client IP address, and the value is the net.Conn for that connection

func NewConnectionMap

func NewConnectionMap() *ConnectionMap

NewConnectionMap creates a new ConnectionMap with an empty map. This map will be used to keep track of connections to clients.

func (*ConnectionMap) Delete

func (cm *ConnectionMap) Delete(ip string)

Delete removes a connection from the connection map

func (*ConnectionMap) Get

func (cm *ConnectionMap) Get(ip string) (net.Conn, bool)

Get retrieves a connection (Net.Conn) by IP from the map.

func (*ConnectionMap) Set

func (cm *ConnectionMap) Set(ip string, conn net.Conn)

Set adds a connection to the map.

type ControlMessage

type ControlMessage struct {
	ClientIp   string   `json:"client_ip"`
	ServerIp   string   `json:"server_ip"`
	SubnetSize uint8    `json:"subnet_size"`
	Routes     []string `json:"routes,omitempty"` // CIDRs
}

ControlMessage represents a message used to tell clients the tunnel IPs and what routes to install on the interface.

func GetControlMessage

func GetControlMessage(conn net.Conn) (*ControlMessage, error)

GetControlMessage reads a control message from a net conn.

func (*ControlMessage) Build

func (m *ControlMessage) Build() ([]byte, error)

Build encodes a control message to ready-to-send bytes.

type IPPool

type IPPool struct {
	sync.Mutex
	// contains filtered or unexported fields
}

This is a pool of IPs that can be allocated to clients.

func NewIPPool

func NewIPPool(cidr string) (*IPPool, error)

NewIPPool creates a new IP pool based on the provided CIDR. This pool will be used to allocate IPs to clients. (think DHCP)

func (*IPPool) Allocate

func (p *IPPool) Allocate() (string, error)

Allocate allocates an available IP address from the pool. This will find a free IP address and mark it as used.

func (*IPPool) GetServerIp

func (p *IPPool) GetServerIp() string

GetServerIp returns the server ip

func (*IPPool) GetSubnetSize

func (p *IPPool) GetSubnetSize() uint8

GetSubnetSize returns the subnet size

func (*IPPool) Release

func (p *IPPool) Release(ip string)

Release releases an IP address back to the pool. This will mark the IP address as available.

type ServerOption

type ServerOption func(*serverConfig)

ServerOption represents a configuration option for the vpn "server" side.

func WithServerVerboseLogs

func WithServerVerboseLogs(verbose bool) ServerOption

WithServerVerboseLogs returns the ServerOption that toggles verbose logging.

Jump to

Keyboard shortcuts

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