account.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package vless
  2. import (
  3. "google.golang.org/protobuf/proto"
  4. "github.com/xtls/xray-core/common/errors"
  5. "github.com/xtls/xray-core/common/protocol"
  6. "github.com/xtls/xray-core/common/uuid"
  7. )
  8. // AsAccount implements protocol.Account.AsAccount().
  9. func (a *Account) AsAccount() (protocol.Account, error) {
  10. id, err := uuid.ParseString(a.Id)
  11. if err != nil {
  12. return nil, errors.New("failed to parse ID").Base(err).AtError()
  13. }
  14. return &MemoryAccount{
  15. ID: protocol.NewID(id),
  16. Flow: a.Flow, // needs parser here?
  17. Encryption: a.Encryption, // needs parser here?
  18. XorMode: a.XorMode,
  19. Seconds: a.Seconds,
  20. Padding: a.Padding,
  21. Reverse: a.Reverse,
  22. }, nil
  23. }
  24. // MemoryAccount is an in-memory form of VLess account.
  25. type MemoryAccount struct {
  26. // ID of the account.
  27. ID *protocol.ID
  28. // Flow of the account. May be "xtls-rprx-vision".
  29. Flow string
  30. Encryption string
  31. XorMode uint32
  32. Seconds uint32
  33. Padding string
  34. Reverse *Reverse
  35. }
  36. // Equals implements protocol.Account.Equals().
  37. func (a *MemoryAccount) Equals(account protocol.Account) bool {
  38. vlessAccount, ok := account.(*MemoryAccount)
  39. if !ok {
  40. return false
  41. }
  42. return a.ID.Equals(vlessAccount.ID)
  43. }
  44. func (a *MemoryAccount) ToProto() proto.Message {
  45. return &Account{
  46. Id: a.ID.String(),
  47. Flow: a.Flow,
  48. Encryption: a.Encryption,
  49. XorMode: a.XorMode,
  50. Seconds: a.Seconds,
  51. Padding: a.Padding,
  52. Reverse: a.Reverse,
  53. }
  54. }