vmess.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. validUsers []*protocol.User
  28. userHash map[[16]byte]indexTimePair
  29. ids []*idEntry
  30. hasher protocol.IDHash
  31. baseTime protocol.Timestamp
  32. }
  33. type indexTimePair struct {
  34. index int
  35. timeInc uint32
  36. }
  37. func NewTimedUserValidator(ctx context.Context, hasher protocol.IDHash) protocol.UserValidator {
  38. tus := &TimedUserValidator{
  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(ctx, 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. common.Must2(idHash.Write(entry.lastSec.Bytes(nil)))
  54. idHash.Sum(hashValue[:0])
  55. idHash.Reset()
  56. common.Must2(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(ctx context.Context, 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 <-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. }