v2ray-core/proxy/vmess/protocol/userset.go

130 lines
2.7 KiB
Go
Raw Normal View History

package protocol
2015-09-07 21:48:19 +00:00
import (
2015-09-19 19:56:00 +00:00
"container/heap"
2015-09-14 16:19:17 +00:00
"time"
2015-09-16 19:13:13 +00:00
2015-09-19 19:56:00 +00:00
"github.com/v2ray/v2ray-core/log"
2015-09-14 16:19:17 +00:00
)
const (
updateIntervalSec = 10
cacheDurationSec = 120
)
2015-09-14 17:03:31 +00:00
type UserSet interface {
AddUser(user User) error
2015-09-14 23:32:55 +00:00
GetUser(timeHash []byte) (*ID, int64, bool)
2015-09-14 17:03:31 +00:00
}
type TimedUserSet struct {
2015-09-14 16:19:17 +00:00
validUserIds []ID
2015-09-14 23:32:55 +00:00
userHashes map[string]indexTimePair
2015-09-19 19:56:00 +00:00
hash2Remove hashEntrySet
2015-09-14 23:32:55 +00:00
}
type indexTimePair struct {
2015-09-15 22:06:22 +00:00
index int
timeSec int64
2015-09-14 16:19:17 +00:00
}
type hashEntry struct {
hash string
timeSec int64
}
2015-09-19 19:56:00 +00:00
type hashEntrySet []*hashEntry
func (set hashEntrySet) Len() int {
return len(set)
}
func (set hashEntrySet) Less(i, j int) bool {
return set[i].timeSec < set[j].timeSec
}
func (set hashEntrySet) Swap(i, j int) {
tmp := set[i]
set[i] = set[j]
set[j] = tmp
}
func (set *hashEntrySet) Push(value interface{}) {
entry := value.(*hashEntry)
*set = append(*set, entry)
}
func (set *hashEntrySet) Pop() interface{} {
old := *set
n := len(old)
v := old[n-1]
*set = old[:n-1]
return v
}
2015-09-14 17:03:31 +00:00
func NewTimedUserSet() UserSet {
vuSet := new(TimedUserSet)
2015-09-12 20:11:54 +00:00
vuSet.validUserIds = make([]ID, 0, 16)
2015-09-14 23:32:55 +00:00
vuSet.userHashes = make(map[string]indexTimePair)
2015-09-19 19:56:00 +00:00
vuSet.hash2Remove = make(hashEntrySet, 0, cacheDurationSec*10)
2015-09-14 16:19:17 +00:00
go vuSet.updateUserHash(time.Tick(updateIntervalSec * time.Second))
return vuSet
}
2015-09-19 19:56:00 +00:00
func (us *TimedUserSet) generateNewHashes(lastSec, nowSec int64, idx int, id ID) {
idHash := NewTimeHash(HMACHash{})
2015-09-19 19:56:00 +00:00
for lastSec < nowSec+cacheDurationSec {
idHash := idHash.Hash(id.Bytes, lastSec)
log.Debug("Valid User Hash: %v", idHash)
heap.Push(&us.hash2Remove, &hashEntry{string(idHash), lastSec})
us.userHashes[string(idHash)] = indexTimePair{idx, lastSec}
lastSec++
}
}
2015-09-14 17:03:31 +00:00
func (us *TimedUserSet) updateUserHash(tick <-chan time.Time) {
2015-09-14 16:19:17 +00:00
now := time.Now().UTC()
2015-09-19 19:56:00 +00:00
lastSec := now.Unix()
2015-09-14 20:45:55 +00:00
lastSec2Remove := now.Unix()
2015-09-19 19:56:00 +00:00
2015-09-14 16:19:17 +00:00
for {
now := <-tick
nowSec := now.UTC().Unix()
2015-09-15 22:06:22 +00:00
remove2Sec := nowSec - cacheDurationSec
2015-09-14 16:19:17 +00:00
if remove2Sec > lastSec2Remove {
for lastSec2Remove+1 < remove2Sec {
2015-09-19 19:56:00 +00:00
front := heap.Pop(&us.hash2Remove)
entry := front.(*hashEntry)
2015-09-14 16:19:17 +00:00
lastSec2Remove = entry.timeSec
delete(us.userHashes, entry.hash)
}
}
2015-09-19 19:56:00 +00:00
for idx, id := range us.validUserIds {
us.generateNewHashes(lastSec, nowSec, idx, id)
2015-09-15 22:06:22 +00:00
}
2015-09-14 16:19:17 +00:00
}
}
2015-09-14 17:03:31 +00:00
func (us *TimedUserSet) AddUser(user User) error {
id := user.Id
2015-09-19 19:56:00 +00:00
idx := len(us.validUserIds)
us.validUserIds = append(us.validUserIds, id)
2015-09-19 19:56:00 +00:00
nowSec := time.Now().UTC().Unix()
lastSec := nowSec - cacheDurationSec
us.generateNewHashes(lastSec, nowSec, idx, id)
return nil
}
2015-09-14 23:32:55 +00:00
func (us TimedUserSet) GetUser(userHash []byte) (*ID, int64, bool) {
pair, found := us.userHashes[string(userHash)]
if found {
2015-09-14 23:32:55 +00:00
return &us.validUserIds[pair.index], pair.timeSec, true
}
2015-09-14 23:32:55 +00:00
return nil, 0, false
2015-09-07 21:48:19 +00:00
}