account.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // +build !confonly
  2. package vmess
  3. import (
  4. "v2ray.com/core/common/dice"
  5. "v2ray.com/core/common/protocol"
  6. "v2ray.com/core/common/uuid"
  7. )
  8. // MemoryAccount is an in-memory form of VMess account.
  9. type MemoryAccount struct {
  10. // ID is the main ID of the account.
  11. ID *protocol.ID
  12. // AlterIDs are the alternative IDs of the account.
  13. AlterIDs []*protocol.ID
  14. // Security type of the account. Used for client connections.
  15. Security protocol.SecurityType
  16. TestsEnabled string
  17. }
  18. // AnyValidID returns an ID that is either the main ID or one of the alternative IDs if any.
  19. func (a *MemoryAccount) AnyValidID() *protocol.ID {
  20. if len(a.AlterIDs) == 0 {
  21. return a.ID
  22. }
  23. return a.AlterIDs[dice.Roll(len(a.AlterIDs))]
  24. }
  25. // Equals implements protocol.Account.
  26. func (a *MemoryAccount) Equals(account protocol.Account) bool {
  27. vmessAccount, ok := account.(*MemoryAccount)
  28. if !ok {
  29. return false
  30. }
  31. // TODO: handle AlterIds difference
  32. return a.ID.Equals(vmessAccount.ID)
  33. }
  34. // AsAccount implements protocol.Account.
  35. func (a *Account) AsAccount() (protocol.Account, error) {
  36. id, err := uuid.ParseString(a.Id)
  37. if err != nil {
  38. return nil, newError("failed to parse ID").Base(err).AtError()
  39. }
  40. protoID := protocol.NewID(id)
  41. return &MemoryAccount{
  42. ID: protoID,
  43. AlterIDs: protocol.NewAlterIDs(protoID, uint16(a.AlterId)),
  44. Security: a.SecuritySettings.GetSecurityType(),
  45. TestsEnabled: a.TestsEnabled,
  46. }, nil
  47. }