1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 01:27:03 -05:00
v2fly/vuserset.go

41 lines
846 B
Go
Raw Normal View History

2015-09-07 17:48:19 -04:00
package core
import (
"encoding/base64"
)
2015-09-07 17:48:19 -04:00
type VUserSet struct {
validUserIds []VID
userIdsAskHash map[string]int
}
func NewVUserSet() *VUserSet {
vuSet := new(VUserSet)
vuSet.validUserIds = make([]VID, 0, 16)
2015-09-08 19:11:02 -04:00
vuSet.userIdsAskHash = make(map[string]int)
return vuSet
}
func hashBytesToString(hash []byte) string {
return base64.StdEncoding.EncodeToString(hash)
}
func (us *VUserSet) AddUser(user VUser) error {
id := user.Id
us.validUserIds = append(us.validUserIds, id)
idBase64 := hashBytesToString(id.Hash([]byte("ASK")))
us.userIdsAskHash[idBase64] = len(us.validUserIds) - 1
return nil
}
func (us VUserSet) IsValidUserId(askHash []byte) (*VID, bool) {
askBase64 := hashBytesToString(askHash)
idIndex, found := us.userIdsAskHash[askBase64]
if found {
return &us.validUserIds[idIndex], true
}
return nil, false
2015-09-07 17:48:19 -04:00
}