vmess.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. "strings"
  10. "sync"
  11. "time"
  12. "v2ray.com/core/common"
  13. "v2ray.com/core/common/protocol"
  14. "v2ray.com/core/common/signal"
  15. )
  16. const (
  17. updateInterval = 10 * time.Second
  18. cacheDurationSec = 120
  19. )
  20. type user struct {
  21. user *protocol.User
  22. account *InternalAccount
  23. lastSec protocol.Timestamp
  24. }
  25. type TimedUserValidator struct {
  26. sync.RWMutex
  27. users []*user
  28. userHash map[[16]byte]indexTimePair
  29. hasher protocol.IDHash
  30. baseTime protocol.Timestamp
  31. task *signal.PeriodicTask
  32. }
  33. type indexTimePair struct {
  34. user *user
  35. timeInc uint32
  36. }
  37. func NewTimedUserValidator(hasher protocol.IDHash) protocol.UserValidator {
  38. tuv := &TimedUserValidator{
  39. users: make([]*user, 0, 16),
  40. userHash: make(map[[16]byte]indexTimePair, 1024),
  41. hasher: hasher,
  42. baseTime: protocol.Timestamp(time.Now().Unix() - cacheDurationSec*3),
  43. }
  44. tuv.task = &signal.PeriodicTask{
  45. Interval: updateInterval,
  46. Execute: func() error {
  47. tuv.updateUserHash()
  48. return nil
  49. },
  50. }
  51. tuv.task.Start()
  52. return tuv
  53. }
  54. func (v *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, user *user) {
  55. var hashValue [16]byte
  56. genHashForID := func(id *protocol.ID) {
  57. idHash := v.hasher(id.Bytes())
  58. for ts := user.lastSec; ts <= nowSec; ts++ {
  59. common.Must2(idHash.Write(ts.Bytes(nil)))
  60. idHash.Sum(hashValue[:0])
  61. idHash.Reset()
  62. v.userHash[hashValue] = indexTimePair{
  63. user: user,
  64. timeInc: uint32(ts - v.baseTime),
  65. }
  66. }
  67. }
  68. genHashForID(user.account.ID)
  69. for _, id := range user.account.AlterIDs {
  70. genHashForID(id)
  71. }
  72. user.lastSec = nowSec
  73. }
  74. func (v *TimedUserValidator) removeExpiredHashes(expire uint32) {
  75. for key, pair := range v.userHash {
  76. if pair.timeInc < expire {
  77. delete(v.userHash, key)
  78. }
  79. }
  80. }
  81. func (v *TimedUserValidator) updateUserHash() {
  82. now := time.Now()
  83. nowSec := protocol.Timestamp(now.Unix() + cacheDurationSec)
  84. v.Lock()
  85. defer v.Unlock()
  86. for _, user := range v.users {
  87. v.generateNewHashes(nowSec, user)
  88. }
  89. expire := protocol.Timestamp(now.Unix() - cacheDurationSec*3)
  90. if expire > v.baseTime {
  91. v.removeExpiredHashes(uint32(expire - v.baseTime))
  92. }
  93. }
  94. func (v *TimedUserValidator) Add(u *protocol.User) error {
  95. v.Lock()
  96. defer v.Unlock()
  97. rawAccount, err := u.GetTypedAccount()
  98. if err != nil {
  99. return err
  100. }
  101. account := rawAccount.(*InternalAccount)
  102. nowSec := time.Now().Unix()
  103. uu := &user{
  104. user: u,
  105. account: account,
  106. lastSec: protocol.Timestamp(nowSec - cacheDurationSec),
  107. }
  108. v.users = append(v.users, uu)
  109. v.generateNewHashes(protocol.Timestamp(nowSec+cacheDurationSec), uu)
  110. return nil
  111. }
  112. func (v *TimedUserValidator) Get(userHash []byte) (*protocol.User, protocol.Timestamp, bool) {
  113. defer v.RUnlock()
  114. v.RLock()
  115. var fixedSizeHash [16]byte
  116. copy(fixedSizeHash[:], userHash)
  117. pair, found := v.userHash[fixedSizeHash]
  118. if found {
  119. return pair.user.user, protocol.Timestamp(pair.timeInc) + v.baseTime, true
  120. }
  121. return nil, 0, false
  122. }
  123. func (v *TimedUserValidator) Remove(email string) bool {
  124. v.Lock()
  125. defer v.Unlock()
  126. email = strings.ToLower(email)
  127. idx := -1
  128. for i, u := range v.users {
  129. if strings.ToLower(u.user.Email) == email {
  130. idx = i
  131. break
  132. }
  133. }
  134. if idx == -1 {
  135. return false
  136. }
  137. ulen := len(v.users)
  138. if idx < len(v.users) {
  139. v.users[idx] = v.users[ulen-1]
  140. v.users[ulen-1] = nil
  141. v.users = v.users[:ulen-1]
  142. }
  143. return true
  144. }
  145. // Close implements common.Closable.
  146. func (v *TimedUserValidator) Close() error {
  147. return v.task.Close()
  148. }