From dd9ef0c65e6546238efd7021992085918384020c Mon Sep 17 00:00:00 2001 From: V2Ray Date: Mon, 7 Sep 2015 17:12:31 +0200 Subject: [PATCH] refactor code --- io/jsonvconfigmarshaller.go | 2 - io/socks/socks.go | 66 +++++++++++++++++++++++++++ io/{vmessreader.go => vmess/vmess.go} | 3 +- 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 io/socks/socks.go rename io/{vmessreader.go => vmess/vmess.go} (90%) diff --git a/io/jsonvconfigmarshaller.go b/io/jsonvconfigmarshaller.go index 1d80b9b68..4261845e0 100644 --- a/io/jsonvconfigmarshaller.go +++ b/io/jsonvconfigmarshaller.go @@ -12,7 +12,6 @@ type JsonVUser struct { } type JsonVConfig struct { - RunAs string `json:"runas"` Port uint8 `json:"port"` Clients []JsonVUser `json:"users"` Protocol string `json:"protocol"` @@ -32,6 +31,5 @@ func (*JsonVConfigUnmarshaller) Unmarshall(data []byte) (*core.VConfig, error) { return nil, err } var vconfig = new(core.VConfig) - vconfig.RunAs = core.VUser{} return vconfig, nil } diff --git a/io/socks/socks.go b/io/socks/socks.go new file mode 100644 index 000000000..347b67ea9 --- /dev/null +++ b/io/socks/socks.go @@ -0,0 +1,66 @@ +// Package socks contains protocol definition and io lib for SOCKS5 protocol +package socks + +import ( + "fmt" + "io" +) + +const ( + socksVersion = uint8(5) +) + +// Authentication request header of Socks5 protocol +type Socks5AuthenticationRequest struct { + version byte + authMethods [256]byte +} + +func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, err error) { + buffer := make([]byte, 2) + nBytes, err := reader.Read(buffer) + if err != nil { + return + } + if nBytes < 2 { + err = fmt.Errorf("Expected 2 bytes read, but actaully %d bytes read", nBytes) + return + } + + auth.version = buffer[0] + if auth.version != socksVersion { + err = fmt.Errorf("Unknown SOCKS version %d", auth.version) + return + } + + nMethods := buffer[1] + if nMethods <= 0 { + err = fmt.Errorf("Zero length of authentication methods") + return + } + + buffer = make([]byte, nMethods) + nBytes, err = reader.Read(buffer) + copy(auth.authMethods[:nBytes], buffer) + return +} + +type Socks5AuthenticationResponse struct { + version byte + authMethod byte +} + +func (r *Socks5AuthenticationResponse) ToBytes() []byte { + buffer := make([]byte, 2 /* size of Socks5AuthenticationResponse */) + buffer[0] = r.version + buffer[1] = r.authMethod + return buffer +} + +func WriteAuthentication(writer io.Writer, response Socks5AuthenticationResponse) error { + _, err := writer.Write(response.ToBytes()) + if err != nil { + return err + } + return nil +} diff --git a/io/vmessreader.go b/io/vmess/vmess.go similarity index 90% rename from io/vmessreader.go rename to io/vmess/vmess.go index 957eeae91..622aed3ca 100644 --- a/io/vmessreader.go +++ b/io/vmess/vmess.go @@ -1,4 +1,5 @@ -package io +// Package vmess contains protocol definition, io lib for VMess. +package vmess import ( "net"