forward

package module
v0.0.0-...-89074c7 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2022 License: Unlicense Imports: 4 Imported by: 0

README

udp-forward

A dead simple Go (golang) package to forward UDP packets like a reverse NAT (i.e. it supports multiple users).

Usage

package main

import "github.com/1lann/udp-forward"

func main() {
	// Forward(src, dst). It's asynchronous.
	forwarder, err := forward.Forward("0.0.0.0:1000", "1.2.3.4:1023", forward.DefaultTimeout)
	if err != nil {
		panic(err)
	}

	// Do something...

	// Stop the forwarder
	forwarder.Close()
}

See the GoDoc for documentation.

License

There is no license.

Documentation

Overview

Package forward contains a UDP packet forwarder.

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

This section is empty.

Types

type Forwarder

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

Forwarder represents a UDP packet forwarder.

func Forward

func Forward(options ...Option) (*Forwarder, error)

Forward forwards UDP packets from the src address to the dst address, with a timeout to "disconnect" clients after the timeout period of inactivity. It implements a reverse NAT and thus supports multiple seperate users. Forward is also asynchronous.

func (*Forwarder) Close

func (f *Forwarder) Close()

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 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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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