client_link.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. if incoming.Secret != "" {
  69. row.Secret = incoming.Secret
  70. }
  71. if incoming.AdTag != "" {
  72. row.AdTag = incoming.AdTag
  73. }
  74. row.Flow = incoming.Flow
  75. if incoming.Security != "" {
  76. row.Security = incoming.Security
  77. }
  78. if incoming.Reverse != "" {
  79. row.Reverse = incoming.Reverse
  80. }
  81. if incoming.PrivateKey != "" {
  82. row.PrivateKey = incoming.PrivateKey
  83. }
  84. if incoming.PublicKey != "" {
  85. row.PublicKey = incoming.PublicKey
  86. }
  87. if incoming.AllowedIPs != "" {
  88. row.AllowedIPs = incoming.AllowedIPs
  89. }
  90. row.PreSharedKey = incoming.PreSharedKey
  91. row.KeepAlive = incoming.KeepAlive
  92. row.SubID = incoming.SubID
  93. row.LimitIP = incoming.LimitIP
  94. row.TotalGB = incoming.TotalGB
  95. row.ExpiryTime = incoming.ExpiryTime
  96. row.Enable = incoming.Enable
  97. row.TgID = incoming.TgID
  98. if incoming.Group != "" {
  99. row.Group = incoming.Group
  100. }
  101. row.Comment = incoming.Comment
  102. row.Reset = incoming.Reset
  103. if incoming.CreatedAt > 0 && (row.CreatedAt == 0 || incoming.CreatedAt < row.CreatedAt) {
  104. row.CreatedAt = incoming.CreatedAt
  105. }
  106. preservedUpdatedAt := max(incoming.UpdatedAt, row.UpdatedAt)
  107. row.UpdatedAt = preservedUpdatedAt
  108. idByEmail[email] = row.Id
  109. if *row == before {
  110. continue
  111. }
  112. if err := tx.Save(row).Error; err != nil {
  113. return err
  114. }
  115. if err := tx.Model(&model.ClientRecord{}).
  116. Where("id = ?", row.Id).
  117. UpdateColumn("updated_at", preservedUpdatedAt).Error; err != nil {
  118. return err
  119. }
  120. }
  121. if len(toCreate) > 0 {
  122. if err := tx.CreateInBatches(toCreate, 200).Error; err != nil {
  123. return err
  124. }
  125. for _, rec := range toCreate {
  126. idByEmail[rec.Email] = rec.Id
  127. }
  128. }
  129. links := make([]model.ClientInbound, 0, len(clients))
  130. linked := make(map[int]struct{}, len(clients))
  131. for i := range clients {
  132. email := strings.TrimSpace(clients[i].Email)
  133. if email == "" {
  134. continue
  135. }
  136. id, ok := idByEmail[email]
  137. if !ok {
  138. continue
  139. }
  140. if _, dup := linked[id]; dup {
  141. continue
  142. }
  143. linked[id] = struct{}{}
  144. links = append(links, model.ClientInbound{
  145. ClientId: id,
  146. InboundId: inboundId,
  147. FlowOverride: clients[i].Flow,
  148. })
  149. }
  150. if len(links) > 0 {
  151. if err := tx.CreateInBatches(links, 200).Error; err != nil {
  152. return err
  153. }
  154. }
  155. return nil
  156. }
  157. func (s *ClientService) DetachInbound(tx *gorm.DB, inboundId int) error {
  158. if tx == nil {
  159. tx = database.GetDB()
  160. }
  161. return tx.Where("inbound_id = ?", inboundId).Delete(&model.ClientInbound{}).Error
  162. }
  163. func (s *ClientService) ListForInbound(tx *gorm.DB, inboundId int) ([]model.Client, error) {
  164. if tx == nil {
  165. tx = database.GetDB()
  166. }
  167. type joinedRow struct {
  168. model.ClientRecord
  169. FlowOverride string
  170. }
  171. var rows []joinedRow
  172. err := tx.Table("clients").
  173. Select("clients.*, client_inbounds.flow_override AS flow_override").
  174. Joins("JOIN client_inbounds ON client_inbounds.client_id = clients.id").
  175. Where("client_inbounds.inbound_id = ?", inboundId).
  176. Order("clients.id ASC").
  177. Find(&rows).Error
  178. if err != nil {
  179. return nil, err
  180. }
  181. out := make([]model.Client, 0, len(rows))
  182. for i := range rows {
  183. c := rows[i].ToClient()
  184. c.Flow = rows[i].FlowOverride
  185. out = append(out, *c)
  186. }
  187. return out, nil
  188. }
  189. // ListForInboundBySubId is ListForInbound narrowed to one subscription id —
  190. // both filter columns are indexed, so the subscription server resolves a
  191. // subscriber's clients without touching the inbound's settings JSON.
  192. func (s *ClientService) ListForInboundBySubId(tx *gorm.DB, inboundId int, subId string) ([]model.Client, error) {
  193. if tx == nil {
  194. tx = database.GetDB()
  195. }
  196. type joinedRow struct {
  197. model.ClientRecord
  198. FlowOverride string
  199. }
  200. var rows []joinedRow
  201. err := tx.Table("clients").
  202. Select("clients.*, client_inbounds.flow_override AS flow_override").
  203. Joins("JOIN client_inbounds ON client_inbounds.client_id = clients.id").
  204. Where("client_inbounds.inbound_id = ? AND clients.sub_id = ?", inboundId, subId).
  205. Order("clients.id ASC").
  206. Find(&rows).Error
  207. if err != nil {
  208. return nil, err
  209. }
  210. out := make([]model.Client, 0, len(rows))
  211. for i := range rows {
  212. c := rows[i].ToClient()
  213. c.Flow = rows[i].FlowOverride
  214. out = append(out, *c)
  215. }
  216. return out, nil
  217. }