1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-10 06:16:53 -05:00
v2fly/proxy/vmess/protocol/user/userset.go

98 lines
2.4 KiB
Go
Raw Normal View History

2015-09-19 18:11:14 -04:00
package user
2015-09-07 17:48:19 -04:00
import (
2015-09-27 19:11:40 -04:00
"sync"
2015-09-14 12:19:17 -04:00
"time"
2015-09-16 15:13:13 -04:00
2015-09-23 12:19:05 -04:00
"github.com/v2ray/v2ray-core/common/collect"
2015-10-16 06:03:22 -04:00
"github.com/v2ray/v2ray-core/proxy/vmess/config"
2015-09-14 12:19:17 -04:00
)
const (
updateIntervalSec = 10
cacheDurationSec = 120
)
2015-09-14 13:03:31 -04:00
type UserSet interface {
2015-10-16 06:03:22 -04:00
AddUser(user config.User) error
2015-10-30 19:38:31 -04:00
GetUser(timeHash []byte) (config.User, int64, bool)
2015-09-14 13:03:31 -04:00
}
type TimedUserSet struct {
2015-10-31 09:08:13 -04:00
validUsers []config.User
2015-09-27 19:11:40 -04:00
userHash map[string]indexTimePair
userHashDeleteQueue *collect.TimedQueue
access sync.RWMutex
2015-09-14 19:32:55 -04:00
}
type indexTimePair struct {
2015-09-15 18:06:22 -04:00
index int
timeSec int64
2015-09-14 12:19:17 -04:00
}
2015-09-14 13:03:31 -04:00
func NewTimedUserSet() UserSet {
2015-09-23 12:19:05 -04:00
tus := &TimedUserSet{
2015-10-31 09:08:13 -04:00
validUsers: make([]config.User, 0, 16),
2015-09-27 19:11:40 -04:00
userHash: make(map[string]indexTimePair, 512),
userHashDeleteQueue: collect.NewTimedQueue(updateIntervalSec),
access: sync.RWMutex{},
2015-09-23 12:19:05 -04:00
}
go tus.updateUserHash(time.Tick(updateIntervalSec * time.Second))
2015-09-27 19:11:40 -04:00
go tus.removeEntries(tus.userHashDeleteQueue.RemovedEntries())
2015-09-23 12:19:05 -04:00
return tus
}
2015-09-27 19:11:40 -04:00
func (us *TimedUserSet) removeEntries(entries <-chan interface{}) {
2015-10-10 15:29:26 -04:00
for entry := range entries {
2015-09-27 19:11:40 -04:00
us.access.Lock()
delete(us.userHash, entry.(string))
us.access.Unlock()
}
}
2015-10-16 06:03:22 -04:00
func (us *TimedUserSet) generateNewHashes(lastSec, nowSec int64, idx int, id *config.ID) {
idHash := NewTimeHash(HMACHash{})
2015-10-10 15:29:26 -04:00
for lastSec < nowSec {
2015-09-23 16:17:25 -04:00
idHash := idHash.Hash(id.Bytes[:], lastSec)
2015-09-27 19:11:40 -04:00
us.access.Lock()
us.userHash[string(idHash)] = indexTimePair{idx, lastSec}
us.access.Unlock()
us.userHashDeleteQueue.Add(string(idHash), lastSec+2*cacheDurationSec)
2015-09-19 15:56:00 -04:00
lastSec++
}
}
2015-09-14 13:03:31 -04:00
func (us *TimedUserSet) updateUserHash(tick <-chan time.Time) {
lastSec := time.Now().Unix() - cacheDurationSec
2015-09-19 15:56:00 -04:00
2015-10-10 15:29:26 -04:00
for now := range tick {
nowSec := now.Unix() + cacheDurationSec
2015-10-30 19:38:31 -04:00
for idx, user := range us.validUsers {
us.generateNewHashes(lastSec, nowSec, idx, user.ID())
2015-09-15 18:06:22 -04:00
}
2015-09-23 12:19:05 -04:00
lastSec = nowSec
2015-09-14 12:19:17 -04:00
}
}
2015-10-16 06:03:22 -04:00
func (us *TimedUserSet) AddUser(user config.User) error {
id := user.ID()
2015-10-30 19:38:31 -04:00
idx := len(us.validUsers)
us.validUsers = append(us.validUsers, user)
2015-09-19 15:56:00 -04:00
nowSec := time.Now().Unix()
2015-09-19 15:56:00 -04:00
lastSec := nowSec - cacheDurationSec
2015-10-10 15:43:40 -04:00
us.generateNewHashes(lastSec, nowSec+cacheDurationSec, idx, id)
2015-09-19 15:56:00 -04:00
return nil
}
2015-10-30 19:38:31 -04:00
func (us TimedUserSet) GetUser(userHash []byte) (config.User, int64, bool) {
2015-09-27 19:11:40 -04:00
defer us.access.RUnlock()
us.access.RLock()
pair, found := us.userHash[string(userHash)]
if found {
2015-10-30 19:38:31 -04:00
return us.validUsers[pair.index], pair.timeSec, true
}
2015-09-14 19:32:55 -04:00
return nil, 0, false
2015-09-07 17:48:19 -04:00
}