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 ¶
- func AddIpToIface(iface, localIp, remoteIp string, subnetSize uint8) error
- func AddRoutesToIface(iface string, routes []string) error
- func AddRoutesViaGateway(gateway string, routes []string) error
- func AddServerIp(iface, localIp string, subnetSize uint8) error
- func AddServerIpDarwin(iface, localIp string, subnetSize uint8) error
- func AddServerIpLinux(iface, localIp string, subnetSize uint8) error
- func CheckIPForwardingEnabled() (bool, error)
- func ConnToTunCopy(ctx context.Context, logger *zap.Logger, conn net.Conn, peerAddress string, ...) error
- func CreateTun() (ifce *water.Interface, err error)
- func DeleteRoutesViaGateway(gateway string, routes []string) error
- func GetDefaultGateway(addressFamily int) (net.IP, string, error)
- func GetDnsByPassRoutes(vpnIfaceName string, routes []string, addressFamily int) (map[string]bool, error)
- func GetDnsServers() ([]string, error)
- func GetLocalInterfacesForIp(ipAddress string) ([]string, error)
- func IsIPInCIDR(ipStr, cidrStr string) (bool, error)
- func RunServer(ctx context.Context, logger *zap.Logger, vpnClientListener net.Listener, ...) error
- func TunToConnCopy(ctx context.Context, logger *zap.Logger, source io.Reader, conn net.Conn) error
- type ConnectionMap
- type ControlMessage
- type IPPool
- type ServerOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddIpToIface ¶
AddIpToIface adds an ip address to a network interface.
func AddRoutesToIface ¶
AddRoutesToIface adds routes to a network interface.
func AddRoutesViaGateway ¶
AddRoutesViaGateway adds routes through a specified gateway IP.
func AddServerIp ¶
func AddServerIpDarwin ¶
func AddServerIpLinux ¶
func ConnToTunCopy ¶
func DeleteRoutesViaGateway ¶
DeleteRoutesViaGateway removes routes that go through a specified gateway IP.
func GetDnsByPassRoutes ¶
func GetDnsServers ¶
GetDnsServers returns a list of all active resolvers used by the system
func GetLocalInterfacesForIp ¶
Returns a list of interfaces on which this IP network was found
func IsIPInCIDR ¶
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 ¶
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
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 ¶
This is a pool of IPs that can be allocated to clients.
func NewIPPool ¶
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 ¶
Allocate allocates an available IP address from the pool. This will find a free IP address and mark it as used.
func (*IPPool) GetServerIp ¶
GetServerIp returns the server ip
func (*IPPool) GetSubnetSize ¶
GetSubnetSize returns the subnet size
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.