client_bulk.go 47 KB

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