receiver.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package outbound
  2. import (
  3. "sync"
  4. "time"
  5. "github.com/v2ray/v2ray-core/common/dice"
  6. v2net "github.com/v2ray/v2ray-core/common/net"
  7. "github.com/v2ray/v2ray-core/common/protocol"
  8. )
  9. type Receiver struct {
  10. sync.RWMutex
  11. Destination v2net.Destination
  12. Accounts []*protocol.User
  13. }
  14. func NewReceiver(dest v2net.Destination, users ...*protocol.User) *Receiver {
  15. return &Receiver{
  16. Destination: dest,
  17. Accounts: users,
  18. }
  19. }
  20. func (this *Receiver) HasUser(user *protocol.User) bool {
  21. this.RLock()
  22. defer this.RUnlock()
  23. account := user.Account.(*protocol.VMessAccount)
  24. for _, u := range this.Accounts {
  25. // TODO: handle AlterIds difference.
  26. uAccount := u.Account.(*protocol.VMessAccount)
  27. if uAccount.ID.Equals(account.ID) {
  28. return true
  29. }
  30. }
  31. return false
  32. }
  33. func (this *Receiver) AddUser(user *protocol.User) {
  34. if this.HasUser(user) {
  35. return
  36. }
  37. this.Lock()
  38. this.Accounts = append(this.Accounts, user)
  39. this.Unlock()
  40. }
  41. func (this *Receiver) PickUser() *protocol.User {
  42. return this.Accounts[dice.Roll(len(this.Accounts))]
  43. }
  44. type ExpiringReceiver struct {
  45. *Receiver
  46. until time.Time
  47. }
  48. func (this *ExpiringReceiver) Expired() bool {
  49. return this.until.Before(time.Now())
  50. }
  51. type ReceiverManager struct {
  52. receivers []*Receiver
  53. detours []*ExpiringReceiver
  54. detourAccess sync.RWMutex
  55. }
  56. func NewReceiverManager(receivers []*Receiver) *ReceiverManager {
  57. return &ReceiverManager{
  58. receivers: receivers,
  59. detours: make([]*ExpiringReceiver, 0, 16),
  60. }
  61. }
  62. func (this *ReceiverManager) AddDetour(rec *Receiver, availableMin byte) {
  63. if availableMin < 2 {
  64. return
  65. }
  66. this.detourAccess.RLock()
  67. destExists := false
  68. for _, r := range this.detours {
  69. if r.Destination == rec.Destination {
  70. destExists = true
  71. // Destination exists, add new user if necessary
  72. for _, u := range rec.Accounts {
  73. r.AddUser(u)
  74. }
  75. break
  76. }
  77. }
  78. this.detourAccess.RUnlock()
  79. if !destExists {
  80. expRec := &ExpiringReceiver{
  81. Receiver: rec,
  82. until: time.Now().Add(time.Duration(availableMin-1) * time.Minute),
  83. }
  84. this.detourAccess.Lock()
  85. this.detours = append(this.detours, expRec)
  86. this.detourAccess.Unlock()
  87. }
  88. }
  89. func (this *ReceiverManager) pickDetour() *Receiver {
  90. if len(this.detours) == 0 {
  91. return nil
  92. }
  93. this.detourAccess.RLock()
  94. idx := dice.Roll(len(this.detours))
  95. rec := this.detours[idx]
  96. this.detourAccess.RUnlock()
  97. if rec.Expired() {
  98. this.detourAccess.Lock()
  99. detourLen := len(this.detours)
  100. if detourLen > idx && this.detours[idx].Expired() {
  101. this.detours[idx] = this.detours[detourLen-1]
  102. this.detours = this.detours[:detourLen-1]
  103. }
  104. this.detourAccess.Unlock()
  105. return nil
  106. }
  107. return rec.Receiver
  108. }
  109. func (this *ReceiverManager) pickStdReceiver() *Receiver {
  110. return this.receivers[dice.Roll(len(this.receivers))]
  111. }
  112. func (this *ReceiverManager) PickReceiver() (v2net.Destination, *protocol.User) {
  113. rec := this.pickDetour()
  114. if rec == nil {
  115. rec = this.pickStdReceiver()
  116. }
  117. user := rec.PickUser()
  118. return rec.Destination, user
  119. }