account.go 1.3 KB

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