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

178 lines
3.9 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 (
2018-02-09 10:32:12 +00:00
"strings"
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"
2018-02-08 14:39:46 +00:00
"v2ray.com/core/common/signal"
2016-07-25 15:36:24 +00:00
)
const (
2018-02-08 14:39:46 +00:00
updateInterval = 10 * time.Second
cacheDurationSec = 120
2016-07-25 15:36:24 +00:00
)
2018-02-09 10:32:12 +00:00
type user struct {
user *protocol.User
account *InternalAccount
2018-02-05 22:39:04 +00:00
lastSec protocol.Timestamp
2016-07-25 15:36:24 +00:00
}
type TimedUserValidator struct {
sync.RWMutex
2018-02-09 10:32:12 +00:00
users []*user
userHash map[[16]byte]indexTimePair
hasher protocol.IDHash
baseTime protocol.Timestamp
task *signal.PeriodicTask
2016-07-25 15:36:24 +00:00
}
type indexTimePair struct {
2018-02-09 10:32:12 +00:00
user *user
2017-04-06 21:11:36 +00:00
timeInc uint32
2016-07-25 15:36:24 +00:00
}
func NewTimedUserValidator(hasher protocol.IDHash) *TimedUserValidator {
2018-02-08 14:39:46 +00:00
tuv := &TimedUserValidator{
2018-02-09 10:32:12 +00:00
users: make([]*user, 0, 16),
userHash: make(map[[16]byte]indexTimePair, 1024),
hasher: hasher,
2018-04-07 07:56:28 +00:00
baseTime: protocol.Timestamp(time.Now().Unix() - cacheDurationSec*2),
2016-07-25 15:36:24 +00:00
}
2018-02-08 14:39:46 +00:00
tuv.task = &signal.PeriodicTask{
Interval: updateInterval,
Execute: func() error {
tuv.updateUserHash()
return nil
},
}
tuv.task.Start()
return tuv
2016-07-25 15:36:24 +00:00
}
2018-02-09 10:32:12 +00:00
func (v *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, user *user) {
2016-07-25 15:36:24 +00:00
var hashValue [16]byte
2018-02-09 10:32:12 +00:00
genHashForID := func(id *protocol.ID) {
idHash := v.hasher(id.Bytes())
lastSec := user.lastSec
if lastSec < nowSec-cacheDurationSec*2 {
lastSec = nowSec - cacheDurationSec*2
}
for ts := lastSec; ts <= nowSec; ts++ {
2018-02-09 10:32:12 +00:00
common.Must2(idHash.Write(ts.Bytes(nil)))
idHash.Sum(hashValue[:0])
idHash.Reset()
v.userHash[hashValue] = indexTimePair{
user: user,
timeInc: uint32(ts - v.baseTime),
}
2017-04-06 21:11:36 +00:00
}
2018-02-09 10:32:12 +00:00
}
2016-07-25 15:36:24 +00:00
2018-02-09 10:32:12 +00:00
genHashForID(user.account.ID)
for _, id := range user.account.AlterIDs {
genHashForID(id)
2018-01-30 20:38:43 +00:00
}
2018-02-09 10:32:12 +00:00
user.lastSec = nowSec
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
}
}
2018-02-08 14:39:46 +00:00
func (v *TimedUserValidator) updateUserHash() {
now := time.Now()
nowSec := protocol.Timestamp(now.Unix() + cacheDurationSec)
v.Lock()
defer v.Unlock()
2018-02-09 10:32:12 +00:00
for _, user := range v.users {
v.generateNewHashes(nowSec, user)
2018-02-08 14:39:46 +00:00
}
2018-04-07 07:56:28 +00:00
expire := protocol.Timestamp(now.Unix() - cacheDurationSec)
2018-02-08 14:39:46 +00:00
if expire > v.baseTime {
v.removeExpiredHashes(uint32(expire - v.baseTime))
2016-07-25 15:36:24 +00:00
}
}
2018-02-09 10:32:12 +00:00
func (v *TimedUserValidator) Add(u *protocol.User) error {
2016-12-27 20:33:34 +00:00
v.Lock()
defer v.Unlock()
2018-02-09 10:32:12 +00:00
rawAccount, err := u.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()
2018-02-09 10:32:12 +00:00
uu := &user{
user: u,
account: account,
2018-02-05 22:39:04 +00:00
lastSec: protocol.Timestamp(nowSec - cacheDurationSec),
2016-07-25 15:36:24 +00:00
}
2018-02-09 10:32:12 +00:00
v.users = append(v.users, uu)
v.generateNewHashes(protocol.Timestamp(nowSec+cacheDurationSec), uu)
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 {
2018-02-09 10:32:12 +00:00
return pair.user.user, protocol.Timestamp(pair.timeInc) + v.baseTime, true
2016-07-25 15:36:24 +00:00
}
return nil, 0, false
}
2018-02-08 14:39:46 +00:00
2018-02-09 10:32:12 +00:00
func (v *TimedUserValidator) Remove(email string) bool {
v.Lock()
defer v.Unlock()
email = strings.ToLower(email)
idx := -1
for i, u := range v.users {
if strings.ToLower(u.user.Email) == email {
idx = i
break
}
}
if idx == -1 {
return false
}
ulen := len(v.users)
2018-02-26 00:57:43 +00:00
if idx < ulen {
2018-02-09 10:32:12 +00:00
v.users[idx] = v.users[ulen-1]
v.users[ulen-1] = nil
v.users = v.users[:ulen-1]
}
return true
}
2018-02-08 14:39:46 +00:00
// Close implements common.Closable.
func (v *TimedUserValidator) Close() error {
return v.task.Close()
}