1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00
v2fly/proxy/vmess/vmess.go

167 lines
4.0 KiB
Go
Raw Normal View History

2015-12-04 06:07:32 -05:00
// 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.
2016-08-19 11:05:15 -04:00
package vmess
2016-07-25 11:36:24 -04:00
import (
"sync"
"time"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/signal"
2016-07-25 11:36:24 -04:00
)
const (
updateIntervalSec = 10
cacheDurationSec = 120
)
type idEntry struct {
id *protocol.ID
userIdx int
lastSec protocol.Timestamp
lastSecRemoval protocol.Timestamp
}
type TimedUserValidator struct {
sync.RWMutex
running bool
validUsers []*protocol.User
userHash map[[16]byte]*indexTimePair
ids []*idEntry
hasher protocol.IDHash
cancel *signal.CancelSignal
}
type indexTimePair struct {
index int
timeSec protocol.Timestamp
}
func NewTimedUserValidator(hasher protocol.IDHash) protocol.UserValidator {
tus := &TimedUserValidator{
validUsers: make([]*protocol.User, 0, 16),
userHash: make(map[[16]byte]*indexTimePair, 512),
ids: make([]*idEntry, 0, 512),
hasher: hasher,
running: true,
cancel: signal.NewCloseSignal(),
}
go tus.updateUserHash(updateIntervalSec * time.Second)
return tus
}
func (this *TimedUserValidator) Release() {
if !this.running {
return
}
this.cancel.Cancel()
2016-11-18 15:30:03 -05:00
this.cancel.WaitForDone()
2016-07-25 11:36:24 -04:00
this.Lock()
defer this.Unlock()
if !this.running {
return
}
this.running = false
this.validUsers = nil
this.userHash = nil
this.ids = nil
this.hasher = nil
this.cancel = nil
}
func (this *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, idx int, entry *idEntry) {
var hashValue [16]byte
var hashValueRemoval [16]byte
idHash := this.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()
this.Lock()
this.userHash[hashValue] = &indexTimePair{idx, entry.lastSec}
delete(this.userHash, hashValueRemoval)
this.Unlock()
entry.lastSec++
entry.lastSecRemoval++
}
}
func (this *TimedUserValidator) updateUserHash(interval time.Duration) {
2016-11-18 15:30:03 -05:00
this.cancel.WaitThread()
defer this.cancel.FinishThread()
2016-07-25 11:36:24 -04:00
for {
select {
case now := <-time.After(interval):
nowSec := protocol.Timestamp(now.Unix() + cacheDurationSec)
for _, entry := range this.ids {
this.generateNewHashes(nowSec, entry.userIdx, entry)
}
case <-this.cancel.WaitForCancel():
2016-11-18 15:30:03 -05:00
return
2016-07-25 11:36:24 -04:00
}
}
}
func (this *TimedUserValidator) Add(user *protocol.User) error {
idx := len(this.validUsers)
this.validUsers = append(this.validUsers, user)
2016-10-16 08:22:21 -04:00
rawAccount, err := user.GetTypedAccount()
2016-09-17 18:41:21 -04:00
if err != nil {
return err
}
2016-10-12 12:43:55 -04:00
account := rawAccount.(*InternalAccount)
2016-07-25 11:36:24 -04:00
nowSec := time.Now().Unix()
entry := &idEntry{
id: account.ID,
userIdx: idx,
lastSec: protocol.Timestamp(nowSec - cacheDurationSec),
lastSecRemoval: protocol.Timestamp(nowSec - cacheDurationSec*3),
}
this.generateNewHashes(protocol.Timestamp(nowSec+cacheDurationSec), idx, entry)
this.ids = append(this.ids, entry)
for _, alterid := range account.AlterIDs {
entry := &idEntry{
id: alterid,
userIdx: idx,
lastSec: protocol.Timestamp(nowSec - cacheDurationSec),
lastSecRemoval: protocol.Timestamp(nowSec - cacheDurationSec*3),
}
this.generateNewHashes(protocol.Timestamp(nowSec+cacheDurationSec), idx, entry)
this.ids = append(this.ids, entry)
}
return nil
}
func (this *TimedUserValidator) Get(userHash []byte) (*protocol.User, protocol.Timestamp, bool) {
defer this.RUnlock()
this.RLock()
if !this.running {
return nil, 0, false
}
var fixedSizeHash [16]byte
copy(fixedSizeHash[:], userHash)
pair, found := this.userHash[fixedSizeHash]
if found {
return this.validUsers[pair.index], pair.timeSec, true
}
return nil, 0, false
}