chore: using internal socks5.ReadAddr0 in trojan

This commit is contained in:
gVisor bot 2023-05-20 18:35:04 +08:00
parent b59df5ac42
commit 57883b4927
3 changed files with 46 additions and 3 deletions

View File

@ -103,7 +103,7 @@ func (l *Listener) HandleConn(conn net.Conn, in chan<- C.ConnContext, additions
conn = l.pickCipher.StreamConn(conn) conn = l.pickCipher.StreamConn(conn)
conn = N.NewDeadlineConn(conn) // embed ss can't handle readDeadline correctly conn = N.NewDeadlineConn(conn) // embed ss can't handle readDeadline correctly
target, err := socks5.ReadAddr(conn, make([]byte, socks5.MaxAddrLen)) target, err := socks5.ReadAddr0(conn)
if err != nil { if err != nil {
_ = conn.Close() _ = conn.Close()
return return

View File

@ -299,6 +299,50 @@ func ReadAddr(r io.Reader, b []byte) (Addr, error) {
return nil, ErrAddressNotSupported return nil, ErrAddressNotSupported
} }
func ReadAddr0(r io.Reader) (Addr, error) {
aType, err := ReadByte(r) // read 1st byte for address type
if err != nil {
return nil, err
}
switch aType {
case AtypDomainName:
var domainLength byte
domainLength, err = ReadByte(r) // read 2nd byte for domain length
if err != nil {
return nil, err
}
b := make([]byte, 1+1+uint16(domainLength)+2)
_, err = io.ReadFull(r, b[2:])
b[0] = aType
b[1] = domainLength
return b, err
case AtypIPv4:
var b [1 + net.IPv4len + 2]byte
_, err = io.ReadFull(r, b[1:])
b[0] = aType
return b[:], err
case AtypIPv6:
var b [1 + net.IPv6len + 2]byte
_, err = io.ReadFull(r, b[1:])
b[0] = aType
return b[:], err
}
return nil, ErrAddressNotSupported
}
func ReadByte(reader io.Reader) (byte, error) {
if br, isBr := reader.(io.ByteReader); isBr {
return br.ReadByte()
}
var b [1]byte
if _, err := io.ReadFull(reader, b[:]); err != nil {
return 0, err
}
return b[0], nil
}
// SplitAddr slices a SOCKS address from beginning of b. Returns nil if failed. // SplitAddr slices a SOCKS address from beginning of b. Returns nil if failed.
func SplitAddr(b []byte) Addr { func SplitAddr(b []byte) Addr {
addrLen := 1 addrLen := 1

View File

@ -21,7 +21,6 @@ import (
"github.com/Dreamacro/clash/transport/vless" "github.com/Dreamacro/clash/transport/vless"
"github.com/Dreamacro/clash/transport/vmess" "github.com/Dreamacro/clash/transport/vmess"
M "github.com/sagernet/sing/common/metadata"
xtls "github.com/xtls/go" xtls "github.com/xtls/go"
) )
@ -358,7 +357,7 @@ func (pc *PacketConn) WaitReadFrom() (data []byte, put func(), addr net.Addr, er
pc.mux.Lock() pc.mux.Lock()
defer pc.mux.Unlock() defer pc.mux.Unlock()
destination, err := M.SocksaddrSerializer.ReadAddrPort(pc.Conn) destination, err := socks5.ReadAddr0(pc.Conn)
if err != nil { if err != nil {
return nil, nil, nil, err return nil, nil, nil, err
} }