account.go 1.9 KB

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