Documentation
¶
Overview ¶
作为客户端使用的代码实现
Package node 节点模块 包含Findings候选池、连接池以及应用节点池的支持。
Index ¶
- Constants
- Variables
- func EncodePeer(node *Node) ([]byte, error)
- func EncodePeers(nodes []*Node) ([]byte, error)
- func Init(ctx context.Context, conf *cfg.Config, stakes map[string]string, ...)
- func NewClientApps() *clientApps
- func Online(conn *websocket.Conn, long time.Duration) error
- func ProcessOnKind(kind *base.Kind, conn *websocket.Conn, w http.ResponseWriter)
- func WebsocketDial(ip netip.Addr, port int, long time.Duration) (*websocket.Conn, error)
- type AppMap
- type AppPool
- type Applier
- type Appliers
- func (a *Appliers) Add(node *Applier) error
- func (a *Appliers) Clean(ctx context.Context, long time.Duration)
- func (a *Appliers) Dispose(conn *websocket.Conn) *Applier
- func (a *Appliers) Get() *Applier
- func (a *Appliers) IsFulled() bool
- func (a *Appliers) List(count int) []*Applier
- func (a *Appliers) Size() int
- type AppliersPool
- func (cp AppliersPool) Appliers(kind string, level NatLevel) (*Appliers, error)
- func (cp AppliersPool) Appliers4(kind string) []*Appliers
- func (cp AppliersPool) Clean(ctx context.Context, kinds []string, long time.Duration)
- func (cp AppliersPool) Init(kind string, size int)
- func (cp AppliersPool) Size() int
- func (cp AppliersPool) Supported(kind string) bool
- type Banner
- type ClientSN
- type Finder
- func (f *Finder) Client(ctx context.Context, notice chan<- *stun.Notice)
- func (f *Finder) NatLevel() (NatLevel, error)
- func (f *Finder) NatLive() (time.Duration, error)
- func (f *Finder) NewHost(peer *stun.Client)
- func (f *Finder) Punching(peer *LinkPeer) error
- func (f *Finder) Quit()
- func (f *Finder) Server(ctx context.Context, notice chan<- *stun.Notice)
- type Finders
- func (f *Finders) Add(node *Finder) error
- func (f *Finders) Dispose(conn *websocket.Conn) *Finder
- func (f *Finders) Get() *Finder
- func (f *Finders) IsFulled() bool
- func (f *Finders) List(count int) []*Finder
- func (f *Finders) Other(old *Finder) *Finder
- func (f *Finders) Size() int
- func (f *Finders) Take() *Finder
- type LinkPeer
- type NatLevel
- type Node
- type Peer
- type PeerList
- type PunchApp
- type Rnd16
- type Shortlist
- func (s *Shortlist) Add(node *Node) error
- func (s *Shortlist) Adds(list ...*Node) int
- func (s *Shortlist) Clean(ctx context.Context)
- func (s *Shortlist) Drop() []*Node
- func (s *Shortlist) IsFulled() bool
- func (s *Shortlist) List(count int) []*Node
- func (s *Shortlist) Size() int
- func (s *Shortlist) Take() *Node
- func (s *Shortlist) Takes(count int) []*Node
- func (s *Shortlist) Unique() int
- type TCPStore
- type TCPStorePool
Constants ¶
const ( NAT_LEVEL_NULL = stun.NAT_LEVEL_NULL NAT_LEVEL_RC = stun.NAT_LEVEL_RC NAT_LEVEL_PRC = stun.NAT_LEVEL_PRC NAT_LEVEL_SYM = stun.NAT_LEVEL_SYM NAT_LEVEL_PRCSYM = stun.NAT_LEVEL_PRCSYM NAT_LEVEL_ERROR = stun.NAT_LEVEL_ERROR )
局部需用常量引用。 注:主要用于 appliers4 类型取成员值。
Variables ¶
var ( // 禁闭查询通道 BanQuery = make(chan *Banner) // 禁闭添加通道 // 单向添加,带缓存无阻塞。 BanAddto = make(chan string, 1) )
var ( // 池已为空。 ErrEmptyPool = errors.New("the pool was empty") // 结束通知 ErrServiceDone = errors.New("service exited successfully") )
var ( // 对端不在线 ErrOnline = errors.New("the target node is offline") // 发送了错误的消息 ErrSendIllegal = errors.New("the client sent an illegal message") // 消息格式错误 ErrMsgFormat = errors.New("invalid message format") // 不支持目标类型应用 ErrAppKind = errors.New("not support the kind of application") )
var ( // 应用端节点池组为空 ErrAppsEmpty = errors.New("the clients pools is empty") // 没有匹配的打洞节点 ErrAppNotFound = errors.New("no matching nodes on STUN service") )
var ErrParseIP = errors.New("parse ip bytes failed")
IP 解析错误。
var File_peer_proto protoreflect.FileDescriptor
var NatNames = []string{ NAT_LEVEL_NULL: "Pub/FullC", NAT_LEVEL_RC: "RC", NAT_LEVEL_PRC: "P-RC", NAT_LEVEL_SYM: "Sym", NAT_LEVEL_PRCSYM: "P-RC|Sym", NAT_LEVEL_ERROR: "Unknown", }
NatNames NAT 类型名集
Functions ¶
func Init ¶
func Init(ctx context.Context, conf *cfg.Config, stakes map[string]string, chpeer <-chan *cfg.Peer, done chan<- struct{})
Init 模块初始化。 根据传入的配置,初始化一些全局变量,启动部分内置全局服务。 @ctx 全局上下文 @conf 用户配置集 @stakes 支持的“服务:权益地址”集 @chpeer 广域搜索节点递送通道 @done 广域搜索终止通知
func Online ¶
Online 检查对端是否在线 向目标连接发送探测消息,检查对端是否回应。 因为是在已有的连接上测试,所以对端无论返回啥消息,都表示在线。 @conn 当前连接 @long 读取等待超时,负值或零表示采用默认值 @return 非nil表示下线
func ProcessOnKind ¶
ProcessOnKind 相应类型处理器。 根据客户端发送的声明信息,提供相应的服务。 顶层有4个服务: - SEEK_ASSISTX 上线协助,提供初始上线的节点一些Findings服务器。 - SEEK_KINDAPP 支持的应用类型探查。方便应用端广域搜寻同类应用端。 - SEEK_FINDNET Finder组网。应当是一个Findings节点请求连入。 - SEEK_APPSERV 应用端寻求服务:NAT 探测或 UDP 打洞协助。 - SEEK_PEERTCP 应用登记自己传递的节点为TCP服务器(同应用类型)。 @kind 应用端声明 @conn 当前TCP连接 @w 原始http写入器
Types ¶
type AppMap ¶
type AppMap struct {
// contains filtered or unexported fields
}
AppMap 应用端集 存储应用端关联节点与应用端自身的映射。 这是一个支持并发的简单封装,用于定向打洞检索目标节点。
type Applier ¶
type Applier struct { *Node // 对端节点 *LinkPeer // 打洞关联节点 Kind string // 应用类别名 Conn *websocket.Conn // 当前连接(TCP) // contains filtered or unexported fields }
Applier 应用端服务员 与 Finder 字段完全相同,但两者所支持的方法集不同。
func NewApplier ¶
NewApplier 创建一个应用端服务员 初始构建时不设置打洞关联节点(LinkPeer)。
func (*Applier) SendPeerUDP ¶
SendPeerUDP 向客户端发送其节点UDP信息。 @addr 客户端的UDP地址
type Appliers ¶
type Appliers struct {
// contains filtered or unexported fields
}
Appliers 应用端服务员缓存池。
func NewAppliers ¶
NewAppliers 创建集合。 size 可以为零或负数,这样就不会创建实例。 比如当前服务器不提供对外应用端服务(NAT 内网 Finder)。 @size 池大小限制 @cleanlen 清理的片段长度。 @net 支持的网络类型(tcp|udp)
func (*Appliers) Clean ¶
Clean 清理缓存池 移除入池时间太久或已经下线的成员。 应用池较大,因此采用并发的清理方式(pool.CleanN)。 @long 指定过期时间长度
type AppliersPool ¶
type AppliersPool map[string]appliers4
AppliersPool 应用服务员池组集 包含任意应用类型,每一个类型对应一个按NAT分类的双协议池组。 key: 应用类型名(kind:name)
func (AppliersPool) Appliers ¶
func (cp AppliersPool) Appliers(kind string, level NatLevel) (*Appliers, error)
Appliers 获取一个应用服务员集。 level: - NAT_LEVEL_NULL - NAT_LEVEL_RC - NAT_LEVEL_PRC - NAT_LEVEL_SYM
@kind 应用类型名(kind:name) @level 目标NAT层级(0 ~ 3) @return 目标类型的节点池
func (AppliersPool) Appliers4 ¶
func (cp AppliersPool) Appliers4(kind string) []*Appliers
AppliersUDP 提取目标类型的UDP打洞信息组 如果不支持目标类型,返回nil。 注: Sym 在 Pub/FullC 主动请求时有用,但单向连接无需打洞。
func (AppliersPool) Init ¶
func (cp AppliersPool) Init(kind string, size int)
Init 初始化应用池组。 每一种应用初始使用时都需要调用该初始化函数。 注意: 不支持并发安全,因此用户需要在程序最开始时初始化自己支持的所有应用。 @kind 应用类型名(kind:name) @size 池大小限制
func (AppliersPool) Supported ¶
func (cp AppliersPool) Supported(kind string) bool
Supported 是否支持目标类型服务。
type Finder ¶
type Finder struct { *Node // TCP 对端节点 Conn *websocket.Conn // 当前 Websocket 连接 // contains filtered or unexported fields }
Finder Findings组网节点 xhost 用于NewHost通知,让本地服务器向对端请求NewHost协作。
func (*Finder) Client ¶
Client 作为客户端启动(连出)。 注:需主动关闭 f.Conn @ctx 当前服务进程上下文 @notice 本地UDP服务器协助通知通道(NewHost)
func (*Finder) NatLevel ¶
NatLevel 请求NAT层级探测服务。 作为客户端,向当前TCP连接的对端请求NAT探测服务。
- 首先请求 STUN:Cone 主服务,
- 视情况决定是否向其它对端请求 STUN:Sym 服务。 注:依然是以应用端连出的对端。
这是一个阻塞的调用,如果未出错,会等到分析出结果才返回。
使用: 初始向对端请求本服务时,需先向服务器声明自己的目的。 即 base.Kind.seek 字段设置为 SEEK_APPSERV。
kind := base.EncodeKind(...) base.EncodeProto(base.COMMAND_KIND, kind)
@return 自身所属的NAT层级
func (*Finder) NatLive ¶
NatLive 请求NAT生存期探测服务。 作为客户端,向当前连接的对端请求NAT生存期探测。 用户通常应当多调用几次本方法,来推算出一个恰当的值。 注意:同上 NatLevel 说明。 @return NAT生命周期(-1表示出错)
type Finders ¶
type Finders struct {
// contains filtered or unexported fields
}
Finders 组网池。 结构和 Shortlist 相同,但方法集稍有差别。
type Node ¶
type Node struct { IP netip.Addr // 对端 IP Port int // 监听/通讯端口 Start time.Time // 加入时间 Ping time.Duration // 节点距离(抵达时长) }
Node 在线节点 记录节点的基本信息,可用于当前连接节点或待创建连接的节点。
func DecodePeers ¶
DecodePeers 解码节点集数据 @data EncodePeers编码的数据。
func NewFromPeer ¶
NewFromPeer 从传输数据Peer构造。 解析错误返回nil(忽略、容错),避免恶意破坏。
func Onlines ¶
Onlines 节点集在线测试 检查目标节点集内的节点是否在线。 测试过程会阻塞进程,达到超时时间后会返回已成功的集合。 如果所有节点都在线,则可能提前返回。 注意: 会记录节点的ping时间,-1值表示不可达。 @nodes 目标节点集 @long 测试超时时间限定,零值表示采用系统默认值 @return 在线的节点集成员
type Peer ¶
type Peer struct { Ip []byte `protobuf:"bytes,1,opt,name=ip,proto3" json:"ip,omitempty"` // 公网 IP Port int32 `protobuf:"varint,2,opt,name=port,proto3" json:"port,omitempty"` // 端口(TCP) // contains filtered or unexported fields }
节点信息 仅有可直连的 NAT:Pub/FullC 类型才会被传递。 因此无需携带 NAT 类型。
func (*Peer) Descriptor
deprecated
func (*Peer) ProtoMessage ¶
func (*Peer) ProtoMessage()
func (*Peer) ProtoReflect ¶
func (x *Peer) ProtoReflect() protoreflect.Message
type PeerList ¶
type PeerList struct { Peers []*Peer `protobuf:"bytes,1,rep,name=peers,proto3" json:"peers,omitempty"` // contains filtered or unexported fields }
节点集定义
func (*PeerList) Descriptor
deprecated
func (*PeerList) ProtoMessage ¶
func (*PeerList) ProtoMessage()
func (*PeerList) ProtoReflect ¶
func (x *PeerList) ProtoReflect() protoreflect.Message
type PunchApp ¶
type PunchApp struct {
// contains filtered or unexported fields
}
PunchApp 打洞应用端 用于登记定向打洞的查询目标节点。
type Shortlist ¶
type Shortlist struct {
// contains filtered or unexported fields
}
Shortlist 候选池类型。
type TCPStore ¶
type TCPStore struct {
// contains filtered or unexported fields
}
TCPStore 应用服务器库 Node存储的是对端提供的TCP服务器的节点信息。 采用快速更新的策略: - 池满即将末尾部分移动到清理游标处。 因此要求池容量大小是单次清理长度的整数倍。
func NewTCPStore ¶
NewTCPStore 新建一个分享池。 池大小应当是单次清理量的整数倍,否则会自动转为整数倍。 池大小至少应当是清理量大小的两倍。 @size 池大小限制 @count 单次清理大小
type TCPStorePool ¶
TCPStorePool 各类TCP分享池集 key: (kind:name)
func (TCPStorePool) Init ¶
func (tp TCPStorePool) Init(kind string, size, count int)
Init 初始化各类型分享池。 @kind 应用类型名(kind:name) @size 分享池大小 @count 单次清理长度
func (TCPStorePool) Supported ¶
func (tp TCPStorePool) Supported(kind string) bool
Supported 是否支持目标类型