v2fly/common/protocol/user.go

42 lines
966 B
Go
Raw Normal View History

package protocol
2015-10-16 10:03:22 +00:00
2021-06-19 13:36:54 +00:00
import "github.com/v2fly/v2ray-core/v4/common/serial"
2017-08-24 20:56:31 +00:00
func (u *User) GetTypedAccount() (Account, error) {
if u.GetAccount() == nil {
return nil, newError("Account missing").AtWarning()
}
2016-10-16 12:22:21 +00:00
2021-06-19 13:36:54 +00:00
rawAccount, err := serial.GetInstanceOf(u.Account)
2016-09-17 22:41:21 +00:00
if err != nil {
return nil, err
}
2016-10-16 12:22:21 +00:00
if asAccount, ok := rawAccount.(AsAccount); ok {
return asAccount.AsAccount()
}
if account, ok := rawAccount.(Account); ok {
return account, nil
}
2021-06-19 13:36:54 +00:00
return nil, newError("Unknown account type: ", serial.V2Type(u.Account))
2015-12-12 23:10:35 +00:00
}
2018-08-26 22:11:32 +00:00
func (u *User) ToMemoryUser() (*MemoryUser, error) {
account, err := u.GetTypedAccount()
if err != nil {
return nil, err
}
return &MemoryUser{
Account: account,
Email: u.Email,
Level: u.Level,
}, nil
}
2018-10-15 06:36:50 +00:00
// MemoryUser is a parsed form of User, to reduce number of parsing of Account proto.
2018-08-26 22:11:32 +00:00
type MemoryUser struct {
2018-10-15 06:36:50 +00:00
// Account is the parsed account of the protocol.
2018-08-26 22:11:32 +00:00
Account Account
Email string
Level uint32
}