1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-03 20:45:23 +00:00
v2fly/common/protocol/user_validator.go

164 lines
3.5 KiB
Go
Raw Normal View History

2016-02-25 15:40:43 +00:00
package protocol
import (
"sync"
"time"
2016-04-25 16:39:30 +00:00
"github.com/v2ray/v2ray-core/common"
"github.com/v2ray/v2ray-core/common/signal"
2016-02-25 15:40:43 +00:00
)
const (
updateIntervalSec = 10
cacheDurationSec = 120
)
type idEntry struct {
id *ID
userIdx int
lastSec Timestamp
lastSecRemoval Timestamp
}
type UserValidator interface {
2016-04-25 16:39:30 +00:00
common.Releasable
2016-02-25 15:40:43 +00:00
Add(user *User) error
Get(timeHash []byte) (*User, Timestamp, bool)
}
type TimedUserValidator struct {
2016-06-22 13:21:40 +00:00
sync.RWMutex
running bool
2016-02-25 15:40:43 +00:00
validUsers []*User
userHash map[[16]byte]*indexTimePair
ids []*idEntry
hasher IDHash
2016-04-25 16:39:30 +00:00
cancel *signal.CancelSignal
2016-02-25 15:40:43 +00: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),
ids: make([]*idEntry, 0, 512),
hasher: hasher,
2016-06-22 13:21:40 +00:00
running: true,
2016-04-25 16:39:30 +00:00
cancel: signal.NewCloseSignal(),
2016-02-25 15:40:43 +00:00
}
2016-06-04 17:59:23 +00:00
go tus.updateUserHash(updateIntervalSec * time.Second)
2016-02-25 15:40:43 +00:00
return tus
}
2016-04-25 16:39:30 +00:00
func (this *TimedUserValidator) Release() {
2016-06-22 13:21:40 +00:00
if !this.running {
return
}
2016-04-25 16:39:30 +00:00
this.cancel.Cancel()
2016-04-28 19:13:51 +00:00
<-this.cancel.WaitForDone()
2016-04-25 16:39:30 +00:00
2016-06-22 13:21:40 +00:00
this.Lock()
defer this.Unlock()
if !this.running {
return
}
this.running = false
2016-04-25 16:39:30 +00:00
this.validUsers = nil
this.userHash = nil
this.ids = nil
this.hasher = nil
this.cancel = nil
}
2016-02-25 15:40:43 +00: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 {
2016-06-26 20:34:48 +00:00
idHash.Write(entry.lastSec.Bytes(nil))
2016-02-25 15:40:43 +00:00
idHash.Sum(hashValue[:0])
idHash.Reset()
2016-06-26 20:34:48 +00:00
idHash.Write(entry.lastSecRemoval.Bytes(nil))
2016-02-25 15:40:43 +00:00
idHash.Sum(hashValueRemoval[:0])
idHash.Reset()
2016-06-22 13:21:40 +00:00
this.Lock()
2016-02-25 15:40:43 +00:00
this.userHash[hashValue] = &indexTimePair{idx, entry.lastSec}
delete(this.userHash, hashValueRemoval)
2016-06-22 13:21:40 +00:00
this.Unlock()
2016-02-25 15:40:43 +00:00
entry.lastSec++
entry.lastSecRemoval++
}
}
2016-06-04 17:59:23 +00:00
func (this *TimedUserValidator) updateUserHash(interval time.Duration) {
2016-04-25 16:39:30 +00:00
L:
for {
select {
2016-06-03 22:38:22 +00:00
case now := <-time.After(interval):
2016-04-25 16:39:30 +00:00
nowSec := Timestamp(now.Unix() + cacheDurationSec)
for _, entry := range this.ids {
this.generateNewHashes(nowSec, entry.userIdx, entry)
}
2016-06-04 17:59:23 +00:00
case <-this.cancel.WaitForCancel():
2016-04-25 16:39:30 +00:00
break L
2016-02-25 15:40:43 +00:00
}
}
2016-06-04 17:59:23 +00:00
this.cancel.Done()
2016-02-25 15:40:43 +00:00
}
func (this *TimedUserValidator) Add(user *User) error {
idx := len(this.validUsers)
this.validUsers = append(this.validUsers, user)
2016-05-28 11:44:11 +00:00
account := user.Account.(*VMessAccount)
2016-02-25 15:40:43 +00:00
nowSec := time.Now().Unix()
entry := &idEntry{
2016-05-28 11:44:11 +00:00
id: account.ID,
2016-02-25 15:40:43 +00:00
userIdx: idx,
lastSec: Timestamp(nowSec - cacheDurationSec),
lastSecRemoval: Timestamp(nowSec - cacheDurationSec*3),
}
this.generateNewHashes(Timestamp(nowSec+cacheDurationSec), idx, entry)
this.ids = append(this.ids, entry)
2016-05-28 11:44:11 +00:00
for _, alterid := range account.AlterIDs {
2016-02-25 15:40:43 +00:00
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) {
2016-06-22 13:21:40 +00:00
defer this.RUnlock()
this.RLock()
if !this.running {
return nil, 0, false
}
2016-02-25 15:40:43 +00:00
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
}