1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-23 02:26:49 -05:00
v2fly/proxy/vmess/vmess.go

146 lines
3.7 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
2017-04-08 19:43:25 -04:00
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg vmess -path Proxy,VMess
2016-07-25 11:36:24 -04:00
import (
2017-01-29 06:58:52 -05:00
"context"
2016-07-25 11:36:24 -04:00
"sync"
"time"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common/protocol"
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
2017-01-29 06:58:52 -05:00
ctx context.Context
2016-07-25 11:36:24 -04:00
validUsers []*protocol.User
2017-04-06 17:11:36 -04:00
userHash map[[16]byte]indexTimePair
2016-07-25 11:36:24 -04:00
ids []*idEntry
hasher protocol.IDHash
2017-04-06 17:11:36 -04:00
baseTime protocol.Timestamp
2016-07-25 11:36:24 -04:00
}
type indexTimePair struct {
index int
2017-04-06 17:11:36 -04:00
timeInc uint32
2016-07-25 11:36:24 -04:00
}
2017-01-29 06:58:52 -05:00
func NewTimedUserValidator(ctx context.Context, hasher protocol.IDHash) protocol.UserValidator {
2016-07-25 11:36:24 -04:00
tus := &TimedUserValidator{
2017-01-29 06:58:52 -05:00
ctx: ctx,
2016-07-25 11:36:24 -04:00
validUsers: make([]*protocol.User, 0, 16),
2017-04-06 17:11:36 -04:00
userHash: make(map[[16]byte]indexTimePair, 512),
2016-07-25 11:36:24 -04:00
ids: make([]*idEntry, 0, 512),
hasher: hasher,
2017-04-06 17:11:36 -04:00
baseTime: protocol.Timestamp(time.Now().Unix() - cacheDurationSec*3),
2016-07-25 11:36:24 -04:00
}
go tus.updateUserHash(updateIntervalSec * time.Second)
return tus
}
2016-11-27 15:39:09 -05:00
func (v *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, idx int, entry *idEntry) {
2016-07-25 11:36:24 -04:00
var hashValue [16]byte
var hashValueRemoval [16]byte
2016-11-27 15:39:09 -05:00
idHash := v.hasher(entry.id.Bytes())
2016-07-25 11:36:24 -04:00
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()
2016-11-27 15:39:09 -05:00
delete(v.userHash, hashValueRemoval)
2017-04-06 17:11:36 -04:00
v.userHash[hashValue] = indexTimePair{
index: idx,
timeInc: uint32(entry.lastSec - v.baseTime),
}
2016-07-25 11:36:24 -04:00
entry.lastSec++
entry.lastSecRemoval++
}
}
2016-11-27 15:39:09 -05:00
func (v *TimedUserValidator) updateUserHash(interval time.Duration) {
2016-07-25 11:36:24 -04:00
for {
select {
case now := <-time.After(interval):
nowSec := protocol.Timestamp(now.Unix() + cacheDurationSec)
2016-12-27 15:33:34 -05:00
v.Lock()
2016-11-27 15:39:09 -05:00
for _, entry := range v.ids {
v.generateNewHashes(nowSec, entry.userIdx, entry)
2016-07-25 11:36:24 -04:00
}
2016-12-27 15:33:34 -05:00
v.Unlock()
2017-01-29 06:58:52 -05:00
case <-v.ctx.Done():
2016-11-18 15:30:03 -05:00
return
2016-07-25 11:36:24 -04:00
}
}
}
2016-11-27 15:39:09 -05:00
func (v *TimedUserValidator) Add(user *protocol.User) error {
2016-12-27 15:33:34 -05:00
v.Lock()
defer v.Unlock()
2016-11-27 15:39:09 -05:00
idx := len(v.validUsers)
v.validUsers = append(v.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),
}
2016-11-27 15:39:09 -05:00
v.generateNewHashes(protocol.Timestamp(nowSec+cacheDurationSec), idx, entry)
v.ids = append(v.ids, entry)
2016-07-25 11:36:24 -04:00
for _, alterid := range account.AlterIDs {
entry := &idEntry{
id: alterid,
userIdx: idx,
lastSec: protocol.Timestamp(nowSec - cacheDurationSec),
lastSecRemoval: protocol.Timestamp(nowSec - cacheDurationSec*3),
}
2016-11-27 15:39:09 -05:00
v.generateNewHashes(protocol.Timestamp(nowSec+cacheDurationSec), idx, entry)
v.ids = append(v.ids, entry)
2016-07-25 11:36:24 -04:00
}
return nil
}
2016-11-27 15:39:09 -05:00
func (v *TimedUserValidator) Get(userHash []byte) (*protocol.User, protocol.Timestamp, bool) {
defer v.RUnlock()
v.RLock()
2016-07-25 11:36:24 -04:00
var fixedSizeHash [16]byte
copy(fixedSizeHash[:], userHash)
2016-11-27 15:39:09 -05:00
pair, found := v.userHash[fixedSizeHash]
2016-07-25 11:36:24 -04:00
if found {
2017-04-06 17:11:36 -04:00
return v.validUsers[pair.index], protocol.Timestamp(pair.timeInc) + v.baseTime, true
2016-07-25 11:36:24 -04:00
}
return nil, 0, false
}