1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-20 02:46:10 -04:00
v2fly/proxy/vmess/protocol/user/userset.go

134 lines
3.1 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"
2016-01-12 05:38:43 -05:00
"github.com/v2ray/v2ray-core/common/serial"
2015-12-07 14:32:38 -05:00
"github.com/v2ray/v2ray-core/proxy/vmess"
2015-09-14 12:19:17 -04:00
)
const (
updateIntervalSec = 10
cacheDurationSec = 120
)
2016-01-12 05:38:43 -05:00
type Timestamp int64
func (this Timestamp) Bytes() []byte {
return serial.Int64Literal(this).Bytes()
}
func (this Timestamp) HashBytes() []byte {
once := this.Bytes()
bytes := make([]byte, 0, 32)
bytes = append(bytes, once...)
bytes = append(bytes, once...)
bytes = append(bytes, once...)
bytes = append(bytes, once...)
return bytes
}
2016-01-11 16:51:35 -05:00
type idEntry struct {
id *vmess.ID
userIdx int
2016-01-12 05:38:43 -05:00
lastSec Timestamp
2016-01-11 16:51:35 -05:00
hashes *collect.SizedQueue
}
2015-09-14 13:03:31 -04:00
type UserSet interface {
2015-12-07 14:32:38 -05:00
AddUser(user vmess.User) error
2016-01-12 05:38:43 -05:00
GetUser(timeHash []byte) (vmess.User, Timestamp, bool)
2015-09-14 13:03:31 -04:00
}
type TimedUserSet struct {
2016-01-11 16:51:35 -05:00
validUsers []vmess.User
userHash map[string]indexTimePair
ids []*idEntry
access sync.RWMutex
2015-09-14 19:32:55 -04:00
}
type indexTimePair struct {
2015-09-15 18:06:22 -04:00
index int
2016-01-12 05:38:43 -05:00
timeSec Timestamp
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{
2016-01-10 03:11:46 -05:00
validUsers: make([]vmess.User, 0, 16),
userHash: make(map[string]indexTimePair, 512),
access: sync.RWMutex{},
2016-01-11 16:51:35 -05:00
ids: make([]*idEntry, 0, 512),
2015-09-23 12:19:05 -04:00
}
go tus.updateUserHash(time.Tick(updateIntervalSec * time.Second))
return tus
}
2016-01-12 05:38:43 -05:00
func (us *TimedUserSet) generateNewHashes(nowSec Timestamp, idx int, entry *idEntry) {
2016-01-11 16:51:35 -05:00
for entry.lastSec <= nowSec {
2016-01-12 05:38:43 -05:00
idHash := IDHash(entry.id.Bytes())
idHash.Write(entry.lastSec.Bytes())
idHashSlice := idHash.Sum(nil)
2016-01-11 16:51:35 -05:00
hashValue := string(idHashSlice)
2015-09-27 19:11:40 -04:00
us.access.Lock()
2016-01-11 16:51:35 -05:00
us.userHash[hashValue] = indexTimePair{idx, entry.lastSec}
2015-09-27 19:11:40 -04:00
us.access.Unlock()
2016-01-11 16:51:35 -05:00
hash2Remove := entry.hashes.Put(hashValue)
if hash2Remove != nil {
us.access.Lock()
delete(us.userHash, hash2Remove.(string))
us.access.Unlock()
}
entry.lastSec++
2015-09-19 15:56:00 -04:00
}
}
2015-09-14 13:03:31 -04:00
func (us *TimedUserSet) updateUserHash(tick <-chan time.Time) {
2015-10-10 15:29:26 -04:00
for now := range tick {
2016-01-12 05:38:43 -05:00
nowSec := Timestamp(now.Unix() + cacheDurationSec)
2016-01-11 16:51:35 -05:00
for _, entry := range us.ids {
us.generateNewHashes(nowSec, entry.userIdx, entry)
2015-09-15 18:06:22 -04:00
}
2015-09-14 12:19:17 -04:00
}
}
2015-12-07 14:32:38 -05:00
func (us *TimedUserSet) AddUser(user vmess.User) error {
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()
2016-01-11 16:51:35 -05:00
entry := &idEntry{
id: user.ID(),
userIdx: idx,
2016-01-12 05:38:43 -05:00
lastSec: Timestamp(nowSec - cacheDurationSec),
2016-01-11 16:51:35 -05:00
hashes: collect.NewSizedQueue(2*cacheDurationSec + 1),
}
2016-01-12 05:38:43 -05:00
us.generateNewHashes(Timestamp(nowSec+cacheDurationSec), idx, entry)
2016-01-11 16:51:35 -05:00
us.ids = append(us.ids, entry)
2016-01-08 18:10:57 -05:00
for _, alterid := range user.AlterIDs() {
2016-01-11 16:51:35 -05:00
entry := &idEntry{
id: alterid,
userIdx: idx,
2016-01-12 05:38:43 -05:00
lastSec: Timestamp(nowSec - cacheDurationSec),
2016-01-11 16:51:35 -05:00
hashes: collect.NewSizedQueue(2*cacheDurationSec + 1),
}
2016-01-12 05:38:43 -05:00
us.generateNewHashes(Timestamp(nowSec+cacheDurationSec), idx, entry)
2016-01-11 16:51:35 -05:00
us.ids = append(us.ids, entry)
2016-01-08 18:10:57 -05:00
}
2015-09-19 15:56:00 -04:00
return nil
}
2016-01-12 05:38:43 -05:00
func (us *TimedUserSet) GetUser(userHash []byte) (vmess.User, Timestamp, 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
}