|
@@ -6,11 +6,11 @@
|
|
|
package vmess
|
|
package vmess
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
|
+ "context"
|
|
|
"sync"
|
|
"sync"
|
|
|
"time"
|
|
"time"
|
|
|
|
|
|
|
|
"v2ray.com/core/common/protocol"
|
|
"v2ray.com/core/common/protocol"
|
|
|
- "v2ray.com/core/common/signal"
|
|
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
const (
|
|
@@ -27,12 +27,11 @@ type idEntry struct {
|
|
|
|
|
|
|
|
type TimedUserValidator struct {
|
|
type TimedUserValidator struct {
|
|
|
sync.RWMutex
|
|
sync.RWMutex
|
|
|
- running bool
|
|
|
|
|
|
|
+ ctx context.Context
|
|
|
validUsers []*protocol.User
|
|
validUsers []*protocol.User
|
|
|
userHash map[[16]byte]*indexTimePair
|
|
userHash map[[16]byte]*indexTimePair
|
|
|
ids []*idEntry
|
|
ids []*idEntry
|
|
|
hasher protocol.IDHash
|
|
hasher protocol.IDHash
|
|
|
- cancel *signal.CancelSignal
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type indexTimePair struct {
|
|
type indexTimePair struct {
|
|
@@ -40,37 +39,18 @@ type indexTimePair struct {
|
|
|
timeSec protocol.Timestamp
|
|
timeSec protocol.Timestamp
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func NewTimedUserValidator(hasher protocol.IDHash) protocol.UserValidator {
|
|
|
|
|
|
|
+func NewTimedUserValidator(ctx context.Context, hasher protocol.IDHash) protocol.UserValidator {
|
|
|
tus := &TimedUserValidator{
|
|
tus := &TimedUserValidator{
|
|
|
|
|
+ ctx: ctx,
|
|
|
validUsers: make([]*protocol.User, 0, 16),
|
|
validUsers: make([]*protocol.User, 0, 16),
|
|
|
userHash: make(map[[16]byte]*indexTimePair, 512),
|
|
userHash: make(map[[16]byte]*indexTimePair, 512),
|
|
|
ids: make([]*idEntry, 0, 512),
|
|
ids: make([]*idEntry, 0, 512),
|
|
|
hasher: hasher,
|
|
hasher: hasher,
|
|
|
- running: true,
|
|
|
|
|
- cancel: signal.NewCloseSignal(),
|
|
|
|
|
}
|
|
}
|
|
|
go tus.updateUserHash(updateIntervalSec * time.Second)
|
|
go tus.updateUserHash(updateIntervalSec * time.Second)
|
|
|
return tus
|
|
return tus
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (v *TimedUserValidator) Release() {
|
|
|
|
|
- if !v.running {
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- v.cancel.Cancel()
|
|
|
|
|
- v.cancel.WaitForDone()
|
|
|
|
|
-
|
|
|
|
|
- v.Lock()
|
|
|
|
|
- defer v.Unlock()
|
|
|
|
|
-
|
|
|
|
|
- if !v.running {
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- v.running = false
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
func (v *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, idx int, entry *idEntry) {
|
|
func (v *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, idx int, entry *idEntry) {
|
|
|
var hashValue [16]byte
|
|
var hashValue [16]byte
|
|
|
var hashValueRemoval [16]byte
|
|
var hashValueRemoval [16]byte
|
|
@@ -93,9 +73,6 @@ func (v *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, idx in
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (v *TimedUserValidator) updateUserHash(interval time.Duration) {
|
|
func (v *TimedUserValidator) updateUserHash(interval time.Duration) {
|
|
|
- v.cancel.WaitThread()
|
|
|
|
|
- defer v.cancel.FinishThread()
|
|
|
|
|
-
|
|
|
|
|
for {
|
|
for {
|
|
|
select {
|
|
select {
|
|
|
case now := <-time.After(interval):
|
|
case now := <-time.After(interval):
|
|
@@ -105,7 +82,7 @@ func (v *TimedUserValidator) updateUserHash(interval time.Duration) {
|
|
|
v.generateNewHashes(nowSec, entry.userIdx, entry)
|
|
v.generateNewHashes(nowSec, entry.userIdx, entry)
|
|
|
}
|
|
}
|
|
|
v.Unlock()
|
|
v.Unlock()
|
|
|
- case <-v.cancel.WaitForCancel():
|
|
|
|
|
|
|
+ case <-v.ctx.Done():
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -151,9 +128,6 @@ func (v *TimedUserValidator) Get(userHash []byte) (*protocol.User, protocol.Time
|
|
|
defer v.RUnlock()
|
|
defer v.RUnlock()
|
|
|
v.RLock()
|
|
v.RLock()
|
|
|
|
|
|
|
|
- if !v.running {
|
|
|
|
|
- return nil, 0, false
|
|
|
|
|
- }
|
|
|
|
|
var fixedSizeHash [16]byte
|
|
var fixedSizeHash [16]byte
|
|
|
copy(fixedSizeHash[:], userHash)
|
|
copy(fixedSizeHash[:], userHash)
|
|
|
pair, found := v.userHash[fixedSizeHash]
|
|
pair, found := v.userHash[fixedSizeHash]
|