Skip to content

Commit

Permalink
chore: using internal socks5.ReadAddr0 in trojan
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed May 20, 2023
1 parent 546b2bc commit 984bf27
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion listener/shadowsocks/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (l *Listener) HandleConn(conn net.Conn, in chan<- C.ConnContext, additions
conn = l.pickCipher.StreamConn(conn)
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 {
_ = conn.Close()
return
Expand Down
44 changes: 44 additions & 0 deletions transport/socks5/socks5.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,50 @@ func ReadAddr(r io.Reader, b []byte) (Addr, error) {
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.
func SplitAddr(b []byte) Addr {
addrLen := 1
Expand Down
3 changes: 1 addition & 2 deletions transport/trojan/trojan.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/Dreamacro/clash/transport/vless"
"github.com/Dreamacro/clash/transport/vmess"

M "github.com/sagernet/sing/common/metadata"
xtls "github.com/xtls/go"
)

Expand Down Expand Up @@ -358,7 +357,7 @@ func (pc *PacketConn) WaitReadFrom() (data []byte, put func(), addr net.Addr, er
pc.mux.Lock()
defer pc.mux.Unlock()

destination, err := M.SocksaddrSerializer.ReadAddrPort(pc.Conn)
destination, err := socks5.ReadAddr0(pc.Conn)
if err != nil {
return nil, nil, nil, err
}
Expand Down

0 comments on commit 984bf27

Please sign in to comment.