autonat

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: 23 Imported by: 0

Documentation

Index

Constants

View Source
const AutoNATProto = "/libp2p/autonat/1.0.0"

AutoNATProto 标识 autonat 服务协议的版本号

View Source
const (
	ServiceName = "dep2p.autonat" // 服务名称

)

Variables

This section is empty.

Functions

func IsDialError

func IsDialError(e error) bool

IsDialError 判断错误是否表示AutoNAT对等节点回拨失败 参数:

  • e: error 错误实例,要检查的错误

返回值:

  • bool: 如果是回拨失败错误则返回true

func IsDialRefused

func IsDialRefused(e error) bool

IsDialRefused 判断错误是否表示AutoNAT对等节点拒绝回拨 参数:

  • e: error 错误实例,要检查的错误

返回值:

  • bool: 如果是拒绝回拨错误则返回true

Types

type AddrFunc

type AddrFunc func() []ma.Multiaddr

AddrFunc 是返回本地主机候选地址的函数类型 返回值:

  • []ma.Multiaddr: 返回本地主机的多地址列表

type AmbientAutoNAT

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

AmbientAutoNAT 实现了环境NAT自动发现

func (*AmbientAutoNAT) Close

func (as *AmbientAutoNAT) Close() error

Close 关闭 AutoNAT 服务 返回值:

  • error 关闭过程中的错误

func (*AmbientAutoNAT) Connected

func (as *AmbientAutoNAT) Connected(net network.Network, c network.Conn)

Connected 实现network.Notifiee接口的连接建立方法 参数:

  • net: network.Network 网络实例
  • c: network.Conn 建立的连接

func (*AmbientAutoNAT) Disconnected

func (as *AmbientAutoNAT) Disconnected(net network.Network, c network.Conn)

Disconnected 实现network.Notifiee接口的连接断开方法 参数:

  • net: network.Network 网络实例
  • c: network.Conn 断开的连接

func (*AmbientAutoNAT) Listen

func (as *AmbientAutoNAT) Listen(net network.Network, a ma.Multiaddr)

Listen 实现network.Notifiee接口的监听方法 参数:

  • net: network.Network 网络实例
  • a: ma.Multiaddr 监听的多地址

func (*AmbientAutoNAT) ListenClose

func (as *AmbientAutoNAT) ListenClose(net network.Network, a ma.Multiaddr)

ListenClose 实现network.Notifiee接口的监听关闭方法 参数:

  • net: network.Network 网络实例
  • a: ma.Multiaddr 关闭监听的多地址

func (*AmbientAutoNAT) Status

func (as *AmbientAutoNAT) Status() network.Reachability

Status 返回AutoNAT观察到的可达性状态 返回值:

  • network.Reachability: 返回当前的可达性状态

type AutoNAT

type AutoNAT interface {
	// Status 返回当前NAT状态
	// 返回值:
	//   - network.Reachability: 返回当前NAT的可达性状态
	Status() network.Reachability
	io.Closer // 继承io.Closer接口,用于关闭资源
}

AutoNAT 是NAT自动发现的接口

func New

func New(h host.Host, options ...Option) (AutoNAT, error)

New 创建一个新的NAT自动发现系统并附加到主机 参数:

  • h: host.Host 主机实例
  • options: ...Option 可选的配置选项

返回值:

  • AutoNAT: 返回创建的AutoNAT实例
  • error: 如果发生错误则返回错误信息

type Client

type Client interface {
	// DialBack 请求提供AutoNAT服务的对等节点进行回拨测试,
	// 并在连接成功时报告地址
	// 参数:
	//   - ctx: context.Context 上下文,用于控制请求的生命周期
	//   - p: peer.ID 对等节点ID,指定要请求回拨的节点
	//
	// 返回值:
	//   - error: 如果发生错误则返回错误信息,nil表示回拨成功
	DialBack(ctx context.Context, p peer.ID) error
}

Client 是与AutoNAT对等节点通信的无状态客户端接口

func NewAutoNATClient

func NewAutoNATClient(h host.Host, addrFunc AddrFunc, mt MetricsTracer) Client

NewAutoNATClient 创建一个新的AutoNAT客户端实例 参数:

  • h: host.Host 主机实例,用于创建流和获取节点ID
  • addrFunc: AddrFunc 地址获取函数,用于获取本地地址,如果为nil则使用h.Addrs
  • mt: MetricsTracer 指标追踪器,用于记录指标数据

返回值:

  • Client: 返回创建的AutoNAT客户端实例,用于执行回拨测试

type Error

type Error struct {
	Status pb.Message_ResponseStatus // 响应状态,表示错误类型
	Text   string                    // 错误文本,提供详细的错误信息
}

Error 封装了AutoNAT服务返回的错误

func (Error) Error

func (e Error) Error() string

Error 实现error接口 返回值:

  • string: 返回格式化的错误信息

func (Error) IsDialError

func (e Error) IsDialError() bool

IsDialError 判断是否为回拨失败错误 返回值:

  • bool: 如果状态为E_DIAL_ERROR则返回true

func (Error) IsDialRefused

func (e Error) IsDialRefused() bool

IsDialRefused 判断是否为拒绝回拨错误 返回值:

  • bool: 如果状态为E_DIAL_REFUSED则返回true

type MetricsTracer

type MetricsTracer interface {
	// ReachabilityStatus 记录可达性状态
	// 参数:
	//   - status: network.Reachability 可达性状态
	ReachabilityStatus(status network.Reachability)

	// ReachabilityStatusConfidence 记录可达性状态置信度
	// 参数:
	//   - confidence: int 置信度值
	ReachabilityStatusConfidence(confidence int)

	// ReceivedDialResponse 记录收到的回拨响应
	// 参数:
	//   - status: pb.Message_ResponseStatus 响应状态
	ReceivedDialResponse(status pb.Message_ResponseStatus)

	// OutgoingDialResponse 记录发出的回拨响应
	// 参数:
	//   - status: pb.Message_ResponseStatus 响应状态
	OutgoingDialResponse(status pb.Message_ResponseStatus)

	// OutgoingDialRefused 记录拒绝的回拨请求
	// 参数:
	//   - reason: string 拒绝原因
	OutgoingDialRefused(reason string)

	// NextProbeTime 记录下次探测时间
	// 参数:
	//   - t: time.Time 探测时间
	NextProbeTime(t time.Time)
}

MetricsTracer 定义了指标追踪器接口

func NewMetricsTracer

func NewMetricsTracer(opts ...MetricsTracerOption) MetricsTracer

NewMetricsTracer 创建新的指标追踪器 参数:

  • opts: ...MetricsTracerOption 配置选项

返回值:

  • MetricsTracer: 返回指标追踪器实例

type MetricsTracerOption

type MetricsTracerOption func(*metricsTracerSetting)

MetricsTracerOption 定义了指标追踪器的配置选项函数类型

func WithRegisterer

func WithRegisterer(reg prometheus.Registerer) MetricsTracerOption

WithRegisterer 设置指标注册器 参数:

  • reg: prometheus.Registerer 指标注册器

返回值:

  • MetricsTracerOption: 返回配置选项函数

type Option

type Option func(*config) error

Option 是用于配置AutoNAT的选项函数类型 参数:

  • *config: 配置对象指针

返回值:

  • error: 如果发生错误则返回错误信息,nil表示配置成功

func EnableService

func EnableService(dialer network.Network) Option

EnableService 指定 AutoNAT 可以运行 NAT 服务来帮助其他节点确定自己的 NAT 状态。 提供的 Network 不应该是传递给 New 的主机的默认网络/拨号器,因为 NAT 系统需要建立并行连接, 因此会修改相关的节点存储并终止此拨号器的连接。但是提供的拨号器应该与 dep2p 网络的传输协议兼容(TCP/UDP)。 参数:

  • dialer: network.Network 用于建立连接的网络拨号器

返回值:

  • Option: 返回配置选项函数

func UsingAddresses

func UsingAddresses(addrFunc AddrFunc) Option

UsingAddresses 允许覆盖 AutoNAT 客户端认为是"自己的"地址。 这对测试很有用,或者对于更特殊的端口转发场景,主机可能在不同于它想要对外广播或验证连接性的端口上监听。 参数:

  • addrFunc: AddrFunc 用于获取地址的函数

返回值:

  • Option: 返回配置选项函数

func WithMetricsTracer

func WithMetricsTracer(mt MetricsTracer) Option

WithMetricsTracer 使用 mt 来跟踪 autonat 指标 参数:

  • mt: MetricsTracer 指标追踪器实例

返回值:

  • Option: 返回配置选项函数

func WithPeerThrottling

func WithPeerThrottling(amount int) Option

WithPeerThrottling 指定此节点在每个间隔内为单个节点提供的 IP 检查的最大数量限制 参数:

  • amount: int 每个节点的限流阈值

返回值:

  • Option: 返回配置选项函数

func WithReachability

func WithReachability(reachability network.Reachability) Option

WithReachability 覆盖 autonat 以简单地报告一个被覆盖的可达性状态 参数:

  • reachability: network.Reachability 要设置的可达性状态

返回值:

  • Option: 返回配置选项函数

func WithSchedule

func WithSchedule(retryInterval, refreshInterval time.Duration) Option

WithSchedule 配置探测主机地址的频率。 retryInterval 表示当主机对其地址缺乏信心时应该多久探测一次, refreshInterval 表示当主机认为它知道其稳态可达性时的定期探测计划。 参数:

  • retryInterval: time.Duration 重试间隔时间
  • refreshInterval: time.Duration 刷新间隔时间

返回值:

  • Option: 返回配置选项函数

func WithThrottling

func WithThrottling(amount int, interval time.Duration) Option

WithThrottling 指定在作为服务器时每个时间间隔(interval)愿意帮助多少个节点(amount) 参数:

  • amount: int 限流阈值
  • interval: time.Duration 限流时间间隔

返回值:

  • Option: 返回配置选项函数

func WithoutStartupDelay

func WithoutStartupDelay() Option

WithoutStartupDelay 移除 NAT 子系统通常用作缓冲的初始延迟, 该延迟用于确保在启动期间连接性和对主机本地接口的猜测已经稳定。 返回值:

  • Option: 返回配置选项函数

func WithoutThrottling

func WithoutThrottling() Option

WithoutThrottling 表示此 autonat 服务在作为服务器时不应限制它愿意帮助的节点数量 返回值:

  • Option: 返回配置选项函数

type StaticAutoNAT

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

StaticAutoNAT 是一个简单的AutoNAT实现,用于单一NAT状态场景

func (*StaticAutoNAT) Close

func (s *StaticAutoNAT) Close() error

Close 关闭静态 AutoNAT 服务 返回值:

  • error 关闭过程中的错误

func (*StaticAutoNAT) Status

func (s *StaticAutoNAT) Status() network.Reachability

Status 返回静态 AutoNAT 的可达性状态 返回值:

  • network.Reachability - 当前的可达性状态

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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