1
0

client_bulk.go 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785
  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. //
  286. // Like BulkDelete, the work is grouped by inbound so each inbound's
  287. // settings JSON is parsed and written exactly once regardless of how
  288. // many target emails it contains.
  289. func (s *ClientService) BulkAdjust(inboundSvc *InboundService, emails []string, addDays int, addBytes int64, flow string) (BulkAdjustResult, bool, error) {
  290. result := BulkAdjustResult{}
  291. if len(emails) == 0 {
  292. return result, false, nil
  293. }
  294. flow = strings.TrimSpace(flow)
  295. if _, ok := bulkFlowAllowed[flow]; !ok {
  296. flow = "" // ignore unknown directives — "" means "leave flow untouched"
  297. }
  298. adjustFlow := flow != ""
  299. if addDays == 0 && addBytes == 0 && !adjustFlow {
  300. return result, false, common.NewError("no adjustment specified")
  301. }
  302. addExpiryMs := int64(addDays) * 24 * 60 * 60 * 1000
  303. seen := map[string]struct{}{}
  304. cleanEmails := make([]string, 0, len(emails))
  305. for _, e := range emails {
  306. e = strings.TrimSpace(e)
  307. if e == "" {
  308. continue
  309. }
  310. if _, ok := seen[e]; ok {
  311. continue
  312. }
  313. seen[e] = struct{}{}
  314. cleanEmails = append(cleanEmails, e)
  315. }
  316. if len(cleanEmails) == 0 {
  317. return result, false, nil
  318. }
  319. db := database.GetDB()
  320. var records []model.ClientRecord
  321. for _, batch := range chunkStrings(cleanEmails, sqlInChunk) {
  322. var rows []model.ClientRecord
  323. if err := db.Where("email IN ?", batch).Find(&rows).Error; err != nil {
  324. return result, false, err
  325. }
  326. records = append(records, rows...)
  327. }
  328. recordsByEmail := make(map[string]*model.ClientRecord, len(records))
  329. for i := range records {
  330. recordsByEmail[records[i].Email] = &records[i]
  331. }
  332. skippedReasons := map[string]string{}
  333. for _, email := range cleanEmails {
  334. if _, ok := recordsByEmail[email]; !ok {
  335. skippedReasons[email] = "client not found"
  336. }
  337. }
  338. plan := map[string]*bulkAdjustEntry{}
  339. for email, rec := range recordsByEmail {
  340. entry := &bulkAdjustEntry{record: rec}
  341. if addDays != 0 {
  342. switch {
  343. case rec.ExpiryTime == 0:
  344. if _, exists := skippedReasons[email]; !exists {
  345. skippedReasons[email] = "unlimited expiry"
  346. }
  347. case rec.ExpiryTime > 0:
  348. next := rec.ExpiryTime + addExpiryMs
  349. if next <= 0 {
  350. if _, exists := skippedReasons[email]; !exists {
  351. skippedReasons[email] = "reduction exceeds remaining time"
  352. }
  353. } else {
  354. entry.applyExpiry = true
  355. entry.newExpiry = next
  356. }
  357. default:
  358. next := rec.ExpiryTime - addExpiryMs
  359. if next >= 0 {
  360. if _, exists := skippedReasons[email]; !exists {
  361. skippedReasons[email] = "reduction exceeds delay window"
  362. }
  363. } else {
  364. entry.applyExpiry = true
  365. entry.newExpiry = next
  366. }
  367. }
  368. }
  369. if addBytes != 0 {
  370. if rec.TotalGB == 0 {
  371. if _, exists := skippedReasons[email]; !exists {
  372. skippedReasons[email] = "unlimited traffic"
  373. }
  374. } else {
  375. next := rec.TotalGB + addBytes
  376. if next <= 0 {
  377. if _, exists := skippedReasons[email]; !exists {
  378. skippedReasons[email] = "reduction exceeds remaining quota"
  379. }
  380. } else {
  381. entry.applyTotal = true
  382. entry.newTotal = next
  383. }
  384. }
  385. }
  386. if entry.applyExpiry || entry.applyTotal || adjustFlow {
  387. plan[email] = entry
  388. }
  389. }
  390. if len(plan) == 0 {
  391. for email, reason := range skippedReasons {
  392. result.Skipped = append(result.Skipped, BulkAdjustReport{Email: email, Reason: reason})
  393. }
  394. return result, false, nil
  395. }
  396. plannedIds := make([]int, 0, len(plan))
  397. recordIdToEmail := make(map[int]string, len(plan))
  398. for email, entry := range plan {
  399. plannedIds = append(plannedIds, entry.record.Id)
  400. recordIdToEmail[entry.record.Id] = email
  401. }
  402. var mappings []model.ClientInbound
  403. for _, batch := range chunkInts(plannedIds, sqlInChunk) {
  404. var rows []model.ClientInbound
  405. if err := db.Where("client_id IN ?", batch).Find(&rows).Error; err != nil {
  406. return result, false, err
  407. }
  408. mappings = append(mappings, rows...)
  409. }
  410. emailsByInbound := map[int][]string{}
  411. for _, m := range mappings {
  412. email, ok := recordIdToEmail[m.ClientId]
  413. if !ok {
  414. continue
  415. }
  416. emailsByInbound[m.InboundId] = append(emailsByInbound[m.InboundId], email)
  417. }
  418. needRestart := false
  419. flowHonored := map[string]bool{}
  420. flowIneligible := map[string]bool{}
  421. execFailed := map[string]bool{}
  422. adjustIds := sortedInboundIds(emailsByInbound)
  423. adjustResults, adjustPanics := fanoutInboundResults(adjustIds, inboundFanoutConcurrency, func(i int) bulkInboundAdjustResult {
  424. return s.bulkAdjustInboundClients(inboundSvc, adjustIds[i], emailsByInbound[adjustIds[i]], plan, flow)
  425. })
  426. for i, ibRes := range adjustResults {
  427. if adjustPanics[i] != nil {
  428. needRestart = true
  429. for _, email := range emailsByInbound[adjustIds[i]] {
  430. execFailed[email] = true
  431. if _, already := skippedReasons[email]; !already {
  432. skippedReasons[email] = adjustPanics[i].Error()
  433. }
  434. }
  435. continue
  436. }
  437. if ibRes.needRestart {
  438. needRestart = true
  439. }
  440. for email := range ibRes.flowHonored {
  441. flowHonored[email] = true
  442. }
  443. for email := range ibRes.flowIneligible {
  444. flowIneligible[email] = true
  445. }
  446. for email, reason := range ibRes.perEmailSkipped {
  447. execFailed[email] = true
  448. if _, already := skippedReasons[email]; !already {
  449. skippedReasons[email] = reason
  450. }
  451. }
  452. }
  453. cond, condArgs := depletedCond(db)
  454. candidateEmails := make([]string, 0, len(plan))
  455. for email, entry := range plan {
  456. if entry.applyExpiry || entry.applyTotal {
  457. candidateEmails = append(candidateEmails, email)
  458. }
  459. }
  460. wasDisabledDepleted := map[string]struct{}{}
  461. for _, batch := range chunkStrings(candidateEmails, sqlInChunk) {
  462. var rows []string
  463. if err := db.Model(xray.ClientTraffic{}).
  464. Where(cond+" AND enable = ? AND email IN ?", append(append([]any{}, condArgs...), false, batch)...).
  465. Pluck("email", &rows).Error; err != nil {
  466. return result, needRestart, err
  467. }
  468. for _, e := range rows {
  469. wasDisabledDepleted[e] = struct{}{}
  470. }
  471. }
  472. adjusted := map[string]struct{}{}
  473. for email, entry := range plan {
  474. if execFailed[email] {
  475. continue
  476. }
  477. updates := map[string]any{}
  478. if entry.applyExpiry {
  479. updates["expiry_time"] = entry.newExpiry
  480. }
  481. if entry.applyTotal {
  482. updates["total"] = entry.newTotal
  483. }
  484. if len(updates) > 0 {
  485. if err := db.Model(xray.ClientTraffic{}).Where("email = ?", email).Updates(updates).Error; err != nil {
  486. if _, already := skippedReasons[email]; !already {
  487. skippedReasons[email] = err.Error()
  488. }
  489. continue
  490. }
  491. }
  492. // Counted when expiry/total changed, or a flow directive was honored
  493. // for this client (flow lives in the inbound JSON, not ClientTraffic).
  494. if len(updates) > 0 || flowHonored[email] {
  495. adjusted[email] = struct{}{}
  496. }
  497. }
  498. result.Adjusted = len(adjusted)
  499. for email, reason := range skippedReasons {
  500. result.Skipped = append(result.Skipped, BulkAdjustReport{Email: email, Reason: reason})
  501. }
  502. // Report a flow directive that no inbound could carry — only when it was not
  503. // honored anywhere and the client has no other (expiry/total) skip reason.
  504. // The expiry/total part, if any, has already been applied and counted above.
  505. for email := range flowIneligible {
  506. if flowHonored[email] {
  507. continue
  508. }
  509. if _, already := skippedReasons[email]; already {
  510. continue
  511. }
  512. result.Skipped = append(result.Skipped, BulkAdjustReport{Email: email, Reason: "flow not supported on inbound"})
  513. }
  514. if len(wasDisabledDepleted) > 0 {
  515. stillDepleted := map[string]struct{}{}
  516. wasList := make([]string, 0, len(wasDisabledDepleted))
  517. for e := range wasDisabledDepleted {
  518. wasList = append(wasList, e)
  519. }
  520. for _, batch := range chunkStrings(wasList, sqlInChunk) {
  521. var rows []string
  522. if err := db.Model(xray.ClientTraffic{}).
  523. Where(cond+" AND email IN ?", append(append([]any{}, condArgs...), batch)...).
  524. Pluck("email", &rows).Error; err != nil {
  525. return result, needRestart, err
  526. }
  527. for _, e := range rows {
  528. stillDepleted[e] = struct{}{}
  529. }
  530. }
  531. reEnable := make([]string, 0, len(wasDisabledDepleted))
  532. for e := range wasDisabledDepleted {
  533. if _, still := stillDepleted[e]; !still {
  534. reEnable = append(reEnable, e)
  535. }
  536. }
  537. if len(reEnable) > 0 {
  538. _, nr, err := s.BulkSetEnable(inboundSvc, reEnable, true)
  539. if err != nil {
  540. return result, needRestart, err
  541. }
  542. if nr {
  543. needRestart = true
  544. }
  545. }
  546. }
  547. return result, needRestart, nil
  548. }
  549. type bulkInboundAdjustResult struct {
  550. perEmailSkipped map[string]string
  551. flowHonored map[string]bool
  552. // flowIneligible is tracked apart from perEmailSkipped: a flow directive
  553. // that an inbound cannot carry must not suppress the expiry/total write for
  554. // the same client (which would diverge the inbound JSON / ClientRecord from
  555. // ClientTraffic). It only feeds the final Skipped report.
  556. flowIneligible map[string]bool
  557. needRestart bool
  558. }
  559. // bulkAdjustInboundClients applies expiry/total deltas to multiple clients
  560. // inside a single inbound's settings JSON. The xray runtime is updated
  561. // only for remote-node inbounds; local nodes do not need a notification
  562. // because the AddUser payload does not include totalGB/expiryTime —
  563. // changing those fields is identity-preserving and the panel's traffic
  564. // enforcement loop picks up the new limits from ClientTraffic directly.
  565. func (s *ClientService) bulkAdjustInboundClients(
  566. inboundSvc *InboundService,
  567. inboundId int,
  568. emails []string,
  569. plan map[string]*bulkAdjustEntry,
  570. flow string,
  571. ) bulkInboundAdjustResult {
  572. res := bulkInboundAdjustResult{perEmailSkipped: map[string]string{}, flowHonored: map[string]bool{}, flowIneligible: map[string]bool{}}
  573. defer lockInbound(inboundId).Unlock()
  574. oldInbound, err := inboundSvc.GetInbound(inboundId)
  575. if err != nil {
  576. logger.Error("Load Old Data Error")
  577. for _, e := range emails {
  578. res.perEmailSkipped[e] = err.Error()
  579. }
  580. return res
  581. }
  582. var settings map[string]any
  583. if err := json.Unmarshal([]byte(oldInbound.Settings), &settings); err != nil {
  584. for _, e := range emails {
  585. res.perEmailSkipped[e] = err.Error()
  586. }
  587. return res
  588. }
  589. // Match by email — the client's stable identity (see Delete). Credentials
  590. // can drift from the inbound JSON, so they are never used for matching.
  591. wantedEmails := make(map[string]struct{}, len(emails))
  592. for _, email := range emails {
  593. if plan[email] == nil {
  594. res.perEmailSkipped[email] = "client not found"
  595. continue
  596. }
  597. wantedEmails[email] = struct{}{}
  598. }
  599. // Flow eligibility is a property of the inbound (protocol + transport), so
  600. // resolve it once. Clearing flow is always allowed; setting a vision flow
  601. // is only honored on an inbound that can carry it.
  602. flowEligible := flow == bulkFlowClear ||
  603. (!oldInbound.DisableFlow &&
  604. inboundCanEnableTlsFlow(string(oldInbound.Protocol), oldInbound.StreamSettings, oldInbound.Settings))
  605. interfaceClients, _ := settings["clients"].([]any)
  606. foundEmails := map[string]bool{}
  607. flowChanged := false
  608. nowMs := time.Now().Unix() * 1000
  609. for i, client := range interfaceClients {
  610. c, ok := client.(map[string]any)
  611. if !ok {
  612. continue
  613. }
  614. targetEmail, _ := c["email"].(string)
  615. if _, want := wantedEmails[targetEmail]; !want || targetEmail == "" {
  616. continue
  617. }
  618. entry := plan[targetEmail]
  619. if entry.applyExpiry {
  620. c["expiryTime"] = entry.newExpiry
  621. }
  622. if entry.applyTotal {
  623. c["totalGB"] = entry.newTotal
  624. }
  625. if flow != "" {
  626. if flowEligible {
  627. want := ""
  628. if flow != bulkFlowClear {
  629. want = flow
  630. }
  631. if cur, _ := c["flow"].(string); cur != want {
  632. c["flow"] = want
  633. flowChanged = true
  634. }
  635. res.flowHonored[targetEmail] = true
  636. } else {
  637. // Record separately so this never suppresses the expiry/total
  638. // write for the same client (see flowIneligible doc).
  639. res.flowIneligible[targetEmail] = true
  640. }
  641. }
  642. c["updated_at"] = nowMs
  643. interfaceClients[i] = c
  644. foundEmails[targetEmail] = true
  645. }
  646. for email := range wantedEmails {
  647. if !foundEmails[email] {
  648. res.perEmailSkipped[email] = "Client Not Found In Inbound"
  649. }
  650. }
  651. if len(foundEmails) == 0 {
  652. return res
  653. }
  654. settings["clients"] = interfaceClients
  655. newSettings, err := json.MarshalIndent(settings, "", " ")
  656. if err != nil {
  657. for email := range foundEmails {
  658. res.perEmailSkipped[email] = err.Error()
  659. }
  660. return res
  661. }
  662. oldInbound.Settings = string(newSettings)
  663. // A flow change rewrites the user's xray config, which the lightweight
  664. // UpdateUser push below does not carry. Local nodes reload via restart;
  665. // remote nodes get a full reconcile (MarkNodeDirty) instead of a per-user push.
  666. if flowChanged && oldInbound.NodeID == nil {
  667. res.needRestart = true
  668. }
  669. // Serialize against the traffic poll to avoid the cross-transaction
  670. // lock-order deadlock on inbounds/client_records (runSerializedTx).
  671. txErr := runSerializedTx(func(tx *gorm.DB) error {
  672. if err := tx.Save(oldInbound).Error; err != nil {
  673. return err
  674. }
  675. finalClients, gcErr := inboundSvc.GetClients(oldInbound)
  676. if gcErr != nil {
  677. return gcErr
  678. }
  679. if err := s.SyncInbound(tx, inboundId, finalClients); err != nil {
  680. return err
  681. }
  682. if oldInbound.NodeID != nil {
  683. return (&NodeService{}).MarkNodeDirtyTx(tx, *oldInbound.NodeID)
  684. }
  685. return nil
  686. })
  687. if txErr != nil {
  688. for email := range foundEmails {
  689. if _, skip := res.perEmailSkipped[email]; !skip {
  690. res.perEmailSkipped[email] = txErr.Error()
  691. }
  692. }
  693. } else if oldInbound.NodeID != nil && !flowChanged && len(foundEmails) <= nodeBulkPushThreshold {
  694. rt, push, _, perr := inboundSvc.nodePushPlan(oldInbound)
  695. if perr != nil {
  696. logger.Warning("BulkAdjust: node runtime lookup after commit failed:", perr)
  697. } else if push {
  698. for email := range foundEmails {
  699. entry := plan[email]
  700. updated := *entry.record.ToClient()
  701. if entry.applyExpiry {
  702. updated.ExpiryTime = entry.newExpiry
  703. }
  704. if entry.applyTotal {
  705. updated.TotalGB = entry.newTotal
  706. }
  707. updated.UpdatedAt = nowMs
  708. ctx, cancel := nodePushContext()
  709. err1 := rt.UpdateUser(ctx, oldInbound, email, updated)
  710. cancel()
  711. if err1 != nil {
  712. logger.Warning("Error in updating client on", rt.Name(), ":", err1)
  713. // First failure ends the batch push; the reconcile converges the rest.
  714. break
  715. }
  716. }
  717. }
  718. }
  719. return res
  720. }
  721. // BulkDeleteResult mirrors BulkAdjustResult: total deleted plus per-email
  722. // skip reasons when an email could not be processed.
  723. type BulkDeleteResult struct {
  724. Deleted int `json:"deleted"`
  725. Skipped []BulkDeleteReport `json:"skipped,omitempty"`
  726. }
  727. type BulkDeleteReport struct {
  728. Email string `json:"email"`
  729. Reason string `json:"reason"`
  730. }
  731. // BulkDelete removes every client in the list in one optimized pass.
  732. // Instead of running the full single-delete pipeline N times (which would
  733. // re-read, re-parse, and re-write each inbound's settings JSON for every
  734. // email), it groups emails by inbound and performs a single
  735. // read-modify-write per inbound. Per-row DB cleanups are also batched with
  736. // IN-clause queries at the end. Errors on a particular email are recorded
  737. // in the Skipped list and processing continues for the rest.
  738. func (s *ClientService) BulkDelete(inboundSvc *InboundService, emails []string, keepTraffic bool) (BulkDeleteResult, bool, error) {
  739. result := BulkDeleteResult{}
  740. seen := map[string]struct{}{}
  741. cleanEmails := make([]string, 0, len(emails))
  742. for _, e := range emails {
  743. e = strings.TrimSpace(e)
  744. if e == "" {
  745. continue
  746. }
  747. if _, ok := seen[e]; ok {
  748. continue
  749. }
  750. seen[e] = struct{}{}
  751. cleanEmails = append(cleanEmails, e)
  752. }
  753. if len(cleanEmails) == 0 {
  754. return result, false, nil
  755. }
  756. db := database.GetDB()
  757. var records []model.ClientRecord
  758. for _, batch := range chunkStrings(cleanEmails, sqlInChunk) {
  759. var rows []model.ClientRecord
  760. if err := db.Where("email IN ?", batch).Find(&rows).Error; err != nil {
  761. return result, false, err
  762. }
  763. records = append(records, rows...)
  764. }
  765. recordsByEmail := make(map[string]*model.ClientRecord, len(records))
  766. tombstoneEmails := make([]string, 0, len(records))
  767. for i := range records {
  768. recordsByEmail[records[i].Email] = &records[i]
  769. tombstoneEmails = append(tombstoneEmails, records[i].Email)
  770. }
  771. tombstoneClientEmails(tombstoneEmails)
  772. skippedReasons := map[string]string{}
  773. for _, email := range cleanEmails {
  774. if _, ok := recordsByEmail[email]; !ok {
  775. skippedReasons[email] = "client not found"
  776. }
  777. }
  778. clientIds := make([]int, 0, len(recordsByEmail))
  779. recordIdToEmail := make(map[int]string, len(recordsByEmail))
  780. for _, r := range recordsByEmail {
  781. clientIds = append(clientIds, r.Id)
  782. recordIdToEmail[r.Id] = r.Email
  783. }
  784. emailsByInbound := map[int][]string{}
  785. if len(clientIds) > 0 {
  786. var mappings []model.ClientInbound
  787. for _, batch := range chunkInts(clientIds, sqlInChunk) {
  788. var rows []model.ClientInbound
  789. if err := db.Where("client_id IN ?", batch).Find(&rows).Error; err != nil {
  790. return result, false, err
  791. }
  792. mappings = append(mappings, rows...)
  793. }
  794. for _, m := range mappings {
  795. email, ok := recordIdToEmail[m.ClientId]
  796. if !ok {
  797. continue
  798. }
  799. emailsByInbound[m.InboundId] = append(emailsByInbound[m.InboundId], email)
  800. }
  801. }
  802. needRestart := false
  803. delIds := sortedInboundIds(emailsByInbound)
  804. delResults, delPanics := fanoutInboundResults(delIds, inboundFanoutConcurrency, func(i int) bulkInboundDeleteResult {
  805. return s.bulkDelInboundClients(inboundSvc, delIds[i], emailsByInbound[delIds[i]], recordsByEmail, keepTraffic)
  806. })
  807. for i, ibResult := range delResults {
  808. if delPanics[i] != nil {
  809. needRestart = true
  810. for _, email := range emailsByInbound[delIds[i]] {
  811. if _, already := skippedReasons[email]; !already {
  812. skippedReasons[email] = delPanics[i].Error()
  813. }
  814. }
  815. continue
  816. }
  817. if ibResult.needRestart {
  818. needRestart = true
  819. }
  820. for email, reason := range ibResult.perEmailSkipped {
  821. if _, already := skippedReasons[email]; !already {
  822. skippedReasons[email] = reason
  823. }
  824. }
  825. }
  826. successEmails := make([]string, 0, len(recordsByEmail))
  827. successIds := make([]int, 0, len(recordsByEmail))
  828. failedEmails := make([]string, 0, len(recordsByEmail))
  829. successSubIDs := make([]string, 0, len(recordsByEmail))
  830. for email, rec := range recordsByEmail {
  831. if _, skipped := skippedReasons[email]; skipped {
  832. failedEmails = append(failedEmails, email)
  833. continue
  834. }
  835. successEmails = append(successEmails, email)
  836. successIds = append(successIds, rec.Id)
  837. successSubIDs = append(successSubIDs, rec.SubID)
  838. }
  839. withdrawClientTombstones(failedEmails...)
  840. if len(successIds) > 0 {
  841. // Serialize the row cleanup against the traffic poll to avoid the
  842. // cross-transaction lock-order deadlock on client_traffics/inbounds.
  843. if err := runSerializedTx(func(tx *gorm.DB) error {
  844. if e := adjustGroupBaselinesForRemovedTraffic(tx, successEmails); e != nil {
  845. return e
  846. }
  847. if e := clearClientHwidsBySubIDTx(tx, successSubIDs...); e != nil {
  848. return e
  849. }
  850. for _, batch := range chunkInts(successIds, sqlInChunk) {
  851. if e := tx.Where("client_id IN ?", batch).Delete(&model.ClientInbound{}).Error; e != nil {
  852. return e
  853. }
  854. if e := tx.Where("client_id IN ?", batch).Delete(&model.ClientExternalLink{}).Error; e != nil {
  855. return e
  856. }
  857. }
  858. if !keepTraffic && len(successEmails) > 0 {
  859. for _, batch := range chunkStrings(successEmails, sqlInChunk) {
  860. if e := tx.Where("email IN ?", batch).Delete(&xray.ClientTraffic{}).Error; e != nil {
  861. return e
  862. }
  863. if e := tx.Where("client_email IN ?", batch).Delete(&model.InboundClientIps{}).Error; e != nil {
  864. return e
  865. }
  866. }
  867. }
  868. for _, batch := range chunkInts(successIds, sqlInChunk) {
  869. if e := tx.Where("id IN ?", batch).Delete(&model.ClientRecord{}).Error; e != nil {
  870. return e
  871. }
  872. }
  873. return nil
  874. }); err != nil {
  875. withdrawClientTombstones(successEmails...)
  876. return result, needRestart, err
  877. }
  878. }
  879. result.Deleted = len(successEmails)
  880. for email, reason := range skippedReasons {
  881. result.Skipped = append(result.Skipped, BulkDeleteReport{Email: email, Reason: reason})
  882. }
  883. return result, needRestart, nil
  884. }
  885. type bulkInboundDeleteResult struct {
  886. perEmailSkipped map[string]string
  887. needRestart bool
  888. }
  889. // bulkDelInboundClients removes multiple clients from a single inbound's
  890. // settings JSON in one read-modify-write cycle, runs the xray runtime
  891. // RemoveUser/DeleteUser calls, and persists the inbound. The returned map
  892. // holds per-email failure reasons; emails not present in the map are
  893. // considered successful for this inbound.
  894. func (s *ClientService) bulkDelInboundClients(
  895. inboundSvc *InboundService,
  896. inboundId int,
  897. emails []string,
  898. records map[string]*model.ClientRecord,
  899. keepTraffic bool,
  900. ) bulkInboundDeleteResult {
  901. res := bulkInboundDeleteResult{perEmailSkipped: map[string]string{}}
  902. defer lockInbound(inboundId).Unlock()
  903. oldInbound, err := inboundSvc.GetInbound(inboundId)
  904. if err != nil {
  905. logger.Error("Load Old Data Error")
  906. for _, e := range emails {
  907. res.perEmailSkipped[e] = err.Error()
  908. }
  909. return res
  910. }
  911. var settings map[string]any
  912. if err := json.Unmarshal([]byte(oldInbound.Settings), &settings); err != nil {
  913. for _, e := range emails {
  914. res.perEmailSkipped[e] = err.Error()
  915. }
  916. return res
  917. }
  918. // Match by email — the client's stable identity (see Delete). The link-derived
  919. // set is deletion intent: an email already absent from settings is successful,
  920. // while foundEmails tracks entries that still need settings-specific cleanup.
  921. wantedEmails := make(map[string]struct{}, len(emails))
  922. for _, email := range emails {
  923. if records[email] == nil {
  924. res.perEmailSkipped[email] = "client not found"
  925. continue
  926. }
  927. wantedEmails[email] = struct{}{}
  928. }
  929. interfaceClients, _ := settings["clients"].([]any)
  930. newClients := make([]any, 0, len(interfaceClients))
  931. foundEmails := map[string]bool{}
  932. enableByEmail := map[string]bool{}
  933. for _, client := range interfaceClients {
  934. c, ok := client.(map[string]any)
  935. if !ok {
  936. newClients = append(newClients, client)
  937. continue
  938. }
  939. em, _ := c["email"].(string)
  940. if _, found := wantedEmails[em]; found && em != "" {
  941. foundEmails[em] = true
  942. en, _ := c["enable"].(bool)
  943. enableByEmail[em] = en
  944. continue
  945. }
  946. newClients = append(newClients, client)
  947. }
  948. db := database.GetDB()
  949. newClients = compactOrphans(db, newClients)
  950. if newClients == nil {
  951. newClients = []any{}
  952. }
  953. settings["clients"] = newClients
  954. newSettings, err := json.MarshalIndent(settings, "", " ")
  955. if err != nil {
  956. for email := range wantedEmails {
  957. if _, skip := res.perEmailSkipped[email]; !skip {
  958. res.perEmailSkipped[email] = err.Error()
  959. }
  960. }
  961. return res
  962. }
  963. oldInbound.Settings = string(newSettings)
  964. foundList := make([]string, 0, len(foundEmails))
  965. for email := range foundEmails {
  966. foundList = append(foundList, email)
  967. }
  968. notDepletedByEmail := map[string]bool{}
  969. if len(foundList) > 0 {
  970. type trafficRow struct {
  971. Email string
  972. Enable bool
  973. }
  974. for _, batch := range chunkStrings(foundList, sqlInChunk) {
  975. var rows []trafficRow
  976. if err := db.Model(xray.ClientTraffic{}).
  977. Where("email IN ?", batch).
  978. Select("email, enable").
  979. Scan(&rows).Error; err == nil {
  980. for _, r := range rows {
  981. notDepletedByEmail[r.Email] = r.Enable
  982. }
  983. }
  984. }
  985. }
  986. var sharedSet map[string]bool
  987. if !keepTraffic {
  988. var sharedErr error
  989. sharedSet, sharedErr = inboundSvc.emailsUsedByOtherInbounds(foundList, inboundId)
  990. if sharedErr != nil {
  991. for email := range wantedEmails {
  992. res.perEmailSkipped[email] = sharedErr.Error()
  993. }
  994. return res
  995. }
  996. }
  997. if !keepTraffic {
  998. purge := make([]string, 0, len(foundEmails))
  999. for email := range foundEmails {
  1000. if !sharedSet[strings.ToLower(strings.TrimSpace(email))] {
  1001. purge = append(purge, email)
  1002. }
  1003. }
  1004. if len(purge) > 0 {
  1005. // Serialize the IP/stat purge against the traffic poll to avoid the
  1006. // cross-transaction lock-order deadlock on client_traffics.
  1007. if delErr := runSerializedTx(func(tx *gorm.DB) error {
  1008. if e := inboundSvc.delClientIPsByEmails(tx, purge); e != nil {
  1009. logger.Error("Error in delete client IPs")
  1010. return e
  1011. }
  1012. if e := inboundSvc.delClientStatsByEmails(tx, purge); e != nil {
  1013. logger.Error("Delete stats Data Error")
  1014. return e
  1015. }
  1016. return nil
  1017. }); delErr != nil {
  1018. for _, email := range purge {
  1019. res.perEmailSkipped[email] = delErr.Error()
  1020. delete(foundEmails, email)
  1021. }
  1022. }
  1023. }
  1024. }
  1025. // Serialize against the traffic poll to avoid the cross-transaction
  1026. // lock-order deadlock on inbounds/client_records (runSerializedTx).
  1027. txErr := runSerializedTx(func(tx *gorm.DB) error {
  1028. if err := tx.Save(oldInbound).Error; err != nil {
  1029. return err
  1030. }
  1031. finalClients, err := inboundSvc.GetClients(oldInbound)
  1032. if err != nil {
  1033. return err
  1034. }
  1035. if err := s.SyncInbound(tx, inboundId, finalClients); err != nil {
  1036. return err
  1037. }
  1038. if oldInbound.NodeID != nil {
  1039. return (&NodeService{}).MarkNodeDirtyTx(tx, *oldInbound.NodeID)
  1040. }
  1041. return nil
  1042. })
  1043. if txErr != nil {
  1044. for email := range wantedEmails {
  1045. if _, skip := res.perEmailSkipped[email]; !skip {
  1046. res.perEmailSkipped[email] = txErr.Error()
  1047. }
  1048. }
  1049. } else if oldInbound.NodeID == nil {
  1050. rt, rterr := inboundSvc.runtimeFor(oldInbound)
  1051. if rterr != nil {
  1052. res.needRestart = true
  1053. } else {
  1054. for email := range foundEmails {
  1055. if !enableByEmail[email] || !notDepletedByEmail[email] {
  1056. continue
  1057. }
  1058. err1 := rt.RemoveUser(context.Background(), oldInbound, email)
  1059. if err1 == nil {
  1060. logger.Debug("Client deleted on", rt.Name(), ":", email)
  1061. } else if strings.Contains(err1.Error(), fmt.Sprintf("User %s not found.", email)) {
  1062. logger.Debug("User is already deleted. Nothing to do more...")
  1063. } else {
  1064. logger.Debug("Error in deleting client on", rt.Name(), ":", err1)
  1065. res.needRestart = true
  1066. }
  1067. }
  1068. }
  1069. } else {
  1070. dispatchEmails := make([]string, 0, len(wantedEmails))
  1071. for email := range wantedEmails {
  1072. if _, skip := res.perEmailSkipped[email]; !skip {
  1073. dispatchEmails = append(dispatchEmails, email)
  1074. }
  1075. }
  1076. if len(dispatchEmails) > nodeBulkPushThreshold {
  1077. return res
  1078. }
  1079. rt, push, _, perr := inboundSvc.nodePushPlan(oldInbound)
  1080. if perr != nil {
  1081. logger.Warning("BulkDelete: node runtime lookup after commit failed:", perr)
  1082. } else if push {
  1083. for _, email := range dispatchEmails {
  1084. ctx, cancel := nodePushContext()
  1085. err1 := rt.DeleteClient(ctx, email)
  1086. cancel()
  1087. if err1 != nil {
  1088. logger.Warning("Error in deleting client on", rt.Name(), ":", err1)
  1089. // The node is already dirty, so one reconcile converges the rest of
  1090. // the batch instead of paying another deadline per client.
  1091. break
  1092. }
  1093. }
  1094. }
  1095. }
  1096. return res
  1097. }
  1098. // BulkCreateResult mirrors BulkAdjustResult for the create flow.
  1099. type BulkCreateResult struct {
  1100. Created int `json:"created"`
  1101. Skipped []BulkCreateReport `json:"skipped,omitempty"`
  1102. }
  1103. type BulkCreateReport struct {
  1104. Email string `json:"email"`
  1105. Reason string `json:"reason"`
  1106. }
  1107. func (s *ClientService) BulkCreate(inboundSvc *InboundService, payloads []ClientCreatePayload) (BulkCreateResult, bool, error) {
  1108. result := BulkCreateResult{}
  1109. if len(payloads) == 0 {
  1110. return result, false, nil
  1111. }
  1112. skip := func(email, reason string) {
  1113. if strings.TrimSpace(email) == "" {
  1114. email = "(missing email)"
  1115. }
  1116. result.Skipped = append(result.Skipped, BulkCreateReport{Email: email, Reason: reason})
  1117. }
  1118. type prepared struct {
  1119. client model.Client
  1120. inboundIds []int
  1121. limitHwid int
  1122. }
  1123. prep := make([]prepared, 0, len(payloads))
  1124. emails := make([]string, 0, len(payloads))
  1125. subIDs := make([]string, 0, len(payloads))
  1126. seenEmail := make(map[string]struct{}, len(payloads))
  1127. seenSubID := make(map[string]string, len(payloads))
  1128. for i := range payloads {
  1129. client := payloads[i].Client
  1130. email := strings.TrimSpace(client.Email)
  1131. if email == "" {
  1132. skip("", "client email is required")
  1133. continue
  1134. }
  1135. if verr := validateClientEmail(email); verr != nil {
  1136. skip(email, verr.Error())
  1137. continue
  1138. }
  1139. if verr := validateClientSubID(client.SubID); verr != nil {
  1140. skip(email, verr.Error())
  1141. continue
  1142. }
  1143. if verr := validateClientResetDay(client.ResetDay); verr != nil {
  1144. skip(email, verr.Error())
  1145. continue
  1146. }
  1147. if verr := validateClientResetMax(client.ResetMax); verr != nil {
  1148. skip(email, verr.Error())
  1149. continue
  1150. }
  1151. if verr := validateClientTrafficReset(client.TrafficReset, client.TrafficResetDay); verr != nil {
  1152. skip(email, verr.Error())
  1153. continue
  1154. }
  1155. if len(payloads[i].InboundIds) == 0 {
  1156. skip(email, "at least one inbound is required")
  1157. continue
  1158. }
  1159. client.Email = email
  1160. if client.SubID == "" {
  1161. client.SubID = uuid.NewString()
  1162. }
  1163. if !client.Enable {
  1164. client.Enable = true
  1165. }
  1166. now := time.Now().UnixMilli()
  1167. if client.CreatedAt == 0 {
  1168. client.CreatedAt = now
  1169. }
  1170. client.UpdatedAt = now
  1171. le := strings.ToLower(email)
  1172. if _, dup := seenEmail[le]; dup {
  1173. skip(email, "email already in use: "+email)
  1174. continue
  1175. }
  1176. if owner, ok := seenSubID[client.SubID]; ok && owner != le {
  1177. skip(email, "subId already in use: "+client.SubID)
  1178. continue
  1179. }
  1180. seenEmail[le] = struct{}{}
  1181. seenSubID[client.SubID] = le
  1182. prep = append(prep, prepared{client: client, inboundIds: payloads[i].InboundIds, limitHwid: payloads[i].LimitHwid})
  1183. emails = append(emails, email)
  1184. subIDs = append(subIDs, client.SubID)
  1185. }
  1186. if len(prep) == 0 {
  1187. return result, false, nil
  1188. }
  1189. db := database.GetDB()
  1190. const lookupChunk = 400
  1191. existingByEmail := make(map[string]model.ClientRecord, len(emails))
  1192. for start := 0; start < len(emails); start += lookupChunk {
  1193. end := min(start+lookupChunk, len(emails))
  1194. var rows []model.ClientRecord
  1195. if e := db.Where("email IN ?", emails[start:end]).Find(&rows).Error; e != nil {
  1196. return result, false, e
  1197. }
  1198. for i := range rows {
  1199. existingByEmail[strings.ToLower(rows[i].Email)] = rows[i]
  1200. }
  1201. }
  1202. existingSubOwner := make(map[string]string, len(subIDs))
  1203. for start := 0; start < len(subIDs); start += lookupChunk {
  1204. end := min(start+lookupChunk, len(subIDs))
  1205. var rows []model.ClientRecord
  1206. if e := db.Where("sub_id IN ?", subIDs[start:end]).Find(&rows).Error; e != nil {
  1207. return result, false, e
  1208. }
  1209. for i := range rows {
  1210. existingSubOwner[rows[i].SubID] = strings.ToLower(rows[i].Email)
  1211. }
  1212. }
  1213. inboundCache := make(map[int]*model.Inbound)
  1214. getIb := func(id int) (*model.Inbound, error) {
  1215. if ib, ok := inboundCache[id]; ok {
  1216. return ib, nil
  1217. }
  1218. ib, e := inboundSvc.GetInbound(id)
  1219. if e != nil {
  1220. return nil, e
  1221. }
  1222. inboundCache[id] = ib
  1223. return ib, nil
  1224. }
  1225. byInbound := make(map[int][]model.Client)
  1226. idxByInbound := make(map[int][]int)
  1227. inboundOrder := make([]int, 0)
  1228. failed := make([]bool, len(prep))
  1229. reason := make([]string, len(prep))
  1230. createAnyTunnel := false
  1231. for idx := range prep {
  1232. le := strings.ToLower(prep[idx].client.Email)
  1233. if rec, ok := existingByEmail[le]; ok {
  1234. if rec.SubID != prep[idx].client.SubID {
  1235. failed[idx] = true
  1236. reason[idx] = "email already in use: " + prep[idx].client.Email
  1237. continue
  1238. }
  1239. if prep[idx].client.ID == "" {
  1240. prep[idx].client.ID = rec.UUID
  1241. }
  1242. if prep[idx].client.Password == "" {
  1243. prep[idx].client.Password = rec.Password
  1244. }
  1245. if prep[idx].client.Auth == "" {
  1246. prep[idx].client.Auth = rec.Auth
  1247. }
  1248. if prep[idx].client.Secret == "" {
  1249. prep[idx].client.Secret = rec.Secret
  1250. }
  1251. }
  1252. if owner, ok := existingSubOwner[prep[idx].client.SubID]; ok && owner != le {
  1253. failed[idx] = true
  1254. reason[idx] = "subId already in use: " + prep[idx].client.SubID
  1255. continue
  1256. }
  1257. ok := true
  1258. for _, ibId := range prep[idx].inboundIds {
  1259. ib, e := getIb(ibId)
  1260. if e != nil {
  1261. failed[idx] = true
  1262. reason[idx] = e.Error()
  1263. ok = false
  1264. break
  1265. }
  1266. if ib.Protocol == model.WireGuard || ib.Protocol == model.AmneziaWG {
  1267. createAnyTunnel = true
  1268. }
  1269. if e := s.fillProtocolDefaults(&prep[idx].client, ib); e != nil {
  1270. failed[idx] = true
  1271. reason[idx] = e.Error()
  1272. ok = false
  1273. break
  1274. }
  1275. }
  1276. if !ok {
  1277. continue
  1278. }
  1279. for _, ibId := range prep[idx].inboundIds {
  1280. ib, _ := getIb(ibId)
  1281. if _, seen := byInbound[ibId]; !seen {
  1282. inboundOrder = append(inboundOrder, ibId)
  1283. }
  1284. byInbound[ibId] = append(byInbound[ibId], clientWithInboundFlow(prep[idx].client, ib))
  1285. idxByInbound[ibId] = append(idxByInbound[ibId], idx)
  1286. }
  1287. }
  1288. needRestart := false
  1289. createResults, createPanics := fanoutInboundResults(inboundOrder, addFanoutLimit(createAnyTunnel), func(i int) inboundApplyOutcome {
  1290. ibId := inboundOrder[i]
  1291. payload, e := json.Marshal(map[string][]model.Client{"clients": byInbound[ibId]})
  1292. if e != nil {
  1293. return inboundApplyOutcome{err: e}
  1294. }
  1295. nr, e := s.AddInboundClient(inboundSvc, &model.Inbound{Id: ibId, Settings: string(payload)})
  1296. return inboundApplyOutcome{needRestart: nr, err: e}
  1297. })
  1298. for i, out := range createResults {
  1299. e := out.err
  1300. if createPanics[i] != nil {
  1301. // See BulkAttach: a panicking apply may already have committed.
  1302. needRestart = true
  1303. e = createPanics[i]
  1304. }
  1305. if e != nil {
  1306. for _, idx := range idxByInbound[inboundOrder[i]] {
  1307. failed[idx] = true
  1308. if reason[idx] == "" {
  1309. reason[idx] = e.Error()
  1310. }
  1311. }
  1312. continue
  1313. }
  1314. if out.needRestart {
  1315. needRestart = true
  1316. }
  1317. }
  1318. for idx := range prep {
  1319. if failed[idx] {
  1320. skip(prep[idx].client.Email, reason[idx])
  1321. continue
  1322. }
  1323. if err := s.setClientLimitHwidByEmail(nil, prep[idx].client.Email, prep[idx].limitHwid); err != nil {
  1324. skip(prep[idx].client.Email, err.Error())
  1325. continue
  1326. }
  1327. result.Created++
  1328. }
  1329. return result, needRestart, nil
  1330. }
  1331. func (s *ClientService) DelDepleted(inboundSvc *InboundService) (int, bool, error) {
  1332. db := database.GetDB()
  1333. now := time.Now().UnixMilli()
  1334. depletedClause := depletedClientsClause
  1335. var rows []xray.ClientTraffic
  1336. if err := db.Where(depletedClause, now).Find(&rows).Error; err != nil {
  1337. return 0, false, err
  1338. }
  1339. if len(rows) == 0 {
  1340. return 0, false, nil
  1341. }
  1342. seen := make(map[string]struct{}, len(rows))
  1343. emails := make([]string, 0, len(rows))
  1344. for _, r := range rows {
  1345. if r.Email == "" {
  1346. continue
  1347. }
  1348. if _, ok := seen[r.Email]; ok {
  1349. continue
  1350. }
  1351. seen[r.Email] = struct{}{}
  1352. emails = append(emails, r.Email)
  1353. }
  1354. if len(emails) == 0 {
  1355. return 0, false, nil
  1356. }
  1357. res, needRestart, err := s.BulkDelete(inboundSvc, emails, false)
  1358. if err != nil {
  1359. return res.Deleted, needRestart, err
  1360. }
  1361. return res.Deleted, needRestart, nil
  1362. }
  1363. type BulkSetEnableResult struct {
  1364. Changed int `json:"changed"`
  1365. Skipped []BulkSetEnableReport `json:"skipped,omitempty"`
  1366. }
  1367. type BulkSetEnableReport struct {
  1368. Email string `json:"email"`
  1369. Reason string `json:"reason"`
  1370. }
  1371. func (s *ClientService) BulkSetEnable(inboundSvc *InboundService, emails []string, enable bool) (BulkSetEnableResult, bool, error) {
  1372. result := BulkSetEnableResult{}
  1373. seen := map[string]struct{}{}
  1374. cleanEmails := make([]string, 0, len(emails))
  1375. for _, e := range emails {
  1376. e = strings.TrimSpace(e)
  1377. if e == "" {
  1378. continue
  1379. }
  1380. if _, ok := seen[e]; ok {
  1381. continue
  1382. }
  1383. seen[e] = struct{}{}
  1384. cleanEmails = append(cleanEmails, e)
  1385. }
  1386. if len(cleanEmails) == 0 {
  1387. return result, false, nil
  1388. }
  1389. db := database.GetDB()
  1390. var records []model.ClientRecord
  1391. for _, batch := range chunkStrings(cleanEmails, sqlInChunk) {
  1392. var rows []model.ClientRecord
  1393. if err := db.Where("email IN ?", batch).Find(&rows).Error; err != nil {
  1394. return result, false, err
  1395. }
  1396. records = append(records, rows...)
  1397. }
  1398. recordsByEmail := make(map[string]*model.ClientRecord, len(records))
  1399. for i := range records {
  1400. recordsByEmail[records[i].Email] = &records[i]
  1401. }
  1402. skippedReasons := map[string]string{}
  1403. for _, email := range cleanEmails {
  1404. if _, ok := recordsByEmail[email]; !ok {
  1405. skippedReasons[email] = "client not found"
  1406. }
  1407. }
  1408. clientIds := make([]int, 0, len(recordsByEmail))
  1409. recordIdToEmail := make(map[int]string, len(recordsByEmail))
  1410. for _, r := range recordsByEmail {
  1411. clientIds = append(clientIds, r.Id)
  1412. recordIdToEmail[r.Id] = r.Email
  1413. }
  1414. emailsByInbound := map[int][]string{}
  1415. if len(clientIds) > 0 {
  1416. var mappings []model.ClientInbound
  1417. for _, batch := range chunkInts(clientIds, sqlInChunk) {
  1418. var rows []model.ClientInbound
  1419. if err := db.Where("client_id IN ?", batch).Find(&rows).Error; err != nil {
  1420. return result, false, err
  1421. }
  1422. mappings = append(mappings, rows...)
  1423. }
  1424. for _, m := range mappings {
  1425. email, ok := recordIdToEmail[m.ClientId]
  1426. if !ok {
  1427. continue
  1428. }
  1429. emailsByInbound[m.InboundId] = append(emailsByInbound[m.InboundId], email)
  1430. }
  1431. }
  1432. needRestart := false
  1433. enableIds := sortedInboundIds(emailsByInbound)
  1434. enableResults, enablePanics := fanoutInboundResults(enableIds, inboundFanoutConcurrency, func(i int) bulkSetEnableInboundResult {
  1435. return s.bulkSetEnableInboundClients(inboundSvc, enableIds[i], emailsByInbound[enableIds[i]], enable)
  1436. })
  1437. for i, ibRes := range enableResults {
  1438. if enablePanics[i] != nil {
  1439. needRestart = true
  1440. for _, email := range emailsByInbound[enableIds[i]] {
  1441. if _, already := skippedReasons[email]; !already {
  1442. skippedReasons[email] = enablePanics[i].Error()
  1443. }
  1444. }
  1445. continue
  1446. }
  1447. if ibRes.needRestart {
  1448. needRestart = true
  1449. }
  1450. for email, reason := range ibRes.perEmailSkipped {
  1451. if _, already := skippedReasons[email]; !already {
  1452. skippedReasons[email] = reason
  1453. }
  1454. }
  1455. }
  1456. successEmails := make([]string, 0, len(recordsByEmail))
  1457. for email := range recordsByEmail {
  1458. if _, skipped := skippedReasons[email]; skipped {
  1459. continue
  1460. }
  1461. successEmails = append(successEmails, email)
  1462. }
  1463. if len(successEmails) > 0 {
  1464. now := time.Now().UnixMilli()
  1465. if err := runSerializedTx(func(tx *gorm.DB) error {
  1466. for _, batch := range chunkStrings(successEmails, sqlInChunk) {
  1467. if e := tx.Model(xray.ClientTraffic{}).Where("email IN ?", batch).Update("enable", enable).Error; e != nil {
  1468. return e
  1469. }
  1470. if e := tx.Model(&model.ClientRecord{}).Where("email IN ?", batch).
  1471. Updates(map[string]any{"enable": enable, "updated_at": now}).Error; e != nil {
  1472. return e
  1473. }
  1474. }
  1475. return nil
  1476. }); err != nil {
  1477. return result, needRestart, err
  1478. }
  1479. }
  1480. result.Changed = len(successEmails)
  1481. for email, reason := range skippedReasons {
  1482. result.Skipped = append(result.Skipped, BulkSetEnableReport{Email: email, Reason: reason})
  1483. }
  1484. return result, needRestart, nil
  1485. }
  1486. type bulkSetEnableInboundResult struct {
  1487. perEmailSkipped map[string]string
  1488. needRestart bool
  1489. }
  1490. func (s *ClientService) bulkSetEnableInboundClients(inboundSvc *InboundService, inboundId int, emails []string, enable bool) bulkSetEnableInboundResult {
  1491. res := bulkSetEnableInboundResult{perEmailSkipped: map[string]string{}}
  1492. defer lockInbound(inboundId).Unlock()
  1493. oldInbound, err := inboundSvc.GetInbound(inboundId)
  1494. if err != nil {
  1495. for _, e := range emails {
  1496. res.perEmailSkipped[e] = err.Error()
  1497. }
  1498. return res
  1499. }
  1500. var settings map[string]any
  1501. if err := json.Unmarshal([]byte(oldInbound.Settings), &settings); err != nil {
  1502. for _, e := range emails {
  1503. res.perEmailSkipped[e] = err.Error()
  1504. }
  1505. return res
  1506. }
  1507. wanted := make(map[string]struct{}, len(emails))
  1508. for _, email := range emails {
  1509. wanted[email] = struct{}{}
  1510. }
  1511. cipher := ""
  1512. if oldInbound.Protocol == model.Shadowsocks {
  1513. cipher, _ = settings["method"].(string)
  1514. }
  1515. type changedClient struct {
  1516. email string
  1517. wasEnable bool
  1518. client model.Client
  1519. }
  1520. var changed []changedClient
  1521. found := map[string]bool{}
  1522. nowMs := time.Now().UnixMilli()
  1523. interfaceClients, _ := settings["clients"].([]any)
  1524. for i, c := range interfaceClients {
  1525. entry, ok := c.(map[string]any)
  1526. if !ok {
  1527. continue
  1528. }
  1529. email, _ := entry["email"].(string)
  1530. if _, want := wanted[email]; !want || email == "" {
  1531. continue
  1532. }
  1533. found[email] = true
  1534. prev, _ := entry["enable"].(bool)
  1535. if prev == enable {
  1536. continue
  1537. }
  1538. entry["enable"] = enable
  1539. entry["updated_at"] = nowMs
  1540. interfaceClients[i] = entry
  1541. // Build the pushed client from the inbound JSON (the per-inbound source of
  1542. // truth), so a remote UpdateUser carries every field and never zeroes
  1543. // subId/totalGB/expiry from drifting ClientRecord columns (#4628/#4792).
  1544. var parsed model.Client
  1545. if b, mErr := json.Marshal(entry); mErr == nil {
  1546. _ = json.Unmarshal(b, &parsed)
  1547. }
  1548. parsed.Email = email
  1549. parsed.Enable = enable
  1550. changed = append(changed, changedClient{email: email, wasEnable: prev, client: parsed})
  1551. }
  1552. for email := range wanted {
  1553. if !found[email] {
  1554. res.perEmailSkipped[email] = "Client Not Found In Inbound"
  1555. }
  1556. }
  1557. if len(changed) == 0 {
  1558. return res
  1559. }
  1560. settings["clients"] = interfaceClients
  1561. newSettings, err := json.MarshalIndent(settings, "", " ")
  1562. if err != nil {
  1563. for _, ch := range changed {
  1564. res.perEmailSkipped[ch.email] = err.Error()
  1565. }
  1566. return res
  1567. }
  1568. prevSettings := oldInbound.Settings
  1569. oldInbound.Settings = string(newSettings)
  1570. rt, push, _, perr := inboundSvc.nodePushPlan(oldInbound)
  1571. if perr != nil {
  1572. for _, ch := range changed {
  1573. res.perEmailSkipped[ch.email] = perr.Error()
  1574. }
  1575. return res
  1576. }
  1577. if oldInbound.NodeID != nil && push && len(changed) > nodeBulkPushThreshold {
  1578. push = false
  1579. }
  1580. txErr := runSerializedTx(func(tx *gorm.DB) error {
  1581. if e := tx.Save(oldInbound).Error; e != nil {
  1582. return e
  1583. }
  1584. finalClients, gcErr := inboundSvc.GetClients(oldInbound)
  1585. if gcErr != nil {
  1586. return gcErr
  1587. }
  1588. if err := s.SyncInbound(tx, inboundId, finalClients); err != nil {
  1589. return err
  1590. }
  1591. if oldInbound.NodeID != nil {
  1592. return (&NodeService{}).MarkNodeDirtyTx(tx, *oldInbound.NodeID)
  1593. }
  1594. return nil
  1595. })
  1596. if txErr != nil {
  1597. for _, ch := range changed {
  1598. res.perEmailSkipped[ch.email] = txErr.Error()
  1599. }
  1600. return res
  1601. }
  1602. if oldInbound.NodeID == nil {
  1603. if !push {
  1604. res.needRestart = true
  1605. } else {
  1606. for _, ch := range changed {
  1607. if enable {
  1608. err1 := rt.AddUser(context.Background(), oldInbound, map[string]any{
  1609. "email": ch.client.Email,
  1610. "id": ch.client.ID,
  1611. "security": ch.client.Security,
  1612. "flow": ch.client.Flow,
  1613. "auth": ch.client.Auth,
  1614. "password": ch.client.Password,
  1615. "cipher": cipher,
  1616. })
  1617. if err1 != nil {
  1618. logger.Debug("Error in adding client on", rt.Name(), ":", err1)
  1619. res.needRestart = true
  1620. }
  1621. } else if ch.wasEnable {
  1622. err1 := rt.RemoveUser(context.Background(), oldInbound, ch.email)
  1623. if err1 != nil && !strings.Contains(err1.Error(), fmt.Sprintf("User %s not found.", ch.email)) {
  1624. logger.Debug("Error in removing client on", rt.Name(), ":", err1)
  1625. res.needRestart = true
  1626. }
  1627. }
  1628. }
  1629. }
  1630. } else if push {
  1631. pushFailed := false
  1632. for _, ch := range changed {
  1633. updated := ch.client
  1634. updated.UpdatedAt = nowMs
  1635. ctx, cancel := nodePushContext()
  1636. err1 := rt.UpdateUser(ctx, oldInbound, ch.email, updated)
  1637. cancel()
  1638. if err1 != nil {
  1639. logger.Warning("Error in updating client on", rt.Name(), ":", err1)
  1640. pushFailed = true
  1641. // First failure ends the batch push; the reconcile converges the rest.
  1642. break
  1643. }
  1644. }
  1645. if !pushFailed {
  1646. advancePushedInbound(rt, prevSettings, oldInbound)
  1647. }
  1648. }
  1649. return res
  1650. }