1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-07 20:45:19 -04:00

Use array instead of slice

This commit is contained in:
V2Ray 2015-09-23 22:17:25 +02:00
parent ef790375ee
commit 6ecb18268e
4 changed files with 9 additions and 11 deletions

View File

@ -14,8 +14,8 @@ const (
// The ID of en entity, in the form of an UUID. // The ID of en entity, in the form of an UUID.
type ID struct { type ID struct {
String string String string
Bytes []byte Bytes [16]byte
cmdKey []byte cmdKey [16]byte
} }
func NewID(id string) (ID, error) { func NewID(id string) (ID, error) {
@ -25,27 +25,25 @@ func NewID(id string) (ID, error) {
} }
md5hash := md5.New() md5hash := md5.New()
md5hash.Write(idBytes) md5hash.Write(idBytes[:])
md5hash.Write([]byte("c48619fe-8f02-49e0-b9e9-edf763e17e21")) md5hash.Write([]byte("c48619fe-8f02-49e0-b9e9-edf763e17e21"))
cmdKey := md5.Sum(nil) cmdKey := md5.Sum(nil)
return ID{ return ID{
String: id, String: id,
Bytes: idBytes, Bytes: idBytes,
cmdKey: cmdKey[:], cmdKey: cmdKey,
}, nil }, nil
} }
func (v ID) CmdKey() []byte { func (v ID) CmdKey() []byte {
return v.cmdKey return v.cmdKey[:]
} }
var byteGroups = []int{8, 4, 4, 4, 12} var byteGroups = []int{8, 4, 4, 4, 12}
// TODO: leverage a full functional UUID library // TODO: leverage a full functional UUID library
func UUIDToID(uuid string) (v []byte, err error) { func UUIDToID(uuid string) (v [16]byte, err error) {
v = make([]byte, 16)
text := []byte(uuid) text := []byte(uuid)
if len(text) < 32 { if len(text) < 32 {
err = log.Error("uuid: invalid UUID string: %s", text) err = log.Error("uuid: invalid UUID string: %s", text)

View File

@ -13,5 +13,5 @@ func TestUUIDToID(t *testing.T) {
expectedBytes := []byte{0x24, 0x18, 0xd0, 0x87, 0x64, 0x8d, 0x49, 0x90, 0x86, 0xe8, 0x19, 0xdc, 0xa1, 0xd0, 0x06, 0xd3} expectedBytes := []byte{0x24, 0x18, 0xd0, 0x87, 0x64, 0x8d, 0x49, 0x90, 0x86, 0xe8, 0x19, 0xdc, 0xa1, 0xd0, 0x06, 0xd3}
actualBytes, _ := NewID(uuid) actualBytes, _ := NewID(uuid)
assert.Bytes(actualBytes.Bytes).Named("UUID").Equals(expectedBytes) assert.Bytes(actualBytes.Bytes[:]).Named("UUID").Equals(expectedBytes)
} }

View File

@ -39,7 +39,7 @@ func NewTimedUserSet() UserSet {
func (us *TimedUserSet) generateNewHashes(lastSec, nowSec int64, idx int, id ID) { func (us *TimedUserSet) generateNewHashes(lastSec, nowSec int64, idx int, id ID) {
idHash := NewTimeHash(HMACHash{}) idHash := NewTimeHash(HMACHash{})
for lastSec < nowSec+cacheDurationSec { for lastSec < nowSec+cacheDurationSec {
idHash := idHash.Hash(id.Bytes, lastSec) idHash := idHash.Hash(id.Bytes[:], lastSec)
log.Debug("Valid User Hash: %v", idHash) log.Debug("Valid User Hash: %v", idHash)
us.userHash.Set(string(idHash), indexTimePair{idx, lastSec}, lastSec+2*cacheDurationSec) us.userHash.Set(string(idHash), indexTimePair{idx, lastSec}, lastSec+2*cacheDurationSec)
lastSec++ lastSec++

View File

@ -172,7 +172,7 @@ func (request *VMessRequest) ToBytes(idHash user.CounterHash, randomRangeInt64 u
} }
counter := randomRangeInt64(time.Now().UTC().Unix(), 30) counter := randomRangeInt64(time.Now().UTC().Unix(), 30)
hash := idHash.Hash(request.UserId.Bytes, counter) hash := idHash.Hash(request.UserId.Bytes[:], counter)
log.Debug("Writing userhash: %v", hash) log.Debug("Writing userhash: %v", hash)
buffer = append(buffer, hash...) buffer = append(buffer, hash...)