From 54fe1ed8b1c7c89555c1848ea3f535e7bbf52dbe Mon Sep 17 00:00:00 2001 From: V2Ray Date: Wed, 16 Sep 2015 00:06:22 +0200 Subject: [PATCH] format code --- id.go | 16 +++++----- io/socks/socks.go | 70 +++++++++++++++++++++--------------------- io/vmess/vmess.go | 10 +++--- io/vmess/vmess_test.go | 2 +- net/socks/socks.go | 44 +++++++++++++------------- net/vmess/config.go | 10 +++--- net/vmess/vmessin.go | 2 +- net/vmess/vmessout.go | 42 ++++++++++++------------- release/server/main.go | 24 +++++++-------- userset.go | 18 +++++------ 10 files changed, 119 insertions(+), 119 deletions(-) diff --git a/id.go b/id.go index 8535aec0f..d00b3395a 100644 --- a/id.go +++ b/id.go @@ -26,8 +26,8 @@ func NewID(id string) (ID, error) { if err != nil { return ID{}, log.Error("Failed to parse id %s", id) } - - md5hash := md5.New() + + md5hash := md5.New() md5hash.Write(idBytes) md5hash.Write([]byte("c48619fe-8f02-49e0-b9e9-edf763e17e21")) cmdKey := md5.Sum(nil) @@ -58,7 +58,7 @@ func (v ID) TimeHash(timeSec int64) []byte { } func (v ID) Hash(data []byte) []byte { - hasher := hmac.New(md5.New, v.Bytes) + hasher := hmac.New(md5.New, v.Bytes) hasher.Write(data) return hasher.Sum(nil) } @@ -68,8 +68,8 @@ func (v ID) CmdKey() []byte { } func TimestampHash(timeSec int64) []byte { - md5hash := md5.New() - buffer := []byte{ + md5hash := md5.New() + buffer := []byte{ byte(timeSec >> 56), byte(timeSec >> 48), byte(timeSec >> 40), @@ -80,9 +80,9 @@ func TimestampHash(timeSec int64) []byte { byte(timeSec), } md5hash.Write(buffer) - md5hash.Write(buffer) - md5hash.Write(buffer) - md5hash.Write(buffer) + md5hash.Write(buffer) + md5hash.Write(buffer) + md5hash.Write(buffer) return md5hash.Sum(nil) } diff --git a/io/socks/socks.go b/io/socks/socks.go index c0432d28e..bfbdfb9d7 100644 --- a/io/socks/socks.go +++ b/io/socks/socks.go @@ -57,7 +57,7 @@ func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, err return } - if nBytes - 2 != int(auth.nMethods) { + if nBytes-2 != int(auth.nMethods) { err = fmt.Errorf("Unmatching number of auth methods, expecting %d, but got %d", auth.nMethods, nBytes) return } @@ -78,7 +78,7 @@ func NewAuthenticationResponse(authMethod byte) *Socks5AuthenticationResponse { } func (r *Socks5AuthenticationResponse) ToBytes() []byte { - return []byte{r.version, r.authMethod} + return []byte{r.version, r.authMethod} } func WriteAuthentication(writer io.Writer, response *Socks5AuthenticationResponse) error { @@ -87,54 +87,54 @@ func WriteAuthentication(writer io.Writer, response *Socks5AuthenticationRespons } type Socks5UserPassRequest struct { - version byte - username string - password string + version byte + username string + password string } func (request Socks5UserPassRequest) IsValid(username string, password string) bool { - return request.username == username && request.password == password + return request.username == username && request.password == password } func ReadUserPassRequest(reader io.Reader) (request Socks5UserPassRequest, err error) { - buffer := make([]byte, 256) - _, err = reader.Read(buffer[0:2]) - if err != nil { - return - } - request.version = buffer[0] - nUsername := buffer[1] - nBytes, err := reader.Read(buffer[:nUsername]) - if err != nil { - return - } - request.username = string(buffer[:nBytes]) - - _, err = reader.Read(buffer[0:1]) - if err != nil { - return - } - nPassword := buffer[0] - nBytes, err = reader.Read(buffer[:nPassword]) - if err != nil { - return - } - request.password = string(buffer[:nBytes]) - return + buffer := make([]byte, 256) + _, err = reader.Read(buffer[0:2]) + if err != nil { + return + } + request.version = buffer[0] + nUsername := buffer[1] + nBytes, err := reader.Read(buffer[:nUsername]) + if err != nil { + return + } + request.username = string(buffer[:nBytes]) + + _, err = reader.Read(buffer[0:1]) + if err != nil { + return + } + nPassword := buffer[0] + nBytes, err = reader.Read(buffer[:nPassword]) + if err != nil { + return + } + request.password = string(buffer[:nBytes]) + return } type Socks5UserPassResponse struct { - version byte - status byte + version byte + status byte } func NewSocks5UserPassResponse(status byte) Socks5UserPassResponse { - return Socks5UserPassResponse{socksVersion, status} + return Socks5UserPassResponse{socksVersion, status} } func WriteUserPassResponse(writer io.Writer, response Socks5UserPassResponse) error { - _, err := writer.Write([]byte{response.version, response.status}) - return err + _, err := writer.Write([]byte{response.version, response.status}) + return err } const ( diff --git a/io/vmess/vmess.go b/io/vmess/vmess.go index aa6e43522..2f5ff895a 100644 --- a/io/vmess/vmess.go +++ b/io/vmess/vmess.go @@ -65,8 +65,8 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) { if err != nil { return nil, err } - - log.Debug("Read user hash: %v", buffer[:nBytes]) + + log.Debug("Read user hash: %v", buffer[:nBytes]) userId, timeSec, valid := r.vUserSet.GetUser(buffer[:nBytes]) if !valid { @@ -187,9 +187,9 @@ func NewVMessRequestWriter() *VMessRequestWriter { func (w *VMessRequestWriter) Write(writer io.Writer, request *VMessRequest) error { buffer := make([]byte, 0, 300) - userHash, timeSec := request.UserId.TimeRangeHash(30) - - log.Debug("Writing userhash: %v", userHash) + userHash, timeSec := request.UserId.TimeRangeHash(30) + + log.Debug("Writing userhash: %v", userHash) buffer = append(buffer, userHash...) encryptionBegin := len(buffer) diff --git a/io/vmess/vmess_test.go b/io/vmess/vmess_test.go index 4a4817ed8..85a3424bd 100644 --- a/io/vmess/vmess_test.go +++ b/io/vmess/vmess_test.go @@ -13,7 +13,7 @@ import ( ) func TestVMessSerialization(t *testing.T) { - t.Skip(); + t.Skip() assert := unit.Assert(t) userId, err := core.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51") diff --git a/net/socks/socks.go b/net/socks/socks.go index c25826e76..bd1dea334 100644 --- a/net/socks/socks.go +++ b/net/socks/socks.go @@ -1,9 +1,9 @@ package socks import ( - "bufio" + "bufio" "errors" - "io" + "io" "net" "strconv" @@ -16,7 +16,7 @@ import ( var ( ErrorAuthenticationFailed = errors.New("None of the authentication methods is allowed.") ErrorCommandNotSupported = errors.New("Client requested an unsupported command.") - ErrorInvalidUser = errors.New("Invalid username or password.") + ErrorInvalidUser = errors.New("Invalid username or password.") ) // SocksServer is a SOCKS 5 proxy server @@ -63,8 +63,8 @@ func (server *SocksServer) AcceptConnections(listener net.Listener) error { func (server *SocksServer) HandleConnection(connection net.Conn) error { defer connection.Close() - - reader := bufio.NewReader(connection) + + reader := bufio.NewReader(connection) auth, err := socksio.ReadAuthentication(reader) if err != nil { @@ -87,23 +87,23 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error { authResponse := socksio.NewAuthenticationResponse(expectedAuthMethod) socksio.WriteAuthentication(connection, authResponse) - - if server.config.AuthMethod == JsonAuthMethodUserPass { - upRequest, err := socksio.ReadUserPassRequest(reader) - if err != nil { - log.Error("Failed to read username and password: %v", err) - return err - } - status := byte(0) - if ! upRequest.IsValid(server.config.Username, server.config.Password) { - status = byte(0xFF) - } - upResponse := socksio.NewSocks5UserPassResponse(status) - socksio.WriteUserPassResponse(connection, upResponse) - if status != byte(0) { - return ErrorInvalidUser - } - } + + if server.config.AuthMethod == JsonAuthMethodUserPass { + upRequest, err := socksio.ReadUserPassRequest(reader) + if err != nil { + log.Error("Failed to read username and password: %v", err) + return err + } + status := byte(0) + if !upRequest.IsValid(server.config.Username, server.config.Password) { + status = byte(0xFF) + } + upResponse := socksio.NewSocks5UserPassResponse(status) + socksio.WriteUserPassResponse(connection, upResponse) + if status != byte(0) { + return ErrorInvalidUser + } + } request, err := socksio.ReadRequest(reader) if err != nil { diff --git a/net/vmess/config.go b/net/vmess/config.go index 2db355299..bfd50834a 100644 --- a/net/vmess/config.go +++ b/net/vmess/config.go @@ -2,7 +2,7 @@ package vmess import ( "encoding/json" - "net" + "net" "github.com/v2ray/v2ray-core" "github.com/v2ray/v2ray-core/log" @@ -44,10 +44,10 @@ func (config VNextConfig) ToVNextServer() VNextServer { } users = append(users, vuser) } - ip := net.ParseIP(config.Address) - if ip == nil { - panic(log.Error("Unable to parse VNext IP: %s", config.Address)) - } + ip := net.ParseIP(config.Address) + if ip == nil { + panic(log.Error("Unable to parse VNext IP: %s", config.Address)) + } return VNextServer{ v2net.IPAddress(ip, config.Port), users} diff --git a/net/vmess/vmessin.go b/net/vmess/vmessin.go index 22e4a451c..d35006b69 100644 --- a/net/vmess/vmessin.go +++ b/net/vmess/vmessin.go @@ -55,7 +55,7 @@ func (handler *VMessInboundHandler) HandleConnection(connection net.Conn) error request, err := reader.Read(connection) if err != nil { - log.Debug("Failed to parse VMess request: %v", err) + log.Debug("Failed to parse VMess request: %v", err) return err } log.Debug("Received request for %s", request.Address.String()) diff --git a/net/vmess/vmessout.go b/net/vmess/vmessout.go index a4b874ca5..8263f9937 100644 --- a/net/vmess/vmessout.go +++ b/net/vmess/vmessout.go @@ -66,24 +66,24 @@ func (handler *VMessOutboundHandler) Start(ray core.OutboundRay) error { } func startCommunicate(request *vmessio.VMessRequest, dest v2net.Address, ray core.OutboundRay) error { - input := ray.OutboundInput() + input := ray.OutboundInput() output := ray.OutboundOutput() - + conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{dest.IP, int(dest.Port), ""}) log.Debug("VMessOutbound dialing tcp: %s", dest.String()) if err != nil { log.Error("Failed to open tcp (%s): %v", dest.String(), err) - close(output) + close(output) return err } defer conn.Close() - defer close(output) - - requestFinish := make(chan bool) - responseFinish := make(chan bool) - - go handleRequest(conn, request, input, requestFinish) - go handleResponse(conn, request, output, responseFinish) + defer close(output) + + requestFinish := make(chan bool) + responseFinish := make(chan bool) + + go handleRequest(conn, request, input, requestFinish) + go handleResponse(conn, request, output, responseFinish) <-requestFinish conn.CloseWrite() @@ -92,27 +92,27 @@ func startCommunicate(request *vmessio.VMessRequest, dest v2net.Address, ray cor } func handleRequest(conn *net.TCPConn, request *vmessio.VMessRequest, input <-chan []byte, finish chan<- bool) error { - defer close(finish) - requestWriter := vmessio.NewVMessRequestWriter() + defer close(finish) + requestWriter := vmessio.NewVMessRequestWriter() err := requestWriter.Write(conn, request) if err != nil { log.Error("Failed to write VMess request: %v", err) return err } - - encryptRequestWriter, err := v2io.NewAesEncryptWriter(request.RequestKey[:], request.RequestIV[:], conn) + + encryptRequestWriter, err := v2io.NewAesEncryptWriter(request.RequestKey[:], request.RequestIV[:], conn) if err != nil { log.Error("Failed to create encrypt writer: %v", err) return err } - - v2net.ChanToWriter(encryptRequestWriter, input) - return nil + + v2net.ChanToWriter(encryptRequestWriter, input) + return nil } func handleResponse(conn *net.TCPConn, request *vmessio.VMessRequest, output chan<- []byte, finish chan<- bool) error { - defer close(finish) - responseKey := md5.Sum(request.RequestKey[:]) + defer close(finish) + responseKey := md5.Sum(request.RequestKey[:]) responseIV := md5.Sum(request.RequestIV[:]) response := vmessio.VMessResponse{} @@ -129,9 +129,9 @@ func handleResponse(conn *net.TCPConn, request *vmessio.VMessRequest, output cha log.Error("Failed to create decrypt reader: %v", err) return err } - + v2net.ReaderToChan(output, decryptResponseReader) - return nil + return nil } type VMessOutboundHandlerFactory struct { diff --git a/release/server/main.go b/release/server/main.go index 66098e0e0..b1749ca7e 100644 --- a/release/server/main.go +++ b/release/server/main.go @@ -16,22 +16,22 @@ import ( var ( configFile = flag.String("config", "", "Config file for this Point server.") - logLevel = flag.String("loglevel", "", "Level of log info to be printed to console, available value: debug, info, warning, error") + logLevel = flag.String("loglevel", "", "Level of log info to be printed to console, available value: debug, info, warning, error") ) func main() { flag.Parse() - - switch *logLevel { - case "debug": - log.SetLogLevel(log.DebugLevel) - case "info": - log.SetLogLevel(log.InfoLevel) - case "warning": - log.SetLogLevel(log.WarningLevel) - case "error": - log.SetLogLevel(log.ErrorLevel) - } + + switch *logLevel { + case "debug": + log.SetLogLevel(log.DebugLevel) + case "info": + log.SetLogLevel(log.InfoLevel) + case "warning": + log.SetLogLevel(log.WarningLevel) + case "error": + log.SetLogLevel(log.ErrorLevel) + } if configFile == nil || len(*configFile) == 0 { panic(log.Error("Config file is not set.")) diff --git a/userset.go b/userset.go index 3e2562777..f16e24b33 100644 --- a/userset.go +++ b/userset.go @@ -20,8 +20,8 @@ type TimedUserSet struct { } type indexTimePair struct { - index int - timeSec int64 + index int + timeSec int64 } type hashEntry struct { @@ -47,8 +47,8 @@ func (us *TimedUserSet) updateUserHash(tick <-chan time.Time) { for { now := <-tick nowSec := now.UTC().Unix() - - remove2Sec := nowSec - cacheDurationSec + + remove2Sec := nowSec - cacheDurationSec if remove2Sec > lastSec2Remove { for lastSec2Remove+1 < remove2Sec { entry := <-hash2Remove @@ -56,15 +56,15 @@ func (us *TimedUserSet) updateUserHash(tick <-chan time.Time) { delete(us.userHashes, entry.hash) } } - - for lastSec < nowSec + cacheDurationSec { - for idx, id := range us.validUserIds { + + for lastSec < nowSec+cacheDurationSec { + for idx, id := range us.validUserIds { idHash := id.TimeHash(lastSec) hash2Remove <- hashEntry{string(idHash), lastSec} us.userHashes[string(idHash)] = indexTimePair{idx, lastSec} } - lastSec ++ - } + lastSec++ + } } }