github.com/xtls/xray-core@v1.8.12-0.20240518155711-3168d27b0bdb/common/protocol/user.go (about)

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