mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-07-26 17:53:48 -04:00
remove context in struct
This commit is contained in:
@@ -48,8 +48,9 @@ func TestRequestSerialization(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
sessionHistory := NewSessionHistory()
|
||||
|
||||
userValidator := vmess.NewTimedUserValidator(ctx, protocol.DefaultIDHash)
|
||||
userValidator := vmess.NewTimedUserValidator(protocol.DefaultIDHash)
|
||||
userValidator.Add(user)
|
||||
defer common.Close(userValidator)
|
||||
|
||||
server := NewServerSession(userValidator, sessionHistory)
|
||||
actualRequest, err := server.DecodeRequestHeader(buffer)
|
||||
|
||||
@@ -30,28 +30,34 @@ type sessionId struct {
|
||||
type SessionHistory struct {
|
||||
sync.RWMutex
|
||||
cache map[sessionId]time.Time
|
||||
token *signal.Semaphore
|
||||
timer *time.Timer
|
||||
task *signal.PeriodicTask
|
||||
}
|
||||
|
||||
func NewSessionHistory() *SessionHistory {
|
||||
h := &SessionHistory{
|
||||
cache: make(map[sessionId]time.Time, 128),
|
||||
token: signal.NewSemaphore(1),
|
||||
}
|
||||
h.task = &signal.PeriodicTask{
|
||||
Interval: time.Second * 30,
|
||||
Execute: func() error {
|
||||
h.removeExpiredEntries()
|
||||
return nil
|
||||
},
|
||||
}
|
||||
common.Must(h.task.Start())
|
||||
return h
|
||||
}
|
||||
|
||||
// Close implements common.Closable.
|
||||
func (h *SessionHistory) Close() error {
|
||||
return h.task.Close()
|
||||
}
|
||||
|
||||
func (h *SessionHistory) add(session sessionId) {
|
||||
h.Lock()
|
||||
defer h.Unlock()
|
||||
|
||||
h.cache[session] = time.Now().Add(time.Minute * 3)
|
||||
select {
|
||||
case <-h.token.Wait():
|
||||
h.timer = time.AfterFunc(time.Minute*3, h.removeExpiredEntries)
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func (h *SessionHistory) has(session sessionId) bool {
|
||||
@@ -75,11 +81,6 @@ func (h *SessionHistory) removeExpiredEntries() {
|
||||
delete(h.cache, session)
|
||||
}
|
||||
}
|
||||
|
||||
if h.timer != nil {
|
||||
h.timer.Stop()
|
||||
h.timer = nil
|
||||
}
|
||||
}
|
||||
|
||||
type ServerSession struct {
|
||||
|
||||
@@ -90,7 +90,7 @@ func New(ctx context.Context, config *Config) (*Handler, error) {
|
||||
handler := &Handler{
|
||||
policyManager: v.PolicyManager(),
|
||||
inboundHandlerManager: v.InboundHandlerManager(),
|
||||
clients: vmess.NewTimedUserValidator(ctx, protocol.DefaultIDHash),
|
||||
clients: vmess.NewTimedUserValidator(protocol.DefaultIDHash),
|
||||
detours: config.Detour,
|
||||
usersByEmail: newUserByEmail(config.User, config.GetDefaultValue()),
|
||||
sessionHistory: encoding.NewSessionHistory(),
|
||||
@@ -105,6 +105,14 @@ func New(ctx context.Context, config *Config) (*Handler, error) {
|
||||
return handler, nil
|
||||
}
|
||||
|
||||
// Close implements common.Closable.
|
||||
func (h *Handler) Close() error {
|
||||
common.Close(h.clients)
|
||||
common.Close(h.sessionHistory)
|
||||
common.Close(h.usersByEmail)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Network implements proxy.Inbound.Network().
|
||||
func (*Handler) Network() net.NetworkList {
|
||||
return net.NetworkList{
|
||||
|
||||
+32
-24
@@ -8,17 +8,17 @@ package vmess
|
||||
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg vmess -path Proxy,VMess
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"v2ray.com/core/common"
|
||||
"v2ray.com/core/common/protocol"
|
||||
"v2ray.com/core/common/signal"
|
||||
)
|
||||
|
||||
const (
|
||||
updateIntervalSec = 10
|
||||
cacheDurationSec = 120
|
||||
updateInterval = 10 * time.Second
|
||||
cacheDurationSec = 120
|
||||
)
|
||||
|
||||
type idEntry struct {
|
||||
@@ -34,6 +34,7 @@ type TimedUserValidator struct {
|
||||
ids []*idEntry
|
||||
hasher protocol.IDHash
|
||||
baseTime protocol.Timestamp
|
||||
task *signal.PeriodicTask
|
||||
}
|
||||
|
||||
type indexTimePair struct {
|
||||
@@ -41,16 +42,23 @@ type indexTimePair struct {
|
||||
timeInc uint32
|
||||
}
|
||||
|
||||
func NewTimedUserValidator(ctx context.Context, hasher protocol.IDHash) protocol.UserValidator {
|
||||
tus := &TimedUserValidator{
|
||||
func NewTimedUserValidator(hasher protocol.IDHash) protocol.UserValidator {
|
||||
tuv := &TimedUserValidator{
|
||||
validUsers: make([]*protocol.User, 0, 16),
|
||||
userHash: make(map[[16]byte]indexTimePair, 512),
|
||||
ids: make([]*idEntry, 0, 512),
|
||||
hasher: hasher,
|
||||
baseTime: protocol.Timestamp(time.Now().Unix() - cacheDurationSec*3),
|
||||
}
|
||||
go tus.updateUserHash(ctx, updateIntervalSec*time.Second)
|
||||
return tus
|
||||
tuv.task = &signal.PeriodicTask{
|
||||
Interval: updateInterval,
|
||||
Execute: func() error {
|
||||
tuv.updateUserHash()
|
||||
return nil
|
||||
},
|
||||
}
|
||||
tuv.task.Start()
|
||||
return tuv
|
||||
}
|
||||
|
||||
func (v *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, idx int, entry *idEntry) {
|
||||
@@ -78,24 +86,19 @@ func (v *TimedUserValidator) removeExpiredHashes(expire uint32) {
|
||||
}
|
||||
}
|
||||
|
||||
func (v *TimedUserValidator) updateUserHash(ctx context.Context, interval time.Duration) {
|
||||
for {
|
||||
select {
|
||||
case now := <-time.After(interval):
|
||||
nowSec := protocol.Timestamp(now.Unix() + cacheDurationSec)
|
||||
v.Lock()
|
||||
for _, entry := range v.ids {
|
||||
v.generateNewHashes(nowSec, entry.userIdx, entry)
|
||||
}
|
||||
func (v *TimedUserValidator) updateUserHash() {
|
||||
now := time.Now()
|
||||
nowSec := protocol.Timestamp(now.Unix() + cacheDurationSec)
|
||||
v.Lock()
|
||||
defer v.Unlock()
|
||||
|
||||
expire := protocol.Timestamp(now.Unix() - cacheDurationSec*3)
|
||||
if expire > v.baseTime {
|
||||
v.removeExpiredHashes(uint32(expire - v.baseTime))
|
||||
}
|
||||
v.Unlock()
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
for _, entry := range v.ids {
|
||||
v.generateNewHashes(nowSec, entry.userIdx, entry)
|
||||
}
|
||||
|
||||
expire := protocol.Timestamp(now.Unix() - cacheDurationSec*3)
|
||||
if expire > v.baseTime {
|
||||
v.removeExpiredHashes(uint32(expire - v.baseTime))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,3 +148,8 @@ func (v *TimedUserValidator) Get(userHash []byte) (*protocol.User, protocol.Time
|
||||
}
|
||||
return nil, 0, false
|
||||
}
|
||||
|
||||
// Close implements common.Closable.
|
||||
func (v *TimedUserValidator) Close() error {
|
||||
return v.task.Close()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user