account.go 1.9 KB

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