account.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package vmess
  2. import (
  3. "google.golang.org/protobuf/proto"
  4. "strings"
  5. "github.com/xtls/xray-core/common/errors"
  6. "github.com/xtls/xray-core/common/protocol"
  7. "github.com/xtls/xray-core/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. // Security type of the account. Used for client connections.
  14. Security protocol.SecurityType
  15. AuthenticatedLengthExperiment bool
  16. NoTerminationSignal bool
  17. }
  18. // Equals implements protocol.Account.
  19. func (a *MemoryAccount) Equals(account protocol.Account) bool {
  20. vmessAccount, ok := account.(*MemoryAccount)
  21. if !ok {
  22. return false
  23. }
  24. return a.ID.Equals(vmessAccount.ID)
  25. }
  26. func (a *MemoryAccount) ToProto() proto.Message {
  27. var test = ""
  28. if a.AuthenticatedLengthExperiment {
  29. test = "AuthenticatedLength|"
  30. }
  31. if a.NoTerminationSignal {
  32. test = test + "NoTerminationSignal"
  33. }
  34. return &Account{
  35. Id: a.ID.String(),
  36. TestsEnabled: test,
  37. SecuritySettings: &protocol.SecurityConfig{Type: a.Security},
  38. }
  39. }
  40. // AsAccount implements protocol.Account.
  41. func (a *Account) AsAccount() (protocol.Account, error) {
  42. id, err := uuid.ParseString(a.Id)
  43. if err != nil {
  44. return nil, errors.New("failed to parse ID").Base(err).AtError()
  45. }
  46. protoID := protocol.NewID(id)
  47. var AuthenticatedLength, NoTerminationSignal bool
  48. if strings.Contains(a.TestsEnabled, "AuthenticatedLength") {
  49. AuthenticatedLength = true
  50. }
  51. if strings.Contains(a.TestsEnabled, "NoTerminationSignal") {
  52. NoTerminationSignal = true
  53. }
  54. return &MemoryAccount{
  55. ID: protoID,
  56. Security: a.SecuritySettings.GetSecurityType(),
  57. AuthenticatedLengthExperiment: AuthenticatedLength,
  58. NoTerminationSignal: NoTerminationSignal,
  59. }, nil
  60. }