2020-11-25 13:01:53 +02:00
|
|
|
package vmess
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha256"
|
|
|
|
"hash/crc64"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2020-12-04 03:36:16 +02:00
|
|
|
"github.com/xtls/xray-core/common/dice"
|
|
|
|
"github.com/xtls/xray-core/common/protocol"
|
|
|
|
"github.com/xtls/xray-core/proxy/vmess/aead"
|
2020-11-25 13:01:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// TimedUserValidator is a user Validator based on time.
|
|
|
|
type TimedUserValidator struct {
|
|
|
|
sync.RWMutex
|
2023-06-18 20:42:17 +03:00
|
|
|
users []*protocol.MemoryUser
|
2020-11-25 13:01:53 +02:00
|
|
|
|
|
|
|
behaviorSeed uint64
|
|
|
|
behaviorFused bool
|
|
|
|
|
|
|
|
aeadDecoderHolder *aead.AuthIDDecoderHolder
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTimedUserValidator creates a new TimedUserValidator.
|
2023-06-12 17:32:25 +03:00
|
|
|
func NewTimedUserValidator() *TimedUserValidator {
|
2020-11-25 13:01:53 +02:00
|
|
|
tuv := &TimedUserValidator{
|
2023-06-12 17:32:25 +03:00
|
|
|
users: make([]*protocol.MemoryUser, 0, 16),
|
2020-11-25 13:01:53 +02:00
|
|
|
aeadDecoderHolder: aead.NewAuthIDDecoderHolder(),
|
|
|
|
}
|
|
|
|
return tuv
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *TimedUserValidator) Add(u *protocol.MemoryUser) error {
|
|
|
|
v.Lock()
|
|
|
|
defer v.Unlock()
|
|
|
|
|
2023-06-12 17:32:25 +03:00
|
|
|
v.users = append(v.users, u)
|
2020-11-25 13:01:53 +02:00
|
|
|
|
2023-06-12 17:32:25 +03:00
|
|
|
account := u.Account.(*MemoryAccount)
|
2020-11-25 13:01:53 +02:00
|
|
|
if !v.behaviorFused {
|
|
|
|
hashkdf := hmac.New(sha256.New, []byte("VMESSBSKDF"))
|
|
|
|
hashkdf.Write(account.ID.Bytes())
|
|
|
|
v.behaviorSeed = crc64.Update(v.behaviorSeed, crc64.MakeTable(crc64.ECMA), hashkdf.Sum(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
var cmdkeyfl [16]byte
|
|
|
|
copy(cmdkeyfl[:], account.ID.CmdKey())
|
|
|
|
v.aeadDecoderHolder.AddUser(cmdkeyfl, u)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *TimedUserValidator) GetAEAD(userHash []byte) (*protocol.MemoryUser, bool, error) {
|
|
|
|
v.RLock()
|
|
|
|
defer v.RUnlock()
|
|
|
|
|
|
|
|
var userHashFL [16]byte
|
|
|
|
copy(userHashFL[:], userHash)
|
|
|
|
|
|
|
|
userd, err := v.aeadDecoderHolder.Match(userHashFL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
return userd.(*protocol.MemoryUser), true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *TimedUserValidator) Remove(email string) bool {
|
|
|
|
v.Lock()
|
|
|
|
defer v.Unlock()
|
|
|
|
|
|
|
|
email = strings.ToLower(email)
|
|
|
|
idx := -1
|
|
|
|
for i, u := range v.users {
|
2023-06-12 17:32:25 +03:00
|
|
|
if strings.EqualFold(u.Email, email) {
|
2020-11-25 13:01:53 +02:00
|
|
|
idx = i
|
|
|
|
var cmdkeyfl [16]byte
|
2023-06-12 17:32:25 +03:00
|
|
|
copy(cmdkeyfl[:], u.Account.(*MemoryAccount).ID.CmdKey())
|
2020-11-25 13:01:53 +02:00
|
|
|
v.aeadDecoderHolder.RemoveUser(cmdkeyfl)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if idx == -1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
ulen := len(v.users)
|
|
|
|
|
|
|
|
v.users[idx] = v.users[ulen-1]
|
|
|
|
v.users[ulen-1] = nil
|
|
|
|
v.users = v.users[:ulen-1]
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *TimedUserValidator) GetBehaviorSeed() uint64 {
|
|
|
|
v.Lock()
|
|
|
|
defer v.Unlock()
|
|
|
|
|
|
|
|
v.behaviorFused = true
|
|
|
|
if v.behaviorSeed == 0 {
|
|
|
|
v.behaviorSeed = dice.RollUint64()
|
|
|
|
}
|
|
|
|
return v.behaviorSeed
|
|
|
|
}
|
|
|
|
|
|
|
|
var ErrNotFound = newError("Not Found")
|
|
|
|
|
|
|
|
var ErrTainted = newError("ErrTainted")
|