1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-09 11:51:02 -05:00
v2fly/common/protocol/user_validator.go

146 lines
3.3 KiB
Go
Raw Normal View History

2016-02-25 10:40:43 -05:00
package protocol
import (
"sync"
"time"
2016-04-25 12:39:30 -04:00
"github.com/v2ray/v2ray-core/common"
"github.com/v2ray/v2ray-core/common/signal"
2016-02-25 10:40:43 -05:00
)
const (
updateIntervalSec = 10
cacheDurationSec = 120
)
type idEntry struct {
id *ID
userIdx int
lastSec Timestamp
lastSecRemoval Timestamp
}
type UserValidator interface {
2016-04-25 12:39:30 -04:00
common.Releasable
2016-02-25 10:40:43 -05:00
Add(user *User) error
Get(timeHash []byte) (*User, Timestamp, bool)
}
type TimedUserValidator struct {
validUsers []*User
userHash map[[16]byte]*indexTimePair
ids []*idEntry
access sync.RWMutex
hasher IDHash
2016-04-25 12:39:30 -04:00
cancel *signal.CancelSignal
2016-02-25 10:40:43 -05:00
}
type indexTimePair struct {
index int
timeSec Timestamp
}
func NewTimedUserValidator(hasher IDHash) UserValidator {
tus := &TimedUserValidator{
validUsers: make([]*User, 0, 16),
userHash: make(map[[16]byte]*indexTimePair, 512),
access: sync.RWMutex{},
ids: make([]*idEntry, 0, 512),
hasher: hasher,
2016-04-25 12:39:30 -04:00
cancel: signal.NewCloseSignal(),
2016-02-25 10:40:43 -05:00
}
2016-04-25 12:39:30 -04:00
go tus.updateUserHash(time.Tick(updateIntervalSec*time.Second), tus.cancel)
2016-02-25 10:40:43 -05:00
return tus
}
2016-04-25 12:39:30 -04:00
func (this *TimedUserValidator) Release() {
this.cancel.Cancel()
2016-04-28 15:13:51 -04:00
<-this.cancel.WaitForDone()
2016-04-25 12:39:30 -04:00
this.validUsers = nil
this.userHash = nil
this.ids = nil
this.hasher = nil
this.cancel = nil
}
2016-02-25 10:40:43 -05:00
func (this *TimedUserValidator) generateNewHashes(nowSec Timestamp, idx int, entry *idEntry) {
var hashValue [16]byte
var hashValueRemoval [16]byte
idHash := this.hasher(entry.id.Bytes())
for entry.lastSec <= nowSec {
idHash.Write(entry.lastSec.Bytes())
idHash.Sum(hashValue[:0])
idHash.Reset()
idHash.Write(entry.lastSecRemoval.Bytes())
idHash.Sum(hashValueRemoval[:0])
idHash.Reset()
this.access.Lock()
this.userHash[hashValue] = &indexTimePair{idx, entry.lastSec}
delete(this.userHash, hashValueRemoval)
this.access.Unlock()
entry.lastSec++
entry.lastSecRemoval++
}
}
2016-04-25 12:39:30 -04:00
func (this *TimedUserValidator) updateUserHash(tick <-chan time.Time, cancel *signal.CancelSignal) {
L:
for {
select {
case now := <-tick:
nowSec := Timestamp(now.Unix() + cacheDurationSec)
for _, entry := range this.ids {
this.generateNewHashes(nowSec, entry.userIdx, entry)
}
case <-cancel.WaitForCancel():
break L
2016-02-25 10:40:43 -05:00
}
}
2016-04-25 12:39:30 -04:00
cancel.Done()
2016-02-25 10:40:43 -05:00
}
func (this *TimedUserValidator) Add(user *User) error {
idx := len(this.validUsers)
this.validUsers = append(this.validUsers, user)
nowSec := time.Now().Unix()
entry := &idEntry{
id: user.ID,
userIdx: idx,
lastSec: Timestamp(nowSec - cacheDurationSec),
lastSecRemoval: Timestamp(nowSec - cacheDurationSec*3),
}
this.generateNewHashes(Timestamp(nowSec+cacheDurationSec), idx, entry)
this.ids = append(this.ids, entry)
for _, alterid := range user.AlterIDs {
entry := &idEntry{
id: alterid,
userIdx: idx,
lastSec: Timestamp(nowSec - cacheDurationSec),
lastSecRemoval: Timestamp(nowSec - cacheDurationSec*3),
}
this.generateNewHashes(Timestamp(nowSec+cacheDurationSec), idx, entry)
this.ids = append(this.ids, entry)
}
return nil
}
func (this *TimedUserValidator) Get(userHash []byte) (*User, Timestamp, bool) {
defer this.access.RUnlock()
this.access.RLock()
var fixedSizeHash [16]byte
copy(fixedSizeHash[:], userHash)
pair, found := this.userHash[fixedSizeHash]
if found {
return this.validUsers[pair.index], pair.timeSec, true
}
return nil, 0, false
}