Documentation
¶
Overview ¶
Package nattraversal provides NAT traversal using UPnP and NAT-PMP protocols with standard Go network interfaces and automatic port renewal.
Index ¶
- type BenchmarkHelper
- type DirectPortMapper
- type MockFirewall
- type MockNetworkConditions
- type MockPortMapper
- func (m *MockPortMapper) ExpireMapping(protocol string, externalPort int)
- func (m *MockPortMapper) GetActiveMappings() map[string]*PortMapping
- func (m *MockPortMapper) GetExternalIP() (string, error)
- func (m *MockPortMapper) MapPort(protocol string, internalPort int, duration time.Duration) (int, error)
- func (m *MockPortMapper) SetExternalIP(ip string)
- func (m *MockPortMapper) SetFailureRate(rate float64)
- func (m *MockPortMapper) SetLatency(d time.Duration)
- func (m *MockPortMapper) SetNATType(natType NATType)
- func (m *MockPortMapper) SetPortExhaustion(exhausted bool)
- func (m *MockPortMapper) SetProtocolSupport(upnp, natpmp bool)
- func (m *MockPortMapper) SetRandomSeed(seed int64)
- func (m *MockPortMapper) SimulateMappingChange(protocol string, oldPort, newPort int)
- func (m *MockPortMapper) UnmapPort(protocol string, externalPort int) error
- type MockRenewalManager
- type MockUDPConn
- func (c *MockUDPConn) AddReadData(data []byte)
- func (c *MockUDPConn) Close() error
- func (c *MockUDPConn) GetWrittenData() [][]byte
- func (c *MockUDPConn) LocalAddr() net.Addr
- func (c *MockUDPConn) Read(b []byte) (n int, err error)
- func (c *MockUDPConn) ReadFrom(p []byte) (n int, addr net.Addr, err error)
- func (c *MockUDPConn) RemoteAddr() net.Addr
- func (c *MockUDPConn) SetDeadline(t time.Time) error
- func (c *MockUDPConn) SetFirewall(firewall *MockFirewall)
- func (c *MockUDPConn) SetNetworkConditions(conditions *MockNetworkConditions)
- func (c *MockUDPConn) SetReadDeadline(t time.Time) error
- func (c *MockUDPConn) SetWriteDeadline(t time.Time) error
- func (c *MockUDPConn) Write(b []byte) (n int, err error)
- func (c *MockUDPConn) WriteTo(p []byte, addr net.Addr) (n int, err error)
- type NATAddr
- type NATConn
- type NATListener
- type NATPMPMapper
- type NATPacketConn
- func (c *NATPacketConn) Close() error
- func (c *NATPacketConn) LocalAddr() net.Addr
- func (c *NATPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error)
- func (c *NATPacketConn) SetDeadline(t time.Time) error
- func (c *NATPacketConn) SetReadDeadline(t time.Time) error
- func (c *NATPacketConn) SetWriteDeadline(t time.Time) error
- func (c *NATPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error)
- type NATPacketListener
- func ListenPacket(port int) (*NATPacketListener, error)
- func ListenPacketContext(ctx context.Context, port int) (*NATPacketListener, error)
- func ListenPacketWithFallback(port int) (*NATPacketListener, error)
- func ListenPacketWithFallbackContext(ctx context.Context, port int) (*NATPacketListener, error)
- type NATType
- type PortChangeCallback
- type PortMapper
- type PortMapping
- type RenewalManager
- type TestHelper
- func (h *TestHelper) AssertEqual(expected, actual interface{}, message string)
- func (h *TestHelper) AssertError(err error, message string)
- func (h *TestHelper) AssertNoError(err error, message string)
- func (h *TestHelper) AssertNotEqual(expected, actual interface{}, message string)
- func (h *TestHelper) AssertPortMappingExists(protocol string, externalPort int, message string)
- func (h *TestHelper) AssertPortMappingNotExists(protocol string, externalPort int, message string)
- func (h *TestHelper) Cleanup()
- func (h *TestHelper) CreateMockConnection(localPort, remotePort int) *MockUDPConn
- func (h *TestHelper) CreatePortMapping(protocol string, internalPort int, duration time.Duration) (int, error)
- func (h *TestHelper) CreateRenewalManager(protocol string, internalPort, externalPort int) *RenewalManager
- func (h *TestHelper) GetFirewall() *MockFirewall
- func (h *TestHelper) GetNetworkConditions() *MockNetworkConditions
- func (h *TestHelper) GetPortMapper() *MockPortMapper
- func (h *TestHelper) Reset()
- func (h *TestHelper) RunScenarios(scenarios []TestScenario, testFunc func(*TestHelper, TestScenario))
- func (h *TestHelper) RunWithCleanup(testFunc func())
- func (h *TestHelper) SetupFullConeNAT()
- func (h *TestHelper) SetupPoorNetwork()
- func (h *TestHelper) SetupRestrictedNAT()
- func (h *TestHelper) SetupRestrictiveFirewall()
- func (h *TestHelper) SetupSymmetricNAT()
- func (h *TestHelper) SimulateNetworkFailure() func()
- func (h *TestHelper) SimulatePortExhaustion() func()
- func (h *TestHelper) VerifyMapping(protocol string, externalPort int) bool
- func (h *TestHelper) WaitForRenewal()
- type TestScenario
- type UPnPMapper
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BenchmarkHelper ¶
type BenchmarkHelper struct {
// contains filtered or unexported fields
}
BenchmarkHelper provides utilities for benchmarking
func NewBenchmarkHelper ¶
func NewBenchmarkHelper() *BenchmarkHelper
NewBenchmarkHelper creates a new benchmark helper
func (*BenchmarkHelper) GetPortMapper ¶
func (b *BenchmarkHelper) GetPortMapper() *MockPortMapper
GetPortMapper returns the port mapper for benchmarking
func (*BenchmarkHelper) SetupForBenchmark ¶
func (b *BenchmarkHelper) SetupForBenchmark()
SetupForBenchmark configures optimal settings for benchmarking
type DirectPortMapper ¶
type DirectPortMapper struct {
// contains filtered or unexported fields
}
DirectPortMapper represents direct public connectivity where NAT traversal protocols are not required.
func (*DirectPortMapper) GetExternalIP ¶
func (d *DirectPortMapper) GetExternalIP() (string, error)
GetExternalIP returns the detected directly-routable public IP.
type MockFirewall ¶
type MockFirewall struct {
// contains filtered or unexported fields
}
MockFirewall simulates firewall behaviors
func NewMockFirewall ¶
func NewMockFirewall() *MockFirewall
NewMockFirewall creates a new mock firewall
func (*MockFirewall) AllowConnection ¶
func (f *MockFirewall) AllowConnection(ip string, port int)
AllowConnection explicitly allows a specific IP:port combination
func (*MockFirewall) BlockIP ¶
func (f *MockFirewall) BlockIP(ip string)
BlockIP adds an IP to the blocked list
func (*MockFirewall) BlockPort ¶
func (f *MockFirewall) BlockPort(port int)
BlockPort adds a port to the blocked list
func (*MockFirewall) IsBlocked ¶
func (f *MockFirewall) IsBlocked(ip string, port int) bool
IsBlocked checks if a connection should be blocked
func (*MockFirewall) Reset ¶
func (f *MockFirewall) Reset()
Reset clears all firewall rules and resets to default allow policy.
func (*MockFirewall) SetDefaultPolicy ¶
func (f *MockFirewall) SetDefaultPolicy(allow bool)
SetDefaultPolicy sets the default firewall policy
type MockNetworkConditions ¶
type MockNetworkConditions struct {
PacketLoss float64 // 0.0 to 1.0
Latency time.Duration
Jitter time.Duration
Bandwidth int64 // bytes per second
Blocked bool
Unreachable bool
// contains filtered or unexported fields
}
MockNetworkConditions simulates various network conditions
func NewMockNetworkConditions ¶
func NewMockNetworkConditions() *MockNetworkConditions
NewMockNetworkConditions creates default network conditions
func (*MockNetworkConditions) SetRandomSeed ¶
func (m *MockNetworkConditions) SetRandomSeed(seed int64)
SetRandomSeed sets a custom random seed for reproducible tests.
func (*MockNetworkConditions) SimulateLatency ¶
func (m *MockNetworkConditions) SimulateLatency()
SimulateLatency adds simulated network latency. Uses seeded RNG for jitter calculation.
func (*MockNetworkConditions) SimulatePacketLoss ¶
func (m *MockNetworkConditions) SimulatePacketLoss() bool
SimulatePacketLoss determines if a packet should be dropped. Uses seeded RNG for reproducible test results.
type MockPortMapper ¶
type MockPortMapper struct {
// contains filtered or unexported fields
}
MockPortMapper implements PortMapper interface for testing
func NewMockPortMapper ¶
func NewMockPortMapper() *MockPortMapper
NewMockPortMapper creates a new mock port mapper
func (*MockPortMapper) ExpireMapping ¶
func (m *MockPortMapper) ExpireMapping(protocol string, externalPort int)
ExpireMapping simulates a mapping expiring
func (*MockPortMapper) GetActiveMappings ¶
func (m *MockPortMapper) GetActiveMappings() map[string]*PortMapping
GetActiveMappings returns all active port mappings
func (*MockPortMapper) GetExternalIP ¶
func (m *MockPortMapper) GetExternalIP() (string, error)
GetExternalIP implements the PortMapper interface
func (*MockPortMapper) MapPort ¶
func (m *MockPortMapper) MapPort(protocol string, internalPort int, duration time.Duration) (int, error)
MapPort implements the PortMapper interface
func (*MockPortMapper) SetExternalIP ¶
func (m *MockPortMapper) SetExternalIP(ip string)
SetExternalIP sets the mock external IP
func (*MockPortMapper) SetFailureRate ¶
func (m *MockPortMapper) SetFailureRate(rate float64)
SetFailureRate sets the probability of operations failing (0.0 to 1.0)
func (*MockPortMapper) SetLatency ¶
func (m *MockPortMapper) SetLatency(d time.Duration)
SetLatency simulates network latency
func (*MockPortMapper) SetNATType ¶
func (m *MockPortMapper) SetNATType(natType NATType)
SetNATType sets the NAT behavior type
func (*MockPortMapper) SetPortExhaustion ¶
func (m *MockPortMapper) SetPortExhaustion(exhausted bool)
SetPortExhaustion simulates port exhaustion scenarios
func (*MockPortMapper) SetProtocolSupport ¶
func (m *MockPortMapper) SetProtocolSupport(upnp, natpmp bool)
SetProtocolSupport configures which protocols are supported
func (*MockPortMapper) SetRandomSeed ¶
func (m *MockPortMapper) SetRandomSeed(seed int64)
SetRandomSeed sets a custom random seed for reproducible tests. Use different seeds to test different random scenarios deterministically.
func (*MockPortMapper) SimulateMappingChange ¶
func (m *MockPortMapper) SimulateMappingChange(protocol string, oldPort, newPort int)
SimulateMappingChange simulates NAT mapping changes mid-connection
type MockRenewalManager ¶
type MockRenewalManager struct {
// contains filtered or unexported fields
}
MockRenewalManager provides a testable renewal manager
func NewMockRenewalManager ¶
func NewMockRenewalManager() *MockRenewalManager
NewMockRenewalManager creates a new mock renewal manager
func (*MockRenewalManager) GetFailureCount ¶
func (m *MockRenewalManager) GetFailureCount() int
GetFailureCount returns the number of failed renewals
func (*MockRenewalManager) GetRenewalCount ¶
func (m *MockRenewalManager) GetRenewalCount() int
GetRenewalCount returns the number of successful renewals
func (*MockRenewalManager) IsStopped ¶
func (m *MockRenewalManager) IsStopped() bool
IsStopped returns whether the renewal manager has been stopped
func (*MockRenewalManager) SetShouldFail ¶
func (m *MockRenewalManager) SetShouldFail(fail bool)
SetShouldFail configures renewal failures
type MockUDPConn ¶
type MockUDPConn struct {
// contains filtered or unexported fields
}
MockUDPConn provides a mock UDP connection for testing
func NewMockUDPConn ¶
func NewMockUDPConn(localAddr, remoteAddr *net.UDPAddr) *MockUDPConn
NewMockUDPConn creates a new mock UDP connection
func (*MockUDPConn) AddReadData ¶
func (c *MockUDPConn) AddReadData(data []byte)
AddReadData adds data to the read buffer
func (*MockUDPConn) GetWrittenData ¶
func (c *MockUDPConn) GetWrittenData() [][]byte
GetWrittenData returns all written data
func (*MockUDPConn) LocalAddr ¶
func (c *MockUDPConn) LocalAddr() net.Addr
LocalAddr implements net.Conn interface
func (*MockUDPConn) Read ¶
func (c *MockUDPConn) Read(b []byte) (n int, err error)
Read implements net.Conn interface
func (*MockUDPConn) RemoteAddr ¶
func (c *MockUDPConn) RemoteAddr() net.Addr
RemoteAddr implements net.Conn interface
func (*MockUDPConn) SetDeadline ¶
func (c *MockUDPConn) SetDeadline(t time.Time) error
SetDeadline implements net.Conn interface
func (*MockUDPConn) SetFirewall ¶
func (c *MockUDPConn) SetFirewall(firewall *MockFirewall)
SetFirewall sets the firewall for this connection
func (*MockUDPConn) SetNetworkConditions ¶
func (c *MockUDPConn) SetNetworkConditions(conditions *MockNetworkConditions)
SetNetworkConditions sets the network conditions for this connection
func (*MockUDPConn) SetReadDeadline ¶
func (c *MockUDPConn) SetReadDeadline(t time.Time) error
SetReadDeadline implements net.Conn interface
func (*MockUDPConn) SetWriteDeadline ¶
func (c *MockUDPConn) SetWriteDeadline(t time.Time) error
SetWriteDeadline implements net.Conn interface
type NATAddr ¶
type NATAddr struct {
// contains filtered or unexported fields
}
NATAddr represents a network address with NAT traversal information. Moved from: addr.go
func NewNATAddr ¶
NewNATAddr creates a new NATAddr with internal and external addresses.
func (*NATAddr) ExternalAddr ¶
ExternalAddr returns the external network address.
func (*NATAddr) InternalAddr ¶
InternalAddr returns the internal network address.
type NATConn ¶
NATConn wraps a net.Conn with NAT-aware addressing. Moved from: conn.go
func (*NATConn) RemoteAddr ¶
RemoteAddr returns the remote network address.
type NATListener ¶
type NATListener struct {
// contains filtered or unexported fields
}
NATListener implements net.Listener with automatic NAT traversal. Moved from: listener.go
func Listen ¶
func Listen(port int) (*NATListener, error)
Listen creates a TCP listener with NAT traversal on the specified port. This is a convenience wrapper around ListenContext using context.Background().
func ListenContext ¶
func ListenContext(ctx context.Context, port int) (*NATListener, error)
ListenContext creates a TCP listener with NAT traversal on the specified port. The context can be used to cancel the discovery and mapping operations. Once the listener is created, the context is no longer used - use Close() to stop the listener.
func ListenWithFallback ¶
func ListenWithFallback(port int) (*NATListener, error)
ListenWithFallback creates a TCP listener with NAT traversal on the specified port. If NAT traversal fails (UPnP and NAT-PMP both unavailable), it falls back to a standard net.Listener without NAT hole-punching. This is a convenience wrapper around ListenWithFallbackContext using context.Background().
func ListenWithFallbackContext ¶
func ListenWithFallbackContext(ctx context.Context, port int) (*NATListener, error)
ListenWithFallbackContext creates a TCP listener with NAT traversal on the specified port. If NAT traversal fails (UPnP and NAT-PMP both unavailable), it falls back to a standard net.Listener without NAT hole-punching. The context can be used to cancel the discovery and mapping operations. Once the listener is created, the context is no longer used - use Close() to stop the listener.
When fallback is used:
- ExternalPort() returns the same as the internal port
- Addr() returns a NATAddr where internal and external addresses are the same
- No port renewal is performed (the renewal manager is nil)
- IsFallback() returns true
func (*NATListener) Accept ¶
func (l *NATListener) Accept() (net.Conn, error)
Accept waits for and returns the next connection to the listener.
func (*NATListener) Addr ¶
func (l *NATListener) Addr() net.Addr
Addr returns the listener's network address.
func (*NATListener) Close ¶
func (l *NATListener) Close() error
Close closes the listener and stops port renewal.
func (*NATListener) ExternalPort ¶
func (l *NATListener) ExternalPort() int
ExternalPort returns the external port number assigned by the NAT device. This value may change if the NAT device assigns a different port during renewal. In fallback mode, this returns the same as the internal port.
func (*NATListener) IsFallback ¶
func (l *NATListener) IsFallback() bool
IsFallback returns true if NAT traversal failed and the listener is using a standard net.Listener without NAT hole-punching.
type NATPMPMapper ¶
type NATPMPMapper struct {
// contains filtered or unexported fields
}
NATPMPMapper implements PortMapper using NAT-PMP protocol. Moved from: addr.go
func NewNATPMPMapper ¶
func NewNATPMPMapper() (*NATPMPMapper, error)
NewNATPMPMapper discovers and creates a NAT-PMP mapper.
func (*NATPMPMapper) GetExternalIP ¶
func (n *NATPMPMapper) GetExternalIP() (string, error)
GetExternalIP returns the external IP address via NAT-PMP.
type NATPacketConn ¶
type NATPacketConn struct {
net.PacketConn
// contains filtered or unexported fields
}
NATPacketConn wraps a net.PacketConn with NAT-aware addressing. It coordinates with NATPacketListener to ensure Close() is idempotent and safe to call from either the connection or the listener.
func (*NATPacketConn) Close ¶
func (c *NATPacketConn) Close() error
Close closes the connection. This method is idempotent - calling it multiple times is safe and will only close the underlying connection once, returning the same error.
func (*NATPacketConn) LocalAddr ¶
func (c *NATPacketConn) LocalAddr() net.Addr
LocalAddr returns the local network address with NAT info.
func (*NATPacketConn) SetDeadline ¶
func (c *NATPacketConn) SetDeadline(t time.Time) error
SetDeadline sets the read and write deadlines.
func (*NATPacketConn) SetReadDeadline ¶
func (c *NATPacketConn) SetReadDeadline(t time.Time) error
SetReadDeadline sets the deadline for future ReadFrom calls.
func (*NATPacketConn) SetWriteDeadline ¶
func (c *NATPacketConn) SetWriteDeadline(t time.Time) error
SetWriteDeadline sets the deadline for future WriteTo calls.
type NATPacketListener ¶
type NATPacketListener struct {
// contains filtered or unexported fields
}
NATPacketListener implements a packet listener with NAT traversal. Moved from: packetlistener.go
func ListenPacket ¶
func ListenPacket(port int) (*NATPacketListener, error)
ListenPacket creates a UDP packet listener with NAT traversal on the specified port. This is a convenience wrapper around ListenPacketContext using context.Background().
func ListenPacketContext ¶
func ListenPacketContext(ctx context.Context, port int) (*NATPacketListener, error)
ListenPacketContext creates a UDP packet listener with NAT traversal on the specified port. The context can be used to cancel the discovery and mapping operations. Once the listener is created, the context is no longer used - use Close() to stop the listener.
func ListenPacketWithFallback ¶
func ListenPacketWithFallback(port int) (*NATPacketListener, error)
ListenPacketWithFallback creates a UDP packet listener with NAT traversal on the specified port. If NAT traversal fails (UPnP and NAT-PMP both unavailable), it falls back to a standard net.PacketConn without NAT hole-punching. This is a convenience wrapper around ListenPacketWithFallbackContext using context.Background().
func ListenPacketWithFallbackContext ¶
func ListenPacketWithFallbackContext(ctx context.Context, port int) (*NATPacketListener, error)
ListenPacketWithFallbackContext creates a UDP packet listener with NAT traversal on the specified port. If NAT traversal fails (UPnP and NAT-PMP both unavailable), it falls back to a standard net.PacketConn without NAT hole-punching. The context can be used to cancel the discovery and mapping operations. Once the listener is created, the context is no longer used - use Close() to stop the listener.
When fallback is used:
- ExternalPort() returns the same as the internal port
- Addr() returns a NATAddr where internal and external addresses are the same
- No port renewal is performed (the renewal manager is nil)
- IsFallback() returns true
func (*NATPacketListener) Accept ¶
func (l *NATPacketListener) Accept() (net.PacketConn, error)
Accept returns a packet connection (satisfies a hypothetical net.PacketListener interface). Note: For UDP, this returns the same cached connection each time since UDP is connectionless. Unlike TCP's Accept which blocks waiting for new connections, this immediately returns the single packet connection associated with this listener.
func (*NATPacketListener) Addr ¶
func (l *NATPacketListener) Addr() net.Addr
Addr returns the listener's network address.
func (*NATPacketListener) Close ¶
func (l *NATPacketListener) Close() error
Close closes the packet listener and stops port renewal. This method is idempotent - calling it multiple times is safe. It coordinates with NATPacketConn.Close() to ensure the underlying connection is only closed once, even if both are called.
func (*NATPacketListener) ExternalPort ¶
func (l *NATPacketListener) ExternalPort() int
ExternalPort returns the external port number assigned by the NAT device. This value may change if the NAT device assigns a different port during renewal. In fallback mode, this returns the same as the internal port.
func (*NATPacketListener) IsFallback ¶
func (l *NATPacketListener) IsFallback() bool
IsFallback returns true if NAT traversal failed and the listener is using a standard net.PacketConn without NAT hole-punching.
func (*NATPacketListener) PacketConn ¶
func (l *NATPacketListener) PacketConn() net.PacketConn
PacketConn returns the underlying packet connection. Returns the same cached instance on each call.
type PortChangeCallback ¶
type PortChangeCallback func(newExternalPort int)
PortChangeCallback is called when the external port changes during renewal. The callback receives the new external port number.
type PortMapper ¶
type PortMapper interface {
MapPort(protocol string, internalPort int, duration time.Duration) (externalPort int, err error)
UnmapPort(protocol string, externalPort int) error
GetExternalIP() (string, error)
}
PortMapper defines the interface for NAT traversal protocols.
func NewPortMapper ¶
func NewPortMapper() (PortMapper, error)
NewPortMapper creates a port mapper, trying direct connectivity first, then UPnP, then NAT-PMP. This is a convenience wrapper around NewPortMapperContext using context.Background().
func NewPortMapperContext ¶
func NewPortMapperContext(ctx context.Context) (PortMapper, error)
NewPortMapperContext creates a port mapper with context support, trying direct connectivity first, then UPnP, then NAT-PMP. The context is passed through to the discovery process, allowing cancellation during slow network operations.
type PortMapping ¶
type PortMapping struct {
Protocol string
InternalPort int
ExternalPort int
ExpiresAt time.Time
Active bool
}
PortMapping represents a mock port mapping
type RenewalManager ¶
type RenewalManager struct {
// contains filtered or unexported fields
}
RenewalManager handles automatic port mapping renewal. Moved from: renew.go
func NewRenewalManager ¶
func NewRenewalManager(mapper PortMapper, protocol string, internalPort, externalPort int) *RenewalManager
NewRenewalManager creates a renewal manager for a port mapping.
func (*RenewalManager) ExternalPort ¶
func (r *RenewalManager) ExternalPort() int
ExternalPort returns the current external port number. This may change if the NAT device assigns a different port during renewal.
func (*RenewalManager) SetPortChangeCallback ¶
func (r *RenewalManager) SetPortChangeCallback(callback PortChangeCallback)
SetPortChangeCallback sets a callback function that will be invoked when the external port changes during renewal. This can happen if the NAT device assigns a different port during renewal (rare but possible).
func (*RenewalManager) Start ¶
func (r *RenewalManager) Start()
Start begins the renewal process in a background goroutine. Multiple Start/Stop cycles are safe - each cycle creates fresh channels and the goroutine captures local references to avoid data races.
func (*RenewalManager) Stop ¶
func (r *RenewalManager) Stop()
Stop terminates the renewal process and unmaps the port.
type TestHelper ¶
type TestHelper struct {
// contains filtered or unexported fields
}
TestHelper provides utilities for NAT traversal testing
func NewTestHelper ¶
func NewTestHelper(t *testing.T) *TestHelper
NewTestHelper creates a new test helper instance
func (*TestHelper) AssertEqual ¶
func (h *TestHelper) AssertEqual(expected, actual interface{}, message string)
AssertEqual fails the test if values are not equal
func (*TestHelper) AssertError ¶
func (h *TestHelper) AssertError(err error, message string)
AssertError fails the test if error is nil
func (*TestHelper) AssertNoError ¶
func (h *TestHelper) AssertNoError(err error, message string)
AssertNoError fails the test if error is not nil
func (*TestHelper) AssertNotEqual ¶
func (h *TestHelper) AssertNotEqual(expected, actual interface{}, message string)
AssertNotEqual fails the test if values are equal
func (*TestHelper) AssertPortMappingExists ¶
func (h *TestHelper) AssertPortMappingExists(protocol string, externalPort int, message string)
AssertPortMappingExists verifies a port mapping exists
func (*TestHelper) AssertPortMappingNotExists ¶
func (h *TestHelper) AssertPortMappingNotExists(protocol string, externalPort int, message string)
AssertPortMappingNotExists verifies a port mapping does not exist
func (*TestHelper) Cleanup ¶
func (h *TestHelper) Cleanup()
Cleanup cleans up all resources created during testing
func (*TestHelper) CreateMockConnection ¶
func (h *TestHelper) CreateMockConnection(localPort, remotePort int) *MockUDPConn
CreateMockConnection creates a mock UDP connection with configured conditions
func (*TestHelper) CreatePortMapping ¶
func (h *TestHelper) CreatePortMapping(protocol string, internalPort int, duration time.Duration) (int, error)
CreatePortMapping creates a port mapping and tracks it for cleanup
func (*TestHelper) CreateRenewalManager ¶
func (h *TestHelper) CreateRenewalManager(protocol string, internalPort, externalPort int) *RenewalManager
CreateRenewalManager creates a renewal manager and tracks it for cleanup
func (*TestHelper) GetFirewall ¶
func (h *TestHelper) GetFirewall() *MockFirewall
GetFirewall returns the mock firewall for configuration
func (*TestHelper) GetNetworkConditions ¶
func (h *TestHelper) GetNetworkConditions() *MockNetworkConditions
GetNetworkConditions returns the mock network conditions for configuration
func (*TestHelper) GetPortMapper ¶
func (h *TestHelper) GetPortMapper() *MockPortMapper
GetPortMapper returns the mock port mapper for advanced operations
func (*TestHelper) Reset ¶
func (h *TestHelper) Reset()
Reset resets the TestHelper state between subtests to prevent state leakage. This should be called at the start of each subtest when using a shared TestHelper.
func (*TestHelper) RunScenarios ¶
func (h *TestHelper) RunScenarios(scenarios []TestScenario, testFunc func(*TestHelper, TestScenario))
RunScenarios runs multiple test scenarios
func (*TestHelper) RunWithCleanup ¶
func (h *TestHelper) RunWithCleanup(testFunc func())
RunWithCleanup runs a test function and ensures cleanup happens
func (*TestHelper) SetupFullConeNAT ¶
func (h *TestHelper) SetupFullConeNAT()
SetupFullConeNAT configures the environment for Full Cone NAT testing
func (*TestHelper) SetupPoorNetwork ¶
func (h *TestHelper) SetupPoorNetwork()
SetupPoorNetwork simulates poor network conditions
func (*TestHelper) SetupRestrictedNAT ¶
func (h *TestHelper) SetupRestrictedNAT()
SetupRestrictedNAT configures the environment for Restricted NAT testing
func (*TestHelper) SetupRestrictiveFirewall ¶
func (h *TestHelper) SetupRestrictiveFirewall()
SetupRestrictiveFirewall configures a restrictive firewall
func (*TestHelper) SetupSymmetricNAT ¶
func (h *TestHelper) SetupSymmetricNAT()
SetupSymmetricNAT configures the environment for Symmetric NAT testing
func (*TestHelper) SimulateNetworkFailure ¶
func (h *TestHelper) SimulateNetworkFailure() func()
SimulateNetworkFailure temporarily increases failure rates
func (*TestHelper) SimulatePortExhaustion ¶
func (h *TestHelper) SimulatePortExhaustion() func()
SimulatePortExhaustion enables port exhaustion simulation
func (*TestHelper) VerifyMapping ¶
func (h *TestHelper) VerifyMapping(protocol string, externalPort int) bool
VerifyMapping checks if a port mapping exists and is active
func (*TestHelper) WaitForRenewal ¶
func (h *TestHelper) WaitForRenewal()
WaitForRenewal waits for at least one renewal cycle to complete
type TestScenario ¶
type TestScenario struct {
Name string
NATType NATType
HasFirewall bool
PoorNetwork bool
FailureRate float64
Expected bool
}
TestScenario represents a test scenario configuration
type UPnPMapper ¶
type UPnPMapper struct {
// contains filtered or unexported fields
}
UPnPMapper implements PortMapper using UPnP IGD protocol. Supports WANIPConnection1, WANIPConnection2, and WANPPPConnection1 services.
func NewUPnPMapper ¶
func NewUPnPMapper() (*UPnPMapper, error)
NewUPnPMapper discovers and creates a UPnP mapper. This is a convenience wrapper around NewUPnPMapperContext using context.Background().
func NewUPnPMapperContext ¶
func NewUPnPMapperContext(ctx context.Context) (*UPnPMapper, error)
NewUPnPMapperContext discovers and creates a UPnP mapper with context support. The context allows cancellation of the discovery process, which can take several seconds. It attempts discovery in order of preference: WANIPConnection2, WANIPConnection1, then WANPPPConnection1, using the first service that responds with available devices.
func (*UPnPMapper) GetExternalIP ¶
func (u *UPnPMapper) GetExternalIP() (string, error)
GetExternalIP returns the external IP address via UPnP.
Source Files
¶
- addr.go
- conn.go
- constants.go
- direct_port_mapper.go
- gateway.go
- gateway_linux.go
- listener.go
- log.go
- mocks.go
- nataddr.go
- natconn.go
- natlistener.go
- natpacketconn.go
- natpacketlistener.go
- natpmpmapper.go
- packetconn.go
- packetlistener.go
- portmapper.go
- renew.go
- renewalmanager.go
- test_helpers.go
- types.go
- upnp.go
- upnpmapper.go
- utils.go