You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/proxy/vmess/vmess.go

139 lines
3.4 KiB

// Package vmess contains the implementation of VMess protocol and transportation.
//
// VMess contains both inbound and outbound connections. VMess inbound is usually used on servers
// together with 'freedom' to talk to final destination, while VMess outbound is usually used on
// clients with 'socks' for proxying.
package vmess
import (
"context"
"sync"
"time"
"v2ray.com/core/common/protocol"
)
const (
updateIntervalSec = 10
cacheDurationSec = 120
)
type idEntry struct {
id *protocol.ID
userIdx int
lastSec protocol.Timestamp
lastSecRemoval protocol.Timestamp
}
type TimedUserValidator struct {
sync.RWMutex
ctx context.Context
validUsers []*protocol.User
userHash map[[16]byte]*indexTimePair
ids []*idEntry
hasher protocol.IDHash
}
type indexTimePair struct {
index int
timeSec protocol.Timestamp
}
func NewTimedUserValidator(ctx context.Context, hasher protocol.IDHash) protocol.UserValidator {
tus := &TimedUserValidator{
ctx: ctx,
validUsers: make([]*protocol.User, 0, 16),
userHash: make(map[[16]byte]*indexTimePair, 512),
ids: make([]*idEntry, 0, 512),
hasher: hasher,
}
go tus.updateUserHash(updateIntervalSec * time.Second)
return tus
}
8 years ago
func (v *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, idx int, entry *idEntry) {
var hashValue [16]byte
var hashValueRemoval [16]byte
8 years ago
idHash := v.hasher(entry.id.Bytes())
for entry.lastSec <= nowSec {
idHash.Write(entry.lastSec.Bytes(nil))
idHash.Sum(hashValue[:0])
idHash.Reset()
idHash.Write(entry.lastSecRemoval.Bytes(nil))
idHash.Sum(hashValueRemoval[:0])
idHash.Reset()
8 years ago
v.userHash[hashValue] = &indexTimePair{idx, entry.lastSec}
delete(v.userHash, hashValueRemoval)
entry.lastSec++
entry.lastSecRemoval++
}
}
8 years ago
func (v *TimedUserValidator) updateUserHash(interval time.Duration) {
for {
select {
case now := <-time.After(interval):
nowSec := protocol.Timestamp(now.Unix() + cacheDurationSec)
8 years ago
v.Lock()
8 years ago
for _, entry := range v.ids {
v.generateNewHashes(nowSec, entry.userIdx, entry)
}
8 years ago
v.Unlock()
case <-v.ctx.Done():
8 years ago
return
}
}
}
8 years ago
func (v *TimedUserValidator) Add(user *protocol.User) error {
8 years ago
v.Lock()
defer v.Unlock()
8 years ago
idx := len(v.validUsers)
v.validUsers = append(v.validUsers, user)
rawAccount, err := user.GetTypedAccount()
if err != nil {
return err
}
8 years ago
account := rawAccount.(*InternalAccount)
nowSec := time.Now().Unix()
entry := &idEntry{
id: account.ID,
userIdx: idx,
lastSec: protocol.Timestamp(nowSec - cacheDurationSec),
lastSecRemoval: protocol.Timestamp(nowSec - cacheDurationSec*3),
}
8 years ago
v.generateNewHashes(protocol.Timestamp(nowSec+cacheDurationSec), idx, entry)
v.ids = append(v.ids, entry)
for _, alterid := range account.AlterIDs {
entry := &idEntry{
id: alterid,
userIdx: idx,
lastSec: protocol.Timestamp(nowSec - cacheDurationSec),
lastSecRemoval: protocol.Timestamp(nowSec - cacheDurationSec*3),
}
8 years ago
v.generateNewHashes(protocol.Timestamp(nowSec+cacheDurationSec), idx, entry)
v.ids = append(v.ids, entry)
}
return nil
}
8 years ago
func (v *TimedUserValidator) Get(userHash []byte) (*protocol.User, protocol.Timestamp, bool) {
defer v.RUnlock()
v.RLock()
var fixedSizeHash [16]byte
copy(fixedSizeHash[:], userHash)
8 years ago
pair, found := v.userHash[fixedSizeHash]
if found {
8 years ago
return v.validUsers[pair.index], pair.timeSec, true
}
return nil, 0, false
}