account.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // +build !confonly
  2. package vmess
  3. import (
  4. "github.com/v2fly/v2ray-core/v4/common/dice"
  5. "github.com/v2fly/v2ray-core/v4/common/protocol"
  6. "github.com/v2fly/v2ray-core/v4/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. }
  17. // AnyValidID returns an ID that is either the main ID or one of the alternative IDs if any.
  18. func (a *MemoryAccount) AnyValidID() *protocol.ID {
  19. if len(a.AlterIDs) == 0 {
  20. return a.ID
  21. }
  22. return a.AlterIDs[dice.Roll(len(a.AlterIDs))]
  23. }
  24. // Equals implements protocol.Account.
  25. func (a *MemoryAccount) Equals(account protocol.Account) bool {
  26. vmessAccount, ok := account.(*MemoryAccount)
  27. if !ok {
  28. return false
  29. }
  30. // TODO: handle AlterIds difference
  31. return a.ID.Equals(vmessAccount.ID)
  32. }
  33. // AsAccount implements protocol.Account.
  34. func (a *Account) AsAccount() (protocol.Account, error) {
  35. id, err := uuid.ParseString(a.Id)
  36. if err != nil {
  37. return nil, newError("failed to parse ID").Base(err).AtError()
  38. }
  39. protoID := protocol.NewID(id)
  40. return &MemoryAccount{
  41. ID: protoID,
  42. AlterIDs: protocol.NewAlterIDs(protoID, uint16(a.AlterId)),
  43. Security: a.SecuritySettings.GetSecurityType(),
  44. }, nil
  45. }