account.go 1.3 KB

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