userset.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package protocol
  2. import (
  3. "sync"
  4. "time"
  5. "github.com/v2ray/v2ray-core/common/collect"
  6. "github.com/v2ray/v2ray-core/common/serial"
  7. "github.com/v2ray/v2ray-core/proxy/vmess"
  8. )
  9. const (
  10. updateIntervalSec = 10
  11. cacheDurationSec = 120
  12. )
  13. type Timestamp int64
  14. func (this Timestamp) Bytes() []byte {
  15. return serial.Int64Literal(this).Bytes()
  16. }
  17. func (this Timestamp) HashBytes() []byte {
  18. once := this.Bytes()
  19. bytes := make([]byte, 0, 32)
  20. bytes = append(bytes, once...)
  21. bytes = append(bytes, once...)
  22. bytes = append(bytes, once...)
  23. bytes = append(bytes, once...)
  24. return bytes
  25. }
  26. type idEntry struct {
  27. id *vmess.ID
  28. userIdx int
  29. lastSec Timestamp
  30. hashes *collect.SizedQueue
  31. }
  32. type UserSet interface {
  33. AddUser(user vmess.User) error
  34. GetUser(timeHash []byte) (vmess.User, Timestamp, bool)
  35. }
  36. type TimedUserSet struct {
  37. validUsers []vmess.User
  38. userHash map[[16]byte]indexTimePair
  39. ids []*idEntry
  40. access sync.RWMutex
  41. }
  42. type indexTimePair struct {
  43. index int
  44. timeSec Timestamp
  45. }
  46. func NewTimedUserSet() UserSet {
  47. tus := &TimedUserSet{
  48. validUsers: make([]vmess.User, 0, 16),
  49. userHash: make(map[[16]byte]indexTimePair, 512),
  50. access: sync.RWMutex{},
  51. ids: make([]*idEntry, 0, 512),
  52. }
  53. go tus.updateUserHash(time.Tick(updateIntervalSec * time.Second))
  54. return tus
  55. }
  56. func (us *TimedUserSet) generateNewHashes(nowSec Timestamp, idx int, entry *idEntry) {
  57. var hashValue [16]byte
  58. idHash := IDHash(entry.id.Bytes())
  59. for entry.lastSec <= nowSec {
  60. idHash.Write(entry.lastSec.Bytes())
  61. idHash.Sum(hashValue[:0])
  62. idHash.Reset()
  63. hash2Remove := entry.hashes.Put(hashValue)
  64. us.access.Lock()
  65. us.userHash[hashValue] = indexTimePair{idx, entry.lastSec}
  66. if hash2Remove != nil {
  67. delete(us.userHash, hash2Remove.([16]byte))
  68. }
  69. us.access.Unlock()
  70. entry.lastSec++
  71. }
  72. }
  73. func (us *TimedUserSet) updateUserHash(tick <-chan time.Time) {
  74. for now := range tick {
  75. nowSec := Timestamp(now.Unix() + cacheDurationSec)
  76. for _, entry := range us.ids {
  77. us.generateNewHashes(nowSec, entry.userIdx, entry)
  78. }
  79. }
  80. }
  81. func (us *TimedUserSet) AddUser(user vmess.User) error {
  82. idx := len(us.validUsers)
  83. us.validUsers = append(us.validUsers, user)
  84. nowSec := time.Now().Unix()
  85. entry := &idEntry{
  86. id: user.ID(),
  87. userIdx: idx,
  88. lastSec: Timestamp(nowSec - cacheDurationSec),
  89. hashes: collect.NewSizedQueue(2*cacheDurationSec + 1),
  90. }
  91. us.generateNewHashes(Timestamp(nowSec+cacheDurationSec), idx, entry)
  92. us.ids = append(us.ids, entry)
  93. for _, alterid := range user.AlterIDs() {
  94. entry := &idEntry{
  95. id: alterid,
  96. userIdx: idx,
  97. lastSec: Timestamp(nowSec - cacheDurationSec),
  98. hashes: collect.NewSizedQueue(2*cacheDurationSec + 1),
  99. }
  100. us.generateNewHashes(Timestamp(nowSec+cacheDurationSec), idx, entry)
  101. us.ids = append(us.ids, entry)
  102. }
  103. return nil
  104. }
  105. func (us *TimedUserSet) GetUser(userHash []byte) (vmess.User, Timestamp, bool) {
  106. defer us.access.RUnlock()
  107. us.access.RLock()
  108. var fixedSizeHash [16]byte
  109. copy(fixedSizeHash[:], userHash)
  110. pair, found := us.userHash[fixedSizeHash]
  111. if found {
  112. return us.validUsers[pair.index], pair.timeSec, true
  113. }
  114. return nil, 0, false
  115. }