user.go 1.2 KB

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