vmess.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Package vmess contains the implementation of VMess protocol and transportation.
  2. //
  3. // VMess contains both inbound and outbound connections. VMess inbound is usually used on servers
  4. // together with 'freedom' to talk to final destination, while VMess outbound is usually used on
  5. // clients with 'socks' for proxying.
  6. package vmess
  7. //go:generate errorgen
  8. import (
  9. "strings"
  10. "sync"
  11. "time"
  12. "v2ray.com/core/common"
  13. "v2ray.com/core/common/protocol"
  14. "v2ray.com/core/common/task"
  15. )
  16. const (
  17. updateInterval = 10 * time.Second
  18. cacheDurationSec = 120
  19. )
  20. type user struct {
  21. user protocol.MemoryUser
  22. lastSec protocol.Timestamp
  23. }
  24. type TimedUserValidator struct {
  25. sync.RWMutex
  26. users []*user
  27. userHash map[[16]byte]indexTimePair
  28. hasher protocol.IDHash
  29. baseTime protocol.Timestamp
  30. task *task.Periodic
  31. }
  32. type indexTimePair struct {
  33. user *user
  34. timeInc uint32
  35. }
  36. func NewTimedUserValidator(hasher protocol.IDHash) *TimedUserValidator {
  37. tuv := &TimedUserValidator{
  38. users: make([]*user, 0, 16),
  39. userHash: make(map[[16]byte]indexTimePair, 1024),
  40. hasher: hasher,
  41. baseTime: protocol.Timestamp(time.Now().Unix() - cacheDurationSec*2),
  42. }
  43. tuv.task = &task.Periodic{
  44. Interval: updateInterval,
  45. Execute: func() error {
  46. tuv.updateUserHash()
  47. return nil
  48. },
  49. }
  50. common.Must(tuv.task.Start())
  51. return tuv
  52. }
  53. func (v *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, user *user) {
  54. var hashValue [16]byte
  55. genHashForID := func(id *protocol.ID) {
  56. idHash := v.hasher(id.Bytes())
  57. lastSec := user.lastSec
  58. if lastSec < nowSec-cacheDurationSec*2 {
  59. lastSec = nowSec - cacheDurationSec*2
  60. }
  61. for ts := lastSec; ts <= nowSec; ts++ {
  62. common.Must2(idHash.Write(ts.Bytes(nil)))
  63. idHash.Sum(hashValue[:0])
  64. idHash.Reset()
  65. v.userHash[hashValue] = indexTimePair{
  66. user: user,
  67. timeInc: uint32(ts - v.baseTime),
  68. }
  69. }
  70. }
  71. account := user.user.Account.(*InternalAccount)
  72. genHashForID(account.ID)
  73. for _, id := range account.AlterIDs {
  74. genHashForID(id)
  75. }
  76. user.lastSec = nowSec
  77. }
  78. func (v *TimedUserValidator) removeExpiredHashes(expire uint32) {
  79. for key, pair := range v.userHash {
  80. if pair.timeInc < expire {
  81. delete(v.userHash, key)
  82. }
  83. }
  84. }
  85. func (v *TimedUserValidator) updateUserHash() {
  86. now := time.Now()
  87. nowSec := protocol.Timestamp(now.Unix() + cacheDurationSec)
  88. v.Lock()
  89. defer v.Unlock()
  90. for _, user := range v.users {
  91. v.generateNewHashes(nowSec, user)
  92. }
  93. expire := protocol.Timestamp(now.Unix() - cacheDurationSec)
  94. if expire > v.baseTime {
  95. v.removeExpiredHashes(uint32(expire - v.baseTime))
  96. }
  97. }
  98. func (v *TimedUserValidator) Add(u *protocol.MemoryUser) error {
  99. v.Lock()
  100. defer v.Unlock()
  101. nowSec := time.Now().Unix()
  102. uu := &user{
  103. user: *u,
  104. lastSec: protocol.Timestamp(nowSec - cacheDurationSec),
  105. }
  106. v.users = append(v.users, uu)
  107. v.generateNewHashes(protocol.Timestamp(nowSec+cacheDurationSec), uu)
  108. return nil
  109. }
  110. func (v *TimedUserValidator) Get(userHash []byte) (*protocol.MemoryUser, protocol.Timestamp, bool) {
  111. defer v.RUnlock()
  112. v.RLock()
  113. var fixedSizeHash [16]byte
  114. copy(fixedSizeHash[:], userHash)
  115. pair, found := v.userHash[fixedSizeHash]
  116. if found {
  117. var user protocol.MemoryUser
  118. user = pair.user.user
  119. return &user, protocol.Timestamp(pair.timeInc) + v.baseTime, true
  120. }
  121. return nil, 0, false
  122. }
  123. func (v *TimedUserValidator) Remove(email string) bool {
  124. v.Lock()
  125. defer v.Unlock()
  126. email = strings.ToLower(email)
  127. idx := -1
  128. for i, u := range v.users {
  129. if strings.ToLower(u.user.Email) == email {
  130. idx = i
  131. break
  132. }
  133. }
  134. if idx == -1 {
  135. return false
  136. }
  137. ulen := len(v.users)
  138. if idx < ulen {
  139. v.users[idx] = v.users[ulen-1]
  140. v.users[ulen-1] = nil
  141. v.users = v.users[:ulen-1]
  142. }
  143. return true
  144. }
  145. // Close implements common.Closable.
  146. func (v *TimedUserValidator) Close() error {
  147. return v.task.Close()
  148. }