1
0

client_bulk.go 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "github.com/google/uuid"
  9. "github.com/mhsanaei/3x-ui/v3/internal/database"
  10. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  11. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  12. "github.com/mhsanaei/3x-ui/v3/internal/util/common"
  13. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  14. "gorm.io/gorm"
  15. )
  16. // BulkAttachResult reports the outcome of a bulk attach across target inbounds.
  17. type BulkAttachResult struct {
  18. Attached []string `json:"attached"`
  19. Skipped []string `json:"skipped"`
  20. Errors []string `json:"errors"`
  21. }
  22. // BulkAttach attaches the given existing clients (by email) to each target inbound,
  23. // reusing their identity (email/UUID/password/subId) and a shared traffic row. It adds
  24. // all clients to a target in a single AddInboundClient call, and reports clients already
  25. // present on a target as skipped.
  26. func (s *ClientService) BulkAttach(inboundSvc *InboundService, emails []string, inboundIds []int) (*BulkAttachResult, bool, error) {
  27. result := &BulkAttachResult{}
  28. if len(emails) == 0 || len(inboundIds) == 0 {
  29. return result, false, nil
  30. }
  31. recordErr := func(format string, args ...any) {
  32. msg := fmt.Sprintf(format, args...)
  33. result.Errors = append(result.Errors, msg)
  34. logger.Warningf("[BulkAttach] %s", msg)
  35. }
  36. records := make([]*model.ClientRecord, 0, len(emails))
  37. seenEmail := make(map[string]struct{}, len(emails))
  38. for _, email := range emails {
  39. if email == "" {
  40. continue
  41. }
  42. key := strings.ToLower(email)
  43. if _, ok := seenEmail[key]; ok {
  44. continue
  45. }
  46. seenEmail[key] = struct{}{}
  47. rec, err := s.GetRecordByEmail(nil, email)
  48. if err != nil {
  49. recordErr("%s: %v", email, err)
  50. continue
  51. }
  52. records = append(records, rec)
  53. }
  54. needRestart := false
  55. // Prepared in order first, as in Create: fillProtocolDefaults mints the
  56. // shared credentials, so only the node pushes below may overlap.
  57. attachIds := make([]int, 0, len(inboundIds))
  58. attachPayloads := make([]string, 0, len(inboundIds))
  59. attachClients := make([][]model.Client, 0, len(inboundIds))
  60. attachAnyTunnel := false
  61. // A repeated id used to be caught by the second pass seeing the client
  62. // already attached; the applies no longer run before the next prep.
  63. seenInbound := make(map[int]struct{}, len(inboundIds))
  64. for _, ibId := range inboundIds {
  65. if _, dup := seenInbound[ibId]; dup {
  66. continue
  67. }
  68. seenInbound[ibId] = struct{}{}
  69. inbound, err := inboundSvc.GetInbound(ibId)
  70. if err != nil {
  71. recordErr("inbound %d: %v", ibId, err)
  72. continue
  73. }
  74. if inbound.Protocol == model.WireGuard || inbound.Protocol == model.AmneziaWG {
  75. attachAnyTunnel = true
  76. }
  77. existingClients, err := inboundSvc.GetClients(inbound)
  78. if err != nil {
  79. recordErr("inbound %d: %v", ibId, err)
  80. continue
  81. }
  82. have := make(map[string]struct{}, len(existingClients))
  83. for _, c := range existingClients {
  84. have[strings.ToLower(c.Email)] = struct{}{}
  85. }
  86. clientsToAdd := make([]model.Client, 0, len(records))
  87. for _, rec := range records {
  88. if _, attached := have[strings.ToLower(rec.Email)]; attached {
  89. result.Skipped = append(result.Skipped, rec.Email)
  90. continue
  91. }
  92. client := *rec.ToClient()
  93. client.UpdatedAt = time.Now().UnixMilli()
  94. if err := s.fillProtocolDefaults(&client, inbound); err != nil {
  95. recordErr("%s -> inbound %d: %v", rec.Email, ibId, err)
  96. continue
  97. }
  98. clientsToAdd = append(clientsToAdd, clientWithInboundFlow(client, inbound))
  99. }
  100. if len(clientsToAdd) == 0 {
  101. continue
  102. }
  103. payload, err := json.Marshal(map[string][]model.Client{"clients": clientsToAdd})
  104. if err != nil {
  105. recordErr("inbound %d: %v", ibId, err)
  106. continue
  107. }
  108. attachIds = append(attachIds, ibId)
  109. attachPayloads = append(attachPayloads, string(payload))
  110. attachClients = append(attachClients, clientsToAdd)
  111. }
  112. attachResults, attachPanics := fanoutInboundResults(attachIds, addFanoutLimit(attachAnyTunnel), func(i int) inboundApplyOutcome {
  113. nr, err := s.AddInboundClient(inboundSvc, &model.Inbound{Id: attachIds[i], Settings: attachPayloads[i]})
  114. return inboundApplyOutcome{needRestart: nr, err: err}
  115. })
  116. for i, out := range attachResults {
  117. err := out.err
  118. if attachPanics[i] != nil {
  119. // The apply may already have committed, so ask for the restart the
  120. // lost return value can no longer report.
  121. needRestart = true
  122. err = attachPanics[i]
  123. }
  124. if err != nil {
  125. recordErr("inbound %d: %v", attachIds[i], err)
  126. continue
  127. }
  128. if out.needRestart {
  129. needRestart = true
  130. }
  131. for _, c := range attachClients[i] {
  132. result.Attached = append(result.Attached, c.Email)
  133. }
  134. }
  135. return result, needRestart, nil
  136. }
  137. // BulkDetachResult reports the outcome of a bulk detach across target inbounds.
  138. type BulkDetachResult struct {
  139. Detached []string `json:"detached"`
  140. Skipped []string `json:"skipped"`
  141. Errors []string `json:"errors"`
  142. }
  143. // BulkDetach detaches the given existing clients (by email) from each target inbound.
  144. // (email, inbound) pairs where the client is not currently attached are silently skipped
  145. // at the inbound level; emails that aren't attached to any of the requested inbounds
  146. // are reported under skipped. ClientRecord rows are kept even when they become orphaned
  147. // (matches single-client detach semantics); callers should use bulkDelete for full removal.
  148. func (s *ClientService) BulkDetach(inboundSvc *InboundService, emails []string, inboundIds []int) (*BulkDetachResult, bool, error) {
  149. result := &BulkDetachResult{}
  150. if len(emails) == 0 || len(inboundIds) == 0 {
  151. return result, false, nil
  152. }
  153. recordErr := func(format string, args ...any) {
  154. msg := fmt.Sprintf(format, args...)
  155. result.Errors = append(result.Errors, msg)
  156. logger.Warningf("[BulkDetach] %s", msg)
  157. }
  158. requested := make(map[int]struct{}, len(inboundIds))
  159. for _, id := range inboundIds {
  160. requested[id] = struct{}{}
  161. }
  162. recsByInbound := make(map[int][]*model.ClientRecord)
  163. emailOrder := make([]string, 0, len(emails))
  164. emailRepr := make(map[string]string, len(emails))
  165. emailFailed := make(map[string]bool, len(emails))
  166. seenEmail := make(map[string]struct{}, len(emails))
  167. for _, email := range emails {
  168. if email == "" {
  169. continue
  170. }
  171. key := strings.ToLower(email)
  172. if _, ok := seenEmail[key]; ok {
  173. continue
  174. }
  175. seenEmail[key] = struct{}{}
  176. rec, err := s.GetRecordByEmail(nil, email)
  177. if err != nil {
  178. recordErr("%s: %v", email, err)
  179. continue
  180. }
  181. currentIds, err := s.GetInboundIdsForRecord(rec.Id)
  182. if err != nil {
  183. recordErr("%s: %v", email, err)
  184. continue
  185. }
  186. matched := false
  187. for _, id := range currentIds {
  188. if _, ok := requested[id]; ok {
  189. recsByInbound[id] = append(recsByInbound[id], rec)
  190. matched = true
  191. }
  192. }
  193. if !matched {
  194. result.Skipped = append(result.Skipped, rec.Email)
  195. continue
  196. }
  197. emailOrder = append(emailOrder, key)
  198. emailRepr[key] = rec.Email
  199. }
  200. needRestart := false
  201. // Ordered and de-duplicated up front: the sequential loop dropped each map
  202. // entry as it went, which the concurrent applies can no longer do.
  203. detachIds := make([]int, 0, len(recsByInbound))
  204. detachRecs := make([][]*model.ClientRecord, 0, len(recsByInbound))
  205. for _, ibId := range inboundIds {
  206. recs, ok := recsByInbound[ibId]
  207. if !ok {
  208. continue
  209. }
  210. delete(recsByInbound, ibId)
  211. detachIds = append(detachIds, ibId)
  212. detachRecs = append(detachRecs, recs)
  213. }
  214. detachResults, detachPanics := fanoutInboundResults(detachIds, inboundFanoutConcurrency, func(i int) inboundApplyOutcome {
  215. nr, err := s.delInboundClients(inboundSvc, detachIds[i], detachRecs[i], true)
  216. return inboundApplyOutcome{needRestart: nr, err: err}
  217. })
  218. for i, out := range detachResults {
  219. err := out.err
  220. if detachPanics[i] != nil {
  221. // See BulkAttach: a panicking apply may already have committed.
  222. needRestart = true
  223. err = detachPanics[i]
  224. }
  225. if err != nil {
  226. recordErr("inbound %d: %v", detachIds[i], err)
  227. for _, rec := range detachRecs[i] {
  228. emailFailed[strings.ToLower(rec.Email)] = true
  229. }
  230. continue
  231. }
  232. if out.needRestart {
  233. needRestart = true
  234. }
  235. }
  236. for _, key := range emailOrder {
  237. if emailFailed[key] {
  238. continue
  239. }
  240. result.Detached = append(result.Detached, emailRepr[key])
  241. }
  242. return result, needRestart, nil
  243. }
  244. // inboundApplyOutcome carries one inbound's apply result out of the fanout.
  245. type inboundApplyOutcome struct {
  246. needRestart bool
  247. err error
  248. }
  249. // BulkAdjustResult is returned by BulkAdjust to report how many clients were
  250. // successfully updated and which were skipped (typically because the field
  251. // being adjusted was unlimited for that client) or failed.
  252. type BulkAdjustResult struct {
  253. Adjusted int `json:"adjusted"`
  254. Skipped []BulkAdjustReport `json:"skipped,omitempty"`
  255. }
  256. type BulkAdjustReport struct {
  257. Email string `json:"email"`
  258. Reason string `json:"reason"`
  259. }
  260. type bulkAdjustEntry struct {
  261. record *model.ClientRecord
  262. applyExpiry bool
  263. newExpiry int64
  264. applyTotal bool
  265. newTotal int64
  266. }
  267. // bulkFlowClear is the directive that strips the XTLS flow from every selected
  268. // client. The vision values are the only positive flows xray accepts.
  269. const bulkFlowClear = "none"
  270. // bulkFlowAllowed whitelists the flow directives BulkAdjust accepts. Anything
  271. // outside this set is treated as "" (leave flow untouched) so a malformed or
  272. // hostile value can never be injected into a client's settings. The dropdown in
  273. // ClientBulkAdjustModal.tsx offers the same set ("" / "none" / TLS_FLOW_CONTROL);
  274. // keep the two in sync.
  275. var bulkFlowAllowed = map[string]struct{}{
  276. "": {},
  277. bulkFlowClear: {},
  278. "xtls-rprx-vision": {},
  279. "xtls-rprx-vision-udp443": {},
  280. }
  281. // BulkAdjust shifts ExpiryTime by addDays (days) and TotalGB by addBytes
  282. // for every email in the list. Clients whose corresponding field is
  283. // unlimited (0) are skipped — bulk extend should not accidentally
  284. // limit an unlimited client. addDays and addBytes may be negative.
  285. // flow sets the XTLS flow, limitHwid the max registered devices (0 = unlimited)
  286. // and adTag the MTProto sponsor channel; "none" clears flow or adTag.
  287. //
  288. // Like BulkDelete, the work is grouped by inbound so each inbound's
  289. // settings JSON is parsed and written exactly once regardless of how
  290. // many target emails it contains.
  291. func (s *ClientService) BulkAdjust(inboundSvc *InboundService, emails []string, addDays int, addBytes int64, flow string, limitHwid *int, adTag string) (BulkAdjustResult, bool, error) {
  292. result := BulkAdjustResult{}
  293. if len(emails) == 0 {
  294. return result, false, nil
  295. }
  296. flow = strings.TrimSpace(flow)
  297. if _, ok := bulkFlowAllowed[flow]; !ok {
  298. flow = "" // ignore unknown directives — "" means "leave flow untouched"
  299. }
  300. adTag = strings.TrimSpace(adTag)
  301. if adTag != "" && adTag != bulkFlowClear && !model.ValidMtprotoAdTag(adTag) {
  302. return result, false, common.NewError("mtproto client ad tag must be 32 hex characters")
  303. }
  304. if limitHwid != nil && *limitHwid < 0 {
  305. zero := 0
  306. limitHwid = &zero
  307. }
  308. adjustFlow := flow != ""
  309. adjustHwid := limitHwid != nil
  310. adjustAdTag := adTag != ""
  311. if addDays == 0 && addBytes == 0 && !adjustFlow && !adjustHwid && !adjustAdTag {
  312. return result, false, common.NewError("no adjustment specified")
  313. }
  314. addExpiryMs := int64(addDays) * 24 * 60 * 60 * 1000
  315. cleanEmails := trimmedUniqueEmails(emails)
  316. if len(cleanEmails) == 0 {
  317. return result, false, nil
  318. }
  319. db := database.GetDB()
  320. recordsByEmail, err := clientRecordsByEmail(db, cleanEmails)
  321. if err != nil {
  322. return result, false, err
  323. }
  324. skippedReasons := map[string]string{}
  325. for _, email := range cleanEmails {
  326. if _, ok := recordsByEmail[email]; !ok {
  327. skippedReasons[email] = "client not found"
  328. }
  329. }
  330. plan := map[string]*bulkAdjustEntry{}
  331. for email, rec := range recordsByEmail {
  332. entry := &bulkAdjustEntry{record: rec}
  333. if addDays != 0 {
  334. switch {
  335. case rec.ExpiryTime == 0:
  336. if _, exists := skippedReasons[email]; !exists {
  337. skippedReasons[email] = "unlimited expiry"
  338. }
  339. case rec.ExpiryTime > 0:
  340. next := rec.ExpiryTime + addExpiryMs
  341. if next <= 0 {
  342. if _, exists := skippedReasons[email]; !exists {
  343. skippedReasons[email] = "reduction exceeds remaining time"
  344. }
  345. } else {
  346. entry.applyExpiry = true
  347. entry.newExpiry = next
  348. }
  349. default:
  350. next := rec.ExpiryTime - addExpiryMs
  351. if next >= 0 {
  352. if _, exists := skippedReasons[email]; !exists {
  353. skippedReasons[email] = "reduction exceeds delay window"
  354. }
  355. } else {
  356. entry.applyExpiry = true
  357. entry.newExpiry = next
  358. }
  359. }
  360. }
  361. if addBytes != 0 {
  362. if rec.TotalGB == 0 {
  363. if _, exists := skippedReasons[email]; !exists {
  364. skippedReasons[email] = "unlimited traffic"
  365. }
  366. } else {
  367. next := rec.TotalGB + addBytes
  368. if next <= 0 {
  369. if _, exists := skippedReasons[email]; !exists {
  370. skippedReasons[email] = "reduction exceeds remaining quota"
  371. }
  372. } else {
  373. entry.applyTotal = true
  374. entry.newTotal = next
  375. }
  376. }
  377. }
  378. if entry.applyExpiry || entry.applyTotal || adjustFlow || adjustHwid || adjustAdTag {
  379. plan[email] = entry
  380. }
  381. }
  382. if len(plan) == 0 {
  383. for email, reason := range skippedReasons {
  384. result.Skipped = append(result.Skipped, BulkAdjustReport{Email: email, Reason: reason})
  385. }
  386. return result, false, nil
  387. }
  388. plannedIds := make([]int, 0, len(plan))
  389. recordIdToEmail := make(map[int]string, len(plan))
  390. for email, entry := range plan {
  391. if entry.applyExpiry || entry.applyTotal || adjustFlow || adjustAdTag {
  392. plannedIds = append(plannedIds, entry.record.Id)
  393. recordIdToEmail[entry.record.Id] = email
  394. }
  395. }
  396. var mappings []model.ClientInbound
  397. for _, batch := range chunkInts(plannedIds, sqlInChunk) {
  398. var rows []model.ClientInbound
  399. if err := db.Where("client_id IN ?", batch).Find(&rows).Error; err != nil {
  400. return result, false, err
  401. }
  402. mappings = append(mappings, rows...)
  403. }
  404. emailsByInbound := map[int][]string{}
  405. for _, m := range mappings {
  406. email, ok := recordIdToEmail[m.ClientId]
  407. if !ok {
  408. continue
  409. }
  410. emailsByInbound[m.InboundId] = append(emailsByInbound[m.InboundId], email)
  411. }
  412. needRestart := false
  413. flowHonored := map[string]bool{}
  414. flowIneligible := map[string]bool{}
  415. adTagHonored := map[string]bool{}
  416. adTagIneligible := map[string]bool{}
  417. execFailed := map[string]bool{}
  418. adjustIds := sortedInboundIds(emailsByInbound)
  419. adjustResults, adjustPanics := fanoutInboundResults(adjustIds, inboundFanoutConcurrency, func(i int) bulkInboundAdjustResult {
  420. return s.bulkAdjustInboundClients(inboundSvc, adjustIds[i], emailsByInbound[adjustIds[i]], plan, flow, adTag)
  421. })
  422. for i, ibRes := range adjustResults {
  423. if adjustPanics[i] != nil {
  424. needRestart = true
  425. for _, email := range emailsByInbound[adjustIds[i]] {
  426. execFailed[email] = true
  427. if _, already := skippedReasons[email]; !already {
  428. skippedReasons[email] = adjustPanics[i].Error()
  429. }
  430. }
  431. continue
  432. }
  433. if ibRes.needRestart {
  434. needRestart = true
  435. }
  436. for email := range ibRes.flowHonored {
  437. flowHonored[email] = true
  438. }
  439. for email := range ibRes.flowIneligible {
  440. flowIneligible[email] = true
  441. }
  442. for email := range ibRes.adTagHonored {
  443. adTagHonored[email] = true
  444. }
  445. for email := range ibRes.adTagIneligible {
  446. adTagIneligible[email] = true
  447. }
  448. for email, reason := range ibRes.perEmailSkipped {
  449. execFailed[email] = true
  450. if _, already := skippedReasons[email]; !already {
  451. skippedReasons[email] = reason
  452. }
  453. }
  454. }
  455. cond, condArgs := depletedCond(db)
  456. candidateEmails := make([]string, 0, len(plan))
  457. for email, entry := range plan {
  458. if entry.applyExpiry || entry.applyTotal {
  459. candidateEmails = append(candidateEmails, email)
  460. }
  461. }
  462. wasDisabledDepleted := map[string]struct{}{}
  463. for _, batch := range chunkStrings(candidateEmails, sqlInChunk) {
  464. var rows []string
  465. if err := db.Model(xray.ClientTraffic{}).
  466. Where(cond+" AND enable = ? AND email IN ?", append(append([]any{}, condArgs...), false, batch)...).
  467. Pluck("email", &rows).Error; err != nil {
  468. return result, needRestart, err
  469. }
  470. for _, e := range rows {
  471. wasDisabledDepleted[e] = struct{}{}
  472. }
  473. }
  474. wantAdTag := ""
  475. if adjustAdTag && adTag != bulkFlowClear {
  476. wantAdTag = strings.ToLower(adTag)
  477. }
  478. adjusted := map[string]struct{}{}
  479. for email, entry := range plan {
  480. if execFailed[email] {
  481. continue
  482. }
  483. updates := map[string]any{}
  484. if entry.applyExpiry {
  485. updates["expiry_time"] = entry.newExpiry
  486. }
  487. if entry.applyTotal {
  488. updates["total"] = entry.newTotal
  489. }
  490. if len(updates) > 0 {
  491. if err := db.Model(xray.ClientTraffic{}).Where("email = ?", email).Updates(updates).Error; err != nil {
  492. if _, already := skippedReasons[email]; !already {
  493. skippedReasons[email] = err.Error()
  494. }
  495. continue
  496. }
  497. }
  498. if adjustHwid {
  499. if err := s.setClientLimitHwidByEmail(db, email, *limitHwid); err != nil {
  500. if _, already := skippedReasons[email]; !already {
  501. skippedReasons[email] = err.Error()
  502. }
  503. continue
  504. }
  505. }
  506. if adjustAdTag && adTagHonored[email] {
  507. if err := db.Model(&model.ClientRecord{}).Where("email = ?", email).UpdateColumn("ad_tag", wantAdTag).Error; err != nil {
  508. if _, already := skippedReasons[email]; !already {
  509. skippedReasons[email] = err.Error()
  510. }
  511. continue
  512. }
  513. }
  514. // Counted when expiry/total changed, flow was honored, adTag was honored, or limitHwid was adjusted.
  515. if len(updates) > 0 || flowHonored[email] || adTagHonored[email] || adjustHwid {
  516. adjusted[email] = struct{}{}
  517. }
  518. }
  519. result.Adjusted = len(adjusted)
  520. for email, reason := range skippedReasons {
  521. result.Skipped = append(result.Skipped, BulkAdjustReport{Email: email, Reason: reason})
  522. }
  523. // Report a flow directive that no inbound could carry — only when it was not
  524. // honored anywhere and the client has no other (expiry/total) skip reason.
  525. // The expiry/total part, if any, has already been applied and counted above.
  526. for email := range flowIneligible {
  527. if flowHonored[email] {
  528. continue
  529. }
  530. if _, already := skippedReasons[email]; already {
  531. continue
  532. }
  533. result.Skipped = append(result.Skipped, BulkAdjustReport{Email: email, Reason: "flow not supported on inbound"})
  534. }
  535. for email := range adTagIneligible {
  536. if adTagHonored[email] {
  537. continue
  538. }
  539. if _, already := skippedReasons[email]; already {
  540. continue
  541. }
  542. result.Skipped = append(result.Skipped, BulkAdjustReport{Email: email, Reason: "adTag not supported on inbound"})
  543. }
  544. if len(wasDisabledDepleted) > 0 {
  545. stillDepleted := map[string]struct{}{}
  546. wasList := make([]string, 0, len(wasDisabledDepleted))
  547. for e := range wasDisabledDepleted {
  548. wasList = append(wasList, e)
  549. }
  550. for _, batch := range chunkStrings(wasList, sqlInChunk) {
  551. var rows []string
  552. if err := db.Model(xray.ClientTraffic{}).
  553. Where(cond+" AND email IN ?", append(append([]any{}, condArgs...), batch)...).
  554. Pluck("email", &rows).Error; err != nil {
  555. return result, needRestart, err
  556. }
  557. for _, e := range rows {
  558. stillDepleted[e] = struct{}{}
  559. }
  560. }
  561. reEnable := make([]string, 0, len(wasDisabledDepleted))
  562. for e := range wasDisabledDepleted {
  563. if _, still := stillDepleted[e]; !still {
  564. reEnable = append(reEnable, e)
  565. }
  566. }
  567. if len(reEnable) > 0 {
  568. _, nr, err := s.BulkSetEnable(inboundSvc, reEnable, true)
  569. if err != nil {
  570. return result, needRestart, err
  571. }
  572. if nr {
  573. needRestart = true
  574. }
  575. }
  576. }
  577. return result, needRestart, nil
  578. }
  579. type bulkInboundAdjustResult struct {
  580. perEmailSkipped map[string]string
  581. flowHonored map[string]bool
  582. // flowIneligible is tracked apart from perEmailSkipped: a flow directive
  583. // that an inbound cannot carry must not suppress the expiry/total write for
  584. // the same client (which would diverge the inbound JSON / ClientRecord from
  585. // ClientTraffic). It only feeds the final Skipped report.
  586. flowIneligible map[string]bool
  587. adTagHonored map[string]bool
  588. adTagIneligible map[string]bool
  589. needRestart bool
  590. }
  591. // bulkAdjustInboundClients applies expiry/total deltas to multiple clients
  592. // inside a single inbound's settings JSON. The xray runtime is updated
  593. // only for remote-node inbounds; local nodes do not need a notification
  594. // because the AddUser payload does not include totalGB/expiryTime —
  595. // changing those fields is identity-preserving and the panel's traffic
  596. // enforcement loop picks up the new limits from ClientTraffic directly.
  597. func (s *ClientService) bulkAdjustInboundClients(
  598. inboundSvc *InboundService,
  599. inboundId int,
  600. emails []string,
  601. plan map[string]*bulkAdjustEntry,
  602. flow string,
  603. adTag string,
  604. ) bulkInboundAdjustResult {
  605. res := bulkInboundAdjustResult{
  606. perEmailSkipped: map[string]string{},
  607. flowHonored: map[string]bool{},
  608. flowIneligible: map[string]bool{},
  609. adTagHonored: map[string]bool{},
  610. adTagIneligible: map[string]bool{},
  611. }
  612. defer lockInbound(inboundId).Unlock()
  613. oldInbound, err := inboundSvc.GetInbound(inboundId)
  614. if err != nil {
  615. logger.Error("Load Old Data Error")
  616. for _, e := range emails {
  617. res.perEmailSkipped[e] = err.Error()
  618. }
  619. return res
  620. }
  621. var settings map[string]any
  622. if err := json.Unmarshal([]byte(oldInbound.Settings), &settings); err != nil {
  623. for _, e := range emails {
  624. res.perEmailSkipped[e] = err.Error()
  625. }
  626. return res
  627. }
  628. // Match by email — the client's stable identity (see Delete). Credentials
  629. // can drift from the inbound JSON, so they are never used for matching.
  630. wantedEmails := make(map[string]struct{}, len(emails))
  631. for _, email := range emails {
  632. if plan[email] == nil {
  633. res.perEmailSkipped[email] = "client not found"
  634. continue
  635. }
  636. wantedEmails[email] = struct{}{}
  637. }
  638. // Flow eligibility is a property of the inbound (protocol + transport), so
  639. // resolve it once. Clearing flow is always allowed; setting a vision flow
  640. // is only honored on an inbound that can carry it.
  641. flowEligible := flow == bulkFlowClear ||
  642. (!oldInbound.DisableFlow &&
  643. inboundCanEnableTlsFlow(string(oldInbound.Protocol), oldInbound.StreamSettings, oldInbound.Settings))
  644. wantAdTag := ""
  645. if adTag != "" && adTag != bulkFlowClear {
  646. wantAdTag = strings.ToLower(adTag)
  647. }
  648. interfaceClients, _ := settings["clients"].([]any)
  649. foundEmails := map[string]bool{}
  650. flowChanged := false
  651. adTagChanged := false
  652. hasInboundChanges := false
  653. nowMs := time.Now().Unix() * 1000
  654. for i, client := range interfaceClients {
  655. c, ok := client.(map[string]any)
  656. if !ok {
  657. continue
  658. }
  659. targetEmail, _ := c["email"].(string)
  660. if _, want := wantedEmails[targetEmail]; !want || targetEmail == "" {
  661. continue
  662. }
  663. clientChanged := false
  664. entry := plan[targetEmail]
  665. if entry.applyExpiry {
  666. c["expiryTime"] = entry.newExpiry
  667. clientChanged = true
  668. }
  669. if entry.applyTotal {
  670. c["totalGB"] = entry.newTotal
  671. clientChanged = true
  672. }
  673. if flow != "" {
  674. if flowEligible {
  675. want := ""
  676. if flow != bulkFlowClear {
  677. want = flow
  678. }
  679. if cur, _ := c["flow"].(string); cur != want {
  680. c["flow"] = want
  681. flowChanged = true
  682. }
  683. res.flowHonored[targetEmail] = true
  684. clientChanged = true
  685. } else {
  686. // Record separately so this never suppresses the expiry/total
  687. // write for the same client (see flowIneligible doc).
  688. res.flowIneligible[targetEmail] = true
  689. }
  690. }
  691. if adTag != "" {
  692. if oldInbound.Protocol == model.MTProto {
  693. if cur, _ := c["adTag"].(string); cur != wantAdTag {
  694. c["adTag"] = wantAdTag
  695. adTagChanged = true
  696. }
  697. res.adTagHonored[targetEmail] = true
  698. clientChanged = true
  699. } else {
  700. res.adTagIneligible[targetEmail] = true
  701. }
  702. }
  703. if clientChanged {
  704. c["updated_at"] = nowMs
  705. hasInboundChanges = true
  706. }
  707. interfaceClients[i] = c
  708. foundEmails[targetEmail] = true
  709. }
  710. for email := range wantedEmails {
  711. if !foundEmails[email] {
  712. res.perEmailSkipped[email] = "Client Not Found In Inbound"
  713. }
  714. }
  715. if len(foundEmails) == 0 || !hasInboundChanges {
  716. return res
  717. }
  718. settings["clients"] = interfaceClients
  719. newSettings, err := json.MarshalIndent(settings, "", " ")
  720. if err != nil {
  721. for email := range foundEmails {
  722. res.perEmailSkipped[email] = err.Error()
  723. }
  724. return res
  725. }
  726. oldInbound.Settings = string(newSettings)
  727. // A flow change rewrites the user's xray config, which the lightweight
  728. // UpdateUser push below does not carry. Local nodes reload via restart;
  729. // remote nodes get a full reconcile (MarkNodeDirty) instead of a per-user push.
  730. if flowChanged && oldInbound.NodeID == nil {
  731. res.needRestart = true
  732. }
  733. // Serialize against the traffic poll to avoid the cross-transaction
  734. // lock-order deadlock on inbounds/client_records (runSerializedTx).
  735. txErr := runSerializedTx(func(tx *gorm.DB) error {
  736. if err := tx.Save(oldInbound).Error; err != nil {
  737. return err
  738. }
  739. finalClients, gcErr := inboundSvc.GetClients(oldInbound)
  740. if gcErr != nil {
  741. return gcErr
  742. }
  743. if err := s.SyncInbound(tx, inboundId, finalClients); err != nil {
  744. return err
  745. }
  746. if oldInbound.NodeID != nil {
  747. return (&NodeService{}).MarkNodeDirtyTx(tx, *oldInbound.NodeID)
  748. }
  749. return nil
  750. })
  751. if txErr != nil {
  752. for email := range foundEmails {
  753. if _, skip := res.perEmailSkipped[email]; !skip {
  754. res.perEmailSkipped[email] = txErr.Error()
  755. }
  756. }
  757. } else {
  758. if adTagChanged && oldInbound.Protocol == model.MTProto && oldInbound.NodeID == nil {
  759. inboundSvc.applyLocalMtproto(oldInbound.Id)
  760. }
  761. if oldInbound.NodeID != nil && !flowChanged && len(foundEmails) <= nodeBulkPushThreshold {
  762. rt, push, _, perr := inboundSvc.nodePushPlan(oldInbound)
  763. if perr != nil {
  764. logger.Warning("BulkAdjust: node runtime lookup after commit failed:", perr)
  765. } else if push {
  766. for email := range foundEmails {
  767. entry := plan[email]
  768. updated := *entry.record.ToClient()
  769. if entry.applyExpiry {
  770. updated.ExpiryTime = entry.newExpiry
  771. }
  772. if entry.applyTotal {
  773. updated.TotalGB = entry.newTotal
  774. }
  775. if adTag != "" && oldInbound.Protocol == model.MTProto {
  776. updated.AdTag = wantAdTag
  777. }
  778. updated.UpdatedAt = nowMs
  779. ctx, cancel := nodePushContext()
  780. err1 := rt.UpdateUser(ctx, oldInbound, email, updated)
  781. cancel()
  782. if err1 != nil {
  783. logger.Warning("Error in updating client on", rt.Name(), ":", err1)
  784. // First failure ends the batch push; the reconcile converges the rest.
  785. break
  786. }
  787. }
  788. }
  789. }
  790. }
  791. return res
  792. }
  793. // BulkDeleteResult mirrors BulkAdjustResult: total deleted plus per-email
  794. // skip reasons when an email could not be processed.
  795. type BulkDeleteResult struct {
  796. Deleted int `json:"deleted"`
  797. Skipped []BulkDeleteReport `json:"skipped,omitempty"`
  798. }
  799. type BulkDeleteReport struct {
  800. Email string `json:"email"`
  801. Reason string `json:"reason"`
  802. }
  803. // BulkDelete removes every client in the list in one optimized pass.
  804. // Instead of running the full single-delete pipeline N times (which would
  805. // re-read, re-parse, and re-write each inbound's settings JSON for every
  806. // email), it groups emails by inbound and performs a single
  807. // read-modify-write per inbound. Per-row DB cleanups are also batched with
  808. // IN-clause queries at the end. Errors on a particular email are recorded
  809. // in the Skipped list and processing continues for the rest.
  810. func (s *ClientService) BulkDelete(inboundSvc *InboundService, emails []string, keepTraffic bool) (BulkDeleteResult, bool, error) {
  811. result := BulkDeleteResult{}
  812. cleanEmails := trimmedUniqueEmails(emails)
  813. if len(cleanEmails) == 0 {
  814. return result, false, nil
  815. }
  816. db := database.GetDB()
  817. recordsByEmail, err := clientRecordsByEmail(db, cleanEmails)
  818. if err != nil {
  819. return result, false, err
  820. }
  821. tombstoneEmails := make([]string, 0, len(recordsByEmail))
  822. for _, email := range cleanEmails {
  823. if recordsByEmail[email] != nil {
  824. tombstoneEmails = append(tombstoneEmails, email)
  825. }
  826. }
  827. tombstoneClientEmails(tombstoneEmails)
  828. skippedReasons := map[string]string{}
  829. for _, email := range cleanEmails {
  830. if _, ok := recordsByEmail[email]; !ok {
  831. skippedReasons[email] = "client not found"
  832. }
  833. }
  834. clientIds := make([]int, 0, len(recordsByEmail))
  835. recordIdToEmail := make(map[int]string, len(recordsByEmail))
  836. for _, r := range recordsByEmail {
  837. clientIds = append(clientIds, r.Id)
  838. recordIdToEmail[r.Id] = r.Email
  839. }
  840. emailsByInbound := map[int][]string{}
  841. if len(clientIds) > 0 {
  842. var mappings []model.ClientInbound
  843. for _, batch := range chunkInts(clientIds, sqlInChunk) {
  844. var rows []model.ClientInbound
  845. if err := db.Where("client_id IN ?", batch).Find(&rows).Error; err != nil {
  846. return result, false, err
  847. }
  848. mappings = append(mappings, rows...)
  849. }
  850. for _, m := range mappings {
  851. email, ok := recordIdToEmail[m.ClientId]
  852. if !ok {
  853. continue
  854. }
  855. emailsByInbound[m.InboundId] = append(emailsByInbound[m.InboundId], email)
  856. }
  857. }
  858. needRestart := false
  859. delIds := sortedInboundIds(emailsByInbound)
  860. delResults, delPanics := fanoutInboundResults(delIds, inboundFanoutConcurrency, func(i int) bulkInboundDeleteResult {
  861. return s.bulkDelInboundClients(inboundSvc, delIds[i], emailsByInbound[delIds[i]], recordsByEmail, keepTraffic)
  862. })
  863. for i, ibResult := range delResults {
  864. if delPanics[i] != nil {
  865. needRestart = true
  866. for _, email := range emailsByInbound[delIds[i]] {
  867. if _, already := skippedReasons[email]; !already {
  868. skippedReasons[email] = delPanics[i].Error()
  869. }
  870. }
  871. continue
  872. }
  873. if ibResult.needRestart {
  874. needRestart = true
  875. }
  876. for email, reason := range ibResult.perEmailSkipped {
  877. if _, already := skippedReasons[email]; !already {
  878. skippedReasons[email] = reason
  879. }
  880. }
  881. }
  882. successEmails := make([]string, 0, len(recordsByEmail))
  883. successIds := make([]int, 0, len(recordsByEmail))
  884. failedEmails := make([]string, 0, len(recordsByEmail))
  885. successSubIDs := make([]string, 0, len(recordsByEmail))
  886. for email, rec := range recordsByEmail {
  887. if _, skipped := skippedReasons[email]; skipped {
  888. failedEmails = append(failedEmails, email)
  889. continue
  890. }
  891. successEmails = append(successEmails, email)
  892. successIds = append(successIds, rec.Id)
  893. successSubIDs = append(successSubIDs, rec.SubID)
  894. }
  895. withdrawClientTombstones(failedEmails...)
  896. if len(successIds) > 0 {
  897. // Serialize the row cleanup against the traffic poll to avoid the
  898. // cross-transaction lock-order deadlock on client_traffics/inbounds.
  899. if err := runSerializedTx(func(tx *gorm.DB) error {
  900. if e := adjustGroupBaselinesForRemovedTraffic(tx, successEmails); e != nil {
  901. return e
  902. }
  903. if e := clearClientHwidsBySubIDTx(tx, successSubIDs...); e != nil {
  904. return e
  905. }
  906. for _, batch := range chunkInts(successIds, sqlInChunk) {
  907. if e := tx.Where("client_id IN ?", batch).Delete(&model.ClientInbound{}).Error; e != nil {
  908. return e
  909. }
  910. if e := tx.Where("client_id IN ?", batch).Delete(&model.ClientExternalLink{}).Error; e != nil {
  911. return e
  912. }
  913. }
  914. if !keepTraffic && len(successEmails) > 0 {
  915. for _, batch := range chunkStrings(successEmails, sqlInChunk) {
  916. if e := tx.Where("email IN ?", batch).Delete(&xray.ClientTraffic{}).Error; e != nil {
  917. return e
  918. }
  919. if e := tx.Where("client_email IN ?", batch).Delete(&model.InboundClientIps{}).Error; e != nil {
  920. return e
  921. }
  922. }
  923. }
  924. for _, batch := range chunkInts(successIds, sqlInChunk) {
  925. if e := tx.Where("id IN ?", batch).Delete(&model.ClientRecord{}).Error; e != nil {
  926. return e
  927. }
  928. }
  929. return nil
  930. }); err != nil {
  931. withdrawClientTombstones(successEmails...)
  932. return result, needRestart, err
  933. }
  934. }
  935. result.Deleted = len(successEmails)
  936. for email, reason := range skippedReasons {
  937. result.Skipped = append(result.Skipped, BulkDeleteReport{Email: email, Reason: reason})
  938. }
  939. return result, needRestart, nil
  940. }
  941. type bulkInboundDeleteResult struct {
  942. perEmailSkipped map[string]string
  943. needRestart bool
  944. }
  945. // bulkDelInboundClients removes multiple clients from a single inbound's
  946. // settings JSON in one read-modify-write cycle, runs the xray runtime
  947. // RemoveUser/DeleteUser calls, and persists the inbound. The returned map
  948. // holds per-email failure reasons; emails not present in the map are
  949. // considered successful for this inbound.
  950. func (s *ClientService) bulkDelInboundClients(
  951. inboundSvc *InboundService,
  952. inboundId int,
  953. emails []string,
  954. records map[string]*model.ClientRecord,
  955. keepTraffic bool,
  956. ) bulkInboundDeleteResult {
  957. res := bulkInboundDeleteResult{perEmailSkipped: map[string]string{}}
  958. defer lockInbound(inboundId).Unlock()
  959. oldInbound, err := inboundSvc.GetInbound(inboundId)
  960. if err != nil {
  961. logger.Error("Load Old Data Error")
  962. for _, e := range emails {
  963. res.perEmailSkipped[e] = err.Error()
  964. }
  965. return res
  966. }
  967. var settings map[string]any
  968. if err := json.Unmarshal([]byte(oldInbound.Settings), &settings); err != nil {
  969. for _, e := range emails {
  970. res.perEmailSkipped[e] = err.Error()
  971. }
  972. return res
  973. }
  974. // Match by email — the client's stable identity (see Delete). The link-derived
  975. // set is deletion intent: an email already absent from settings is successful,
  976. // while foundEmails tracks entries that still need settings-specific cleanup.
  977. wantedEmails := make(map[string]struct{}, len(emails))
  978. for _, email := range emails {
  979. if records[email] == nil {
  980. res.perEmailSkipped[email] = "client not found"
  981. continue
  982. }
  983. wantedEmails[email] = struct{}{}
  984. }
  985. interfaceClients, _ := settings["clients"].([]any)
  986. newClients := make([]any, 0, len(interfaceClients))
  987. foundEmails := map[string]bool{}
  988. enableByEmail := map[string]bool{}
  989. for _, client := range interfaceClients {
  990. c, ok := client.(map[string]any)
  991. if !ok {
  992. newClients = append(newClients, client)
  993. continue
  994. }
  995. em, _ := c["email"].(string)
  996. if _, found := wantedEmails[em]; found && em != "" {
  997. foundEmails[em] = true
  998. en, _ := c["enable"].(bool)
  999. enableByEmail[em] = en
  1000. continue
  1001. }
  1002. newClients = append(newClients, client)
  1003. }
  1004. db := database.GetDB()
  1005. newClients = compactOrphans(db, newClients)
  1006. if newClients == nil {
  1007. newClients = []any{}
  1008. }
  1009. settings["clients"] = newClients
  1010. newSettings, err := json.MarshalIndent(settings, "", " ")
  1011. if err != nil {
  1012. for email := range wantedEmails {
  1013. if _, skip := res.perEmailSkipped[email]; !skip {
  1014. res.perEmailSkipped[email] = err.Error()
  1015. }
  1016. }
  1017. return res
  1018. }
  1019. oldInbound.Settings = string(newSettings)
  1020. foundList := make([]string, 0, len(foundEmails))
  1021. for email := range foundEmails {
  1022. foundList = append(foundList, email)
  1023. }
  1024. notDepletedByEmail := map[string]bool{}
  1025. if len(foundList) > 0 {
  1026. type trafficRow struct {
  1027. Email string
  1028. Enable bool
  1029. }
  1030. for _, batch := range chunkStrings(foundList, sqlInChunk) {
  1031. var rows []trafficRow
  1032. if err := db.Model(xray.ClientTraffic{}).
  1033. Where("email IN ?", batch).
  1034. Select("email, enable").
  1035. Scan(&rows).Error; err == nil {
  1036. for _, r := range rows {
  1037. notDepletedByEmail[r.Email] = r.Enable
  1038. }
  1039. }
  1040. }
  1041. }
  1042. var sharedSet map[string]bool
  1043. if !keepTraffic {
  1044. var sharedErr error
  1045. sharedSet, sharedErr = inboundSvc.emailsUsedByOtherInbounds(foundList, inboundId)
  1046. if sharedErr != nil {
  1047. for email := range wantedEmails {
  1048. res.perEmailSkipped[email] = sharedErr.Error()
  1049. }
  1050. return res
  1051. }
  1052. }
  1053. if !keepTraffic {
  1054. purge := make([]string, 0, len(foundEmails))
  1055. for email := range foundEmails {
  1056. if !sharedSet[strings.ToLower(strings.TrimSpace(email))] {
  1057. purge = append(purge, email)
  1058. }
  1059. }
  1060. if len(purge) > 0 {
  1061. // Serialize the IP/stat purge against the traffic poll to avoid the
  1062. // cross-transaction lock-order deadlock on client_traffics.
  1063. if delErr := runSerializedTx(func(tx *gorm.DB) error {
  1064. if e := inboundSvc.delClientIPsByEmails(tx, purge); e != nil {
  1065. logger.Error("Error in delete client IPs")
  1066. return e
  1067. }
  1068. if e := inboundSvc.delClientStatsByEmails(tx, purge); e != nil {
  1069. logger.Error("Delete stats Data Error")
  1070. return e
  1071. }
  1072. return nil
  1073. }); delErr != nil {
  1074. for _, email := range purge {
  1075. res.perEmailSkipped[email] = delErr.Error()
  1076. delete(foundEmails, email)
  1077. }
  1078. }
  1079. }
  1080. }
  1081. // Serialize against the traffic poll to avoid the cross-transaction
  1082. // lock-order deadlock on inbounds/client_records (runSerializedTx).
  1083. txErr := runSerializedTx(func(tx *gorm.DB) error {
  1084. if err := tx.Save(oldInbound).Error; err != nil {
  1085. return err
  1086. }
  1087. finalClients, err := inboundSvc.GetClients(oldInbound)
  1088. if err != nil {
  1089. return err
  1090. }
  1091. if err := s.SyncInbound(tx, inboundId, finalClients); err != nil {
  1092. return err
  1093. }
  1094. if oldInbound.NodeID != nil {
  1095. return (&NodeService{}).MarkNodeDirtyTx(tx, *oldInbound.NodeID)
  1096. }
  1097. return nil
  1098. })
  1099. if txErr != nil {
  1100. for email := range wantedEmails {
  1101. if _, skip := res.perEmailSkipped[email]; !skip {
  1102. res.perEmailSkipped[email] = txErr.Error()
  1103. }
  1104. }
  1105. } else if oldInbound.NodeID == nil {
  1106. rt, rterr := inboundSvc.runtimeFor(oldInbound)
  1107. if rterr != nil {
  1108. res.needRestart = true
  1109. } else {
  1110. for email := range foundEmails {
  1111. if !enableByEmail[email] || !notDepletedByEmail[email] {
  1112. continue
  1113. }
  1114. err1 := rt.RemoveUser(context.Background(), oldInbound, email)
  1115. if err1 == nil {
  1116. logger.Debug("Client deleted on", rt.Name(), ":", email)
  1117. } else if strings.Contains(err1.Error(), fmt.Sprintf("User %s not found.", email)) {
  1118. logger.Debug("User is already deleted. Nothing to do more...")
  1119. } else {
  1120. logger.Debug("Error in deleting client on", rt.Name(), ":", err1)
  1121. res.needRestart = true
  1122. }
  1123. }
  1124. }
  1125. } else {
  1126. dispatchEmails := make([]string, 0, len(wantedEmails))
  1127. for email := range wantedEmails {
  1128. if _, skip := res.perEmailSkipped[email]; !skip {
  1129. dispatchEmails = append(dispatchEmails, email)
  1130. }
  1131. }
  1132. if len(dispatchEmails) > nodeBulkPushThreshold {
  1133. return res
  1134. }
  1135. rt, push, _, perr := inboundSvc.nodePushPlan(oldInbound)
  1136. if perr != nil {
  1137. logger.Warning("BulkDelete: node runtime lookup after commit failed:", perr)
  1138. } else if push {
  1139. for _, email := range dispatchEmails {
  1140. ctx, cancel := nodePushContext()
  1141. err1 := rt.DeleteClient(ctx, email)
  1142. cancel()
  1143. if err1 != nil {
  1144. logger.Warning("Error in deleting client on", rt.Name(), ":", err1)
  1145. // The node is already dirty, so one reconcile converges the rest of
  1146. // the batch instead of paying another deadline per client.
  1147. break
  1148. }
  1149. }
  1150. }
  1151. }
  1152. return res
  1153. }
  1154. // BulkCreateResult mirrors BulkAdjustResult for the create flow.
  1155. type BulkCreateResult struct {
  1156. Created int `json:"created"`
  1157. Skipped []BulkCreateReport `json:"skipped,omitempty"`
  1158. }
  1159. type BulkCreateReport struct {
  1160. Email string `json:"email"`
  1161. Reason string `json:"reason"`
  1162. }
  1163. func (s *ClientService) BulkCreate(inboundSvc *InboundService, payloads []ClientCreatePayload) (BulkCreateResult, bool, error) {
  1164. result := BulkCreateResult{}
  1165. if len(payloads) == 0 {
  1166. return result, false, nil
  1167. }
  1168. skip := func(email, reason string) {
  1169. if strings.TrimSpace(email) == "" {
  1170. email = "(missing email)"
  1171. }
  1172. result.Skipped = append(result.Skipped, BulkCreateReport{Email: email, Reason: reason})
  1173. }
  1174. type prepared struct {
  1175. client model.Client
  1176. inboundIds []int
  1177. limitHwid int
  1178. }
  1179. prep := make([]prepared, 0, len(payloads))
  1180. emails := make([]string, 0, len(payloads))
  1181. subIDs := make([]string, 0, len(payloads))
  1182. seenEmail := make(map[string]struct{}, len(payloads))
  1183. seenSubID := make(map[string]string, len(payloads))
  1184. for i := range payloads {
  1185. client := payloads[i].Client
  1186. email := strings.TrimSpace(client.Email)
  1187. if email == "" {
  1188. skip("", "client email is required")
  1189. continue
  1190. }
  1191. if verr := validateClientEmail(email); verr != nil {
  1192. skip(email, verr.Error())
  1193. continue
  1194. }
  1195. if verr := validateClientSubID(client.SubID); verr != nil {
  1196. skip(email, verr.Error())
  1197. continue
  1198. }
  1199. if verr := validateClientResetDay(client.ResetDay); verr != nil {
  1200. skip(email, verr.Error())
  1201. continue
  1202. }
  1203. if verr := validateClientResetMax(client.ResetMax); verr != nil {
  1204. skip(email, verr.Error())
  1205. continue
  1206. }
  1207. if verr := validateClientTrafficReset(client.TrafficReset, client.TrafficResetDay); verr != nil {
  1208. skip(email, verr.Error())
  1209. continue
  1210. }
  1211. if len(payloads[i].InboundIds) == 0 {
  1212. skip(email, "at least one inbound is required")
  1213. continue
  1214. }
  1215. client.Email = email
  1216. if client.SubID == "" {
  1217. client.SubID = uuid.NewString()
  1218. }
  1219. if !client.Enable {
  1220. client.Enable = true
  1221. }
  1222. now := time.Now().UnixMilli()
  1223. if client.CreatedAt == 0 {
  1224. client.CreatedAt = now
  1225. }
  1226. client.UpdatedAt = now
  1227. le := strings.ToLower(email)
  1228. if _, dup := seenEmail[le]; dup {
  1229. skip(email, "email already in use: "+email)
  1230. continue
  1231. }
  1232. if owner, ok := seenSubID[client.SubID]; ok && owner != le {
  1233. skip(email, "subId already in use: "+client.SubID)
  1234. continue
  1235. }
  1236. seenEmail[le] = struct{}{}
  1237. seenSubID[client.SubID] = le
  1238. prep = append(prep, prepared{client: client, inboundIds: payloads[i].InboundIds, limitHwid: payloads[i].LimitHwid})
  1239. emails = append(emails, email)
  1240. subIDs = append(subIDs, client.SubID)
  1241. }
  1242. if len(prep) == 0 {
  1243. return result, false, nil
  1244. }
  1245. db := database.GetDB()
  1246. const lookupChunk = 400
  1247. existingByEmail := make(map[string]model.ClientRecord, len(emails))
  1248. for start := 0; start < len(emails); start += lookupChunk {
  1249. end := min(start+lookupChunk, len(emails))
  1250. var rows []model.ClientRecord
  1251. if e := db.Where("email IN ?", emails[start:end]).Find(&rows).Error; e != nil {
  1252. return result, false, e
  1253. }
  1254. for i := range rows {
  1255. existingByEmail[strings.ToLower(rows[i].Email)] = rows[i]
  1256. }
  1257. }
  1258. existingSubOwner := make(map[string]string, len(subIDs))
  1259. for start := 0; start < len(subIDs); start += lookupChunk {
  1260. end := min(start+lookupChunk, len(subIDs))
  1261. var rows []model.ClientRecord
  1262. if e := db.Where("sub_id IN ?", subIDs[start:end]).Find(&rows).Error; e != nil {
  1263. return result, false, e
  1264. }
  1265. for i := range rows {
  1266. existingSubOwner[rows[i].SubID] = strings.ToLower(rows[i].Email)
  1267. }
  1268. }
  1269. inboundCache := make(map[int]*model.Inbound)
  1270. getIb := func(id int) (*model.Inbound, error) {
  1271. if ib, ok := inboundCache[id]; ok {
  1272. return ib, nil
  1273. }
  1274. ib, e := inboundSvc.GetInbound(id)
  1275. if e != nil {
  1276. return nil, e
  1277. }
  1278. inboundCache[id] = ib
  1279. return ib, nil
  1280. }
  1281. byInbound := make(map[int][]model.Client)
  1282. idxByInbound := make(map[int][]int)
  1283. inboundOrder := make([]int, 0)
  1284. failed := make([]bool, len(prep))
  1285. reason := make([]string, len(prep))
  1286. createAnyTunnel := false
  1287. for idx := range prep {
  1288. le := strings.ToLower(prep[idx].client.Email)
  1289. if rec, ok := existingByEmail[le]; ok {
  1290. if rec.SubID != prep[idx].client.SubID {
  1291. failed[idx] = true
  1292. reason[idx] = "email already in use: " + prep[idx].client.Email
  1293. continue
  1294. }
  1295. if prep[idx].client.ID == "" {
  1296. prep[idx].client.ID = rec.UUID
  1297. }
  1298. if prep[idx].client.Password == "" {
  1299. prep[idx].client.Password = rec.Password
  1300. }
  1301. if prep[idx].client.Auth == "" {
  1302. prep[idx].client.Auth = rec.Auth
  1303. }
  1304. if prep[idx].client.Secret == "" {
  1305. prep[idx].client.Secret = rec.Secret
  1306. }
  1307. }
  1308. if owner, ok := existingSubOwner[prep[idx].client.SubID]; ok && owner != le {
  1309. failed[idx] = true
  1310. reason[idx] = "subId already in use: " + prep[idx].client.SubID
  1311. continue
  1312. }
  1313. ok := true
  1314. for _, ibId := range prep[idx].inboundIds {
  1315. ib, e := getIb(ibId)
  1316. if e != nil {
  1317. failed[idx] = true
  1318. reason[idx] = e.Error()
  1319. ok = false
  1320. break
  1321. }
  1322. if ib.Protocol == model.WireGuard || ib.Protocol == model.AmneziaWG {
  1323. createAnyTunnel = true
  1324. }
  1325. if e := s.fillProtocolDefaults(&prep[idx].client, ib); e != nil {
  1326. failed[idx] = true
  1327. reason[idx] = e.Error()
  1328. ok = false
  1329. break
  1330. }
  1331. }
  1332. if !ok {
  1333. continue
  1334. }
  1335. for _, ibId := range prep[idx].inboundIds {
  1336. ib, _ := getIb(ibId)
  1337. if _, seen := byInbound[ibId]; !seen {
  1338. inboundOrder = append(inboundOrder, ibId)
  1339. }
  1340. byInbound[ibId] = append(byInbound[ibId], clientWithInboundFlow(prep[idx].client, ib))
  1341. idxByInbound[ibId] = append(idxByInbound[ibId], idx)
  1342. }
  1343. }
  1344. needRestart := false
  1345. createResults, createPanics := fanoutInboundResults(inboundOrder, addFanoutLimit(createAnyTunnel), func(i int) inboundApplyOutcome {
  1346. ibId := inboundOrder[i]
  1347. payload, e := json.Marshal(map[string][]model.Client{"clients": byInbound[ibId]})
  1348. if e != nil {
  1349. return inboundApplyOutcome{err: e}
  1350. }
  1351. nr, e := s.AddInboundClient(inboundSvc, &model.Inbound{Id: ibId, Settings: string(payload)})
  1352. return inboundApplyOutcome{needRestart: nr, err: e}
  1353. })
  1354. for i, out := range createResults {
  1355. e := out.err
  1356. if createPanics[i] != nil {
  1357. // See BulkAttach: a panicking apply may already have committed.
  1358. needRestart = true
  1359. e = createPanics[i]
  1360. }
  1361. if e != nil {
  1362. for _, idx := range idxByInbound[inboundOrder[i]] {
  1363. failed[idx] = true
  1364. if reason[idx] == "" {
  1365. reason[idx] = e.Error()
  1366. }
  1367. }
  1368. continue
  1369. }
  1370. if out.needRestart {
  1371. needRestart = true
  1372. }
  1373. }
  1374. createdEmails := make([]string, 0, len(prep))
  1375. for idx := range prep {
  1376. if failed[idx] {
  1377. skip(prep[idx].client.Email, reason[idx])
  1378. continue
  1379. }
  1380. if err := s.setClientLimitHwidByEmail(nil, prep[idx].client.Email, prep[idx].limitHwid); err != nil {
  1381. skip(prep[idx].client.Email, err.Error())
  1382. continue
  1383. }
  1384. createdEmails = append(createdEmails, prep[idx].client.Email)
  1385. result.Created++
  1386. }
  1387. // A re-created email is a live identity again: a delete tombstone left
  1388. // standing makes the next node merge prune the new client's inbound links.
  1389. withdrawClientTombstones(createdEmails...)
  1390. return result, needRestart, nil
  1391. }
  1392. func (s *ClientService) DelDepleted(inboundSvc *InboundService) (int, bool, error) {
  1393. db := database.GetDB()
  1394. now := time.Now().UnixMilli()
  1395. depletedClause := depletedClientsClause
  1396. var rows []xray.ClientTraffic
  1397. if err := db.Where(depletedClause, now).Find(&rows).Error; err != nil {
  1398. return 0, false, err
  1399. }
  1400. if len(rows) == 0 {
  1401. return 0, false, nil
  1402. }
  1403. seen := make(map[string]struct{}, len(rows))
  1404. emails := make([]string, 0, len(rows))
  1405. for _, r := range rows {
  1406. if r.Email == "" {
  1407. continue
  1408. }
  1409. if _, ok := seen[r.Email]; ok {
  1410. continue
  1411. }
  1412. seen[r.Email] = struct{}{}
  1413. emails = append(emails, r.Email)
  1414. }
  1415. if len(emails) == 0 {
  1416. return 0, false, nil
  1417. }
  1418. res, needRestart, err := s.BulkDelete(inboundSvc, emails, false)
  1419. if err != nil {
  1420. return res.Deleted, needRestart, err
  1421. }
  1422. return res.Deleted, needRestart, nil
  1423. }
  1424. type BulkSetEnableResult struct {
  1425. Changed int `json:"changed"`
  1426. Skipped []BulkSetEnableReport `json:"skipped,omitempty"`
  1427. }
  1428. type BulkSetEnableReport struct {
  1429. Email string `json:"email"`
  1430. Reason string `json:"reason"`
  1431. }
  1432. func (s *ClientService) BulkSetEnable(inboundSvc *InboundService, emails []string, enable bool) (BulkSetEnableResult, bool, error) {
  1433. result := BulkSetEnableResult{}
  1434. cleanEmails := trimmedUniqueEmails(emails)
  1435. if len(cleanEmails) == 0 {
  1436. return result, false, nil
  1437. }
  1438. db := database.GetDB()
  1439. recordsByEmail, err := clientRecordsByEmail(db, cleanEmails)
  1440. if err != nil {
  1441. return result, false, err
  1442. }
  1443. skippedReasons := map[string]string{}
  1444. for _, email := range cleanEmails {
  1445. if _, ok := recordsByEmail[email]; !ok {
  1446. skippedReasons[email] = "client not found"
  1447. }
  1448. }
  1449. clientIds := make([]int, 0, len(recordsByEmail))
  1450. recordIdToEmail := make(map[int]string, len(recordsByEmail))
  1451. for _, r := range recordsByEmail {
  1452. clientIds = append(clientIds, r.Id)
  1453. recordIdToEmail[r.Id] = r.Email
  1454. }
  1455. emailsByInbound := map[int][]string{}
  1456. if len(clientIds) > 0 {
  1457. var mappings []model.ClientInbound
  1458. for _, batch := range chunkInts(clientIds, sqlInChunk) {
  1459. var rows []model.ClientInbound
  1460. if err := db.Where("client_id IN ?", batch).Find(&rows).Error; err != nil {
  1461. return result, false, err
  1462. }
  1463. mappings = append(mappings, rows...)
  1464. }
  1465. for _, m := range mappings {
  1466. email, ok := recordIdToEmail[m.ClientId]
  1467. if !ok {
  1468. continue
  1469. }
  1470. emailsByInbound[m.InboundId] = append(emailsByInbound[m.InboundId], email)
  1471. }
  1472. }
  1473. needRestart := false
  1474. enableIds := sortedInboundIds(emailsByInbound)
  1475. enableResults, enablePanics := fanoutInboundResults(enableIds, inboundFanoutConcurrency, func(i int) bulkSetEnableInboundResult {
  1476. return s.bulkSetEnableInboundClients(inboundSvc, enableIds[i], emailsByInbound[enableIds[i]], enable)
  1477. })
  1478. for i, ibRes := range enableResults {
  1479. if enablePanics[i] != nil {
  1480. needRestart = true
  1481. for _, email := range emailsByInbound[enableIds[i]] {
  1482. if _, already := skippedReasons[email]; !already {
  1483. skippedReasons[email] = enablePanics[i].Error()
  1484. }
  1485. }
  1486. continue
  1487. }
  1488. if ibRes.needRestart {
  1489. needRestart = true
  1490. }
  1491. for email, reason := range ibRes.perEmailSkipped {
  1492. if _, already := skippedReasons[email]; !already {
  1493. skippedReasons[email] = reason
  1494. }
  1495. }
  1496. }
  1497. successEmails := make([]string, 0, len(recordsByEmail))
  1498. for email := range recordsByEmail {
  1499. if _, skipped := skippedReasons[email]; skipped {
  1500. continue
  1501. }
  1502. successEmails = append(successEmails, email)
  1503. }
  1504. if len(successEmails) > 0 {
  1505. now := time.Now().UnixMilli()
  1506. if err := runSerializedTx(func(tx *gorm.DB) error {
  1507. for _, batch := range chunkStrings(successEmails, sqlInChunk) {
  1508. if e := tx.Model(xray.ClientTraffic{}).Where("email IN ?", batch).Update("enable", enable).Error; e != nil {
  1509. return e
  1510. }
  1511. if e := tx.Model(&model.ClientRecord{}).Where("email IN ?", batch).
  1512. Updates(map[string]any{"enable": enable, "updated_at": now}).Error; e != nil {
  1513. return e
  1514. }
  1515. }
  1516. return nil
  1517. }); err != nil {
  1518. return result, needRestart, err
  1519. }
  1520. }
  1521. result.Changed = len(successEmails)
  1522. for email, reason := range skippedReasons {
  1523. result.Skipped = append(result.Skipped, BulkSetEnableReport{Email: email, Reason: reason})
  1524. }
  1525. return result, needRestart, nil
  1526. }
  1527. type bulkSetEnableInboundResult struct {
  1528. perEmailSkipped map[string]string
  1529. needRestart bool
  1530. }
  1531. func (s *ClientService) bulkSetEnableInboundClients(inboundSvc *InboundService, inboundId int, emails []string, enable bool) bulkSetEnableInboundResult {
  1532. res := bulkSetEnableInboundResult{perEmailSkipped: map[string]string{}}
  1533. defer lockInbound(inboundId).Unlock()
  1534. oldInbound, err := inboundSvc.GetInbound(inboundId)
  1535. if err != nil {
  1536. for _, e := range emails {
  1537. res.perEmailSkipped[e] = err.Error()
  1538. }
  1539. return res
  1540. }
  1541. var settings map[string]any
  1542. if err := json.Unmarshal([]byte(oldInbound.Settings), &settings); err != nil {
  1543. for _, e := range emails {
  1544. res.perEmailSkipped[e] = err.Error()
  1545. }
  1546. return res
  1547. }
  1548. wanted := make(map[string]struct{}, len(emails))
  1549. for _, email := range emails {
  1550. wanted[email] = struct{}{}
  1551. }
  1552. cipher := ""
  1553. if oldInbound.Protocol == model.Shadowsocks {
  1554. cipher, _ = settings["method"].(string)
  1555. }
  1556. type changedClient struct {
  1557. email string
  1558. wasEnable bool
  1559. client model.Client
  1560. }
  1561. var changed []changedClient
  1562. found := map[string]bool{}
  1563. nowMs := time.Now().UnixMilli()
  1564. interfaceClients, _ := settings["clients"].([]any)
  1565. for i, c := range interfaceClients {
  1566. entry, ok := c.(map[string]any)
  1567. if !ok {
  1568. continue
  1569. }
  1570. email, _ := entry["email"].(string)
  1571. if _, want := wanted[email]; !want || email == "" {
  1572. continue
  1573. }
  1574. found[email] = true
  1575. prev, _ := entry["enable"].(bool)
  1576. if prev == enable {
  1577. continue
  1578. }
  1579. entry["enable"] = enable
  1580. entry["updated_at"] = nowMs
  1581. interfaceClients[i] = entry
  1582. // Build the pushed client from the inbound JSON (the per-inbound source of
  1583. // truth), so a remote UpdateUser carries every field and never zeroes
  1584. // subId/totalGB/expiry from drifting ClientRecord columns (#4628/#4792).
  1585. var parsed model.Client
  1586. if b, mErr := json.Marshal(entry); mErr == nil {
  1587. _ = json.Unmarshal(b, &parsed)
  1588. }
  1589. parsed.Email = email
  1590. parsed.Enable = enable
  1591. changed = append(changed, changedClient{email: email, wasEnable: prev, client: parsed})
  1592. }
  1593. for email := range wanted {
  1594. if !found[email] {
  1595. res.perEmailSkipped[email] = "Client Not Found In Inbound"
  1596. }
  1597. }
  1598. if len(changed) == 0 {
  1599. return res
  1600. }
  1601. settings["clients"] = interfaceClients
  1602. newSettings, err := json.MarshalIndent(settings, "", " ")
  1603. if err != nil {
  1604. for _, ch := range changed {
  1605. res.perEmailSkipped[ch.email] = err.Error()
  1606. }
  1607. return res
  1608. }
  1609. prevSettings := oldInbound.Settings
  1610. oldInbound.Settings = string(newSettings)
  1611. rt, push, _, perr := inboundSvc.nodePushPlan(oldInbound)
  1612. if perr != nil {
  1613. for _, ch := range changed {
  1614. res.perEmailSkipped[ch.email] = perr.Error()
  1615. }
  1616. return res
  1617. }
  1618. if oldInbound.NodeID != nil && push && len(changed) > nodeBulkPushThreshold {
  1619. push = false
  1620. }
  1621. txErr := runSerializedTx(func(tx *gorm.DB) error {
  1622. if e := tx.Save(oldInbound).Error; e != nil {
  1623. return e
  1624. }
  1625. finalClients, gcErr := inboundSvc.GetClients(oldInbound)
  1626. if gcErr != nil {
  1627. return gcErr
  1628. }
  1629. if err := s.SyncInbound(tx, inboundId, finalClients); err != nil {
  1630. return err
  1631. }
  1632. if oldInbound.NodeID != nil {
  1633. return (&NodeService{}).MarkNodeDirtyTx(tx, *oldInbound.NodeID)
  1634. }
  1635. return nil
  1636. })
  1637. if txErr != nil {
  1638. for _, ch := range changed {
  1639. res.perEmailSkipped[ch.email] = txErr.Error()
  1640. }
  1641. return res
  1642. }
  1643. if oldInbound.NodeID == nil {
  1644. if !push {
  1645. res.needRestart = true
  1646. } else {
  1647. for _, ch := range changed {
  1648. if enable {
  1649. err1 := rt.AddUser(context.Background(), oldInbound, map[string]any{
  1650. "email": ch.client.Email,
  1651. "id": ch.client.ID,
  1652. "security": ch.client.Security,
  1653. "flow": ch.client.Flow,
  1654. "auth": ch.client.Auth,
  1655. "password": ch.client.Password,
  1656. "cipher": cipher,
  1657. })
  1658. if err1 != nil {
  1659. logger.Debug("Error in adding client on", rt.Name(), ":", err1)
  1660. res.needRestart = true
  1661. }
  1662. } else if ch.wasEnable {
  1663. err1 := rt.RemoveUser(context.Background(), oldInbound, ch.email)
  1664. if err1 != nil && !strings.Contains(err1.Error(), fmt.Sprintf("User %s not found.", ch.email)) {
  1665. logger.Debug("Error in removing client on", rt.Name(), ":", err1)
  1666. res.needRestart = true
  1667. }
  1668. }
  1669. }
  1670. }
  1671. } else if push {
  1672. pushFailed := false
  1673. for _, ch := range changed {
  1674. updated := ch.client
  1675. updated.UpdatedAt = nowMs
  1676. ctx, cancel := nodePushContext()
  1677. err1 := rt.UpdateUser(ctx, oldInbound, ch.email, updated)
  1678. cancel()
  1679. if err1 != nil {
  1680. logger.Warning("Error in updating client on", rt.Name(), ":", err1)
  1681. pushFailed = true
  1682. // First failure ends the batch push; the reconcile converges the rest.
  1683. break
  1684. }
  1685. }
  1686. if !pushFailed {
  1687. advancePushedInbound(rt, prevSettings, oldInbound)
  1688. }
  1689. }
  1690. return res
  1691. }