vmess.go 3.7 KB

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