Implementation read waiter for socks5 UDP and UoT

This commit is contained in:
世界 2023-12-06 20:16:37 +08:00
parent ae8098ad39
commit aa34723225
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
4 changed files with 97 additions and 1 deletions

View file

@ -7,6 +7,7 @@ import (
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
@ -17,6 +18,8 @@ import (
// | 2 | 1 | 1 | Variable | 2 | Variable |
// +----+------+------+----------+----------+----------+
var ErrInvalidPacket = E.New("socks5: invalid packet")
type AssociatePacketConn struct {
N.NetPacketConn
remoteAddr M.Socksaddr
@ -31,6 +34,7 @@ func NewAssociatePacketConn(conn net.PacketConn, remoteAddr M.Socksaddr, underly
}
}
// Deprecated: NewAssociatePacketConn(bufio.NewUnbindPacketConn(conn), remoteAddr, underlying) instead.
func NewAssociateConn(conn net.Conn, remoteAddr M.Socksaddr, underlying net.Conn) *AssociatePacketConn {
return &AssociatePacketConn{
NetPacketConn: bufio.NewUnbindPacketConn(conn),
@ -49,6 +53,9 @@ func (c *AssociatePacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err erro
if err != nil {
return
}
if n < 3 {
return 0, nil, ErrInvalidPacket
}
c.remoteAddr = M.SocksaddrFromNet(addr)
reader := bytes.NewReader(p[3:n])
destination, err := M.SocksaddrSerializer.ReadAddrPort(reader)
@ -92,6 +99,9 @@ func (c *AssociatePacketConn) ReadPacket(buffer *buf.Buffer) (destination M.Sock
if err != nil {
return M.Socksaddr{}, err
}
if buffer.Len() < 3 {
return M.Socksaddr{}, ErrInvalidPacket
}
c.remoteAddr = destination
buffer.Advance(3)
destination, err = M.SocksaddrSerializer.ReadAddrPort(buffer)