simplify socks input

pull/182/head^2
V2Ray 9 years ago
parent 6a8014dbef
commit ca79db0de1

@ -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 {

Loading…
Cancel
Save