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