From b40be74b8664dc1345a41456f0fee50167db8edc Mon Sep 17 00:00:00 2001 From: V2Ray Date: Sun, 20 Sep 2015 00:11:14 +0200 Subject: [PATCH] Fix test break --- proxy/vmess/config.go | 10 +++++----- proxy/vmess/protocol/{ => user}/id.go | 2 +- proxy/vmess/protocol/{ => user}/id_test.go | 2 +- proxy/vmess/protocol/{ => user}/idhash.go | 2 +- proxy/vmess/protocol/{ => user}/inthash.go | 2 +- proxy/vmess/protocol/{ => user}/rand.go | 2 +- proxy/vmess/protocol/{ => user}/rand_test.go | 2 +- proxy/vmess/protocol/{ => user}/user.go | 2 +- proxy/vmess/protocol/{ => user}/userset.go | 2 +- proxy/vmess/protocol/vmess.go | 15 ++++++++------- proxy/vmess/protocol/vmess_test.go | 18 +++++++++--------- proxy/vmess/vmessin.go | 9 +++++---- proxy/vmess/vmessout.go | 9 +++++---- testing/coverage/coverall | 13 ++++++------- testing/mocks/mockuserset.go | 8 ++++---- 15 files changed, 50 insertions(+), 48 deletions(-) rename proxy/vmess/protocol/{ => user}/id.go (98%) rename proxy/vmess/protocol/{ => user}/id_test.go (95%) rename proxy/vmess/protocol/{ => user}/idhash.go (98%) rename proxy/vmess/protocol/{ => user}/inthash.go (93%) rename proxy/vmess/protocol/{ => user}/rand.go (93%) rename proxy/vmess/protocol/{ => user}/rand_test.go (95%) rename proxy/vmess/protocol/{ => user}/user.go (88%) rename proxy/vmess/protocol/{ => user}/userset.go (99%) diff --git a/proxy/vmess/config.go b/proxy/vmess/config.go index 4c0788c1e..87fc00d42 100644 --- a/proxy/vmess/config.go +++ b/proxy/vmess/config.go @@ -6,7 +6,7 @@ import ( v2net "github.com/v2ray/v2ray-core/common/net" "github.com/v2ray/v2ray-core/log" - "github.com/v2ray/v2ray-core/proxy/vmess/protocol" + "github.com/v2ray/v2ray-core/proxy/vmess/protocol/user" ) type VMessUser struct { @@ -14,9 +14,9 @@ type VMessUser struct { Email string `json:"email"` } -func (u *VMessUser) ToUser() (protocol.User, error) { - id, err := protocol.NewID(u.Id) - return protocol.User{ +func (u *VMessUser) ToUser() (user.User, error) { + id, err := user.NewID(u.Id) + return user.User{ Id: id, }, err } @@ -38,7 +38,7 @@ type VNextConfig struct { } func (config VNextConfig) ToVNextServer() VNextServer { - users := make([]protocol.User, 0, len(config.Users)) + users := make([]user.User, 0, len(config.Users)) for _, user := range config.Users { vuser, err := user.ToUser() if err != nil { diff --git a/proxy/vmess/protocol/id.go b/proxy/vmess/protocol/user/id.go similarity index 98% rename from proxy/vmess/protocol/id.go rename to proxy/vmess/protocol/user/id.go index 92cfa1927..c99e29b0e 100644 --- a/proxy/vmess/protocol/id.go +++ b/proxy/vmess/protocol/user/id.go @@ -1,4 +1,4 @@ -package protocol +package user import ( "crypto/md5" diff --git a/proxy/vmess/protocol/id_test.go b/proxy/vmess/protocol/user/id_test.go similarity index 95% rename from proxy/vmess/protocol/id_test.go rename to proxy/vmess/protocol/user/id_test.go index 4d6313963..0e35d96b9 100644 --- a/proxy/vmess/protocol/id_test.go +++ b/proxy/vmess/protocol/user/id_test.go @@ -1,4 +1,4 @@ -package protocol +package user import ( "testing" diff --git a/proxy/vmess/protocol/idhash.go b/proxy/vmess/protocol/user/idhash.go similarity index 98% rename from proxy/vmess/protocol/idhash.go rename to proxy/vmess/protocol/user/idhash.go index e382e792e..5c56afe3f 100644 --- a/proxy/vmess/protocol/idhash.go +++ b/proxy/vmess/protocol/user/idhash.go @@ -1,4 +1,4 @@ -package protocol +package user import ( "crypto/hmac" diff --git a/proxy/vmess/protocol/inthash.go b/proxy/vmess/protocol/user/inthash.go similarity index 93% rename from proxy/vmess/protocol/inthash.go rename to proxy/vmess/protocol/user/inthash.go index 41258da82..e8cf2ab13 100644 --- a/proxy/vmess/protocol/inthash.go +++ b/proxy/vmess/protocol/user/inthash.go @@ -1,4 +1,4 @@ -package protocol +package user import ( "crypto/md5" diff --git a/proxy/vmess/protocol/rand.go b/proxy/vmess/protocol/user/rand.go similarity index 93% rename from proxy/vmess/protocol/rand.go rename to proxy/vmess/protocol/user/rand.go index af9e9b7b1..3ca257f0f 100644 --- a/proxy/vmess/protocol/rand.go +++ b/proxy/vmess/protocol/user/rand.go @@ -1,4 +1,4 @@ -package protocol +package user import ( "math/rand" diff --git a/proxy/vmess/protocol/rand_test.go b/proxy/vmess/protocol/user/rand_test.go similarity index 95% rename from proxy/vmess/protocol/rand_test.go rename to proxy/vmess/protocol/user/rand_test.go index 3a36a1968..b68bb865f 100644 --- a/proxy/vmess/protocol/rand_test.go +++ b/proxy/vmess/protocol/user/rand_test.go @@ -1,4 +1,4 @@ -package protocol +package user import ( "testing" diff --git a/proxy/vmess/protocol/user.go b/proxy/vmess/protocol/user/user.go similarity index 88% rename from proxy/vmess/protocol/user.go rename to proxy/vmess/protocol/user/user.go index 47c3e70cc..4ef10733e 100644 --- a/proxy/vmess/protocol/user.go +++ b/proxy/vmess/protocol/user/user.go @@ -1,4 +1,4 @@ -package protocol +package user // User is the user account that is used for connection to a Point type User struct { diff --git a/proxy/vmess/protocol/userset.go b/proxy/vmess/protocol/user/userset.go similarity index 99% rename from proxy/vmess/protocol/userset.go rename to proxy/vmess/protocol/user/userset.go index 20903907f..d944fb6ad 100644 --- a/proxy/vmess/protocol/userset.go +++ b/proxy/vmess/protocol/user/userset.go @@ -1,4 +1,4 @@ -package protocol +package user import ( "container/heap" diff --git a/proxy/vmess/protocol/vmess.go b/proxy/vmess/protocol/vmess.go index 521a76d62..c165b30a7 100644 --- a/proxy/vmess/protocol/vmess.go +++ b/proxy/vmess/protocol/vmess.go @@ -15,6 +15,7 @@ import ( v2io "github.com/v2ray/v2ray-core/common/io" v2net "github.com/v2ray/v2ray-core/common/net" "github.com/v2ray/v2ray-core/log" + "github.com/v2ray/v2ray-core/proxy/vmess/protocol/user" ) const ( @@ -38,7 +39,7 @@ var ( type VMessRequest struct { Version byte - UserId ID + UserId user.ID RequestIV [16]byte RequestKey [16]byte ResponseHeader [4]byte @@ -47,10 +48,10 @@ type VMessRequest struct { } type VMessRequestReader struct { - vUserSet UserSet + vUserSet user.UserSet } -func NewVMessRequestReader(vUserSet UserSet) *VMessRequestReader { +func NewVMessRequestReader(vUserSet user.UserSet) *VMessRequestReader { return &VMessRequestReader{ vUserSet: vUserSet, } @@ -59,7 +60,7 @@ func NewVMessRequestReader(vUserSet UserSet) *VMessRequestReader { func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) { buffer := make([]byte, 256) - nBytes, err := reader.Read(buffer[:IDBytesLen]) + nBytes, err := reader.Read(buffer[:user.IDBytesLen]) if err != nil { return nil, err } @@ -75,7 +76,7 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) { if err != nil { return nil, err } - aesStream := cipher.NewCFBDecrypter(aesCipher, Int64Hash(timeSec)) + aesStream := cipher.NewCFBDecrypter(aesCipher, user.Int64Hash(timeSec)) decryptor := v2io.NewCryptionReader(aesStream, reader) if err != nil { @@ -178,7 +179,7 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) { return request, nil } -func (request *VMessRequest) ToBytes(idHash CounterHash, randomRangeInt64 RandomInt64InRange) ([]byte, error) { +func (request *VMessRequest) ToBytes(idHash user.CounterHash, randomRangeInt64 user.RandomInt64InRange) ([]byte, error) { buffer := make([]byte, 0, 300) counter := randomRangeInt64(time.Now().UTC().Unix(), 30) @@ -235,7 +236,7 @@ func (request *VMessRequest) ToBytes(idHash CounterHash, randomRangeInt64 Random if err != nil { return nil, err } - aesStream := cipher.NewCFBEncrypter(aesCipher, Int64Hash(counter)) + aesStream := cipher.NewCFBEncrypter(aesCipher, user.Int64Hash(counter)) aesStream.XORKeyStream(buffer[encryptionBegin:encryptionEnd], buffer[encryptionBegin:encryptionEnd]) return buffer, nil diff --git a/proxy/vmess/protocol/vmess_test.go b/proxy/vmess/protocol/vmess_test.go index 13ff6e06f..0cb90abcc 100644 --- a/proxy/vmess/protocol/vmess_test.go +++ b/proxy/vmess/protocol/vmess_test.go @@ -5,8 +5,8 @@ import ( "crypto/rand" "testing" - "github.com/v2ray/v2ray-core" v2net "github.com/v2ray/v2ray-core/common/net" + "github.com/v2ray/v2ray-core/proxy/vmess/protocol/user" "github.com/v2ray/v2ray-core/testing/mocks" "github.com/v2ray/v2ray-core/testing/unit" ) @@ -14,13 +14,13 @@ import ( func TestVMessSerialization(t *testing.T) { assert := unit.Assert(t) - userId, err := core.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51") + userId, err := user.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51") if err != nil { t.Fatal(err) } - userSet := mocks.MockUserSet{[]core.ID{}, make(map[string]int), make(map[string]int64)} - userSet.AddUser(core.User{userId}) + userSet := mocks.MockUserSet{[]user.ID{}, make(map[string]int), make(map[string]int64)} + userSet.AddUser(user.User{userId}) request := new(VMessRequest) request.Version = byte(0x01) @@ -45,7 +45,7 @@ func TestVMessSerialization(t *testing.T) { request.Address = v2net.DomainAddress("v2ray.com", 80) mockTime := int64(1823730) - buffer, err := request.ToBytes(NewTimeHash(HMACHash{}), func(base int64, delta int) int64 { return mockTime }) + buffer, err := request.ToBytes(user.NewTimeHash(user.HMACHash{}), func(base int64, delta int) int64 { return mockTime }) if err != nil { t.Fatal(err) } @@ -69,9 +69,9 @@ func TestVMessSerialization(t *testing.T) { } func BenchmarkVMessRequestWriting(b *testing.B) { - userId, _ := core.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51") - userSet := mocks.MockUserSet{[]core.ID{}, make(map[string]int), make(map[string]int64)} - userSet.AddUser(core.User{userId}) + userId, _ := user.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51") + userSet := mocks.MockUserSet{[]user.ID{}, make(map[string]int), make(map[string]int64)} + userSet.AddUser(user.User{userId}) request := new(VMessRequest) request.Version = byte(0x01) @@ -85,6 +85,6 @@ func BenchmarkVMessRequestWriting(b *testing.B) { request.Address = v2net.DomainAddress("v2ray.com", 80) for i := 0; i < b.N; i++ { - request.ToBytes(NewTimeHash(HMACHash{}), GenerateRandomInt64InRange) + request.ToBytes(user.NewTimeHash(user.HMACHash{}), user.GenerateRandomInt64InRange) } } diff --git a/proxy/vmess/vmessin.go b/proxy/vmess/vmessin.go index 40c735ff9..b1ae34fc9 100644 --- a/proxy/vmess/vmessin.go +++ b/proxy/vmess/vmessin.go @@ -10,16 +10,17 @@ import ( v2io "github.com/v2ray/v2ray-core/common/io" v2net "github.com/v2ray/v2ray-core/common/net" "github.com/v2ray/v2ray-core/log" - protocol "github.com/v2ray/v2ray-core/proxy/vmess/protocol" + "github.com/v2ray/v2ray-core/proxy/vmess/protocol" + "github.com/v2ray/v2ray-core/proxy/vmess/protocol/user" ) type VMessInboundHandler struct { vPoint *core.Point - clients protocol.UserSet + clients user.UserSet accepting bool } -func NewVMessInboundHandler(vp *core.Point, clients protocol.UserSet) *VMessInboundHandler { +func NewVMessInboundHandler(vp *core.Point, clients user.UserSet) *VMessInboundHandler { return &VMessInboundHandler{ vPoint: vp, clients: clients, @@ -123,7 +124,7 @@ func (factory *VMessInboundHandlerFactory) Create(vp *core.Point, rawConfig []by if err != nil { panic(log.Error("VMessIn: Failed to load VMess inbound config: %v", err)) } - allowedClients := protocol.NewTimedUserSet() + allowedClients := user.NewTimedUserSet() for _, client := range config.AllowedClients { user, err := client.ToUser() if err != nil { diff --git a/proxy/vmess/vmessout.go b/proxy/vmess/vmessout.go index 4ec59029a..74117aa25 100644 --- a/proxy/vmess/vmessout.go +++ b/proxy/vmess/vmessout.go @@ -11,13 +11,14 @@ import ( v2io "github.com/v2ray/v2ray-core/common/io" v2net "github.com/v2ray/v2ray-core/common/net" "github.com/v2ray/v2ray-core/log" - protocol "github.com/v2ray/v2ray-core/proxy/vmess/protocol" + "github.com/v2ray/v2ray-core/proxy/vmess/protocol" + "github.com/v2ray/v2ray-core/proxy/vmess/protocol/user" ) // VNext is the next Point server in the connection chain. type VNextServer struct { Address v2net.Address // Address of VNext server - Users []protocol.User // User accounts for accessing VNext. + Users []user.User // User accounts for accessing VNext. } type VMessOutboundHandler struct { @@ -34,7 +35,7 @@ func NewVMessOutboundHandler(vp *core.Point, vNextList []VNextServer, dest v2net } } -func (handler *VMessOutboundHandler) pickVNext() (v2net.Address, protocol.User) { +func (handler *VMessOutboundHandler) pickVNext() (v2net.Address, user.User) { vNextLen := len(handler.vNextList) if vNextLen == 0 { panic("VMessOut: Zero vNext is configured.") @@ -101,7 +102,7 @@ func handleRequest(conn *net.TCPConn, request *protocol.VMessRequest, input <-ch return } - buffer, err := request.ToBytes(protocol.NewTimeHash(protocol.HMACHash{}), protocol.GenerateRandomInt64InRange) + buffer, err := request.ToBytes(user.NewTimeHash(user.HMACHash{}), user.GenerateRandomInt64InRange) if err != nil { log.Error("VMessOut: Failed to serialize VMess request: %v", err) return diff --git a/testing/coverage/coverall b/testing/coverage/coverall index 53745cde0..bb39793bf 100755 --- a/testing/coverage/coverall +++ b/testing/coverage/coverall @@ -19,13 +19,12 @@ function test_package { echo "mode: set" > coverall.out -test_package "." "." -test_package "io/socks" "io/socks" -test_package "io/vmess" "hash,io,io/vmess" -test_package "math" "math" -test_package "net" "net" -test_package "net/socks" ".,io/socks,net,net/socks" -test_package "net/vmess" ".,io,math,net,net/vmess" +test_package "common/net" "common/net" +test_package "proxy/socks" "proxy/socks,proxy/socks/protocol" +test_package "proxy/socks/protocol" "proxy/socks/protocol" +test_package "proxy/vmess" "common/io,common/net,proxy/vmess,proxy/vmess/protocol,proxy/vmess/protocol/user" +test_package "proxy/vmess/protocol" "proxy/vmess/protocol,proxy/vmess/protocol/user" +test_package "proxy/vmess/protocol/user" "proxy/vmess/protocol/user" if [ "$FAIL" -eq 0 ]; then $HOME/gopath/bin/goveralls -v -coverprofile=coverall.out -service=travis-ci -repotoken $COVERALLS_TOKEN diff --git a/testing/mocks/mockuserset.go b/testing/mocks/mockuserset.go index 53f125278..5b12b76ac 100644 --- a/testing/mocks/mockuserset.go +++ b/testing/mocks/mockuserset.go @@ -1,21 +1,21 @@ package mocks import ( - "github.com/v2ray/v2ray-core/proxy/vmess/protocol" + "github.com/v2ray/v2ray-core/proxy/vmess/protocol/user" ) type MockUserSet struct { - UserIds []protocol.ID + UserIds []user.ID UserHashes map[string]int Timestamps map[string]int64 } -func (us *MockUserSet) AddUser(user protocol.User) error { +func (us *MockUserSet) AddUser(user user.User) error { us.UserIds = append(us.UserIds, user.Id) return nil } -func (us *MockUserSet) GetUser(userhash []byte) (*protocol.ID, int64, bool) { +func (us *MockUserSet) GetUser(userhash []byte) (*user.ID, int64, bool) { idx, found := us.UserHashes[string(userhash)] if found { return &us.UserIds[idx], us.Timestamps[string(userhash)], true