Documentation
¶
Index ¶
- Variables
- func Auto_P2P_TCP_NAT_Traversal(network, sessionUid string, p2pInfo *P2PAddressInfo, needSharedKey bool, ...) (net.Conn, bool, []byte, error)
- func Auto_P2P_UDP_NAT_Traversal(network, sessionUid string, p2pInfo *P2PAddressInfo, needSharedKey bool, ...) (net.Conn, bool, []byte, error)
- func CalculateMD5(input string) string
- func CompareP2PAddresses(info *P2PAddressInfo) (sameNATIP bool, similarLAN bool)
- func ControlTCP(network, address string, c syscall.RawConn) error
- func ControlTCPTTL(network, address string, c syscall.RawConn) error
- func ControlUDP(network, address string, c syscall.RawConn) error
- func GetFreePort() (int, error)
- func GetPublicIP(network, bind string, timeout time.Duration) (index int, localAddr, natAddr string, err error)
- func IsConnRefused(err error) bool
- func IsIPv6(addr string) bool
- func IsPeerSameLAN(conn net.Conn) bool
- func IsSameLAN(ip1, ip2 string) bool
- func IsUnRetryable(err error) bool
- func MQTT_Exchange_Symmetric(sendData, topicSalt, sessionUid string, timeout time.Duration) (recvData string, recvIndex int, err error)
- func MqttPush(msg, sessionUid string, logWriter io.Writer) error
- func MqttWait(sessionUid string, timeout time.Duration, logWriter io.Writer) (string, error)
- func Mqtt_P2P_Round_Sync(sessionUid string, isClient bool, round int, timeout time.Duration, ...) error
- func SelectRole(p2pInfo *P2PAddressInfo) bool
- func SetUDPTTL(conn *net.UDPConn, ttl int) error
- func WrapUnRetryable(err error) error
- type AnalyzedStunResult
- type BoundUDPConn
- func (b *BoundUDPConn) Close() error
- func (b *BoundUDPConn) CloseWrite() error
- func (b *BoundUDPConn) GetLastPacketRemoteAddr() string
- func (b *BoundUDPConn) LocalAddr() net.Addr
- func (b *BoundUDPConn) Read(p []byte) (int, error)
- func (b *BoundUDPConn) ReadFrom(p []byte) (n int, addr net.Addr, err error)
- func (b *BoundUDPConn) Rebuild() (*net.UDPConn, error)
- func (b *BoundUDPConn) RemoteAddr() net.Addr
- func (b *BoundUDPConn) SetDeadline(t time.Time) error
- func (b *BoundUDPConn) SetIdleTimeout(timeout time.Duration)
- func (b *BoundUDPConn) SetReadDeadline(t time.Time) error
- func (b *BoundUDPConn) SetRemoteAddr(addr string) error
- func (b *BoundUDPConn) SetWriteDeadline(t time.Time) error
- func (b *BoundUDPConn) WaitAndLockRemote() error
- func (b *BoundUDPConn) Write(p []byte) (int, error)
- func (b *BoundUDPConn) WriteTo(p []byte, addr net.Addr) (n int, err error)
- type NatIPLocalKey
- type P2PAddressInfo
- type P2PConnInfo
- type PacketConnWrapper
- func (d *PacketConnWrapper) Close() error
- func (d *PacketConnWrapper) LocalAddr() net.Addr
- func (d *PacketConnWrapper) ReadFrom(b []byte) (int, net.Addr, error)
- func (d *PacketConnWrapper) SetDeadline(t time.Time) error
- func (d *PacketConnWrapper) SetReadDeadline(t time.Time) error
- func (d *PacketConnWrapper) SetWriteDeadline(t time.Time) error
- func (d *PacketConnWrapper) WriteTo(b []byte, addr net.Addr) (int, error)
- type STUNResult
- type UDPCustomConn
- func (c *UDPCustomConn) Close() error
- func (c *UDPCustomConn) LocalAddr() net.Addr
- func (c *UDPCustomConn) Read(b []byte) (n int, err error)
- func (c *UDPCustomConn) RemoteAddr() net.Addr
- func (c *UDPCustomConn) SetDeadline(t time.Time) error
- func (c *UDPCustomConn) SetReadDeadline(t time.Time) error
- func (c *UDPCustomConn) SetWriteDeadline(t time.Time) error
- func (c *UDPCustomConn) Write(b []byte) (n int, err error)
- type UDPCustomDialer
- type UDPCustomListener
- type UnRetryableError
Constants ¶
This section is empty.
Variables ¶
var ( TopicExchange = "nat-exchange/" TopicExchangeWait = "nat-exchange-wait/" MQTTBrokerServers []string = []string{ "tcp://broker.hivemq.com:1883", "tcp://broker.emqx.io:1883", "tcp://test.mosquitto.org:1883", } DebugServerRole string PunchingShortTTL int = 5 PunchingRandomPortCount int = 600 UDPIdleTimeoutSecond int = 41 )
var (
STUNServers []string = []string{
"tcp://turn.cloudflare.com:80",
"udp://turn.cloudflare.com:53",
"udp://stun.l.google.com:19302",
"udp://stun.miwifi.com:3478",
"global.turn.twilio.com:3478",
"stun.nextcloud.com:443",
}
)
Functions ¶
func CalculateMD5 ¶
func CompareP2PAddresses ¶
func CompareP2PAddresses(info *P2PAddressInfo) (sameNATIP bool, similarLAN bool)
func GetPublicIP ¶
func GetPublicIP(network, bind string, timeout time.Duration) (index int, localAddr, natAddr string, err error)
GetPublicIP 获取公网IP,返回第一个成功响应的STUN服务器的结果
func IsConnRefused ¶
func IsPeerSameLAN ¶ added in v1.9.4
func IsUnRetryable ¶ added in v1.9.1
func MQTT_Exchange_Symmetric ¶
func Mqtt_P2P_Round_Sync ¶
func SelectRole ¶
func SelectRole(p2pInfo *P2PAddressInfo) bool
func WrapUnRetryable ¶ added in v1.9.1
Types ¶
type AnalyzedStunResult ¶
type BoundUDPConn ¶
type BoundUDPConn struct {
// contains filtered or unexported fields
}
func NewBoundUDPConn ¶
func NewBoundUDPConn(conn net.PacketConn, raddr string, keepOpen bool) *BoundUDPConn
NewBoundUDPConn 创建连接,remoteAddr为nil时允许任意源地址
func (*BoundUDPConn) GetLastPacketRemoteAddr ¶
func (b *BoundUDPConn) GetLastPacketRemoteAddr() string
func (*BoundUDPConn) Rebuild ¶ added in v1.9.4
func (b *BoundUDPConn) Rebuild() (*net.UDPConn, error)
func (*BoundUDPConn) SetDeadline ¶
func (b *BoundUDPConn) SetDeadline(t time.Time) error
SetDeadline 设置读写超时
func (*BoundUDPConn) SetIdleTimeout ¶
func (b *BoundUDPConn) SetIdleTimeout(timeout time.Duration)
SetIdleTimeout 设置最大空闲时间,如果超过这个时间没收到数据,则Read返回错误
func (*BoundUDPConn) SetReadDeadline ¶
func (b *BoundUDPConn) SetReadDeadline(t time.Time) error
SetReadDeadline 设置读超时
func (*BoundUDPConn) SetRemoteAddr ¶
func (b *BoundUDPConn) SetRemoteAddr(addr string) error
SetRemoteAddr 动态设置目标地址
func (*BoundUDPConn) SetWriteDeadline ¶
func (b *BoundUDPConn) SetWriteDeadline(t time.Time) error
SetWriteDeadline 设置写超时
func (*BoundUDPConn) WaitAndLockRemote ¶
func (b *BoundUDPConn) WaitAndLockRemote() error
WaitAndLockRemote 阻塞接收首个包并锁定源地址
type NatIPLocalKey ¶
NatIPLocalKey serves as a key to group results by NAT IP and local address, to check port consistency for 'hard' vs 'symm' behavior within a specific NAT IP.
type P2PAddressInfo ¶
type P2PAddressInfo struct {
Network string
LocalLAN string
LocalNAT string
LocalNATType string
RemoteLAN string
RemoteNAT string
RemoteNATType string
}
func Do_autoP2P ¶
func Do_autoP2PEx ¶
func SortP2PAddressInfos ¶
func SortP2PAddressInfos(addrs []*P2PAddressInfo) []*P2PAddressInfo
SortP2PAddressInfos takes a slice of *P2PAddressInfo pointers, sorts it based on the specified priority, and returns the sorted slice. The original slice is not modified.
type P2PConnInfo ¶ added in v1.9.5
func Easy_P2P ¶
func Easy_P2P(network, sessionUid string, logWriter io.Writer) (*P2PConnInfo, error)
func Easy_P2P_MP ¶ added in v1.9.5
type PacketConnWrapper ¶
type PacketConnWrapper struct {
// contains filtered or unexported fields
}
func NewPacketConnWrapper ¶
func NewPacketConnWrapper(c net.Conn, r net.Addr) *PacketConnWrapper
func (*PacketConnWrapper) Close ¶
func (d *PacketConnWrapper) Close() error
func (*PacketConnWrapper) LocalAddr ¶
func (d *PacketConnWrapper) LocalAddr() net.Addr
func (*PacketConnWrapper) SetDeadline ¶
func (d *PacketConnWrapper) SetDeadline(t time.Time) error
func (*PacketConnWrapper) SetReadDeadline ¶
func (d *PacketConnWrapper) SetReadDeadline(t time.Time) error
func (*PacketConnWrapper) SetWriteDeadline ¶
func (d *PacketConnWrapper) SetWriteDeadline(t time.Time) error
type STUNResult ¶
type STUNResult struct {
Index int // Original index of the STUN server in the input slice
Network string
Local string // Local IP address and port used for the STUN request
Nat string // NAT IP address and port returned by the STUN server
Err error // Error, if any, encountered during the STUN request
}
STUNResult struct holds the outcome of a single STUN request. It's used both internally and as the return type for the function.
func GetNetworksPublicIPs ¶
func GetPublicIPs ¶
func GetPublicIPs(network, bind string, timeout time.Duration, natIPUniq bool) ([]*STUNResult, error)
GetPublicIPs attempts to discover public IP addresses using STUN servers. It collects as many unique NAT IP addresses (by IP address only, ignoring port) as possible within the specified timeout, and returns all results (unique successful ones and errors).
type UDPCustomConn ¶
type UDPCustomConn struct {
// contains filtered or unexported fields
}
UDPCustomConn 结构体定义 (省略大部分方法实现,只保留需要注入logger的部分)
func (*UDPCustomConn) Close ¶
func (c *UDPCustomConn) Close() error
func (*UDPCustomConn) LocalAddr ¶
func (c *UDPCustomConn) LocalAddr() net.Addr
LocalAddr returns the local address for this logical connection. It uses the derived local IP and the shared listener's port.
func (*UDPCustomConn) RemoteAddr ¶
func (c *UDPCustomConn) RemoteAddr() net.Addr
func (*UDPCustomConn) SetDeadline ¶
func (c *UDPCustomConn) SetDeadline(t time.Time) error
func (*UDPCustomConn) SetReadDeadline ¶
func (c *UDPCustomConn) SetReadDeadline(t time.Time) error
func (*UDPCustomConn) SetWriteDeadline ¶
func (c *UDPCustomConn) SetWriteDeadline(t time.Time) error
type UDPCustomDialer ¶
type UDPCustomDialer struct {
// contains filtered or unexported fields
}
UDPCustomDialer 结构体定义 (省略大部分方法实现,只保留需要注入logger的部分)
func NewUDPCustomDialer ¶
func NewUDPCustomDialer(localUDPConn *net.UDPConn, maxPacketSize int, logger *log.Logger) (*UDPCustomDialer, error)
NewUDPCustomDialer 创建一个新的 UDPCustomDialer。
func (*UDPCustomDialer) Close ¶
func (d *UDPCustomDialer) Close() error
func (*UDPCustomDialer) DialContext ¶
type UDPCustomListener ¶ added in v1.9.6
type UDPCustomListener struct {
// contains filtered or unexported fields
}
UDPCustomListener 结构体,模仿 net.Listener
func NewUDPCustomListener ¶ added in v1.9.6
func NewUDPCustomListener(localUDPConn *net.UDPConn, logger *log.Logger) (*UDPCustomListener, error)
NewUDPCustomListener 创建并返回一个 UDPCustomListener。 localAddr 是监听地址,例如 "udp://:8080"
func (*UDPCustomListener) Accept ¶ added in v1.9.6
func (l *UDPCustomListener) Accept() (net.Conn, error)
Accept 实现了 net.Listener 接口的 Accept 方法。 它会阻塞直到有一个新的入站连接被建立。
func (*UDPCustomListener) Addr ¶ added in v1.9.6
func (l *UDPCustomListener) Addr() net.Addr
Addr 返回监听器的本地网络地址。
func (*UDPCustomListener) Close ¶ added in v1.9.6
func (l *UDPCustomListener) Close() error
Close 实现了 net.Listener 接口的 Close 方法。 它关闭底层的 UDPCustomDialer 和 UDPConn。
type UnRetryableError ¶ added in v1.9.1
type UnRetryableError struct {
Err error
}
func (UnRetryableError) Error ¶ added in v1.9.1
func (e UnRetryableError) Error() string
func (UnRetryableError) Unwrap ¶ added in v1.9.1
func (e UnRetryableError) Unwrap() error