client_link.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. if incoming.PrivateKey != "" {
  76. row.PrivateKey = incoming.PrivateKey
  77. }
  78. if incoming.PublicKey != "" {
  79. row.PublicKey = incoming.PublicKey
  80. }
  81. if incoming.AllowedIPs != "" {
  82. row.AllowedIPs = incoming.AllowedIPs
  83. }
  84. row.PreSharedKey = incoming.PreSharedKey
  85. row.KeepAlive = incoming.KeepAlive
  86. row.SubID = incoming.SubID
  87. row.LimitIP = incoming.LimitIP
  88. row.TotalGB = incoming.TotalGB
  89. row.ExpiryTime = incoming.ExpiryTime
  90. row.Enable = incoming.Enable
  91. row.TgID = incoming.TgID
  92. if incoming.Group != "" {
  93. row.Group = incoming.Group
  94. }
  95. row.Comment = incoming.Comment
  96. row.Reset = incoming.Reset
  97. if incoming.CreatedAt > 0 && (row.CreatedAt == 0 || incoming.CreatedAt < row.CreatedAt) {
  98. row.CreatedAt = incoming.CreatedAt
  99. }
  100. preservedUpdatedAt := max(incoming.UpdatedAt, row.UpdatedAt)
  101. row.UpdatedAt = preservedUpdatedAt
  102. idByEmail[email] = row.Id
  103. if *row == before {
  104. continue
  105. }
  106. if err := tx.Save(row).Error; err != nil {
  107. return err
  108. }
  109. if err := tx.Model(&model.ClientRecord{}).
  110. Where("id = ?", row.Id).
  111. UpdateColumn("updated_at", preservedUpdatedAt).Error; err != nil {
  112. return err
  113. }
  114. }
  115. if len(toCreate) > 0 {
  116. if err := tx.CreateInBatches(toCreate, 200).Error; err != nil {
  117. return err
  118. }
  119. for _, rec := range toCreate {
  120. idByEmail[rec.Email] = rec.Id
  121. }
  122. }
  123. links := make([]model.ClientInbound, 0, len(clients))
  124. linked := make(map[int]struct{}, len(clients))
  125. for i := range clients {
  126. email := strings.TrimSpace(clients[i].Email)
  127. if email == "" {
  128. continue
  129. }
  130. id, ok := idByEmail[email]
  131. if !ok {
  132. continue
  133. }
  134. if _, dup := linked[id]; dup {
  135. continue
  136. }
  137. linked[id] = struct{}{}
  138. links = append(links, model.ClientInbound{
  139. ClientId: id,
  140. InboundId: inboundId,
  141. FlowOverride: clients[i].Flow,
  142. })
  143. }
  144. if len(links) > 0 {
  145. if err := tx.CreateInBatches(links, 200).Error; err != nil {
  146. return err
  147. }
  148. }
  149. return nil
  150. }
  151. func (s *ClientService) DetachInbound(tx *gorm.DB, inboundId int) error {
  152. if tx == nil {
  153. tx = database.GetDB()
  154. }
  155. return tx.Where("inbound_id = ?", inboundId).Delete(&model.ClientInbound{}).Error
  156. }
  157. func (s *ClientService) ListForInbound(tx *gorm.DB, inboundId int) ([]model.Client, error) {
  158. if tx == nil {
  159. tx = database.GetDB()
  160. }
  161. type joinedRow struct {
  162. model.ClientRecord
  163. FlowOverride string
  164. }
  165. var rows []joinedRow
  166. err := tx.Table("clients").
  167. Select("clients.*, client_inbounds.flow_override AS flow_override").
  168. Joins("JOIN client_inbounds ON client_inbounds.client_id = clients.id").
  169. Where("client_inbounds.inbound_id = ?", inboundId).
  170. Order("clients.id ASC").
  171. Find(&rows).Error
  172. if err != nil {
  173. return nil, err
  174. }
  175. out := make([]model.Client, 0, len(rows))
  176. for i := range rows {
  177. c := rows[i].ToClient()
  178. c.Flow = rows[i].FlowOverride
  179. out = append(out, *c)
  180. }
  181. return out, nil
  182. }