From 1c2dea15f9a645623c0a456a21efccf7751441b3 Mon Sep 17 00:00:00 2001 From: xiaoqidun Date: Thu, 22 Jan 2026 16:05:28 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E4=BF=AE=E5=A4=8D=E5=8A=9F=E8=83=BD):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8DIPv6=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- probe_nat.go | 4 +++- probe_socks5.go | 10 ++++++++-- probe_stun.go | 25 ++++++++++++++----------- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/probe_nat.go b/probe_nat.go index d615338..b35b2cf 100644 --- a/probe_nat.go +++ b/probe_nat.go @@ -103,7 +103,9 @@ func performTest(conn net.PacketConn, serverAddr string, network string, timeout return nil, nil, err } txID := [12]byte{} - rand.Read(txID[:]) + if _, err := rand.Read(txID[:]); err != nil { + return nil, nil, err + } req := encodeSTUNRequest(txID, changeIP, changePort) if _, err := conn.WriteTo(req, dst); err != nil { return nil, nil, err diff --git a/probe_socks5.go b/probe_socks5.go index 132a3a7..e5f9afb 100644 --- a/probe_socks5.go +++ b/probe_socks5.go @@ -93,6 +93,8 @@ func (c *socks5PacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) port := binary.BigEndian.Uint16(buf[20:22]) rAddr = &net.UDPAddr{IP: ip, Port: int(port)} dataOffset = 22 + default: + return 0, nil, fmt.Errorf("unknown address type: 0x%x", atyp) } copy(p, buf[dataOffset:n]) return n - dataOffset, rAddr, nil @@ -142,8 +144,12 @@ func (c *socks5PacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) { // Close 关闭连接 // 返回: err 关闭错误 func (c *socks5PacketConn) Close() error { - c.tcpConn.Close() - return c.udpConn.Close() + err1 := c.tcpConn.Close() + err2 := c.udpConn.Close() + if err1 != nil { + return err1 + } + return err2 } // LocalAddr 获取本地地址 diff --git a/probe_stun.go b/probe_stun.go index 5e315aa..2ba49fc 100644 --- a/probe_stun.go +++ b/probe_stun.go @@ -136,9 +136,9 @@ func decodeSTUNResponse(data []byte, txID [12]byte) (*stunMessage, error) { } // parseAddress 解析STUN属性中的地址信息 -// 入参: attrType 属性类型, data 属性值数据 +// 入参: attrType 属性类型, data 属性值数据, txID 事务ID用于XOR解码 // 返回: addr 解析出的UDP地址, err 解析错误信息 -func parseAddress(attrType uint16, data []byte) (*net.UDPAddr, error) { +func parseAddress(attrType uint16, data []byte, txID [12]byte) (*net.UDPAddr, error) { if len(data) < 4 { return nil, errors.New("attribute too short") } @@ -157,11 +157,14 @@ func parseAddress(attrType uint16, data []byte) (*net.UDPAddr, error) { copy(ip, data[4:4+ipLen]) if attrType == attrXorMappedAddress { port ^= uint16(stunMagicCookie >> 16) - if ipLen == 4 { - mc := make([]byte, 4) - binary.BigEndian.PutUint32(mc, stunMagicCookie) - for i := 0; i < 4; i++ { - ip[i] ^= mc[i] + mc := make([]byte, 4) + binary.BigEndian.PutUint32(mc, stunMagicCookie) + for i := 0; i < 4; i++ { + ip[i] ^= mc[i] + } + if ipLen == 16 { + for i := 4; i < 16; i++ { + ip[i] ^= txID[i-4] } } } @@ -173,14 +176,14 @@ func parseAddress(attrType uint16, data []byte) (*net.UDPAddr, error) { func (m *stunMessage) GetMappedAddress() *net.UDPAddr { for _, attr := range m.Attributes { if attr.Type == attrXorMappedAddress { - if addr, err := parseAddress(attr.Type, attr.Value); err == nil { + if addr, err := parseAddress(attr.Type, attr.Value, m.Header.ID); err == nil { return addr } } } for _, attr := range m.Attributes { if attr.Type == attrMappedAddress { - if addr, err := parseAddress(attr.Type, attr.Value); err == nil { + if addr, err := parseAddress(attr.Type, attr.Value, m.Header.ID); err == nil { return addr } } @@ -193,14 +196,14 @@ func (m *stunMessage) GetMappedAddress() *net.UDPAddr { func (m *stunMessage) GetChangedAddress() *net.UDPAddr { for _, attr := range m.Attributes { if attr.Type == attrChangedAddress { - if addr, err := parseAddress(attr.Type, attr.Value); err == nil { + if addr, err := parseAddress(attr.Type, attr.Value, m.Header.ID); err == nil { return addr } } } for _, attr := range m.Attributes { if attr.Type == attrOtherAddress { - if addr, err := parseAddress(attr.Type, attr.Value); err == nil { + if addr, err := parseAddress(attr.Type, attr.Value, m.Header.ID); err == nil { return addr } }