1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-27 01:45:23 +00:00

format code

This commit is contained in:
V2Ray 2015-09-07 17:16:30 +02:00
parent dd9ef0c65e
commit bec765a2fe

View File

@ -2,65 +2,65 @@
package socks package socks
import ( import (
"fmt" "fmt"
"io" "io"
) )
const ( const (
socksVersion = uint8(5) socksVersion = uint8(5)
) )
// Authentication request header of Socks5 protocol // Authentication request header of Socks5 protocol
type Socks5AuthenticationRequest struct { type Socks5AuthenticationRequest struct {
version byte version byte
authMethods [256]byte authMethods [256]byte
} }
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, 2)
nBytes, err := reader.Read(buffer) nBytes, err := reader.Read(buffer)
if err != nil { if err != nil {
return return
} }
if nBytes < 2 { if nBytes < 2 {
err = fmt.Errorf("Expected 2 bytes read, but actaully %d bytes read", nBytes) err = fmt.Errorf("Expected 2 bytes read, but actaully %d bytes read", nBytes)
return return
} }
auth.version = buffer[0] auth.version = buffer[0]
if auth.version != socksVersion { if auth.version != socksVersion {
err = fmt.Errorf("Unknown SOCKS version %d", auth.version) err = fmt.Errorf("Unknown SOCKS version %d", auth.version)
return return
} }
nMethods := buffer[1] nMethods := buffer[1]
if nMethods <= 0 { if nMethods <= 0 {
err = fmt.Errorf("Zero length of authentication methods") err = fmt.Errorf("Zero length of authentication methods")
return return
} }
buffer = make([]byte, nMethods) buffer = make([]byte, nMethods)
nBytes, err = reader.Read(buffer) nBytes, err = reader.Read(buffer)
copy(auth.authMethods[:nBytes], buffer) copy(auth.authMethods[:nBytes], buffer)
return return
} }
type Socks5AuthenticationResponse struct { type Socks5AuthenticationResponse struct {
version byte version byte
authMethod byte authMethod byte
} }
func (r *Socks5AuthenticationResponse) ToBytes() []byte { func (r *Socks5AuthenticationResponse) ToBytes() []byte {
buffer := make([]byte, 2 /* size of Socks5AuthenticationResponse */) buffer := make([]byte, 2 /* size of Socks5AuthenticationResponse */)
buffer[0] = r.version buffer[0] = r.version
buffer[1] = r.authMethod buffer[1] = r.authMethod
return buffer return buffer
} }
func WriteAuthentication(writer io.Writer, response Socks5AuthenticationResponse) error { func WriteAuthentication(writer io.Writer, response Socks5AuthenticationResponse) error {
_, err := writer.Write(response.ToBytes()) _, err := writer.Write(response.ToBytes())
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }