mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-17 23:06:30 -05:00
refactor alter id generation
This commit is contained in:
parent
8a07534586
commit
b5f43031d4
@ -57,3 +57,15 @@ func NewID(uuid *uuid.UUID) *ID {
|
|||||||
md5hash.Sum(id.cmdKey[:0])
|
md5hash.Sum(id.cmdKey[:0])
|
||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewAlterIDs(primary *ID, alterIDCount uint16) []*ID {
|
||||||
|
alterIDs := make([]*ID, alterIDCount)
|
||||||
|
prevID := primary.UUID()
|
||||||
|
for idx := range alterIDs {
|
||||||
|
newid := prevID.Next()
|
||||||
|
// TODO: check duplicates
|
||||||
|
alterIDs[idx] = NewID(newid)
|
||||||
|
prevID = newid
|
||||||
|
}
|
||||||
|
return alterIDs
|
||||||
|
}
|
||||||
|
@ -18,8 +18,8 @@ func TestRequestSerialization(t *testing.T) {
|
|||||||
|
|
||||||
user := protocol.NewUser(
|
user := protocol.NewUser(
|
||||||
protocol.NewID(uuid.New()),
|
protocol.NewID(uuid.New()),
|
||||||
|
nil,
|
||||||
protocol.UserLevelUntrusted,
|
protocol.UserLevelUntrusted,
|
||||||
0,
|
|
||||||
"test@v2ray.com")
|
"test@v2ray.com")
|
||||||
|
|
||||||
expectedRequest := &protocol.RequestHeader{
|
expectedRequest := &protocol.RequestHeader{
|
||||||
|
@ -18,23 +18,13 @@ type User struct {
|
|||||||
Email string
|
Email string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUser(id *ID, level UserLevel, alterIdCount uint16, email string) *User {
|
func NewUser(primary *ID, secondary []*ID, level UserLevel, email string) *User {
|
||||||
u := &User{
|
return &User{
|
||||||
ID: id,
|
ID: primary,
|
||||||
Level: level,
|
AlterIDs: secondary,
|
||||||
Email: email,
|
Level: level,
|
||||||
|
Email: email,
|
||||||
}
|
}
|
||||||
if alterIdCount > 0 {
|
|
||||||
u.AlterIDs = make([]*ID, alterIdCount)
|
|
||||||
prevId := id.UUID()
|
|
||||||
for idx := range u.AlterIDs {
|
|
||||||
newid := prevId.Next()
|
|
||||||
// TODO: check duplicate
|
|
||||||
u.AlterIDs[idx] = NewID(newid)
|
|
||||||
prevId = newid
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return u
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *User) AnyValidID() *ID {
|
func (this *User) AnyValidID() *ID {
|
||||||
|
@ -23,7 +23,9 @@ func (u *User) UnmarshalJSON(data []byte) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
*u = *NewUser(NewID(id), UserLevel(rawUserValue.LevelByte), rawUserValue.AlterIdCount, rawUserValue.EmailString)
|
primaryID := NewID(id)
|
||||||
|
alterIDs := NewAlterIDs(primaryID, rawUserValue.AlterIdCount)
|
||||||
|
*u = *NewUser(primaryID, alterIDs, UserLevel(rawUserValue.LevelByte), rawUserValue.EmailString)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,8 @@ func (this *userByEmail) Get(email string) (*protocol.User, bool) {
|
|||||||
user, found = this.cache[email]
|
user, found = this.cache[email]
|
||||||
if !found {
|
if !found {
|
||||||
id := protocol.NewID(uuid.New())
|
id := protocol.NewID(uuid.New())
|
||||||
user = protocol.NewUser(id, this.defaultLevel, this.defaultAlterIDs, email)
|
alterIDs := protocol.NewAlterIDs(id, this.defaultAlterIDs)
|
||||||
|
user = protocol.NewUser(id, alterIDs, this.defaultLevel, email)
|
||||||
this.cache[email] = user
|
this.cache[email] = user
|
||||||
}
|
}
|
||||||
this.Unlock()
|
this.Unlock()
|
||||||
|
@ -6,7 +6,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (this *VMessOutboundHandler) handleSwitchAccount(cmd *protocol.CommandSwitchAccount) {
|
func (this *VMessOutboundHandler) handleSwitchAccount(cmd *protocol.CommandSwitchAccount) {
|
||||||
user := protocol.NewUser(protocol.NewID(cmd.ID), cmd.Level, cmd.AlterIds.Value(), "")
|
primary := protocol.NewID(cmd.ID)
|
||||||
|
alters := protocol.NewAlterIDs(primary, cmd.AlterIds.Value())
|
||||||
|
user := protocol.NewUser(primary, alters, cmd.Level, "")
|
||||||
dest := v2net.TCPDestination(cmd.Host, cmd.Port)
|
dest := v2net.TCPDestination(cmd.Host, cmd.Port)
|
||||||
this.receiverManager.AddDetour(NewReceiver(dest, user), cmd.ValidMin)
|
this.receiverManager.AddDetour(NewReceiver(dest, user), cmd.ValidMin)
|
||||||
}
|
}
|
||||||
|
@ -15,13 +15,15 @@ func TestReceiverUser(t *testing.T) {
|
|||||||
v2testing.Current(t)
|
v2testing.Current(t)
|
||||||
|
|
||||||
id := protocol.NewID(uuid.New())
|
id := protocol.NewID(uuid.New())
|
||||||
user := protocol.NewUser(id, protocol.UserLevel(0), 100, "")
|
alters := protocol.NewAlterIDs(id, 100)
|
||||||
|
user := protocol.NewUser(id, alters, protocol.UserLevel(0), "")
|
||||||
rec := NewReceiver(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80), user)
|
rec := NewReceiver(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80), user)
|
||||||
assert.Bool(rec.HasUser(user)).IsTrue()
|
assert.Bool(rec.HasUser(user)).IsTrue()
|
||||||
assert.Int(len(rec.Accounts)).Equals(1)
|
assert.Int(len(rec.Accounts)).Equals(1)
|
||||||
|
|
||||||
id2 := protocol.NewID(uuid.New())
|
id2 := protocol.NewID(uuid.New())
|
||||||
user2 := protocol.NewUser(id2, protocol.UserLevel(0), 100, "")
|
alters2 := protocol.NewAlterIDs(id2, 100)
|
||||||
|
user2 := protocol.NewUser(id2, alters2, protocol.UserLevel(0), "")
|
||||||
assert.Bool(rec.HasUser(user2)).IsFalse()
|
assert.Bool(rec.HasUser(user2)).IsFalse()
|
||||||
|
|
||||||
rec.AddUser(user2)
|
rec.AddUser(user2)
|
||||||
|
Loading…
Reference in New Issue
Block a user