client_traffic.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. if err := clearGlobalTraffic(tx, cleanEmails...); err != nil {
  95. return err
  96. }
  97. for _, batch := range chunkStrings(cleanEmails, sqlInChunk) {
  98. if err := tx.Where("email IN ?", batch).Delete(&model.NodeClientTraffic{}).Error; err != nil {
  99. return err
  100. }
  101. }
  102. return nil
  103. })
  104. })
  105. if err != nil {
  106. return 0, err
  107. }
  108. return affected, nil
  109. }
  110. func (s *ClientService) ResetAllClientTraffics(inboundSvc *InboundService, id int) error {
  111. return submitTrafficWrite(func() error {
  112. return s.resetAllClientTrafficsLocked(id)
  113. })
  114. }
  115. func (s *ClientService) resetAllClientTrafficsLocked(id int) error {
  116. db := database.GetDB()
  117. now := time.Now().Unix() * 1000
  118. if err := db.Transaction(func(tx *gorm.DB) error {
  119. // client_traffics.inbound_id is stale: it reflects the inbound the row was
  120. // first inserted under and is never refreshed. Use the client_inbounds join
  121. // as the authoritative source for which emails belong to a given inbound.
  122. var resetEmails []string
  123. if id == -1 {
  124. if err := tx.Model(xray.ClientTraffic{}).Pluck("email", &resetEmails).Error; err != nil {
  125. return err
  126. }
  127. } else {
  128. if err := tx.Table("client_inbounds ci").
  129. Select("c.email").
  130. Joins("JOIN clients c ON c.id = ci.client_id").
  131. Where("ci.inbound_id = ?", id).
  132. Pluck("c.email", &resetEmails).Error; err != nil {
  133. return err
  134. }
  135. }
  136. if len(resetEmails) == 0 {
  137. return nil
  138. }
  139. result := tx.Model(xray.ClientTraffic{}).
  140. Where("email IN ?", resetEmails).
  141. Updates(map[string]any{"enable": true, "up": 0, "down": 0})
  142. if result.Error != nil {
  143. return result.Error
  144. }
  145. if err := clearGlobalTraffic(tx, resetEmails...); err != nil {
  146. return err
  147. }
  148. for _, batch := range chunkStrings(resetEmails, sqlInChunk) {
  149. if err := tx.Where("email IN ?", batch).Delete(&model.NodeClientTraffic{}).Error; err != nil {
  150. return err
  151. }
  152. }
  153. inboundWhereText := "id "
  154. if id == -1 {
  155. inboundWhereText += " > ?"
  156. } else {
  157. inboundWhereText += " = ?"
  158. }
  159. result = tx.Model(model.Inbound{}).
  160. Where(inboundWhereText, id).
  161. Update("last_traffic_reset_time", now)
  162. return result.Error
  163. }); err != nil {
  164. return err
  165. }
  166. return nil
  167. }
  168. func (s *ClientService) ResetAllTraffics() (bool, error) {
  169. db := database.GetDB()
  170. res := db.Model(&xray.ClientTraffic{}).
  171. Where("1 = 1").
  172. Updates(map[string]any{"up": 0, "down": 0})
  173. if res.Error != nil {
  174. return false, res.Error
  175. }
  176. if err := db.Where("1 = 1").Delete(&model.ClientGlobalTraffic{}).Error; err != nil {
  177. return false, err
  178. }
  179. return res.RowsAffected > 0, nil
  180. }