user.go 947 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package protocol
  2. import "github.com/xtls/xray-core/common/errors"
  3. func (u *User) GetTypedAccount() (Account, error) {
  4. if u.GetAccount() == nil {
  5. return nil, errors.New("Account missing").AtWarning()
  6. }
  7. rawAccount, err := u.Account.GetInstance()
  8. if err != nil {
  9. return nil, err
  10. }
  11. if asAccount, ok := rawAccount.(AsAccount); ok {
  12. return asAccount.AsAccount()
  13. }
  14. if account, ok := rawAccount.(Account); ok {
  15. return account, nil
  16. }
  17. return nil, errors.New("Unknown account type: ", u.Account.Type)
  18. }
  19. func (u *User) ToMemoryUser() (*MemoryUser, error) {
  20. account, err := u.GetTypedAccount()
  21. if err != nil {
  22. return nil, err
  23. }
  24. return &MemoryUser{
  25. Account: account,
  26. Email: u.Email,
  27. Level: u.Level,
  28. }, nil
  29. }
  30. // MemoryUser is a parsed form of User, to reduce number of parsing of Account proto.
  31. type MemoryUser struct {
  32. // Account is the parsed account of the protocol.
  33. Account Account
  34. Email string
  35. Level uint32
  36. }