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-12-02 19:04:57 -05:00
|
|
|
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg vmess -path Proxy,VMess
|
2017-04-08 19:43:25 -04:00
|
|
|
|
2016-07-25 11:36:24 -04:00
|
|
|
import (
|
2018-02-09 05:32:12 -05:00
|
|
|
"strings"
|
2016-07-25 11:36:24 -04:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2017-09-19 17:27:49 -04:00
|
|
|
"v2ray.com/core/common"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/protocol"
|
2018-05-27 07:02:29 -04:00
|
|
|
"v2ray.com/core/common/task"
|
2016-07-25 11:36:24 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-02-08 09:39:46 -05:00
|
|
|
updateInterval = 10 * time.Second
|
|
|
|
cacheDurationSec = 120
|
2016-07-25 11:36:24 -04:00
|
|
|
)
|
|
|
|
|
2018-02-09 05:32:12 -05:00
|
|
|
type user struct {
|
|
|
|
user *protocol.User
|
|
|
|
account *InternalAccount
|
2018-02-05 17:39:04 -05:00
|
|
|
lastSec protocol.Timestamp
|
2016-07-25 11:36:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type TimedUserValidator struct {
|
|
|
|
sync.RWMutex
|
2018-02-09 05:32:12 -05:00
|
|
|
users []*user
|
|
|
|
userHash map[[16]byte]indexTimePair
|
|
|
|
hasher protocol.IDHash
|
|
|
|
baseTime protocol.Timestamp
|
2018-05-27 07:02:29 -04:00
|
|
|
task *task.Periodic
|
2016-07-25 11:36:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type indexTimePair struct {
|
2018-02-09 05:32:12 -05:00
|
|
|
user *user
|
2017-04-06 17:11:36 -04:00
|
|
|
timeInc uint32
|
2016-07-25 11:36:24 -04:00
|
|
|
}
|
|
|
|
|
2018-04-14 09:05:49 -04:00
|
|
|
func NewTimedUserValidator(hasher protocol.IDHash) *TimedUserValidator {
|
2018-02-08 09:39:46 -05:00
|
|
|
tuv := &TimedUserValidator{
|
2018-02-09 05:32:12 -05:00
|
|
|
users: make([]*user, 0, 16),
|
|
|
|
userHash: make(map[[16]byte]indexTimePair, 1024),
|
|
|
|
hasher: hasher,
|
2018-04-07 03:56:28 -04:00
|
|
|
baseTime: protocol.Timestamp(time.Now().Unix() - cacheDurationSec*2),
|
2016-07-25 11:36:24 -04:00
|
|
|
}
|
2018-05-27 07:02:29 -04:00
|
|
|
tuv.task = &task.Periodic{
|
2018-02-08 09:39:46 -05:00
|
|
|
Interval: updateInterval,
|
|
|
|
Execute: func() error {
|
|
|
|
tuv.updateUserHash()
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2018-05-25 17:20:24 -04:00
|
|
|
common.Must(tuv.task.Start())
|
2018-02-08 09:39:46 -05:00
|
|
|
return tuv
|
2016-07-25 11:36:24 -04:00
|
|
|
}
|
|
|
|
|
2018-02-09 05:32:12 -05:00
|
|
|
func (v *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, user *user) {
|
2016-07-25 11:36:24 -04:00
|
|
|
var hashValue [16]byte
|
2018-02-09 05:32:12 -05:00
|
|
|
genHashForID := func(id *protocol.ID) {
|
|
|
|
idHash := v.hasher(id.Bytes())
|
2018-04-06 16:02:52 -04:00
|
|
|
lastSec := user.lastSec
|
|
|
|
if lastSec < nowSec-cacheDurationSec*2 {
|
|
|
|
lastSec = nowSec - cacheDurationSec*2
|
|
|
|
}
|
|
|
|
for ts := lastSec; ts <= nowSec; ts++ {
|
2018-02-09 05:32:12 -05: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 17:11:36 -04:00
|
|
|
}
|
2018-02-09 05:32:12 -05:00
|
|
|
}
|
2016-07-25 11:36:24 -04:00
|
|
|
|
2018-02-09 05:32:12 -05:00
|
|
|
genHashForID(user.account.ID)
|
|
|
|
for _, id := range user.account.AlterIDs {
|
|
|
|
genHashForID(id)
|
2018-01-30 15:38:43 -05:00
|
|
|
}
|
2018-02-09 05:32:12 -05:00
|
|
|
user.lastSec = nowSec
|
2018-01-30 15:38:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *TimedUserValidator) removeExpiredHashes(expire uint32) {
|
|
|
|
for key, pair := range v.userHash {
|
|
|
|
if pair.timeInc < expire {
|
|
|
|
delete(v.userHash, key)
|
|
|
|
}
|
2016-07-25 11:36:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 09:39:46 -05:00
|
|
|
func (v *TimedUserValidator) updateUserHash() {
|
|
|
|
now := time.Now()
|
|
|
|
nowSec := protocol.Timestamp(now.Unix() + cacheDurationSec)
|
|
|
|
v.Lock()
|
|
|
|
defer v.Unlock()
|
|
|
|
|
2018-02-09 05:32:12 -05:00
|
|
|
for _, user := range v.users {
|
|
|
|
v.generateNewHashes(nowSec, user)
|
2018-02-08 09:39:46 -05:00
|
|
|
}
|
|
|
|
|
2018-04-07 03:56:28 -04:00
|
|
|
expire := protocol.Timestamp(now.Unix() - cacheDurationSec)
|
2018-02-08 09:39:46 -05:00
|
|
|
if expire > v.baseTime {
|
|
|
|
v.removeExpiredHashes(uint32(expire - v.baseTime))
|
2016-07-25 11:36:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-09 05:32:12 -05:00
|
|
|
func (v *TimedUserValidator) Add(u *protocol.User) error {
|
2016-12-27 15:33:34 -05:00
|
|
|
v.Lock()
|
|
|
|
defer v.Unlock()
|
|
|
|
|
2018-02-09 05:32:12 -05:00
|
|
|
rawAccount, err := u.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()
|
|
|
|
|
2018-02-09 05:32:12 -05:00
|
|
|
uu := &user{
|
|
|
|
user: u,
|
|
|
|
account: account,
|
2018-02-05 17:39:04 -05:00
|
|
|
lastSec: protocol.Timestamp(nowSec - cacheDurationSec),
|
2016-07-25 11:36:24 -04:00
|
|
|
}
|
2018-02-09 05:32:12 -05:00
|
|
|
v.users = append(v.users, uu)
|
|
|
|
v.generateNewHashes(protocol.Timestamp(nowSec+cacheDurationSec), uu)
|
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 {
|
2018-02-09 05:32:12 -05:00
|
|
|
return pair.user.user, protocol.Timestamp(pair.timeInc) + v.baseTime, true
|
2016-07-25 11:36:24 -04:00
|
|
|
}
|
|
|
|
return nil, 0, false
|
|
|
|
}
|
2018-02-08 09:39:46 -05:00
|
|
|
|
2018-02-09 05:32:12 -05: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-25 19:57:43 -05:00
|
|
|
if idx < ulen {
|
2018-02-09 05:32:12 -05:00
|
|
|
v.users[idx] = v.users[ulen-1]
|
|
|
|
v.users[ulen-1] = nil
|
|
|
|
v.users = v.users[:ulen-1]
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-02-08 09:39:46 -05:00
|
|
|
// Close implements common.Closable.
|
|
|
|
func (v *TimedUserValidator) Close() error {
|
|
|
|
return v.task.Close()
|
|
|
|
}
|