natmap

package
v0.0.0-...-84bd548 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package forward contains a UDP packet forwarder. https://github.com/gwangyi/udp-forward

Index

Constants

View Source
const DefaultTimeout = time.Minute * 5

DefaultTimeout is the default timeout period of inactivity for convenience sake. It is equivelant to 5 minutes.

Variables

This section is empty.

Functions

func Forward

func Forward(ctx context.Context, laddr netip.AddrPort, target string, log func(string)) (io.Closer, error)

func ForwardUdp

func ForwardUdp(ctx context.Context, laddr netip.AddrPort, target string, log func(string)) (io.Closer, error)

func GetLocalAddr

func GetLocalAddr() (net.Addr, error)

Types

type Forwarder

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

Forwarder represents a UDP packet forwarder.

func (*Forwarder) Close

func (f *Forwarder) Close() error

Close stops the forwarder.

func (*Forwarder) Connected

func (f *Forwarder) Connected() []string

Connected returns the list of connected clients in IP:port form.

func (*Forwarder) LocalAddr

func (f *Forwarder) LocalAddr() *net.UDPAddr

LocalAddr returns LocalAddr of listening connection.

func (*Forwarder) OnConnect

func (f *Forwarder) OnConnect(callback func(addr string))

OnConnect can be called with a callback function to be called whenever a new client connects.

func (*Forwarder) OnDisconnect

func (f *Forwarder) OnDisconnect(callback func(addr string))

OnDisconnect can be called with a callback function to be called whenever a new client disconnects (after 5 minutes of inactivity).

type Logger

type Logger interface {
	Println(v ...any)
}

type Map

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

func NatMap

func NatMap(ctx context.Context, stunAddr string, laddr netip.AddrPort, log func(error)) (*Map, netip.AddrPort, error)

func NatMapUdp

func NatMapUdp(ctx context.Context, stunAddr string, laddr netip.AddrPort, log func(error)) (*Map, netip.AddrPort, error)

func (Map) Close

func (m Map) Close() error

type Option

type Option func(*config) error

Option gives the way to customize the forwarder.

func WithAddr

func WithAddr(src string) Option

WithAddr lets the new forwarder listen from given address.

func WithBufferSize

func WithBufferSize(size int) Option

WithBufferSize sets the buffer size that is used by forwarding. Larger packet can be discarded.

func WithConn

func WithConn(conn *net.UDPConn) Option

WithConn lets the new forwarder to use given conn instead of new one.

func WithDestination

func WithDestination(dest string) Option

WithDestination lets the new forwarder forward packets to the given address.

func WithLogger

func WithLogger(logger Logger) Option

WithLogger sets a logger.

func WithRouter

func WithRouter(router Router) Option

WithRouter lets the new forwarder forward packets according to the given router.

func WithRouterFunc

func WithRouterFunc(router func(*net.UDPAddr) *net.UDPAddr) Option

WithRouterFunc does the same as WithRouter, but with a function.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout sets the timeout. No interaction more than the timeout will remove the connection from the NAT table.

func WithoutLogger

func WithoutLogger() Option

WithoutLogger lets forwarder not log anything.

type Router

type Router interface {
	Route(*net.UDPAddr) *net.UDPAddr
}

Router represents a router that gives the destination address.

Jump to

Keyboard shortcuts

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