github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/proxy/vmess/account.go (about) 1 //go:build !confonly 2 // +build !confonly 3 4 package vmess 5 6 import ( 7 "strings" 8 9 "github.com/v2fly/v2ray-core/v5/common/dice" 10 "github.com/v2fly/v2ray-core/v5/common/protocol" 11 "github.com/v2fly/v2ray-core/v5/common/uuid" 12 ) 13 14 // MemoryAccount is an in-memory form of VMess account. 15 type MemoryAccount struct { 16 // ID is the main ID of the account. 17 ID *protocol.ID 18 // AlterIDs are the alternative IDs of the account. 19 AlterIDs []*protocol.ID 20 // Security type of the account. Used for client connections. 21 Security protocol.SecurityType 22 23 AuthenticatedLengthExperiment bool 24 NoTerminationSignal bool 25 } 26 27 // AnyValidID returns an ID that is either the main ID or one of the alternative IDs if any. 28 func (a *MemoryAccount) AnyValidID() *protocol.ID { 29 if len(a.AlterIDs) == 0 { 30 return a.ID 31 } 32 return a.AlterIDs[dice.Roll(len(a.AlterIDs))] 33 } 34 35 // Equals implements protocol.Account. 36 func (a *MemoryAccount) Equals(account protocol.Account) bool { 37 vmessAccount, ok := account.(*MemoryAccount) 38 if !ok { 39 return false 40 } 41 // TODO: handle AlterIds difference 42 return a.ID.Equals(vmessAccount.ID) 43 } 44 45 // AsAccount implements protocol.Account. 46 func (a *Account) AsAccount() (protocol.Account, error) { 47 id, err := uuid.ParseString(a.Id) 48 if err != nil { 49 return nil, newError("failed to parse ID").Base(err).AtError() 50 } 51 protoID := protocol.NewID(id) 52 var AuthenticatedLength, NoTerminationSignal bool 53 if strings.Contains(a.TestsEnabled, "AuthenticatedLength") { 54 AuthenticatedLength = true 55 } 56 if strings.Contains(a.TestsEnabled, "NoTerminationSignal") { 57 NoTerminationSignal = true 58 } 59 return &MemoryAccount{ 60 ID: protoID, 61 AlterIDs: protocol.NewAlterIDs(protoID, uint16(a.AlterId)), 62 Security: a.SecuritySettings.GetSecurityType(), 63 AuthenticatedLengthExperiment: AuthenticatedLength, 64 NoTerminationSignal: NoTerminationSignal, 65 }, nil 66 }