easyp2p

package
v1.9.7 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2025 License: MIT Imports: 30 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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
)
View Source
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 Auto_P2P_TCP_NAT_Traversal

func Auto_P2P_TCP_NAT_Traversal(network, sessionUid string, p2pInfo *P2PAddressInfo, needSharedKey bool, round int, logWriter io.Writer) (net.Conn, bool, []byte, error)

func Auto_P2P_UDP_NAT_Traversal

func Auto_P2P_UDP_NAT_Traversal(network, sessionUid string, p2pInfo *P2PAddressInfo, needSharedKey bool, round int, logWriter io.Writer) (net.Conn, bool, []byte, error)

func CalculateMD5

func CalculateMD5(input string) string

func CompareP2PAddresses

func CompareP2PAddresses(info *P2PAddressInfo) (sameNATIP bool, similarLAN bool)

func ControlTCP

func ControlTCP(network, address string, c syscall.RawConn) error

func ControlTCPTTL

func ControlTCPTTL(network, address string, c syscall.RawConn) error

func ControlUDP

func ControlUDP(network, address string, c syscall.RawConn) error

func GetFreePort

func GetFreePort() (int, error)

GetFreePort 尝试找到一个可同时绑定 TCP 和 UDP 的端口

func GetPublicIP

func GetPublicIP(network, bind string, timeout time.Duration) (index int, localAddr, natAddr string, err error)

GetPublicIP 获取公网IP,返回第一个成功响应的STUN服务器的结果

func IsConnRefused

func IsConnRefused(err error) bool

func IsIPv6

func IsIPv6(addr string) bool

func IsPeerSameLAN added in v1.9.4

func IsPeerSameLAN(conn net.Conn) bool

func IsSameLAN

func IsSameLAN(ip1, ip2 string) bool

func IsUnRetryable added in v1.9.1

func IsUnRetryable(err error) bool

func MQTT_Exchange_Symmetric

func MQTT_Exchange_Symmetric(sendData, topicSalt, sessionUid string, timeout time.Duration) (recvData string, recvIndex int, err error)

func MqttPush

func MqttPush(msg, sessionUid string, logWriter io.Writer) error

func MqttWait

func MqttWait(sessionUid string, timeout time.Duration, logWriter io.Writer) (string, error)

func Mqtt_P2P_Round_Sync

func Mqtt_P2P_Round_Sync(sessionUid string, isClient bool, round int, timeout time.Duration, logWriter io.Writer) error

func SelectRole

func SelectRole(p2pInfo *P2PAddressInfo) bool

func SetUDPTTL

func SetUDPTTL(conn *net.UDPConn, ttl int) error

func WrapUnRetryable added in v1.9.1

func WrapUnRetryable(err error) error

Types

type AnalyzedStunResult

type AnalyzedStunResult struct {
	NATType string `json:"nattype"` // "easy", "hard", "symm"
	Network string `json:"network"`
	LAN     string `json:"lan"`
	NAT     string `json:"nat"`
}

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) Close

func (b *BoundUDPConn) Close() error

Close 全关闭(保持不变)

func (*BoundUDPConn) CloseWrite

func (b *BoundUDPConn) CloseWrite() error

CloseWrite 半关闭(保持不变)

func (*BoundUDPConn) GetLastPacketRemoteAddr

func (b *BoundUDPConn) GetLastPacketRemoteAddr() string

func (*BoundUDPConn) LocalAddr

func (b *BoundUDPConn) LocalAddr() net.Addr

LocalAddr 返回本地地址

func (*BoundUDPConn) Read

func (b *BoundUDPConn) Read(p []byte) (int, error)

func (*BoundUDPConn) ReadFrom added in v1.9.6

func (b *BoundUDPConn) ReadFrom(p []byte) (n int, addr net.Addr, err error)

func (*BoundUDPConn) Rebuild added in v1.9.4

func (b *BoundUDPConn) Rebuild() (*net.UDPConn, error)

func (*BoundUDPConn) RemoteAddr

func (b *BoundUDPConn) RemoteAddr() net.Addr

RemoteAddr 返回绑定的远端地址

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 阻塞接收首个包并锁定源地址

func (*BoundUDPConn) Write

func (b *BoundUDPConn) Write(p []byte) (int, error)

Write 发送数据(线程安全版)

func (*BoundUDPConn) WriteTo added in v1.9.6

func (b *BoundUDPConn) WriteTo(p []byte, addr net.Addr) (n int, err error)

type NatIPLocalKey

type NatIPLocalKey struct {
	Network string
	Local   string
	NATIP   string // Only NAT IP part
}

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
	SharedKey     [32]byte
}

func Do_autoP2P

func Do_autoP2P(network string, sessionUid string, stunServers, brokerServers []string, timeout time.Duration, needSharedKey bool, logWriter io.Writer) (*P2PAddressInfo, error)

func Do_autoP2PEx

func Do_autoP2PEx(networks []string, sessionUid string, timeout time.Duration, needSharedKey bool, logWriter io.Writer) ([]*P2PAddressInfo, error)

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

type P2PConnInfo struct {
	Conns     []net.Conn
	SharedKey [32]byte
	IsClient  bool
}

func Easy_P2P

func Easy_P2P(network, sessionUid string, logWriter io.Writer) (*P2PConnInfo, error)

func Easy_P2P_MP added in v1.9.5

func Easy_P2P_MP(network, sessionUid string, multipathEnabled bool, logWriter io.Writer) (*P2PConnInfo, error)

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) ReadFrom

func (d *PacketConnWrapper) ReadFrom(b []byte) (int, net.Addr, error)

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

func (*PacketConnWrapper) WriteTo

func (d *PacketConnWrapper) WriteTo(b []byte, addr net.Addr) (int, 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 GetNetworksPublicIPs(networkList []string, bind string, timeout time.Duration) ([]*STUNResult, error)

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) Read

func (c *UDPCustomConn) Read(b []byte) (n int, err error)

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

func (*UDPCustomConn) Write

func (c *UDPCustomConn) Write(b []byte) (n int, err 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

func (d *UDPCustomDialer) DialContext(ctx context.Context, network string, address string) (net.Conn, error)

func (*UDPCustomDialer) DialUDP

func (d *UDPCustomDialer) DialUDP(network string, remoteAddr *net.UDPAddr) (net.Conn, error)

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

Jump to

Keyboard shortcuts

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