nat

package
v3.0.0-alpha5.0...-c974331 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2024 License: LGPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package nat provides access to common network port mapping protocols.

Index

Constants

View Source
const STUNDefaultServerAddr = "stun.l.google.com:19302"

Variables

This section is empty.

Functions

func Map

func Map(m Interface, c <-chan struct{}, protocol string, extport, intport int, name string, logger log.Logger)

Map adds a port mapping on m and keeps it alive until c is closed. This function is typically invoked in its own goroutine.

Types

type ExtIP

type ExtIP net.IP

ExtIP assumes that the local machine is reachable on the given external IP address, and that any required ports were mapped manually. Mapping operations will not return an error but won't actually do anything.

func (ExtIP) AddMapping

func (ExtIP) AddMapping(string, int, int, string, time.Duration) error

func (ExtIP) DeleteMapping

func (ExtIP) DeleteMapping(string, int, int) error

func (ExtIP) ExternalIP

func (n ExtIP) ExternalIP() (net.IP, error)

func (ExtIP) String

func (n ExtIP) String() string

func (ExtIP) SupportsMapping

func (ExtIP) SupportsMapping() bool

type Interface

type Interface interface {
	// These methods manage a mapping between a port on the local
	// machine to a port that can be connected to from the internet.
	//
	// protocol is "UDP" or "TCP". Some implementations allow setting
	// a display name for the mapping. The mapping may be removed by
	// the gateway when its lifetime ends.
	AddMapping(protocol string, extport, intport int, name string, lifetime time.Duration) error
	DeleteMapping(protocol string, extport, intport int) error
	SupportsMapping() bool

	// This method should return the external (Internet-facing)
	// address of the gateway device.
	ExternalIP() (net.IP, error)

	// Should return name of the method. This is used for logging.
	String() string
}

An implementation of nat.Interface can map local ports to ports accessible from the Internet.

func Any

func Any() Interface

Any returns a port mapper that tries to discover any supported mechanism on the local network.

func PMP

func PMP(gateway net.IP) Interface

PMP returns a port mapper that uses NAT-PMP. The provided gateway address should be the IP of your router. If the given gateway address is nil, PMP will attempt to auto-discover the router.

func Parse

func Parse(spec string) (Interface, error)

Parse parses a NAT interface description. The following formats are currently accepted. Note that mechanism names are not case-sensitive.

"" or "none"         return nil
"extip:77.12.33.4"   will assume the local machine is reachable on the given IP
"any"                uses the first auto-detected mechanism
"upnp"               uses the Universal Plug and Play protocol
"pmp"                uses NAT-PMP with an auto-detected gateway address
"pmp:192.168.0.1"    uses NAT-PMP with the given gateway address
"stun"               uses STUN to detect an external IP using a default server
"stun:<server>"      uses STUN to detect an external IP using the given server (host:port)

func UPnP

func UPnP() Interface

UPnP returns a port mapper that uses UPnP. It will attempt to discover the address of your router using UDP broadcasts.

type STUN

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

func NewSTUN

func NewSTUN(serverAddr string) STUN

func (STUN) AddMapping

func (STUN) AddMapping(string, int, int, string, time.Duration) error

func (STUN) DeleteMapping

func (STUN) DeleteMapping(string, int, int) error

func (STUN) ExternalIP

func (s STUN) ExternalIP() (net.IP, error)

func (STUN) String

func (s STUN) String() string

func (STUN) SupportsMapping

func (STUN) SupportsMapping() bool

Jump to

Keyboard shortcuts

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