vmess.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 (this *TimedUserValidator) Release() {
  49. if !this.running {
  50. return
  51. }
  52. this.cancel.Cancel()
  53. <-this.cancel.WaitForDone()
  54. this.Lock()
  55. defer this.Unlock()
  56. if !this.running {
  57. return
  58. }
  59. this.running = false
  60. this.validUsers = nil
  61. this.userHash = nil
  62. this.ids = nil
  63. this.hasher = nil
  64. this.cancel = nil
  65. }
  66. func (this *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, idx int, entry *idEntry) {
  67. var hashValue [16]byte
  68. var hashValueRemoval [16]byte
  69. idHash := this.hasher(entry.id.Bytes())
  70. for entry.lastSec <= nowSec {
  71. idHash.Write(entry.lastSec.Bytes(nil))
  72. idHash.Sum(hashValue[:0])
  73. idHash.Reset()
  74. idHash.Write(entry.lastSecRemoval.Bytes(nil))
  75. idHash.Sum(hashValueRemoval[:0])
  76. idHash.Reset()
  77. this.Lock()
  78. this.userHash[hashValue] = &indexTimePair{idx, entry.lastSec}
  79. delete(this.userHash, hashValueRemoval)
  80. this.Unlock()
  81. entry.lastSec++
  82. entry.lastSecRemoval++
  83. }
  84. }
  85. func (this *TimedUserValidator) updateUserHash(interval time.Duration) {
  86. L:
  87. for {
  88. select {
  89. case now := <-time.After(interval):
  90. nowSec := protocol.Timestamp(now.Unix() + cacheDurationSec)
  91. for _, entry := range this.ids {
  92. this.generateNewHashes(nowSec, entry.userIdx, entry)
  93. }
  94. case <-this.cancel.WaitForCancel():
  95. break L
  96. }
  97. }
  98. this.cancel.Done()
  99. }
  100. func (this *TimedUserValidator) Add(user *protocol.User) error {
  101. idx := len(this.validUsers)
  102. this.validUsers = append(this.validUsers, user)
  103. rawAccount, err := user.GetTypedAccount()
  104. if err != nil {
  105. return err
  106. }
  107. account := rawAccount.(*InternalAccount)
  108. nowSec := time.Now().Unix()
  109. entry := &idEntry{
  110. id: account.ID,
  111. userIdx: idx,
  112. lastSec: protocol.Timestamp(nowSec - cacheDurationSec),
  113. lastSecRemoval: protocol.Timestamp(nowSec - cacheDurationSec*3),
  114. }
  115. this.generateNewHashes(protocol.Timestamp(nowSec+cacheDurationSec), idx, entry)
  116. this.ids = append(this.ids, entry)
  117. for _, alterid := range account.AlterIDs {
  118. entry := &idEntry{
  119. id: alterid,
  120. userIdx: idx,
  121. lastSec: protocol.Timestamp(nowSec - cacheDurationSec),
  122. lastSecRemoval: protocol.Timestamp(nowSec - cacheDurationSec*3),
  123. }
  124. this.generateNewHashes(protocol.Timestamp(nowSec+cacheDurationSec), idx, entry)
  125. this.ids = append(this.ids, entry)
  126. }
  127. return nil
  128. }
  129. func (this *TimedUserValidator) Get(userHash []byte) (*protocol.User, protocol.Timestamp, bool) {
  130. defer this.RUnlock()
  131. this.RLock()
  132. if !this.running {
  133. return nil, 0, false
  134. }
  135. var fixedSizeHash [16]byte
  136. copy(fixedSizeHash[:], userHash)
  137. pair, found := this.userHash[fixedSizeHash]
  138. if found {
  139. return this.validUsers[pair.index], pair.timeSec, true
  140. }
  141. return nil, 0, false
  142. }