vmess.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. import (
  8. "sync"
  9. "time"
  10. "v2ray.com/core/common/protocol"
  11. "v2ray.com/core/common/signal"
  12. )
  13. const (
  14. updateIntervalSec = 10
  15. cacheDurationSec = 120
  16. )
  17. type idEntry struct {
  18. id *protocol.ID
  19. userIdx int
  20. lastSec protocol.Timestamp
  21. lastSecRemoval protocol.Timestamp
  22. }
  23. type TimedUserValidator struct {
  24. sync.RWMutex
  25. running bool
  26. validUsers []*protocol.User
  27. userHash map[[16]byte]*indexTimePair
  28. ids []*idEntry
  29. hasher protocol.IDHash
  30. cancel *signal.CancelSignal
  31. }
  32. type indexTimePair struct {
  33. index int
  34. timeSec protocol.Timestamp
  35. }
  36. func NewTimedUserValidator(hasher protocol.IDHash) protocol.UserValidator {
  37. tus := &TimedUserValidator{
  38. validUsers: make([]*protocol.User, 0, 16),
  39. userHash: make(map[[16]byte]*indexTimePair, 512),
  40. ids: make([]*idEntry, 0, 512),
  41. hasher: hasher,
  42. running: true,
  43. cancel: signal.NewCloseSignal(),
  44. }
  45. go tus.updateUserHash(updateIntervalSec * time.Second)
  46. return tus
  47. }
  48. func (v *TimedUserValidator) Release() {
  49. if !v.running {
  50. return
  51. }
  52. v.cancel.Cancel()
  53. v.cancel.WaitForDone()
  54. v.Lock()
  55. defer v.Unlock()
  56. if !v.running {
  57. return
  58. }
  59. v.running = false
  60. }
  61. func (v *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, idx int, entry *idEntry) {
  62. var hashValue [16]byte
  63. var hashValueRemoval [16]byte
  64. idHash := v.hasher(entry.id.Bytes())
  65. for entry.lastSec <= nowSec {
  66. idHash.Write(entry.lastSec.Bytes(nil))
  67. idHash.Sum(hashValue[:0])
  68. idHash.Reset()
  69. idHash.Write(entry.lastSecRemoval.Bytes(nil))
  70. idHash.Sum(hashValueRemoval[:0])
  71. idHash.Reset()
  72. v.userHash[hashValue] = &indexTimePair{idx, entry.lastSec}
  73. delete(v.userHash, hashValueRemoval)
  74. entry.lastSec++
  75. entry.lastSecRemoval++
  76. }
  77. }
  78. func (v *TimedUserValidator) updateUserHash(interval time.Duration) {
  79. v.cancel.WaitThread()
  80. defer v.cancel.FinishThread()
  81. for {
  82. select {
  83. case now := <-time.After(interval):
  84. nowSec := protocol.Timestamp(now.Unix() + cacheDurationSec)
  85. v.Lock()
  86. for _, entry := range v.ids {
  87. v.generateNewHashes(nowSec, entry.userIdx, entry)
  88. }
  89. v.Unlock()
  90. case <-v.cancel.WaitForCancel():
  91. return
  92. }
  93. }
  94. }
  95. func (v *TimedUserValidator) Add(user *protocol.User) error {
  96. v.Lock()
  97. defer v.Unlock()
  98. idx := len(v.validUsers)
  99. v.validUsers = append(v.validUsers, user)
  100. rawAccount, err := user.GetTypedAccount()
  101. if err != nil {
  102. return err
  103. }
  104. account := rawAccount.(*InternalAccount)
  105. nowSec := time.Now().Unix()
  106. entry := &idEntry{
  107. id: account.ID,
  108. userIdx: idx,
  109. lastSec: protocol.Timestamp(nowSec - cacheDurationSec),
  110. lastSecRemoval: protocol.Timestamp(nowSec - cacheDurationSec*3),
  111. }
  112. v.generateNewHashes(protocol.Timestamp(nowSec+cacheDurationSec), idx, entry)
  113. v.ids = append(v.ids, entry)
  114. for _, alterid := range account.AlterIDs {
  115. entry := &idEntry{
  116. id: alterid,
  117. userIdx: idx,
  118. lastSec: protocol.Timestamp(nowSec - cacheDurationSec),
  119. lastSecRemoval: protocol.Timestamp(nowSec - cacheDurationSec*3),
  120. }
  121. v.generateNewHashes(protocol.Timestamp(nowSec+cacheDurationSec), idx, entry)
  122. v.ids = append(v.ids, entry)
  123. }
  124. return nil
  125. }
  126. func (v *TimedUserValidator) Get(userHash []byte) (*protocol.User, protocol.Timestamp, bool) {
  127. defer v.RUnlock()
  128. v.RLock()
  129. if !v.running {
  130. return nil, 0, false
  131. }
  132. var fixedSizeHash [16]byte
  133. copy(fixedSizeHash[:], userHash)
  134. pair, found := v.userHash[fixedSizeHash]
  135. if found {
  136. return v.validUsers[pair.index], pair.timeSec, true
  137. }
  138. return nil, 0, false
  139. }