vmess.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/task"
  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 *task.Periodic
  32. }
  33. type indexTimePair struct {
  34. user *user
  35. timeInc uint32
  36. }
  37. func NewTimedUserValidator(hasher protocol.IDHash) *TimedUserValidator {
  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*2),
  43. }
  44. tuv.task = &task.Periodic{
  45. Interval: updateInterval,
  46. Execute: func() error {
  47. tuv.updateUserHash()
  48. return nil
  49. },
  50. }
  51. common.Must(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. lastSec := user.lastSec
  59. if lastSec < nowSec-cacheDurationSec*2 {
  60. lastSec = nowSec - cacheDurationSec*2
  61. }
  62. for ts := lastSec; ts <= nowSec; ts++ {
  63. common.Must2(idHash.Write(ts.Bytes(nil)))
  64. idHash.Sum(hashValue[:0])
  65. idHash.Reset()
  66. v.userHash[hashValue] = indexTimePair{
  67. user: user,
  68. timeInc: uint32(ts - v.baseTime),
  69. }
  70. }
  71. }
  72. genHashForID(user.account.ID)
  73. for _, id := range user.account.AlterIDs {
  74. genHashForID(id)
  75. }
  76. user.lastSec = nowSec
  77. }
  78. func (v *TimedUserValidator) removeExpiredHashes(expire uint32) {
  79. for key, pair := range v.userHash {
  80. if pair.timeInc < expire {
  81. delete(v.userHash, key)
  82. }
  83. }
  84. }
  85. func (v *TimedUserValidator) updateUserHash() {
  86. now := time.Now()
  87. nowSec := protocol.Timestamp(now.Unix() + cacheDurationSec)
  88. v.Lock()
  89. defer v.Unlock()
  90. for _, user := range v.users {
  91. v.generateNewHashes(nowSec, user)
  92. }
  93. expire := protocol.Timestamp(now.Unix() - cacheDurationSec)
  94. if expire > v.baseTime {
  95. v.removeExpiredHashes(uint32(expire - v.baseTime))
  96. }
  97. }
  98. func (v *TimedUserValidator) Add(u *protocol.User) error {
  99. v.Lock()
  100. defer v.Unlock()
  101. rawAccount, err := u.GetTypedAccount()
  102. if err != nil {
  103. return err
  104. }
  105. account := rawAccount.(*InternalAccount)
  106. nowSec := time.Now().Unix()
  107. uu := &user{
  108. user: u,
  109. account: account,
  110. lastSec: protocol.Timestamp(nowSec - cacheDurationSec),
  111. }
  112. v.users = append(v.users, uu)
  113. v.generateNewHashes(protocol.Timestamp(nowSec+cacheDurationSec), uu)
  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 pair.user.user, protocol.Timestamp(pair.timeInc) + v.baseTime, true
  124. }
  125. return nil, 0, false
  126. }
  127. func (v *TimedUserValidator) Remove(email string) bool {
  128. v.Lock()
  129. defer v.Unlock()
  130. email = strings.ToLower(email)
  131. idx := -1
  132. for i, u := range v.users {
  133. if strings.ToLower(u.user.Email) == email {
  134. idx = i
  135. break
  136. }
  137. }
  138. if idx == -1 {
  139. return false
  140. }
  141. ulen := len(v.users)
  142. if idx < ulen {
  143. v.users[idx] = v.users[ulen-1]
  144. v.users[ulen-1] = nil
  145. v.users = v.users[:ulen-1]
  146. }
  147. return true
  148. }
  149. // Close implements common.Closable.
  150. func (v *TimedUserValidator) Close() error {
  151. return v.task.Close()
  152. }