shadowsocks / go-shadowsocks2

Modern Shadowsocks in Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SOCKS5握手问题

damoye opened this issue · comments

commented
// Handshake fast-tracks SOCKS initialization to get target address to connect.
func Handshake(rw io.ReadWriter) (Addr, error) {
	// Read RFC 1928 section 4 for request and reply structure and sizes
	buf := make([]byte, MaxReqLen)

	_, err := rw.Read(buf) // SOCKS version and auth methods
	if err != nil {
		return nil, err
	}

	_, err = rw.Write([]byte{5, 0}) // SOCKS v5, no auth required
	if err != nil {
		return nil, err
	}

	n, err := rw.Read(buf) // SOCKS request: VER, CMD, RSV, Addr
	if err != nil {
		return nil, err
	}
	buf = buf[:n]

	if buf[1] != CmdConnect {
		return nil, ErrCommandNotSupported
	}

	_, err = rw.Write([]byte{5, 0, 0, 1, 0, 0, 0, 0, 0, 0}) // SOCKS v5, reply succeeded
	return buf[3:], err                                     // skip VER, CMD, RSV fields
}

上面SOCKS5握手的代码是不是太简单了,Read的时候没有判断是不是已经读到了想要的数据量,比如可以用io.ReadFull。

commented

Yeah, it was indeed naive. A pull request is welcome :)

commented

@riobard Please check this pull request: #52

commented

Close by #52