client_traffic.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 clearGlobalTraffic(tx, cleanEmails...)
  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. // client_traffics.inbound_id is stale: it reflects the inbound the row was
  112. // first inserted under and is never refreshed. Use the client_inbounds join
  113. // as the authoritative source for which emails belong to a given inbound.
  114. var resetEmails []string
  115. if id == -1 {
  116. if err := tx.Model(xray.ClientTraffic{}).Pluck("email", &resetEmails).Error; err != nil {
  117. return err
  118. }
  119. } else {
  120. if err := tx.Table("client_inbounds ci").
  121. Select("c.email").
  122. Joins("JOIN clients c ON c.id = ci.client_id").
  123. Where("ci.inbound_id = ?", id).
  124. Pluck("c.email", &resetEmails).Error; err != nil {
  125. return err
  126. }
  127. }
  128. if len(resetEmails) == 0 {
  129. return nil
  130. }
  131. result := tx.Model(xray.ClientTraffic{}).
  132. Where("email IN ?", resetEmails).
  133. Updates(map[string]any{"enable": true, "up": 0, "down": 0})
  134. if result.Error != nil {
  135. return result.Error
  136. }
  137. if err := clearGlobalTraffic(tx, resetEmails...); err != nil {
  138. return err
  139. }
  140. inboundWhereText := "id "
  141. if id == -1 {
  142. inboundWhereText += " > ?"
  143. } else {
  144. inboundWhereText += " = ?"
  145. }
  146. result = tx.Model(model.Inbound{}).
  147. Where(inboundWhereText, id).
  148. Update("last_traffic_reset_time", now)
  149. return result.Error
  150. }); err != nil {
  151. return err
  152. }
  153. return nil
  154. }
  155. func (s *ClientService) ResetAllTraffics() (bool, error) {
  156. db := database.GetDB()
  157. res := db.Model(&xray.ClientTraffic{}).
  158. Where("1 = 1").
  159. Updates(map[string]any{"up": 0, "down": 0})
  160. if res.Error != nil {
  161. return false, res.Error
  162. }
  163. if err := db.Where("1 = 1").Delete(&model.ClientGlobalTraffic{}).Error; err != nil {
  164. return false, err
  165. }
  166. return res.RowsAffected > 0, nil
  167. }