client_traffic.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package service
  2. import (
  3. "strings"
  4. "time"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database"
  6. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  7. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  8. "github.com/mhsanaei/3x-ui/v3/internal/util/common"
  9. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  10. "gorm.io/gorm"
  11. )
  12. func (s *ClientService) ResetTrafficByEmail(inboundSvc *InboundService, email string) (bool, error) {
  13. if email == "" {
  14. return false, common.NewError("client email is required")
  15. }
  16. rec, err := s.GetRecordByEmail(nil, email)
  17. if err != nil {
  18. return false, err
  19. }
  20. inboundIds, err := s.GetInboundIdsForRecord(rec.Id)
  21. if err != nil {
  22. return false, err
  23. }
  24. needRestart := false
  25. if !rec.Enable {
  26. updated := rec.ToClient()
  27. updated.Enable = true
  28. nr, uErr := s.Update(inboundSvc, rec.Id, *updated)
  29. if uErr != nil {
  30. logger.Warning("Failed to auto-enable client during traffic reset:", uErr)
  31. }
  32. if nr {
  33. needRestart = true
  34. }
  35. }
  36. if len(inboundIds) == 0 {
  37. if rErr := inboundSvc.ResetClientTrafficByEmail(email); rErr != nil {
  38. return false, rErr
  39. }
  40. return needRestart, nil
  41. }
  42. for _, ibId := range inboundIds {
  43. nr, rErr := inboundSvc.ResetClientTraffic(ibId, email)
  44. if rErr != nil {
  45. return needRestart, rErr
  46. }
  47. if nr {
  48. needRestart = true
  49. }
  50. }
  51. return needRestart, nil
  52. }
  53. func (s *ClientService) BulkResetTraffic(inboundSvc *InboundService, emails []string) (int, error) {
  54. if len(emails) == 0 {
  55. return 0, nil
  56. }
  57. seen := map[string]struct{}{}
  58. cleanEmails := make([]string, 0, len(emails))
  59. for _, e := range emails {
  60. e = strings.TrimSpace(e)
  61. if e == "" {
  62. continue
  63. }
  64. if _, ok := seen[e]; ok {
  65. continue
  66. }
  67. seen[e] = struct{}{}
  68. cleanEmails = append(cleanEmails, e)
  69. }
  70. if len(cleanEmails) == 0 {
  71. return 0, nil
  72. }
  73. for _, e := range cleanEmails {
  74. rec, err := s.GetRecordByEmail(nil, e)
  75. if err == nil && !rec.Enable {
  76. updated := rec.ToClient()
  77. updated.Enable = true
  78. s.Update(inboundSvc, rec.Id, *updated)
  79. }
  80. }
  81. affected := 0
  82. err := submitTrafficWrite(func() error {
  83. db := database.GetDB()
  84. return db.Transaction(func(tx *gorm.DB) error {
  85. for _, batch := range chunkStrings(cleanEmails, sqlInChunk) {
  86. res := tx.Model(xray.ClientTraffic{}).
  87. Where("email IN ?", batch).
  88. Updates(map[string]any{"enable": true, "up": 0, "down": 0})
  89. if res.Error != nil {
  90. return res.Error
  91. }
  92. affected += int(res.RowsAffected)
  93. }
  94. return nil
  95. })
  96. })
  97. if err != nil {
  98. return 0, err
  99. }
  100. return affected, nil
  101. }
  102. func (s *ClientService) ResetAllClientTraffics(inboundSvc *InboundService, id int) error {
  103. return submitTrafficWrite(func() error {
  104. return s.resetAllClientTrafficsLocked(id)
  105. })
  106. }
  107. func (s *ClientService) resetAllClientTrafficsLocked(id int) error {
  108. db := database.GetDB()
  109. now := time.Now().Unix() * 1000
  110. if err := db.Transaction(func(tx *gorm.DB) error {
  111. whereText := "inbound_id "
  112. if id == -1 {
  113. whereText += " > ?"
  114. } else {
  115. whereText += " = ?"
  116. }
  117. result := tx.Model(xray.ClientTraffic{}).
  118. Where(whereText, id).
  119. Updates(map[string]any{"enable": true, "up": 0, "down": 0})
  120. if result.Error != nil {
  121. return result.Error
  122. }
  123. inboundWhereText := "id "
  124. if id == -1 {
  125. inboundWhereText += " > ?"
  126. } else {
  127. inboundWhereText += " = ?"
  128. }
  129. result = tx.Model(model.Inbound{}).
  130. Where(inboundWhereText, id).
  131. Update("last_traffic_reset_time", now)
  132. return result.Error
  133. }); err != nil {
  134. return err
  135. }
  136. return nil
  137. }
  138. func (s *ClientService) ResetAllTraffics() (bool, error) {
  139. res := database.GetDB().Model(&xray.ClientTraffic{}).
  140. Where("1 = 1").
  141. Updates(map[string]any{"up": 0, "down": 0})
  142. if res.Error != nil {
  143. return false, res.Error
  144. }
  145. return res.RowsAffected > 0, nil
  146. }