Browse Source

refactor alter id generation

pull/168/head
v2ray 9 years ago
parent
commit
b5f43031d4
  1. 12
      common/protocol/id.go
  2. 2
      common/protocol/raw/encoding_test.go
  3. 22
      common/protocol/user.go
  4. 4
      common/protocol/user_json.go
  5. 3
      proxy/vmess/inbound/inbound.go
  6. 4
      proxy/vmess/outbound/command.go
  7. 6
      proxy/vmess/outbound/receiver_test.go

12
common/protocol/id.go

@ -57,3 +57,15 @@ func NewID(uuid *uuid.UUID) *ID {
md5hash.Sum(id.cmdKey[:0])
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
}

2
common/protocol/raw/encoding_test.go

@ -18,8 +18,8 @@ func TestRequestSerialization(t *testing.T) {
user := protocol.NewUser(
protocol.NewID(uuid.New()),
nil,
protocol.UserLevelUntrusted,
0,
"test@v2ray.com")
expectedRequest := &protocol.RequestHeader{

22
common/protocol/user.go

@ -18,23 +18,13 @@ type User struct {
Email string
}
func NewUser(id *ID, level UserLevel, alterIdCount uint16, email string) *User {
u := &User{
ID: id,
Level: level,
Email: email,
func NewUser(primary *ID, secondary []*ID, level UserLevel, email string) *User {
return &User{
ID: primary,
AlterIDs: secondary,
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 {

4
common/protocol/user_json.go

@ -23,7 +23,9 @@ func (u *User) UnmarshalJSON(data []byte) error {
if err != nil {
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
}

3
proxy/vmess/inbound/inbound.go

@ -50,7 +50,8 @@ func (this *userByEmail) Get(email string) (*protocol.User, bool) {
user, found = this.cache[email]
if !found {
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.Unlock()

4
proxy/vmess/outbound/command.go

@ -6,7 +6,9 @@ import (
)
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)
this.receiverManager.AddDetour(NewReceiver(dest, user), cmd.ValidMin)
}

6
proxy/vmess/outbound/receiver_test.go

@ -15,13 +15,15 @@ func TestReceiverUser(t *testing.T) {
v2testing.Current(t)
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)
assert.Bool(rec.HasUser(user)).IsTrue()
assert.Int(len(rec.Accounts)).Equals(1)
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()
rec.AddUser(user2)

Loading…
Cancel
Save