From a63670311eff1b1fd0c32f4ddd830c0ef5addab5 Mon Sep 17 00:00:00 2001 From: v2ray Date: Sat, 12 Dec 2015 21:40:16 +0100 Subject: [PATCH] use uuid in vmess id --- proxy/vmess/id.go | 58 +++++-------------- proxy/vmess/id_test.go | 18 ------ proxy/vmess/json/user.go | 5 +- proxy/vmess/outbound/json/outbound_test.go | 2 +- .../user/testing/mocks/static_userset.go | 5 +- proxy/vmess/protocol/user/userset.go | 2 +- proxy/vmess/protocol/vmess.go | 2 +- proxy/vmess/protocol/vmess_test.go | 16 +++-- proxy/vmess/vmess_test.go | 5 +- 9 files changed, 36 insertions(+), 77 deletions(-) delete mode 100644 proxy/vmess/id_test.go diff --git a/proxy/vmess/id.go b/proxy/vmess/id.go index 63e48b23..dd656862 100644 --- a/proxy/vmess/id.go +++ b/proxy/vmess/id.go @@ -2,10 +2,9 @@ package vmess import ( "crypto/md5" - "encoding/hex" "errors" - "github.com/v2ray/v2ray-core/common/log" + "github.com/v2ray/v2ray-core/common/uuid" ) const ( @@ -18,61 +17,30 @@ var ( // The ID of en entity, in the form of an UUID. type ID struct { - String string - Bytes [IDBytesLen]byte + uuid *uuid.UUID cmdKey [IDBytesLen]byte } -func NewID(id string) (*ID, error) { - idBytes, err := UUIDToID(id) - if err != nil { - log.Error("Failed to parse id %s", id) - return &ID{}, InvalidID - } +func (this *ID) Bytes() []byte { + return this.uuid.Bytes() +} +func (this *ID) String() string { + return this.uuid.String() +} + +func NewID(uuid *uuid.UUID) *ID { md5hash := md5.New() - md5hash.Write(idBytes[:]) + md5hash.Write(uuid.Bytes()) md5hash.Write([]byte("c48619fe-8f02-49e0-b9e9-edf763e17e21")) cmdKey := md5.Sum(nil) return &ID{ - String: id, - Bytes: idBytes, + uuid: uuid, cmdKey: cmdKey, - }, nil + } } func (v ID) CmdKey() []byte { return v.cmdKey[:] } - -var byteGroups = []int{8, 4, 4, 4, 12} - -// TODO: leverage a full functional UUID library -func UUIDToID(uuid string) (v [IDBytesLen]byte, err error) { - text := []byte(uuid) - if len(text) < 32 { - log.Error("uuid: invalid UUID string: %s", text) - err = InvalidID - return - } - - b := v[:] - - for _, byteGroup := range byteGroups { - if text[0] == '-' { - text = text[1:] - } - - _, err = hex.Decode(b[:byteGroup/2], text[:byteGroup]) - - if err != nil { - return - } - - text = text[byteGroup:] - b = b[byteGroup/2:] - } - - return -} diff --git a/proxy/vmess/id_test.go b/proxy/vmess/id_test.go deleted file mode 100644 index d44e84f7..00000000 --- a/proxy/vmess/id_test.go +++ /dev/null @@ -1,18 +0,0 @@ -package vmess - -import ( - "testing" - - v2testing "github.com/v2ray/v2ray-core/testing" - "github.com/v2ray/v2ray-core/testing/assert" -) - -func TestUUIDToID(t *testing.T) { - v2testing.Current(t) - - uuid := "2418d087-648d-4990-86e8-19dca1d006d3" - expectedBytes := []byte{0x24, 0x18, 0xd0, 0x87, 0x64, 0x8d, 0x49, 0x90, 0x86, 0xe8, 0x19, 0xdc, 0xa1, 0xd0, 0x06, 0xd3} - - actualBytes, _ := NewID(uuid) - assert.Bytes(actualBytes.Bytes[:]).Named("UUID").Equals(expectedBytes) -} diff --git a/proxy/vmess/json/user.go b/proxy/vmess/json/user.go index 3a9639c8..397a9224 100644 --- a/proxy/vmess/json/user.go +++ b/proxy/vmess/json/user.go @@ -3,6 +3,7 @@ package json import ( "encoding/json" + "github.com/v2ray/v2ray-core/common/uuid" "github.com/v2ray/v2ray-core/proxy/vmess" ) @@ -23,11 +24,11 @@ func (u *ConfigUser) UnmarshalJSON(data []byte) error { if err := json.Unmarshal(data, &rawUserValue); err != nil { return err } - id, err := vmess.NewID(rawUserValue.IdString) + id, err := uuid.ParseString(rawUserValue.IdString) if err != nil { return err } - u.Id = id + u.Id = vmess.NewID(id) u.Email = rawUserValue.EmailString u.LevelValue = vmess.UserLevel(rawUserValue.LevelInt) return nil diff --git a/proxy/vmess/outbound/json/outbound_test.go b/proxy/vmess/outbound/json/outbound_test.go index c72c2444..69bfb33f 100644 --- a/proxy/vmess/outbound/json/outbound_test.go +++ b/proxy/vmess/outbound/json/outbound_test.go @@ -29,5 +29,5 @@ func TestConfigTargetParsing(t *testing.T) { assert.Error(err).IsNil() assert.String(target.Address).Equals("127.0.0.1:80") assert.Int(len(target.Users)).Equals(1) - assert.StringLiteral(target.Users[0].ID().String).Equals("e641f5ad-9397-41e3-bf1a-e8740dfed019") + assert.String(target.Users[0].ID()).Equals("e641f5ad-9397-41e3-bf1a-e8740dfed019") } diff --git a/proxy/vmess/protocol/user/testing/mocks/static_userset.go b/proxy/vmess/protocol/user/testing/mocks/static_userset.go index 075a4e66..558295c2 100644 --- a/proxy/vmess/protocol/user/testing/mocks/static_userset.go +++ b/proxy/vmess/protocol/user/testing/mocks/static_userset.go @@ -1,6 +1,7 @@ package mocks import ( + "github.com/v2ray/v2ray-core/common/uuid" "github.com/v2ray/v2ray-core/proxy/vmess" ) @@ -24,6 +25,6 @@ func (us *StaticUserSet) AddUser(user vmess.User) error { } func (us *StaticUserSet) GetUser(userhash []byte) (vmess.User, int64, bool) { - id, _ := vmess.NewID("703e9102-eb57-499c-8b59-faf4f371bb21") - return &StaticUser{id: id}, 0, true + id, _ := uuid.ParseString("703e9102-eb57-499c-8b59-faf4f371bb21") + return &StaticUser{id: vmess.NewID(id)}, 0, true } diff --git a/proxy/vmess/protocol/user/userset.go b/proxy/vmess/protocol/user/userset.go index e7a5e43f..69aaf4e3 100644 --- a/proxy/vmess/protocol/user/userset.go +++ b/proxy/vmess/protocol/user/userset.go @@ -53,7 +53,7 @@ func (us *TimedUserSet) removeEntries(entries <-chan interface{}) { func (us *TimedUserSet) generateNewHashes(lastSec, nowSec int64, idx int, id *vmess.ID) { idHash := NewTimeHash(HMACHash{}) for lastSec < nowSec { - idHash := idHash.Hash(id.Bytes[:], lastSec) + idHash := idHash.Hash(id.Bytes(), lastSec) us.access.Lock() us.userHash[string(idHash)] = indexTimePair{idx, lastSec} us.access.Unlock() diff --git a/proxy/vmess/protocol/vmess.go b/proxy/vmess/protocol/vmess.go index 408fb593..bb482acb 100644 --- a/proxy/vmess/protocol/vmess.go +++ b/proxy/vmess/protocol/vmess.go @@ -161,7 +161,7 @@ func (this *VMessRequest) ToBytes(idHash user.CounterHash, randomRangeInt64 user } counter := randomRangeInt64(time.Now().Unix(), 30) - hash := idHash.Hash(this.User.ID().Bytes[:], counter) + hash := idHash.Hash(this.User.ID().Bytes(), counter) buffer.Append(hash) diff --git a/proxy/vmess/protocol/vmess_test.go b/proxy/vmess/protocol/vmess_test.go index ed672128..46826aac 100644 --- a/proxy/vmess/protocol/vmess_test.go +++ b/proxy/vmess/protocol/vmess_test.go @@ -7,6 +7,7 @@ import ( "testing" v2net "github.com/v2ray/v2ray-core/common/net" + "github.com/v2ray/v2ray-core/common/uuid" "github.com/v2ray/v2ray-core/proxy/vmess" "github.com/v2ray/v2ray-core/proxy/vmess/protocol/user" "github.com/v2ray/v2ray-core/proxy/vmess/protocol/user/testing/mocks" @@ -30,10 +31,10 @@ func (this *TestUser) Level() vmess.UserLevel { func TestVMessSerialization(t *testing.T) { v2testing.Current(t) - userId, err := vmess.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51") - if err != nil { - t.Fatal(err) - } + id, err := uuid.ParseString("2b2966ac-16aa-4fbf-8d81-c5f172a3da51") + assert.Error(err).IsNil() + + userId := vmess.NewID(id) testUser := &TestUser{ id: userId, @@ -73,7 +74,7 @@ func TestVMessSerialization(t *testing.T) { } assert.Byte(actualRequest.Version).Named("Version").Equals(byte(0x01)) - assert.StringLiteral(actualRequest.User.ID().String).Named("UserId").Equals(request.User.ID().String) + assert.String(actualRequest.User.ID()).Named("UserId").Equals(request.User.ID().String()) assert.Bytes(actualRequest.RequestIV).Named("RequestIV").Equals(request.RequestIV[:]) assert.Bytes(actualRequest.RequestKey).Named("RequestKey").Equals(request.RequestKey[:]) assert.Bytes(actualRequest.ResponseHeader).Named("ResponseHeader").Equals(request.ResponseHeader[:]) @@ -90,7 +91,10 @@ func TestReadSingleByte(t *testing.T) { } func BenchmarkVMessRequestWriting(b *testing.B) { - userId, _ := vmess.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51") + id, err := uuid.ParseString("2b2966ac-16aa-4fbf-8d81-c5f172a3da51") + assert.Error(err).IsNil() + + userId := vmess.NewID(id) userSet := mocks.MockUserSet{[]vmess.User{}, make(map[string]int), make(map[string]int64)} testUser := &TestUser{ diff --git a/proxy/vmess/vmess_test.go b/proxy/vmess/vmess_test.go index 7e99bf8a..64ec1e9d 100644 --- a/proxy/vmess/vmess_test.go +++ b/proxy/vmess/vmess_test.go @@ -6,6 +6,7 @@ import ( v2net "github.com/v2ray/v2ray-core/common/net" v2nettesting "github.com/v2ray/v2ray-core/common/net/testing" + "github.com/v2ray/v2ray-core/common/uuid" "github.com/v2ray/v2ray-core/proxy/common/connhandler" proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks" vmess "github.com/v2ray/v2ray-core/proxy/vmess" @@ -23,9 +24,11 @@ import ( func TestVMessInAndOut(t *testing.T) { v2testing.Current(t) - testAccount, err := vmess.NewID("ad937d9d-6e23-4a5a-ba23-bce5092a7c51") + id, err := uuid.ParseString("ad937d9d-6e23-4a5a-ba23-bce5092a7c51") assert.Error(err).IsNil() + testAccount := vmess.NewID(id) + portA := v2nettesting.PickPort() portB := v2nettesting.PickPort()