mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-20 08:16:55 -05:00
remove cache of existing user hashes
This commit is contained in:
parent
1a10f7c4da
commit
4624de091b
@ -1,24 +0,0 @@
|
|||||||
package collect
|
|
||||||
|
|
||||||
type SizedQueue struct {
|
|
||||||
elements []interface{}
|
|
||||||
nextPos int
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSizedQueue(size int) *SizedQueue {
|
|
||||||
return &SizedQueue{
|
|
||||||
elements: make([]interface{}, size),
|
|
||||||
nextPos: 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Put puts a new element into the queue and pop out the first element if queue is full.
|
|
||||||
func (this *SizedQueue) Put(element interface{}) interface{} {
|
|
||||||
res := this.elements[this.nextPos]
|
|
||||||
this.elements[this.nextPos] = element
|
|
||||||
this.nextPos++
|
|
||||||
if this.nextPos == len(this.elements) {
|
|
||||||
this.nextPos = 0
|
|
||||||
}
|
|
||||||
return res
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
package collect_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core/common/collect"
|
|
||||||
v2testing "github.com/v2ray/v2ray-core/testing"
|
|
||||||
"github.com/v2ray/v2ray-core/testing/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestSizedQueue(t *testing.T) {
|
|
||||||
v2testing.Current(t)
|
|
||||||
|
|
||||||
queue := collect.NewSizedQueue(2)
|
|
||||||
assert.Pointer(queue.Put(1)).IsNil()
|
|
||||||
assert.Pointer(queue.Put(2)).IsNil()
|
|
||||||
assert.Int(queue.Put(3).(int)).Equals(1)
|
|
||||||
}
|
|
@ -4,7 +4,6 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core/common/collect"
|
|
||||||
"github.com/v2ray/v2ray-core/common/serial"
|
"github.com/v2ray/v2ray-core/common/serial"
|
||||||
"github.com/v2ray/v2ray-core/proxy/vmess"
|
"github.com/v2ray/v2ray-core/proxy/vmess"
|
||||||
)
|
)
|
||||||
@ -34,7 +33,7 @@ type idEntry struct {
|
|||||||
id *vmess.ID
|
id *vmess.ID
|
||||||
userIdx int
|
userIdx int
|
||||||
lastSec Timestamp
|
lastSec Timestamp
|
||||||
hashes *collect.SizedQueue
|
lastSecRemoval Timestamp
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserSet interface {
|
type UserSet interface {
|
||||||
@ -67,21 +66,24 @@ func NewTimedUserSet() UserSet {
|
|||||||
|
|
||||||
func (us *TimedUserSet) generateNewHashes(nowSec Timestamp, idx int, entry *idEntry) {
|
func (us *TimedUserSet) generateNewHashes(nowSec Timestamp, idx int, entry *idEntry) {
|
||||||
var hashValue [16]byte
|
var hashValue [16]byte
|
||||||
|
var hashValueRemoval [16]byte
|
||||||
idHash := IDHash(entry.id.Bytes())
|
idHash := IDHash(entry.id.Bytes())
|
||||||
for entry.lastSec <= nowSec {
|
for entry.lastSec <= nowSec {
|
||||||
idHash.Write(entry.lastSec.Bytes())
|
idHash.Write(entry.lastSec.Bytes())
|
||||||
idHash.Sum(hashValue[:0])
|
idHash.Sum(hashValue[:0])
|
||||||
idHash.Reset()
|
idHash.Reset()
|
||||||
|
|
||||||
hash2Remove := entry.hashes.Put(hashValue)
|
idHash.Write(entry.lastSecRemoval.Bytes())
|
||||||
|
idHash.Sum(hashValueRemoval[:0])
|
||||||
|
idHash.Reset()
|
||||||
|
|
||||||
us.access.Lock()
|
us.access.Lock()
|
||||||
us.userHash[hashValue] = indexTimePair{idx, entry.lastSec}
|
us.userHash[hashValue] = indexTimePair{idx, entry.lastSec}
|
||||||
if hash2Remove != nil {
|
delete(us.userHash, hashValueRemoval)
|
||||||
delete(us.userHash, hash2Remove.([16]byte))
|
|
||||||
}
|
|
||||||
us.access.Unlock()
|
us.access.Unlock()
|
||||||
|
|
||||||
entry.lastSec++
|
entry.lastSec++
|
||||||
|
entry.lastSecRemoval++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,7 +106,7 @@ func (us *TimedUserSet) AddUser(user *vmess.User) error {
|
|||||||
id: user.ID,
|
id: user.ID,
|
||||||
userIdx: idx,
|
userIdx: idx,
|
||||||
lastSec: Timestamp(nowSec - cacheDurationSec),
|
lastSec: Timestamp(nowSec - cacheDurationSec),
|
||||||
hashes: collect.NewSizedQueue(2*cacheDurationSec + 1),
|
lastSecRemoval: Timestamp(nowSec - cacheDurationSec*3),
|
||||||
}
|
}
|
||||||
us.generateNewHashes(Timestamp(nowSec+cacheDurationSec), idx, entry)
|
us.generateNewHashes(Timestamp(nowSec+cacheDurationSec), idx, entry)
|
||||||
us.ids = append(us.ids, entry)
|
us.ids = append(us.ids, entry)
|
||||||
@ -113,7 +115,7 @@ func (us *TimedUserSet) AddUser(user *vmess.User) error {
|
|||||||
id: alterid,
|
id: alterid,
|
||||||
userIdx: idx,
|
userIdx: idx,
|
||||||
lastSec: Timestamp(nowSec - cacheDurationSec),
|
lastSec: Timestamp(nowSec - cacheDurationSec),
|
||||||
hashes: collect.NewSizedQueue(2*cacheDurationSec + 1),
|
lastSecRemoval: Timestamp(nowSec - cacheDurationSec*3),
|
||||||
}
|
}
|
||||||
us.generateNewHashes(Timestamp(nowSec+cacheDurationSec), idx, entry)
|
us.generateNewHashes(Timestamp(nowSec+cacheDurationSec), idx, entry)
|
||||||
us.ids = append(us.ids, entry)
|
us.ids = append(us.ids, entry)
|
||||||
|
Loading…
Reference in New Issue
Block a user