bind

package
v1.8.1 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2025 License: AGPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MagicPacketDataLen is the length of random data included in test packets
	MagicPacketDataLen = 8

	// MagicTestRequestLen is the total length of a test request packet
	MagicTestRequestLen = 17 + MagicPacketDataLen // len("PANGOLIN_TEST_REQ") + 8

	// MagicTestResponseLen is the total length of a test response packet
	MagicTestResponseLen = 17 + MagicPacketDataLen // len("PANGOLIN_TEST_RSP") + 8
)

Variables

View Source
var (
	// MagicTestRequest is the prefix for a test request packet
	// Format: PANGOLIN_TEST_REQ + 8 bytes of random data (for echo)
	MagicTestRequest = []byte("PANGOLIN_TEST_REQ")

	// MagicTestResponse is the prefix for a test response packet
	// Format: PANGOLIN_TEST_RSP + 8 bytes echoed from request
	MagicTestResponse = []byte("PANGOLIN_TEST_RSP")
)

Magic packet constants for connection testing These packets are intercepted by SharedBind and responded to directly, without being passed to the WireGuard device.

Functions

This section is empty.

Types

type Endpoint

type Endpoint struct {
	AddrPort netip.AddrPort
}

Endpoint represents a network endpoint for the SharedBind

func (*Endpoint) ClearSrc

func (e *Endpoint) ClearSrc()

ClearSrc implements the wgConn.Endpoint interface

func (*Endpoint) DstIP

func (e *Endpoint) DstIP() netip.Addr

DstIP implements the wgConn.Endpoint interface

func (*Endpoint) DstToBytes

func (e *Endpoint) DstToBytes() []byte

DstToBytes implements the wgConn.Endpoint interface

func (*Endpoint) DstToString

func (e *Endpoint) DstToString() string

DstToString implements the wgConn.Endpoint interface

func (*Endpoint) SrcIP

func (e *Endpoint) SrcIP() netip.Addr

SrcIP implements the wgConn.Endpoint interface

func (*Endpoint) SrcToString

func (e *Endpoint) SrcToString() string

SrcToString implements the wgConn.Endpoint interface

type MagicResponseCallback

type MagicResponseCallback func(addr netip.AddrPort, echoData []byte)

MagicResponseCallback is the function signature for magic packet response callbacks

type PacketSource

type PacketSource uint8

PacketSource identifies where a packet came from

const (
	SourceSocket   PacketSource = iota // From physical UDP socket (hole-punched clients)
	SourceNetstack                     // From netstack (relay through main tunnel)
)

type SharedBind

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

SharedBind is a thread-safe UDP bind that can be shared between WireGuard and hole punch senders. It wraps a single UDP connection and implements reference counting to prevent premature closure. It also supports receiving packets from a netstack and routing responses back through the appropriate source.

func New

func New(udpConn *net.UDPConn) (*SharedBind, error)

New creates a new SharedBind from an existing UDP connection. The SharedBind takes ownership of the connection and will close it when all references are released.

func (*SharedBind) AddRef

func (b *SharedBind) AddRef()

AddRef increments the reference count. Call this when sharing the bind with another component.

func (*SharedBind) BatchSize

func (b *SharedBind) BatchSize() int

BatchSize returns the preferred batch size for sending packets.

func (*SharedBind) ClearNetstackConn

func (b *SharedBind) ClearNetstackConn()

ClearNetstackConn clears the netstack connection and tracked endpoints. Call this when stopping the relay.

func (*SharedBind) Close

func (b *SharedBind) Close() error

Close implements the WireGuard Bind interface. It decrements the reference count and closes the connection if no references remain.

func (*SharedBind) GetNetstackConn

func (b *SharedBind) GetNetstackConn() net.PacketConn

GetNetstackConn returns the netstack connection if set

func (*SharedBind) GetRefCount

func (b *SharedBind) GetRefCount() int32

GetRefCount returns the current reference count (for debugging)

func (*SharedBind) GetUDPConn

func (b *SharedBind) GetUDPConn() *net.UDPConn

GetUDPConn returns the underlying UDP connection. The caller must not close this connection directly.

func (*SharedBind) InjectPacket

func (b *SharedBind) InjectPacket(data []byte, fromAddr netip.AddrPort) error

InjectPacket allows injecting a packet directly into the SharedBind's receive path. This is used for direct relay from netstack without going through the host network. The fromAddr should be the address the packet appears to come from.

func (*SharedBind) IsClosed

func (b *SharedBind) IsClosed() bool

IsClosed returns whether the bind is closed

func (*SharedBind) Open

func (b *SharedBind) Open(uport uint16) ([]wgConn.ReceiveFunc, uint16, error)

Open implements the WireGuard Bind interface. Since the connection is already open, this just sets up the receive functions.

func (*SharedBind) ParseEndpoint

func (b *SharedBind) ParseEndpoint(s string) (wgConn.Endpoint, error)

ParseEndpoint creates a new endpoint from a string address.

func (*SharedBind) Release

func (b *SharedBind) Release() error

Release decrements the reference count. When it reaches zero, the underlying UDP connection is closed.

func (*SharedBind) Send

func (b *SharedBind) Send(bufs [][]byte, ep wgConn.Endpoint) error

Send implements the WireGuard Bind interface. It sends packets to the specified endpoint, routing through the appropriate source (netstack or physical socket) based on where the endpoint's packets came from.

func (*SharedBind) SetMagicResponseCallback

func (b *SharedBind) SetMagicResponseCallback(callback MagicResponseCallback)

SetMagicResponseCallback sets a callback function that will be called when a magic test response packet is received. This is used for holepunch testing. Pass nil to clear the callback.

func (*SharedBind) SetMark

func (b *SharedBind) SetMark(mark uint32) error

SetMark implements the WireGuard Bind interface. It's a no-op for this implementation.

func (*SharedBind) SetNetstackConn

func (b *SharedBind) SetNetstackConn(conn net.PacketConn)

SetNetstackConn sets the netstack connection for receiving/sending packets through the tunnel. This connection is used for relay traffic that should go back through the main tunnel.

func (*SharedBind) WriteToUDP

func (b *SharedBind) WriteToUDP(data []byte, addr *net.UDPAddr) (int, error)

WriteToUDP writes data to a specific UDP address. This is thread-safe and can be used by hole punch senders.

type SourceAwareEndpoint

type SourceAwareEndpoint struct {
	wgConn.Endpoint
	// contains filtered or unexported fields
}

SourceAwareEndpoint wraps an endpoint with source information

func (*SourceAwareEndpoint) GetSource

func (e *SourceAwareEndpoint) GetSource() PacketSource

GetSource returns the source of this endpoint

Jump to

Keyboard shortcuts

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