vmess.go 3.7 KB

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