udp

package
v0.67.3 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2026 License: BSD-3-Clause, AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultSessionTTL is the default idle timeout for UDP sessions before cleanup.
	DefaultSessionTTL = 30 * time.Second

	// DefaultMaxSessions is the default cap on concurrent UDP sessions per relay.
	DefaultMaxSessions = 1024
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Relay

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

Relay listens for incoming UDP packets on a dedicated port and maintains per-client sessions that relay packets to a backend through the WireGuard tunnel.

func New

func New(parentCtx context.Context, cfg RelayConfig) *Relay

New creates a UDP relay for the given listener and backend target. MaxSessions caps the number of concurrent sessions; use 0 for DefaultMaxSessions. DialTimeout controls how long to wait for backend connections; use 0 for default. SessionTTL is the idle timeout before a session is reaped; use 0 for DefaultSessionTTL.

func (*Relay) Close

func (r *Relay) Close()

Close stops the relay, waits for all session goroutines to exit, and cleans up remaining sessions.

func (*Relay) Serve

func (r *Relay) Serve()

Serve starts the relay loop. It blocks until the context is canceled or the listener is closed.

func (*Relay) ServiceID

func (r *Relay) ServiceID() types.ServiceID

ServiceID returns the service ID associated with this relay.

func (*Relay) SetObserver

func (r *Relay) SetObserver(obs SessionObserver)

SetObserver sets the session lifecycle observer. Must be called before Serve.

type RelayConfig

type RelayConfig struct {
	Logger      *log.Entry
	Listener    net.PacketConn
	Target      string
	Domain      string
	AccountID   types.AccountID
	ServiceID   types.ServiceID
	DialFunc    types.DialContextFunc
	DialTimeout time.Duration
	SessionTTL  time.Duration
	MaxSessions int
	AccessLog   l4Logger
	// Filter holds connection-level IP/geo restrictions. Nil means no restrictions.
	Filter *restrict.Filter
	// Geo is the geolocation lookup used for country-based restrictions.
	Geo restrict.GeoResolver
}

RelayConfig holds the configuration for a UDP relay.

type SessionObserver

type SessionObserver interface {
	UDPSessionStarted(accountID types.AccountID)
	UDPSessionEnded(accountID types.AccountID)
	UDPSessionDialError(accountID types.AccountID)
	UDPSessionRejected(accountID types.AccountID)
	UDPPacketRelayed(direction types.RelayDirection, bytes int)
}

SessionObserver receives callbacks for UDP session lifecycle events. All methods must be safe for concurrent use.

Jump to

Keyboard shortcuts

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