mirror of
https://github.com/xiaoqidun/probe.git
synced 2026-01-29 04:58:46 +08:00
fix(修复功能): 修复IPv6支持
This commit is contained in:
+3
-1
@@ -103,7 +103,9 @@ func performTest(conn net.PacketConn, serverAddr string, network string, timeout
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
txID := [12]byte{}
|
txID := [12]byte{}
|
||||||
rand.Read(txID[:])
|
if _, err := rand.Read(txID[:]); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
req := encodeSTUNRequest(txID, changeIP, changePort)
|
req := encodeSTUNRequest(txID, changeIP, changePort)
|
||||||
if _, err := conn.WriteTo(req, dst); err != nil {
|
if _, err := conn.WriteTo(req, dst); err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
|
|||||||
+8
-2
@@ -93,6 +93,8 @@ func (c *socks5PacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error)
|
|||||||
port := binary.BigEndian.Uint16(buf[20:22])
|
port := binary.BigEndian.Uint16(buf[20:22])
|
||||||
rAddr = &net.UDPAddr{IP: ip, Port: int(port)}
|
rAddr = &net.UDPAddr{IP: ip, Port: int(port)}
|
||||||
dataOffset = 22
|
dataOffset = 22
|
||||||
|
default:
|
||||||
|
return 0, nil, fmt.Errorf("unknown address type: 0x%x", atyp)
|
||||||
}
|
}
|
||||||
copy(p, buf[dataOffset:n])
|
copy(p, buf[dataOffset:n])
|
||||||
return n - dataOffset, rAddr, nil
|
return n - dataOffset, rAddr, nil
|
||||||
@@ -142,8 +144,12 @@ func (c *socks5PacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
|
|||||||
// Close 关闭连接
|
// Close 关闭连接
|
||||||
// 返回: err 关闭错误
|
// 返回: err 关闭错误
|
||||||
func (c *socks5PacketConn) Close() error {
|
func (c *socks5PacketConn) Close() error {
|
||||||
c.tcpConn.Close()
|
err1 := c.tcpConn.Close()
|
||||||
return c.udpConn.Close()
|
err2 := c.udpConn.Close()
|
||||||
|
if err1 != nil {
|
||||||
|
return err1
|
||||||
|
}
|
||||||
|
return err2
|
||||||
}
|
}
|
||||||
|
|
||||||
// LocalAddr 获取本地地址
|
// LocalAddr 获取本地地址
|
||||||
|
|||||||
+10
-7
@@ -136,9 +136,9 @@ func decodeSTUNResponse(data []byte, txID [12]byte) (*stunMessage, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// parseAddress 解析STUN属性中的地址信息
|
// parseAddress 解析STUN属性中的地址信息
|
||||||
// 入参: attrType 属性类型, data 属性值数据
|
// 入参: attrType 属性类型, data 属性值数据, txID 事务ID用于XOR解码
|
||||||
// 返回: addr 解析出的UDP地址, err 解析错误信息
|
// 返回: 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 {
|
if len(data) < 4 {
|
||||||
return nil, errors.New("attribute too short")
|
return nil, errors.New("attribute too short")
|
||||||
}
|
}
|
||||||
@@ -157,12 +157,15 @@ func parseAddress(attrType uint16, data []byte) (*net.UDPAddr, error) {
|
|||||||
copy(ip, data[4:4+ipLen])
|
copy(ip, data[4:4+ipLen])
|
||||||
if attrType == attrXorMappedAddress {
|
if attrType == attrXorMappedAddress {
|
||||||
port ^= uint16(stunMagicCookie >> 16)
|
port ^= uint16(stunMagicCookie >> 16)
|
||||||
if ipLen == 4 {
|
|
||||||
mc := make([]byte, 4)
|
mc := make([]byte, 4)
|
||||||
binary.BigEndian.PutUint32(mc, stunMagicCookie)
|
binary.BigEndian.PutUint32(mc, stunMagicCookie)
|
||||||
for i := 0; i < 4; i++ {
|
for i := 0; i < 4; i++ {
|
||||||
ip[i] ^= mc[i]
|
ip[i] ^= mc[i]
|
||||||
}
|
}
|
||||||
|
if ipLen == 16 {
|
||||||
|
for i := 4; i < 16; i++ {
|
||||||
|
ip[i] ^= txID[i-4]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return &net.UDPAddr{IP: ip, Port: int(port)}, nil
|
return &net.UDPAddr{IP: ip, Port: int(port)}, nil
|
||||||
@@ -173,14 +176,14 @@ func parseAddress(attrType uint16, data []byte) (*net.UDPAddr, error) {
|
|||||||
func (m *stunMessage) GetMappedAddress() *net.UDPAddr {
|
func (m *stunMessage) GetMappedAddress() *net.UDPAddr {
|
||||||
for _, attr := range m.Attributes {
|
for _, attr := range m.Attributes {
|
||||||
if attr.Type == attrXorMappedAddress {
|
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
|
return addr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, attr := range m.Attributes {
|
for _, attr := range m.Attributes {
|
||||||
if attr.Type == attrMappedAddress {
|
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
|
return addr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -193,14 +196,14 @@ func (m *stunMessage) GetMappedAddress() *net.UDPAddr {
|
|||||||
func (m *stunMessage) GetChangedAddress() *net.UDPAddr {
|
func (m *stunMessage) GetChangedAddress() *net.UDPAddr {
|
||||||
for _, attr := range m.Attributes {
|
for _, attr := range m.Attributes {
|
||||||
if attr.Type == attrChangedAddress {
|
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
|
return addr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, attr := range m.Attributes {
|
for _, attr := range m.Attributes {
|
||||||
if attr.Type == attrOtherAddress {
|
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
|
return addr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user