account.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. Testpre: a.Testpre,
  23. Testseed: a.Testseed,
  24. }, nil
  25. }
  26. // MemoryAccount is an in-memory form of VLess account.
  27. type MemoryAccount struct {
  28. // ID of the account.
  29. ID *protocol.ID
  30. // Flow of the account. May be "xtls-rprx-vision".
  31. Flow string
  32. Encryption string
  33. XorMode uint32
  34. Seconds uint32
  35. Padding string
  36. Reverse *Reverse
  37. Testpre uint32
  38. Testseed []uint32
  39. }
  40. // Equals implements protocol.Account.Equals().
  41. func (a *MemoryAccount) Equals(account protocol.Account) bool {
  42. vlessAccount, ok := account.(*MemoryAccount)
  43. if !ok {
  44. return false
  45. }
  46. return a.ID.Equals(vlessAccount.ID)
  47. }
  48. func (a *MemoryAccount) ToProto() proto.Message {
  49. return &Account{
  50. Id: a.ID.String(),
  51. Flow: a.Flow,
  52. Encryption: a.Encryption,
  53. XorMode: a.XorMode,
  54. Seconds: a.Seconds,
  55. Padding: a.Padding,
  56. Reverse: a.Reverse,
  57. Testpre: a.Testpre,
  58. Testseed: a.Testseed,
  59. }
  60. }