Refactor vmess config

This commit is contained in:
Claire Raymond
2015-10-16 10:03:22 +00:00
parent f10f08c87d
commit 771d0225c7
18 changed files with 263 additions and 184 deletions
-78
View File
@@ -1,78 +0,0 @@
package user
import (
"crypto/md5"
"encoding/hex"
"errors"
"github.com/v2ray/v2ray-core/common/log"
)
const (
IDBytesLen = 16
)
var (
InvalidID = errors.New("Invalid ID.")
)
// The ID of en entity, in the form of an UUID.
type ID struct {
String string
Bytes [IDBytesLen]byte
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
}
md5hash := md5.New()
md5hash.Write(idBytes[:])
md5hash.Write([]byte("c48619fe-8f02-49e0-b9e9-edf763e17e21"))
cmdKey := md5.Sum(nil)
return ID{
String: id,
Bytes: idBytes,
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
}
-17
View File
@@ -1,17 +0,0 @@
package user
import (
"testing"
"github.com/v2ray/v2ray-core/testing/unit"
)
func TestUUIDToID(t *testing.T) {
assert := unit.Assert(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)
}
-6
View File
@@ -1,6 +0,0 @@
package user
// User is the user account that is used for connection to a Point
type User struct {
Id ID `json:"id"` // The ID of this User.
}
+10 -9
View File
@@ -5,6 +5,7 @@ import (
"time"
"github.com/v2ray/v2ray-core/common/collect"
"github.com/v2ray/v2ray-core/proxy/vmess/config"
)
const (
@@ -13,12 +14,12 @@ const (
)
type UserSet interface {
AddUser(user User) error
GetUser(timeHash []byte) (*ID, int64, bool)
AddUser(user config.User) error
GetUser(timeHash []byte) (*config.ID, int64, bool)
}
type TimedUserSet struct {
validUserIds []ID
validUserIds []*config.ID
userHash map[string]indexTimePair
userHashDeleteQueue *collect.TimedQueue
access sync.RWMutex
@@ -31,7 +32,7 @@ type indexTimePair struct {
func NewTimedUserSet() UserSet {
tus := &TimedUserSet{
validUserIds: make([]ID, 0, 16),
validUserIds: make([]*config.ID, 0, 16),
userHash: make(map[string]indexTimePair, 512),
userHashDeleteQueue: collect.NewTimedQueue(updateIntervalSec),
access: sync.RWMutex{},
@@ -49,7 +50,7 @@ func (us *TimedUserSet) removeEntries(entries <-chan interface{}) {
}
}
func (us *TimedUserSet) generateNewHashes(lastSec, nowSec int64, idx int, id ID) {
func (us *TimedUserSet) generateNewHashes(lastSec, nowSec int64, idx int, id *config.ID) {
idHash := NewTimeHash(HMACHash{})
for lastSec < nowSec {
idHash := idHash.Hash(id.Bytes[:], lastSec)
@@ -73,8 +74,8 @@ func (us *TimedUserSet) updateUserHash(tick <-chan time.Time) {
}
}
func (us *TimedUserSet) AddUser(user User) error {
id := user.Id
func (us *TimedUserSet) AddUser(user config.User) error {
id := user.ID()
idx := len(us.validUserIds)
us.validUserIds = append(us.validUserIds, id)
@@ -85,12 +86,12 @@ func (us *TimedUserSet) AddUser(user User) error {
return nil
}
func (us TimedUserSet) GetUser(userHash []byte) (*ID, int64, bool) {
func (us TimedUserSet) GetUser(userHash []byte) (*config.ID, int64, bool) {
defer us.access.RUnlock()
us.access.RLock()
pair, found := us.userHash[string(userHash)]
if found {
return &us.validUserIds[pair.index], pair.timeSec, true
return us.validUserIds[pair.index], pair.timeSec, true
}
return nil, 0, false
}
+3 -2
View File
@@ -13,6 +13,7 @@ import (
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy"
"github.com/v2ray/v2ray-core/proxy/vmess/config"
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
"github.com/v2ray/v2ray-core/transport"
)
@@ -35,7 +36,7 @@ const (
// streaming.
type VMessRequest struct {
Version byte
UserId user.ID
UserId config.ID
RequestIV []byte
RequestKey []byte
ResponseHeader []byte
@@ -68,7 +69,7 @@ func NewVMessRequestReader(vUserSet user.UserSet) *VMessRequestReader {
func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
buffer := make([]byte, 256)
nBytes, err := reader.Read(buffer[:user.IDBytesLen])
nBytes, err := reader.Read(buffer[:config.IDBytesLen])
if err != nil {
return nil, err
}
+17 -8
View File
@@ -6,25 +6,34 @@ import (
"testing"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/proxy/vmess/config"
"github.com/v2ray/v2ray-core/proxy/vmess/protocol/user"
"github.com/v2ray/v2ray-core/testing/mocks"
"github.com/v2ray/v2ray-core/testing/unit"
)
type TestUser struct {
id *config.ID
}
func (u *TestUser) ID() *config.ID {
return u.id
}
func TestVMessSerialization(t *testing.T) {
assert := unit.Assert(t)
userId, err := user.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51")
userId, err := config.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51")
if err != nil {
t.Fatal(err)
}
userSet := mocks.MockUserSet{[]user.ID{}, make(map[string]int), make(map[string]int64)}
userSet.AddUser(user.User{userId})
userSet := mocks.MockUserSet{[]*config.ID{}, make(map[string]int), make(map[string]int64)}
userSet.AddUser(&TestUser{userId})
request := new(VMessRequest)
request.Version = byte(0x01)
request.UserId = userId
request.UserId = *userId
randBytes := make([]byte, 36)
_, err = rand.Read(randBytes)
@@ -61,13 +70,13 @@ func TestVMessSerialization(t *testing.T) {
}
func BenchmarkVMessRequestWriting(b *testing.B) {
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})
userId, _ := config.NewID("2b2966ac-16aa-4fbf-8d81-c5f172a3da51")
userSet := mocks.MockUserSet{[]*config.ID{}, make(map[string]int), make(map[string]int64)}
userSet.AddUser(&TestUser{userId})
request := new(VMessRequest)
request.Version = byte(0x01)
request.UserId = userId
request.UserId = *userId
randBytes := make([]byte, 36)
rand.Read(randBytes)