mirror of https://github.com/v2ray/v2ray-core
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
836 B
41 lines
836 B
9 years ago
|
package core
|
||
|
|
||
9 years ago
|
import (
|
||
|
"encoding/base64"
|
||
|
)
|
||
|
|
||
9 years ago
|
type UserSet struct {
|
||
|
validUserIds []ID
|
||
9 years ago
|
userIdsAskHash map[string]int
|
||
|
}
|
||
|
|
||
9 years ago
|
func NewUserSet() *UserSet {
|
||
|
vuSet := new(UserSet)
|
||
|
vuSet.validUserIds = make([]ID, 0, 16)
|
||
9 years ago
|
vuSet.userIdsAskHash = make(map[string]int)
|
||
9 years ago
|
return vuSet
|
||
|
}
|
||
|
|
||
|
func hashBytesToString(hash []byte) string {
|
||
|
return base64.StdEncoding.EncodeToString(hash)
|
||
|
}
|
||
|
|
||
9 years ago
|
func (us *UserSet) AddUser(user User) error {
|
||
9 years ago
|
id := user.Id
|
||
|
us.validUserIds = append(us.validUserIds, id)
|
||
|
|
||
|
idBase64 := hashBytesToString(id.Hash([]byte("ASK")))
|
||
|
us.userIdsAskHash[idBase64] = len(us.validUserIds) - 1
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
9 years ago
|
func (us UserSet) IsValidUserId(askHash []byte) (*ID, bool) {
|
||
9 years ago
|
askBase64 := hashBytesToString(askHash)
|
||
|
idIndex, found := us.userIdsAskHash[askBase64]
|
||
|
if found {
|
||
|
return &us.validUserIds[idIndex], true
|
||
|
}
|
||
|
return nil, false
|
||
9 years ago
|
}
|