client_link.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package service
  2. import (
  3. "strings"
  4. "github.com/mhsanaei/3x-ui/v3/internal/database"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  6. "gorm.io/gorm"
  7. )
  8. func (s *ClientService) SyncInbound(tx *gorm.DB, inboundId int, clients []model.Client) error {
  9. if tx == nil {
  10. tx = database.GetDB()
  11. }
  12. if err := tx.Where("inbound_id = ?", inboundId).Delete(&model.ClientInbound{}).Error; err != nil {
  13. return err
  14. }
  15. emails := make([]string, 0, len(clients))
  16. seen := make(map[string]struct{}, len(clients))
  17. for i := range clients {
  18. email := strings.TrimSpace(clients[i].Email)
  19. if email == "" {
  20. continue
  21. }
  22. if _, ok := seen[email]; ok {
  23. continue
  24. }
  25. seen[email] = struct{}{}
  26. emails = append(emails, email)
  27. }
  28. existing := make(map[string]*model.ClientRecord, len(emails))
  29. const selectChunk = 400
  30. for start := 0; start < len(emails); start += selectChunk {
  31. end := min(start+selectChunk, len(emails))
  32. var rows []model.ClientRecord
  33. if err := tx.Where("email IN ?", emails[start:end]).Find(&rows).Error; err != nil {
  34. return err
  35. }
  36. for i := range rows {
  37. r := rows[i]
  38. existing[r.Email] = &r
  39. }
  40. }
  41. idByEmail := make(map[string]int, len(emails))
  42. pending := make(map[string]*model.ClientRecord, len(emails))
  43. toCreate := make([]*model.ClientRecord, 0, len(emails))
  44. for i := range clients {
  45. email := strings.TrimSpace(clients[i].Email)
  46. if email == "" {
  47. continue
  48. }
  49. incoming := clients[i].ToRecord()
  50. row, ok := existing[email]
  51. if !ok {
  52. if _, dup := pending[email]; !dup {
  53. pending[email] = incoming
  54. toCreate = append(toCreate, incoming)
  55. }
  56. continue
  57. }
  58. before := *row
  59. if incoming.UUID != "" {
  60. row.UUID = incoming.UUID
  61. }
  62. if incoming.Password != "" {
  63. row.Password = incoming.Password
  64. }
  65. if incoming.Auth != "" {
  66. row.Auth = incoming.Auth
  67. }
  68. row.Flow = incoming.Flow
  69. if incoming.Security != "" {
  70. row.Security = incoming.Security
  71. }
  72. if incoming.Reverse != "" {
  73. row.Reverse = incoming.Reverse
  74. }
  75. row.SubID = incoming.SubID
  76. row.LimitIP = incoming.LimitIP
  77. row.TotalGB = incoming.TotalGB
  78. row.ExpiryTime = incoming.ExpiryTime
  79. row.Enable = incoming.Enable
  80. row.TgID = incoming.TgID
  81. if incoming.Group != "" {
  82. row.Group = incoming.Group
  83. }
  84. row.Comment = incoming.Comment
  85. row.Reset = incoming.Reset
  86. if incoming.CreatedAt > 0 && (row.CreatedAt == 0 || incoming.CreatedAt < row.CreatedAt) {
  87. row.CreatedAt = incoming.CreatedAt
  88. }
  89. preservedUpdatedAt := max(incoming.UpdatedAt, row.UpdatedAt)
  90. row.UpdatedAt = preservedUpdatedAt
  91. idByEmail[email] = row.Id
  92. if *row == before {
  93. continue
  94. }
  95. if err := tx.Save(row).Error; err != nil {
  96. return err
  97. }
  98. if err := tx.Model(&model.ClientRecord{}).
  99. Where("id = ?", row.Id).
  100. UpdateColumn("updated_at", preservedUpdatedAt).Error; err != nil {
  101. return err
  102. }
  103. }
  104. if len(toCreate) > 0 {
  105. if err := tx.CreateInBatches(toCreate, 200).Error; err != nil {
  106. return err
  107. }
  108. for _, rec := range toCreate {
  109. idByEmail[rec.Email] = rec.Id
  110. }
  111. }
  112. links := make([]model.ClientInbound, 0, len(clients))
  113. linked := make(map[int]struct{}, len(clients))
  114. for i := range clients {
  115. email := strings.TrimSpace(clients[i].Email)
  116. if email == "" {
  117. continue
  118. }
  119. id, ok := idByEmail[email]
  120. if !ok {
  121. continue
  122. }
  123. if _, dup := linked[id]; dup {
  124. continue
  125. }
  126. linked[id] = struct{}{}
  127. links = append(links, model.ClientInbound{
  128. ClientId: id,
  129. InboundId: inboundId,
  130. FlowOverride: clients[i].Flow,
  131. })
  132. }
  133. if len(links) > 0 {
  134. if err := tx.CreateInBatches(links, 200).Error; err != nil {
  135. return err
  136. }
  137. }
  138. return nil
  139. }
  140. func (s *ClientService) DetachInbound(tx *gorm.DB, inboundId int) error {
  141. if tx == nil {
  142. tx = database.GetDB()
  143. }
  144. return tx.Where("inbound_id = ?", inboundId).Delete(&model.ClientInbound{}).Error
  145. }
  146. func (s *ClientService) ListForInbound(tx *gorm.DB, inboundId int) ([]model.Client, error) {
  147. if tx == nil {
  148. tx = database.GetDB()
  149. }
  150. type joinedRow struct {
  151. model.ClientRecord
  152. FlowOverride string
  153. }
  154. var rows []joinedRow
  155. err := tx.Table("clients").
  156. Select("clients.*, client_inbounds.flow_override AS flow_override").
  157. Joins("JOIN client_inbounds ON client_inbounds.client_id = clients.id").
  158. Where("client_inbounds.inbound_id = ?", inboundId).
  159. Order("clients.id ASC").
  160. Find(&rows).Error
  161. if err != nil {
  162. return nil, err
  163. }
  164. out := make([]model.Client, 0, len(rows))
  165. for i := range rows {
  166. c := rows[i].ToClient()
  167. c.Flow = rows[i].FlowOverride
  168. out = append(out, *c)
  169. }
  170. return out, nil
  171. }