portmap

package
v0.4.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 25 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 is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.

Package portmap opens and renews router port mappings for player-hosted servers. On native targets, the default provider tries PCP (RFC 6887), NAT-PMP (RFC 6886), and UPnP IGD v2/v1 in order, checks that the gateway's external address is publicly routable (reporting ErrDoubleNAT for private or CGNAT addresses), renews the mapping in the background until Close, and removes it on Close or CloseAll. On web targets (js), the default provider returns ErrUnsupported. Callers can override the provider with SetProvider for tests or custom gateways.

Index

Examples

Constants

View Source
const DefaultTimeout = 5 * time.Second

DefaultTimeout is the overall discovery and mapping timeout applied by the default native provider when the caller's context has no deadline.

Variables

View Source
var (
	// ErrUnsupported reports that port mapping is unsupported on this target.
	ErrUnsupported = errors.New("portmap: unsupported on this target")
	// ErrNoGateway reports that no gateway answered any supported protocol.
	ErrNoGateway = errors.New("portmap: no gateway found")
	// ErrRefused reports that the gateway answered but refused the mapping.
	ErrRefused = errors.New("portmap: gateway refused mapping")
	// ErrDoubleNAT reports that the gateway's external address is private or carrier-grade NAT, so a mapping would not be reachable from the internet.
	ErrDoubleNAT = errors.New("portmap: gateway external address is private")
)

Functions

func CloseAll

func CloseAll() error

CloseAll removes every active mapping opened by the built-in native provider in this process. Call it from a graceful shutdown hook; on js it is a no-op.

func SetProvider

func SetProvider(p Provider)

SetProvider makes p the provider Open uses on this target; nil restores the target's default provider (native PCP/NAT-PMP/UPnP on non-js targets, ErrUnsupported on js). It is safe to call while other goroutines call Open.

Types

type Mapping

type Mapping interface {
	External() netip.AddrPort
	Protocol() Protocol
	InternalPort() uint16
	Renew(context.Context) error
	Close() error
}

Mapping is a port the gateway forwards: its public address, protocol and internal port. Renew extends it by its lifetime and Close removes it.

func Open

func Open(ctx context.Context, req Request) (Mapping, error)

Open validates req and asks the active provider to map it. On native targets the default provider tries PCP, NAT-PMP, and UPnP IGD v2/v1 and renews the mapping in the background until Close; on js it returns ErrUnsupported.

Example

SetProvider installs the protocol implementation Open uses; Open validates the request and asks that provider for the mapping.

package main

import (
	"context"
	"fmt"
	"net/netip"
	"time"

	"github.com/nsgo-dev/game/pkg/online/portmap"
)

// provider stands in for a UPnP, NAT-PMP or PCP implementation; a real one sends the request to the gateway.
type provider struct{}

func (provider) Open(context.Context, portmap.Request) (portmap.Mapping, error) {
	return gatewayMapping{}, nil
}

type gatewayMapping struct{}

func (gatewayMapping) External() netip.AddrPort   { return netip.MustParseAddrPort("203.0.113.7:7777") }
func (gatewayMapping) Protocol() portmap.Protocol { return portmap.UDP }
func (gatewayMapping) InternalPort() uint16       { return 7777 }
func (gatewayMapping) Renew(context.Context) error {
	return nil
}
func (gatewayMapping) Close() error { return nil }

// SetProvider installs the protocol implementation Open uses; Open validates the request and asks that provider for the mapping.
func main() {
	portmap.SetProvider(provider{})
	defer portmap.SetProvider(nil)

	m, err := portmap.Open(context.Background(), portmap.Request{
		Protocol:     portmap.UDP,
		InternalPort: 7777,
		Lifetime:     time.Hour,
		Description:  "game server",
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	defer m.Close()
	fmt.Println(m.External(), m.InternalPort())
}
Output:
203.0.113.7:7777 7777

type Protocol

type Protocol uint8

Protocol is the transport protocol of a mapped port.

const (
	// UDP maps a UDP port.
	UDP Protocol = iota
	// TCP maps a TCP port.
	TCP
)

func (Protocol) String

func (p Protocol) String() string

String returns "UDP" or "TCP" for valid protocols.

type Provider

type Provider interface {
	Open(context.Context, Request) (Mapping, error)
}

Provider opens mappings with one or more gateway protocols, such as PCP, NAT-PMP or UPnP IGD.

type Request

type Request struct {
	Protocol     Protocol
	InternalPort uint16
	ExternalPort uint16
	Description  string
	Lifetime     time.Duration
}

Request asks the gateway to forward ExternalPort (0 lets the gateway choose) to InternalPort on this host for Protocol, for Lifetime, under a description the router shows.

func (Request) Validate

func (r Request) Validate() error

Validate reports an error unless the internal port is non-zero, the protocol is UDP or TCP and the lifetime is positive.

Jump to

Keyboard shortcuts

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