account.go 1.8 KB

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