portmap

package
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package portmap keeps the listening port reachable from the internet through the router (UPnP IGD, NAT-PMP). Unlike fire-and-forget mapping it renews the lease, verifies that the mapping still exists and re-creates it after a router reboot or an IP change, which is the usual reason a forwarded port "breaks after a while".

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

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

Manager runs the mapping loop.

func New

func New(port int, opts Options, mappers ...Mapper) *Manager

New creates a manager for port. Pass nil mappers for the default UPnP + NAT-PMP set.

func (*Manager) Refresh

func (m *Manager) Refresh()

Refresh asks for an immediate check (e.g. after a network change).

func (*Manager) Start

func (m *Manager) Start()

Start launches the background loop.

func (*Manager) Status

func (m *Manager) Status() Status

Status returns a snapshot.

func (*Manager) Stop

func (m *Manager) Stop()

Stop ends the loop and removes the mapping from the router. It never waits long: closing the program waits for it.

type Mapper

type Mapper interface {
	Name() string
	// Map creates or refreshes a mapping and returns the external IP.
	Map(ctx context.Context, proto string, port int, lease time.Duration) (extIP string, err error)
	// Exists reports whether the mapping is still present on the router. Protocols that
	// cannot be queried return (true, nil) and rely on regular renewal.
	Exists(ctx context.Context, proto string, port int) (bool, error)
	Unmap(ctx context.Context, proto string, port int) error
}

Mapper is one port-forwarding protocol.

type NATPMP

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

NATPMP implements Mapper on top of NAT-PMP (also answered by PCP-capable routers).

func NewNATPMP

func NewNATPMP() *NATPMP

func NewNATPMPWith added in v1.1.0

func NewNATPMPWith(router func() string) *NATPMP

NewNATPMPWith asks the router at the address router returns first (read each time, so a change in the settings applies at once); with "" it finds the router itself.

func (*NATPMP) Exists

func (n *NATPMP) Exists(ctx context.Context, proto string, port int) (bool, error)

NAT-PMP has no query operation; regular lease renewal keeps the mapping alive.

func (*NATPMP) Map

func (n *NATPMP) Map(ctx context.Context, proto string, port int, lease time.Duration) (string, error)

func (*NATPMP) Name

func (n *NATPMP) Name() string

func (*NATPMP) Unmap

func (n *NATPMP) Unmap(ctx context.Context, proto string, port int) error

type Options

type Options struct {
	Lease         time.Duration // requested lease, default 1h
	CheckInterval time.Duration // verify/renew period, default 5m
	MinBackoff    time.Duration // after a failure, default 15s
	MaxBackoff    time.Duration // default 5m
	// Router returns the router's address given in the settings ("" = find it), for NAT-PMP where the default
	// gateway is not the router (a container). Nil is the same as "".
	Router func() string
}

Options tune timings; zero values pick defaults.

type Status

type Status struct {
	Enabled    bool      `json:"enabled"`
	Mapped     bool      `json:"mapped"`
	Method     string    `json:"method,omitempty"`
	Port       int       `json:"port"`
	ExternalIP string    `json:"externalIP,omitempty"`
	LastOK     time.Time `json:"lastOk,omitempty"`
	LastError  string    `json:"lastError,omitempty"`
	Failures   int       `json:"failures"` // consecutive failed cycles
	Remaps     int       `json:"remaps"`   // times a lost mapping had to be recreated
}

Status describes the current state for the UI.

type UPnP

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

UPnP implements Mapper on top of UPnP Internet Gateway Device.

func NewUPnP

func NewUPnP() *UPnP

func (*UPnP) Exists

func (u *UPnP) Exists(ctx context.Context, proto string, port int) (bool, error)

func (*UPnP) Map

func (u *UPnP) Map(ctx context.Context, proto string, port int, lease time.Duration) (string, error)

func (*UPnP) Name

func (u *UPnP) Name() string

func (*UPnP) Unmap

func (u *UPnP) Unmap(ctx context.Context, proto string, port int) error

Jump to

Keyboard shortcuts

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