network

package
v0.0.0-...-1d4be15 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2023 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// EmptyUDPSize is the size of an empty UDP packet
	EmptyUDPSize = 28

	// PacketHandshakeInit identifies handhshake initiation packets
	PacketHandshakeInit byte = 1
	// PacketHandshakeResp identifies handhshake response packets
	PacketHandshakeResp byte = 2
	// PacketData identifies regular data packets
	PacketData byte = 3
)

Variables

View Source
var (
	// ErrPacketType is returned when an unexepcted packet type is enountered
	ErrPacketType = errors.New("client/network: incorrect packet type")
	// ErrNonce is returned when the nonce on a packet isn't valid
	ErrNonce = errors.New("client/network: invalid nonce")

	// RekeyDuration is the time after which keys are invalid and a new handshake is required.
	RekeyDuration = 5 * time.Minute
)

Functions

func ApplyBPF

func ApplyBPF(rawConn *ipv4.RawConn, server *Server, client *Peer)

ApplyBPF constructs a BPF program and applies it to the RawConn

func GetClientIP

func GetClientIP(dstIP net.IP) net.IP

GetClientIP gets source ip address that will be used when sending data to dstIP

func Handshake

func Handshake(conn *ipv4.RawConn, privkey Key, server *Server, client *Peer) (sendCipher, recvCipher *auth.CipherState, index uint32, err error)

Handshake performs a Noise-IK handshake with the Server

func HostToAddr

func HostToAddr(hostStr string) *net.IPAddr

HostToAddr resolves a hostname, whether DNS or IP to a valid net.IPAddr

func MakePacket

func MakePacket(payload []byte, server *Server, client *Peer) []byte

MakePacket constructs a request packet to send to the server

func ParseResponse

func ParseResponse(response []byte) (net.IP, uint16)

ParseResponse takes a response packet and parses it into an IP and port. There's no error checking, we assume that data passed in is valid

func RecvDataPacket

func RecvDataPacket(cipher *auth.CipherState, conn *ipv4.RawConn, server *Server, client *Peer) (body, header []byte, packetType byte, n int, err error)

RecvDataPacket recieves a UDP packet from server

func RecvPacket

func RecvPacket(conn *ipv4.RawConn, server *Server, client *Peer) ([]byte, int, error)

RecvPacket recieves a UDP packet from server

func SendDataPacket

func SendDataPacket(cipher *auth.CipherState, index uint32, data []byte, conn *ipv4.RawConn, server *Server, client *Peer) error

SendDataPacket encrypts and sends packet to the Server

func SendPacket

func SendPacket(packet []byte, conn *ipv4.RawConn, server *Server, client *Peer) error

SendPacket sends packet to the Server

func SetupRawConn

func SetupRawConn(server *Server, client *Peer) *ipv4.RawConn

SetupRawConn creates an ipv4 and udp only RawConn and applies packet filtering

Types

type Key

type Key [32]byte

Key stores a 32 byte representation of a Wireguard key

type Peer

type Peer struct {
	Resolved bool
	IP       net.IP
	Port     uint16
	Pubkey   Key
}

Peer stores data about a peer's key and endpoint, whether it's another peer or the client While Resolved == false, we consider IP and Port to be uninitialized I could have done a nested struct with Endpoint containing IP and Port but that's unnecessary right now.

type Server

type Server struct {
	Hostname string
	Addr     *net.IPAddr
	Port     uint16
	Pubkey   Key

	LastHandshake time.Time
}

Server stores data relating to the server

Jump to

Keyboard shortcuts

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