vmess.go 3.6 KB

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