account.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. Seed: a.Seed,
  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. // Seed. Details TBD
  35. Seed string
  36. }
  37. // Equals implements protocol.Account.Equals().
  38. func (a *MemoryAccount) Equals(account protocol.Account) bool {
  39. vlessAccount, ok := account.(*MemoryAccount)
  40. if !ok {
  41. return false
  42. }
  43. return a.ID.Equals(vlessAccount.ID)
  44. }
  45. func (a *MemoryAccount) ToProto() proto.Message {
  46. return &Account{
  47. Id: a.ID.String(),
  48. Flow: a.Flow,
  49. Encryption: a.Encryption,
  50. XorMode: a.XorMode,
  51. Seconds: a.Seconds,
  52. Padding: a.Padding,
  53. }
  54. }