userset.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package user
  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[string]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[string]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. for entry.lastSec <= nowSec {
  58. idHash := IDHash(entry.id.Bytes())
  59. idHash.Write(entry.lastSec.Bytes())
  60. idHashSlice := idHash.Sum(nil)
  61. hashValue := string(idHashSlice)
  62. us.access.Lock()
  63. us.userHash[hashValue] = indexTimePair{idx, entry.lastSec}
  64. us.access.Unlock()
  65. hash2Remove := entry.hashes.Put(hashValue)
  66. if hash2Remove != nil {
  67. us.access.Lock()
  68. delete(us.userHash, hash2Remove.(string))
  69. us.access.Unlock()
  70. }
  71. entry.lastSec++
  72. }
  73. }
  74. func (us *TimedUserSet) updateUserHash(tick <-chan time.Time) {
  75. for now := range tick {
  76. nowSec := Timestamp(now.Unix() + cacheDurationSec)
  77. for _, entry := range us.ids {
  78. us.generateNewHashes(nowSec, entry.userIdx, entry)
  79. }
  80. }
  81. }
  82. func (us *TimedUserSet) AddUser(user vmess.User) error {
  83. idx := len(us.validUsers)
  84. us.validUsers = append(us.validUsers, user)
  85. nowSec := time.Now().Unix()
  86. entry := &idEntry{
  87. id: user.ID(),
  88. userIdx: idx,
  89. lastSec: Timestamp(nowSec - cacheDurationSec),
  90. hashes: collect.NewSizedQueue(2*cacheDurationSec + 1),
  91. }
  92. us.generateNewHashes(Timestamp(nowSec+cacheDurationSec), idx, entry)
  93. us.ids = append(us.ids, entry)
  94. for _, alterid := range user.AlterIDs() {
  95. entry := &idEntry{
  96. id: alterid,
  97. userIdx: idx,
  98. lastSec: Timestamp(nowSec - cacheDurationSec),
  99. hashes: collect.NewSizedQueue(2*cacheDurationSec + 1),
  100. }
  101. us.generateNewHashes(Timestamp(nowSec+cacheDurationSec), idx, entry)
  102. us.ids = append(us.ids, entry)
  103. }
  104. return nil
  105. }
  106. func (us *TimedUserSet) GetUser(userHash []byte) (vmess.User, Timestamp, bool) {
  107. defer us.access.RUnlock()
  108. us.access.RLock()
  109. pair, found := us.userHash[string(userHash)]
  110. if found {
  111. return us.validUsers[pair.index], pair.timeSec, true
  112. }
  113. return nil, 0, false
  114. }