diff --git a/adapters/outbound/shadowsocks.go b/adapters/outbound/shadowsocks.go index 16c2dc7e..5e748f1c 100644 --- a/adapters/outbound/shadowsocks.go +++ b/adapters/outbound/shadowsocks.go @@ -4,6 +4,7 @@ import ( "context" "crypto/tls" "encoding/json" + "errors" "fmt" "net" "strconv" @@ -209,7 +210,12 @@ func (spc *ssPacketConn) ReadFrom(b []byte) (int, net.Addr, error) { if e != nil { return 0, nil, e } + addr := socks5.SplitAddr(b[:n]) + if addr == nil { + return 0, nil, errors.New("parse addr error") + } + copy(b, b[len(addr):]) return n - len(addr), addr.UDPAddr(), e } diff --git a/adapters/outbound/socks5.go b/adapters/outbound/socks5.go index 41294f7c..ed976a6a 100644 --- a/adapters/outbound/socks5.go +++ b/adapters/outbound/socks5.go @@ -161,7 +161,7 @@ func (uc *socksPacketConn) WriteWithMetadata(p []byte, metadata *C.Metadata) (n } func (uc *socksPacketConn) ReadFrom(b []byte) (int, net.Addr, error) { - n, a, e := uc.PacketConn.ReadFrom(b) + n, _, e := uc.PacketConn.ReadFrom(b) if e != nil { return 0, nil, e } @@ -170,9 +170,8 @@ func (uc *socksPacketConn) ReadFrom(b []byte) (int, net.Addr, error) { return 0, nil, err } // due to DecodeUDPPacket is mutable, record addr length - addrLength := len(addr) copy(b, payload) - return n - addrLength - 3, a, nil + return n - len(addr) - 3, addr.UDPAddr(), nil } func (uc *socksPacketConn) Close() error {