1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00

simplify socks input

This commit is contained in:
V2Ray 2015-09-15 21:40:40 +02:00
parent 6a8014dbef
commit ca79db0de1

View File

@ -5,7 +5,6 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io" "io"
_ "log"
v2net "github.com/v2ray/v2ray-core/net" v2net "github.com/v2ray/v2ray-core/net"
) )
@ -36,7 +35,7 @@ func (request *Socks5AuthenticationRequest) HasAuthMethod(method byte) bool {
} }
func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, err error) { func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, err error) {
buffer := make([]byte, 2) buffer := make([]byte, 256)
nBytes, err := reader.Read(buffer) nBytes, err := reader.Read(buffer)
if err != nil { if err != nil {
return return
@ -58,16 +57,11 @@ func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, err
return return
} }
buffer = make([]byte, auth.nMethods) if nBytes - 2 != int(auth.nMethods) {
nBytes, err = reader.Read(buffer)
if err != nil {
return
}
if nBytes != int(auth.nMethods) {
err = fmt.Errorf("Unmatching number of auth methods, expecting %d, but got %d", auth.nMethods, nBytes) err = fmt.Errorf("Unmatching number of auth methods, expecting %d, but got %d", auth.nMethods, nBytes)
return return
} }
copy(auth.authMethods[:nBytes], buffer) copy(auth.authMethods[:], buffer[2:nBytes])
return return
} }
@ -84,10 +78,7 @@ func NewAuthenticationResponse(authMethod byte) *Socks5AuthenticationResponse {
} }
func (r *Socks5AuthenticationResponse) ToBytes() []byte { func (r *Socks5AuthenticationResponse) ToBytes() []byte {
buffer := make([]byte, 2 /* size of Socks5AuthenticationResponse */) return []byte{r.version, r.authMethod}
buffer[0] = r.version
buffer[1] = r.authMethod
return buffer
} }
func WriteAuthentication(writer io.Writer, response *Socks5AuthenticationResponse) error { func WriteAuthentication(writer io.Writer, response *Socks5AuthenticationResponse) error {