nat

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const CacheTime = 15 * time.Second

CacheTime 是映射缓存外部地址的时间

View Source
const MappingDuration = time.Minute

MappingDuration 是默认的端口映射持续时间 端口映射每 (MappingDuration / 3) 更新一次

Variables

View Source
var ErrNoMapping = errors.New("未建立映射")

ErrNoMapping 表示地址没有映射

Functions

This section is empty.

Types

type NAT

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

NAT 是一个管理 NAT(网络地址转换器)中地址端口映射的对象 它是一个长期运行的服务,会定期更新端口映射, 并保持所有外部地址的最新列表

func DiscoverNAT

func DiscoverNAT(ctx context.Context) (*NAT, error)

DiscoverNAT 在网络中查找 NAT 设备并返回可以管理端口映射的对象 参数:

  • ctx: context.Context 上下文对象

返回值:

  • *NAT: NAT 管理对象
  • error: 错误信息

func (*NAT) AddMapping

func (nat *NAT) AddMapping(ctx context.Context, protocol string, port int) error

AddMapping 尝试在指定协议和内部端口上构建映射 它会阻塞直到映射建立。一旦添加,它会定期更新映射。

可能不会成功,映射可能随时间变化; NAT 设备可能不遵守我们的端口请求,甚至会欺骗。 参数:

  • ctx: context.Context 上下文对象
  • protocol: string 协议名称
  • port: int 端口号

返回值:

  • error: 错误信息

func (*NAT) Close

func (nat *NAT) Close() error

Close 关闭所有端口映射。NAT 不能再被使用 返回值:

  • error: 错误信息

func (*NAT) GetMapping

func (nat *NAT) GetMapping(protocol string, port int) (addr netip.AddrPort, found bool)

GetMapping 获取指定协议和端口的映射 参数:

  • protocol: string 协议名称
  • port: int 端口号

返回值:

  • addr: netip.AddrPort 映射的外部地址和端口
  • found: bool 是否找到映射

func (*NAT) RemoveMapping

func (nat *NAT) RemoveMapping(ctx context.Context, protocol string, port int) error

RemoveMapping 移除端口映射 它会阻塞直到 NAT 移除映射 参数:

  • ctx: context.Context 上下文对象
  • protocol: string 协议名称
  • port: int 端口号

返回值:

  • error: 错误信息

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳