1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-04 21:15:24 +00:00
v2fly/proxy/vmess/vmess.go

148 lines
3.6 KiB
Go
Raw Normal View History

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