vmess.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 // import "github.com/v2ray/v2ray-core/proxy/vmess"
  7. import (
  8. "sync"
  9. "time"
  10. "github.com/v2ray/v2ray-core/common/dice"
  11. "github.com/v2ray/v2ray-core/common/protocol"
  12. "github.com/v2ray/v2ray-core/common/signal"
  13. )
  14. type Account struct {
  15. ID *protocol.ID
  16. AlterIDs []*protocol.ID
  17. }
  18. func (this *Account) AnyValidID() *protocol.ID {
  19. if len(this.AlterIDs) == 0 {
  20. return this.ID
  21. }
  22. return this.AlterIDs[dice.Roll(len(this.AlterIDs))]
  23. }
  24. func (this *Account) Equals(account protocol.Account) bool {
  25. vmessAccount, ok := account.(*Account)
  26. if !ok {
  27. return false
  28. }
  29. // TODO: handle AlterIds difference
  30. return this.ID.Equals(vmessAccount.ID)
  31. }
  32. const (
  33. updateIntervalSec = 10
  34. cacheDurationSec = 120
  35. )
  36. type idEntry struct {
  37. id *protocol.ID
  38. userIdx int
  39. lastSec protocol.Timestamp
  40. lastSecRemoval protocol.Timestamp
  41. }
  42. type TimedUserValidator struct {
  43. sync.RWMutex
  44. running bool
  45. validUsers []*protocol.User
  46. userHash map[[16]byte]*indexTimePair
  47. ids []*idEntry
  48. hasher protocol.IDHash
  49. cancel *signal.CancelSignal
  50. }
  51. type indexTimePair struct {
  52. index int
  53. timeSec protocol.Timestamp
  54. }
  55. func NewTimedUserValidator(hasher protocol.IDHash) protocol.UserValidator {
  56. tus := &TimedUserValidator{
  57. validUsers: make([]*protocol.User, 0, 16),
  58. userHash: make(map[[16]byte]*indexTimePair, 512),
  59. ids: make([]*idEntry, 0, 512),
  60. hasher: hasher,
  61. running: true,
  62. cancel: signal.NewCloseSignal(),
  63. }
  64. go tus.updateUserHash(updateIntervalSec * time.Second)
  65. return tus
  66. }
  67. func (this *TimedUserValidator) Release() {
  68. if !this.running {
  69. return
  70. }
  71. this.cancel.Cancel()
  72. <-this.cancel.WaitForDone()
  73. this.Lock()
  74. defer this.Unlock()
  75. if !this.running {
  76. return
  77. }
  78. this.running = false
  79. this.validUsers = nil
  80. this.userHash = nil
  81. this.ids = nil
  82. this.hasher = nil
  83. this.cancel = nil
  84. }
  85. func (this *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, idx int, entry *idEntry) {
  86. var hashValue [16]byte
  87. var hashValueRemoval [16]byte
  88. idHash := this.hasher(entry.id.Bytes())
  89. for entry.lastSec <= nowSec {
  90. idHash.Write(entry.lastSec.Bytes(nil))
  91. idHash.Sum(hashValue[:0])
  92. idHash.Reset()
  93. idHash.Write(entry.lastSecRemoval.Bytes(nil))
  94. idHash.Sum(hashValueRemoval[:0])
  95. idHash.Reset()
  96. this.Lock()
  97. this.userHash[hashValue] = &indexTimePair{idx, entry.lastSec}
  98. delete(this.userHash, hashValueRemoval)
  99. this.Unlock()
  100. entry.lastSec++
  101. entry.lastSecRemoval++
  102. }
  103. }
  104. func (this *TimedUserValidator) updateUserHash(interval time.Duration) {
  105. L:
  106. for {
  107. select {
  108. case now := <-time.After(interval):
  109. nowSec := protocol.Timestamp(now.Unix() + cacheDurationSec)
  110. for _, entry := range this.ids {
  111. this.generateNewHashes(nowSec, entry.userIdx, entry)
  112. }
  113. case <-this.cancel.WaitForCancel():
  114. break L
  115. }
  116. }
  117. this.cancel.Done()
  118. }
  119. func (this *TimedUserValidator) Add(user *protocol.User) error {
  120. idx := len(this.validUsers)
  121. this.validUsers = append(this.validUsers, user)
  122. account := user.Account.(*Account)
  123. nowSec := time.Now().Unix()
  124. entry := &idEntry{
  125. id: account.ID,
  126. userIdx: idx,
  127. lastSec: protocol.Timestamp(nowSec - cacheDurationSec),
  128. lastSecRemoval: protocol.Timestamp(nowSec - cacheDurationSec*3),
  129. }
  130. this.generateNewHashes(protocol.Timestamp(nowSec+cacheDurationSec), idx, entry)
  131. this.ids = append(this.ids, entry)
  132. for _, alterid := range account.AlterIDs {
  133. entry := &idEntry{
  134. id: alterid,
  135. userIdx: idx,
  136. lastSec: protocol.Timestamp(nowSec - cacheDurationSec),
  137. lastSecRemoval: protocol.Timestamp(nowSec - cacheDurationSec*3),
  138. }
  139. this.generateNewHashes(protocol.Timestamp(nowSec+cacheDurationSec), idx, entry)
  140. this.ids = append(this.ids, entry)
  141. }
  142. return nil
  143. }
  144. func (this *TimedUserValidator) Get(userHash []byte) (*protocol.User, protocol.Timestamp, bool) {
  145. defer this.RUnlock()
  146. this.RLock()
  147. if !this.running {
  148. return nil, 0, false
  149. }
  150. var fixedSizeHash [16]byte
  151. copy(fixedSizeHash[:], userHash)
  152. pair, found := this.userHash[fixedSizeHash]
  153. if found {
  154. return this.validUsers[pair.index], pair.timeSec, true
  155. }
  156. return nil, 0, false
  157. }