client_inbound_apply.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "github.com/mhsanaei/3x-ui/v3/internal/database"
  9. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  10. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  11. "github.com/mhsanaei/3x-ui/v3/internal/util/common"
  12. "github.com/mhsanaei/3x-ui/v3/internal/util/random"
  13. "github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
  14. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  15. "gorm.io/gorm"
  16. )
  17. // delInboundClients removes several clients from a single inbound in one pass:
  18. // one settings rewrite, one runtime sweep, one Save and one SyncInbound for the
  19. // whole batch, instead of repeating the full per-client cycle. It mirrors the
  20. // semantics of DelInboundClientByEmail for each removed client. needRestart is
  21. // the OR across all removals.
  22. func (s *ClientService) delInboundClients(inboundSvc *InboundService, inboundId int, recs []*model.ClientRecord, keepTraffic bool) (bool, error) {
  23. if len(recs) == 0 {
  24. return false, nil
  25. }
  26. defer lockInbound(inboundId).Unlock()
  27. oldInbound, err := inboundSvc.GetInbound(inboundId)
  28. if err != nil {
  29. logger.Error("Load Old Data Error")
  30. return false, err
  31. }
  32. var settings map[string]any
  33. if err := json.Unmarshal([]byte(oldInbound.Settings), &settings); err != nil {
  34. return false, err
  35. }
  36. // Match by email — the client's stable identity (see Delete). Removes every
  37. // entry carrying a wanted email, independent of credential drift.
  38. wanted := make(map[string]struct{}, len(recs))
  39. for _, rec := range recs {
  40. if rec.Email != "" {
  41. wanted[rec.Email] = struct{}{}
  42. }
  43. }
  44. interfaceClients, ok := settings["clients"].([]any)
  45. if !ok {
  46. return false, common.NewError("invalid clients format in inbound settings")
  47. }
  48. type removedClient struct {
  49. email string
  50. needApiDel bool
  51. }
  52. removed := make([]removedClient, 0, len(wanted))
  53. newClients := make([]any, 0, len(interfaceClients))
  54. for _, client := range interfaceClients {
  55. c, ok := client.(map[string]any)
  56. if !ok {
  57. newClients = append(newClients, client)
  58. continue
  59. }
  60. email, _ := c["email"].(string)
  61. if _, hit := wanted[email]; hit && email != "" {
  62. enable, _ := c["enable"].(bool)
  63. removed = append(removed, removedClient{email: email, needApiDel: enable})
  64. continue
  65. }
  66. newClients = append(newClients, client)
  67. }
  68. if len(removed) == 0 {
  69. return false, nil
  70. }
  71. db := database.GetDB()
  72. newClients = compactOrphans(db, newClients)
  73. if newClients == nil {
  74. newClients = []any{}
  75. }
  76. settings["clients"] = newClients
  77. newSettings, err := json.MarshalIndent(settings, "", " ")
  78. if err != nil {
  79. return false, err
  80. }
  81. oldInbound.Settings = string(newSettings)
  82. var sharedSet map[string]bool
  83. if !keepTraffic {
  84. removedEmails := make([]string, 0, len(removed))
  85. for _, r := range removed {
  86. if r.email != "" {
  87. removedEmails = append(removedEmails, r.email)
  88. }
  89. }
  90. var sharedErr error
  91. sharedSet, sharedErr = inboundSvc.emailsUsedByOtherInbounds(removedEmails, inboundId)
  92. if sharedErr != nil {
  93. return false, sharedErr
  94. }
  95. }
  96. needRestart := false
  97. markDirty := false
  98. // Read each client's live state before the DB write (DelClientStat would
  99. // erase the enable flag we need to decide on a runtime removal).
  100. type delTarget struct {
  101. email string
  102. emailShared bool
  103. notDepleted bool
  104. needApiDel bool
  105. }
  106. targets := make([]delTarget, 0, len(removed))
  107. for _, r := range removed {
  108. email := r.email
  109. emailShared := sharedSet[strings.ToLower(strings.TrimSpace(email))]
  110. notDepleted := false
  111. if len(email) > 0 {
  112. var enables []bool
  113. if err := db.Model(xray.ClientTraffic{}).Where("email = ?", email).Limit(1).Pluck("enable", &enables).Error; err != nil {
  114. logger.Error("Get stats error")
  115. return needRestart, err
  116. }
  117. notDepleted = len(enables) > 0 && enables[0]
  118. }
  119. targets = append(targets, delTarget{email: email, emailShared: emailShared, notDepleted: notDepleted, needApiDel: r.needApiDel})
  120. }
  121. // Persist the batch deletion atomically, serialized against the traffic poll
  122. // to avoid the cross-transaction lock-order deadlock (runSerializedTx).
  123. if txErr := runSerializedTx(func(tx *gorm.DB) error {
  124. for _, t := range targets {
  125. if t.emailShared || keepTraffic {
  126. continue
  127. }
  128. if e := inboundSvc.DelClientIPs(tx, t.email); e != nil {
  129. logger.Error("Error in delete client IPs")
  130. return e
  131. }
  132. if len(t.email) > 0 {
  133. if e := inboundSvc.DelClientStat(tx, t.email); e != nil {
  134. logger.Error("Delete stats Data Error")
  135. return e
  136. }
  137. }
  138. }
  139. if e := tx.Save(oldInbound).Error; e != nil {
  140. return e
  141. }
  142. finalClients, gcErr := inboundSvc.GetClients(oldInbound)
  143. if gcErr != nil {
  144. return gcErr
  145. }
  146. return s.SyncInbound(tx, inboundId, finalClients)
  147. }); txErr != nil {
  148. return needRestart, txErr
  149. }
  150. // Resolve the node push plan once for the whole batch instead of per email.
  151. var nodeRt runtime.Runtime
  152. nodePush := false
  153. if oldInbound.NodeID != nil {
  154. rt, push, dirty, perr := inboundSvc.nodePushPlan(oldInbound)
  155. if perr != nil {
  156. return needRestart, perr
  157. }
  158. if dirty {
  159. markDirty = true
  160. }
  161. nodeRt, nodePush = rt, push
  162. // Large batches collapse into one reconcile push rather than M deletes.
  163. if nodePush && len(targets) > nodeBulkPushThreshold {
  164. markDirty = true
  165. nodePush = false
  166. }
  167. }
  168. // Apply runtime deletes after commit — outside the serialized writer so a
  169. // slow node call can't stall traffic accounting.
  170. for _, t := range targets {
  171. if len(t.email) == 0 {
  172. continue
  173. }
  174. if oldInbound.NodeID == nil {
  175. if t.needApiDel && t.notDepleted {
  176. rt, rterr := inboundSvc.runtimeFor(oldInbound)
  177. if rterr != nil {
  178. needRestart = true
  179. } else if err1 := rt.RemoveUser(context.Background(), oldInbound, t.email); err1 != nil {
  180. if !strings.Contains(err1.Error(), fmt.Sprintf("User %s not found.", t.email)) {
  181. needRestart = true
  182. }
  183. }
  184. }
  185. } else if nodePush {
  186. if err1 := nodeRt.DeleteUser(context.Background(), oldInbound, t.email); err1 != nil {
  187. logger.Warning("Error in deleting client on", nodeRt.Name(), ":", err1)
  188. markDirty = true
  189. }
  190. }
  191. }
  192. if markDirty && oldInbound.NodeID != nil {
  193. if dErr := (&NodeService{}).MarkNodeDirty(*oldInbound.NodeID); dErr != nil {
  194. logger.Warning("mark node dirty failed:", dErr)
  195. }
  196. }
  197. return needRestart, nil
  198. }
  199. func (s *ClientService) checkEmailsExistForClients(inboundSvc *InboundService, clients []model.Client, emailSubIDs map[string]string) (string, error) {
  200. if emailSubIDs == nil {
  201. var err error
  202. emailSubIDs, err = inboundSvc.getAllEmailSubIDs()
  203. if err != nil {
  204. return "", err
  205. }
  206. }
  207. seen := make(map[string]string, len(clients))
  208. for _, client := range clients {
  209. if client.Email == "" {
  210. continue
  211. }
  212. key := strings.ToLower(client.Email)
  213. if prev, ok := seen[key]; ok {
  214. if prev != client.SubID || client.SubID == "" {
  215. return client.Email, nil
  216. }
  217. continue
  218. }
  219. seen[key] = client.SubID
  220. if existingSub, ok := emailSubIDs[key]; ok {
  221. if client.SubID == "" || existingSub == "" || existingSub != client.SubID {
  222. return client.Email, nil
  223. }
  224. }
  225. }
  226. return "", nil
  227. }
  228. func (s *ClientService) AddInboundClient(inboundSvc *InboundService, data *model.Inbound) (bool, error) {
  229. return s.addInboundClient(inboundSvc, data, nil)
  230. }
  231. // addInboundClient is AddInboundClient with an optional precomputed email→subId
  232. // map. Bulk callers pass a single snapshot so the global getAllEmailSubIDs scan
  233. // runs once for the whole batch instead of once per target inbound; a nil map
  234. // makes it compute its own (the single-add path).
  235. func (s *ClientService) addInboundClient(inboundSvc *InboundService, data *model.Inbound, emailSubIDs map[string]string) (bool, error) {
  236. defer lockInbound(data.Id).Unlock()
  237. clients, err := inboundSvc.GetClients(data)
  238. if err != nil {
  239. return false, err
  240. }
  241. var settings map[string]any
  242. err = json.Unmarshal([]byte(data.Settings), &settings)
  243. if err != nil {
  244. return false, err
  245. }
  246. interfaceClients := settings["clients"].([]any)
  247. nowTs := time.Now().Unix() * 1000
  248. for i := range interfaceClients {
  249. if cm, ok := interfaceClients[i].(map[string]any); ok {
  250. if _, ok2 := cm["created_at"]; !ok2 {
  251. cm["created_at"] = nowTs
  252. }
  253. cm["updated_at"] = nowTs
  254. existingSub, _ := cm["subId"].(string)
  255. if strings.TrimSpace(existingSub) == "" {
  256. cm["subId"] = random.NumLower(16)
  257. }
  258. interfaceClients[i] = cm
  259. }
  260. }
  261. existEmail, err := s.checkEmailsExistForClients(inboundSvc, clients, emailSubIDs)
  262. if err != nil {
  263. return false, err
  264. }
  265. if existEmail != "" {
  266. return false, common.NewError("Duplicate email:", existEmail)
  267. }
  268. oldInbound, err := inboundSvc.GetInbound(data.Id)
  269. if err != nil {
  270. return false, err
  271. }
  272. for _, client := range clients {
  273. if strings.TrimSpace(client.Email) == "" {
  274. return false, common.NewError("client email is required")
  275. }
  276. switch oldInbound.Protocol {
  277. case "trojan":
  278. if client.Password == "" {
  279. return false, common.NewError("empty client ID")
  280. }
  281. case "shadowsocks":
  282. if client.Email == "" {
  283. return false, common.NewError("empty client ID")
  284. }
  285. case "hysteria":
  286. if client.Auth == "" {
  287. return false, common.NewError("empty client ID")
  288. }
  289. default:
  290. if client.ID == "" {
  291. return false, common.NewError("empty client ID")
  292. }
  293. }
  294. }
  295. var oldSettings map[string]any
  296. err = json.Unmarshal([]byte(oldInbound.Settings), &oldSettings)
  297. if err != nil {
  298. return false, err
  299. }
  300. if oldInbound.Protocol == model.Shadowsocks {
  301. applyShadowsocksClientMethod(interfaceClients, oldSettings)
  302. }
  303. oldClients := oldSettings["clients"].([]any)
  304. oldClients = compactOrphans(database.GetDB(), oldClients)
  305. oldClients = append(oldClients, interfaceClients...)
  306. oldSettings["clients"] = oldClients
  307. newSettings, err := json.MarshalIndent(oldSettings, "", " ")
  308. if err != nil {
  309. return false, err
  310. }
  311. oldInbound.Settings = string(newSettings)
  312. needRestart := false
  313. markDirty := false
  314. rt, push, dirty, perr := inboundSvc.nodePushPlan(oldInbound)
  315. if perr != nil {
  316. return false, perr
  317. }
  318. if dirty {
  319. markDirty = true
  320. }
  321. // Persist client stats + inbound atomically, serialized against the traffic
  322. // poll to avoid the cross-transaction lock-order deadlock (runSerializedTx).
  323. if txErr := runSerializedTx(func(tx *gorm.DB) error {
  324. for i := range clients {
  325. if len(clients[i].Email) == 0 {
  326. continue
  327. }
  328. if e := inboundSvc.AddClientStat(tx, data.Id, &clients[i]); e != nil {
  329. return e
  330. }
  331. }
  332. if e := tx.Save(oldInbound).Error; e != nil {
  333. return e
  334. }
  335. finalClients, gcErr := inboundSvc.GetClients(oldInbound)
  336. if gcErr != nil {
  337. return gcErr
  338. }
  339. return s.SyncInbound(tx, oldInbound.Id, finalClients)
  340. }); txErr != nil {
  341. return false, txErr
  342. }
  343. // Apply to the running runtime after commit — outside the serialized writer
  344. // so a slow node call can't stall traffic accounting.
  345. if oldInbound.NodeID == nil {
  346. if !push {
  347. needRestart = true
  348. } else {
  349. for _, client := range clients {
  350. if len(client.Email) == 0 {
  351. needRestart = true
  352. continue
  353. }
  354. if !client.Enable {
  355. continue
  356. }
  357. cipher := ""
  358. if oldInbound.Protocol == "shadowsocks" {
  359. cipher = oldSettings["method"].(string)
  360. }
  361. err1 := rt.AddUser(context.Background(), oldInbound, map[string]any{
  362. "email": client.Email,
  363. "id": client.ID,
  364. "auth": client.Auth,
  365. "security": client.Security,
  366. "flow": client.Flow,
  367. "password": client.Password,
  368. "cipher": cipher,
  369. })
  370. if err1 == nil {
  371. logger.Debug("Client added on", rt.Name(), ":", client.Email)
  372. } else {
  373. logger.Debug("Error in adding client on", rt.Name(), ":", err1)
  374. needRestart = true
  375. }
  376. }
  377. }
  378. } else {
  379. // Large batches would be M sequential per-client RPCs; the inbound's saved
  380. // settings already hold the final set, so mark dirty and let one reconcile
  381. // push converge the node instead.
  382. if push && len(clients) > nodeBulkPushThreshold {
  383. markDirty = true
  384. push = false
  385. }
  386. for _, client := range clients {
  387. if push {
  388. if err1 := rt.AddClient(context.Background(), oldInbound, client); err1 != nil {
  389. logger.Warning("Error in adding client on", rt.Name(), ":", err1)
  390. markDirty = true
  391. push = false
  392. }
  393. }
  394. }
  395. }
  396. if markDirty && oldInbound.NodeID != nil {
  397. if dErr := (&NodeService{}).MarkNodeDirty(*oldInbound.NodeID); dErr != nil {
  398. logger.Warning("mark node dirty failed:", dErr)
  399. }
  400. }
  401. return needRestart, nil
  402. }
  403. func (s *ClientService) UpdateInboundClient(inboundSvc *InboundService, data *model.Inbound, oldEmail string) (bool, error) {
  404. defer lockInbound(data.Id).Unlock()
  405. clients, err := inboundSvc.GetClients(data)
  406. if err != nil {
  407. return false, err
  408. }
  409. var settings map[string]any
  410. err = json.Unmarshal([]byte(data.Settings), &settings)
  411. if err != nil {
  412. return false, err
  413. }
  414. interfaceClients := settings["clients"].([]any)
  415. oldInbound, err := inboundSvc.GetInbound(data.Id)
  416. if err != nil {
  417. return false, err
  418. }
  419. oldClients, err := inboundSvc.GetClients(oldInbound)
  420. if err != nil {
  421. return false, err
  422. }
  423. newClientId := ""
  424. switch oldInbound.Protocol {
  425. case "trojan":
  426. newClientId = clients[0].Password
  427. case "shadowsocks":
  428. newClientId = clients[0].Email
  429. case "hysteria":
  430. newClientId = clients[0].Auth
  431. default:
  432. newClientId = clients[0].ID
  433. }
  434. // Locate the client to replace by email — the client's stable identity.
  435. // Credentials (uuid/password/auth) can drift from the inbound JSON, so they
  436. // are never used for matching.
  437. clientIndex := -1
  438. for index, oldClient := range oldClients {
  439. if strings.EqualFold(oldClient.Email, oldEmail) {
  440. oldEmail = oldClient.Email
  441. clientIndex = index
  442. break
  443. }
  444. }
  445. if newClientId == "" || clientIndex == -1 {
  446. return false, common.NewError("empty client ID")
  447. }
  448. if strings.TrimSpace(clients[0].Email) == "" {
  449. return false, common.NewError("client email is required")
  450. }
  451. if clients[0].Email != oldEmail {
  452. existEmail, err := s.checkEmailsExistForClients(inboundSvc, clients, nil)
  453. if err != nil {
  454. return false, err
  455. }
  456. if existEmail != "" {
  457. return false, common.NewError("Duplicate email:", existEmail)
  458. }
  459. }
  460. var oldSettings map[string]any
  461. err = json.Unmarshal([]byte(oldInbound.Settings), &oldSettings)
  462. if err != nil {
  463. return false, err
  464. }
  465. settingsClients := oldSettings["clients"].([]any)
  466. var preservedCreated any
  467. var preservedSubID string
  468. if clientIndex >= 0 && clientIndex < len(settingsClients) {
  469. if oldMap, ok := settingsClients[clientIndex].(map[string]any); ok {
  470. if v, ok2 := oldMap["created_at"]; ok2 {
  471. preservedCreated = v
  472. }
  473. preservedSubID, _ = oldMap["subId"].(string)
  474. }
  475. }
  476. if len(interfaceClients) > 0 {
  477. if newMap, ok := interfaceClients[0].(map[string]any); ok {
  478. if preservedCreated == nil {
  479. preservedCreated = time.Now().Unix() * 1000
  480. }
  481. newMap["created_at"] = preservedCreated
  482. newMap["updated_at"] = time.Now().Unix() * 1000
  483. newSub, _ := newMap["subId"].(string)
  484. if strings.TrimSpace(newSub) == "" {
  485. if strings.TrimSpace(preservedSubID) != "" {
  486. newMap["subId"] = preservedSubID
  487. } else {
  488. newMap["subId"] = random.NumLower(16)
  489. }
  490. }
  491. interfaceClients[0] = newMap
  492. }
  493. }
  494. if oldInbound.Protocol == model.Shadowsocks {
  495. applyShadowsocksClientMethod(interfaceClients, oldSettings)
  496. }
  497. settingsClients[clientIndex] = interfaceClients[0]
  498. oldSettings["clients"] = settingsClients
  499. if oldInbound.Protocol == model.VLESS {
  500. hasVisionFlow := false
  501. for _, c := range settingsClients {
  502. cm, ok := c.(map[string]any)
  503. if !ok {
  504. continue
  505. }
  506. if flow, _ := cm["flow"].(string); flow == "xtls-rprx-vision" {
  507. hasVisionFlow = true
  508. break
  509. }
  510. }
  511. if !hasVisionFlow {
  512. delete(oldSettings, "testseed")
  513. }
  514. }
  515. newSettings, err := json.MarshalIndent(oldSettings, "", " ")
  516. if err != nil {
  517. return false, err
  518. }
  519. oldInbound.Settings = string(newSettings)
  520. needRestart := false
  521. markDirty := false
  522. // Resolve the push plan before the DB write so a node-state lookup failure
  523. // still aborts the whole update without committing anything (it used to roll
  524. // the transaction back). nodePushPlan only reads, so order doesn't matter.
  525. var rt runtime.Runtime
  526. var push bool
  527. if len(oldEmail) > 0 {
  528. var dirty bool
  529. var perr error
  530. rt, push, dirty, perr = inboundSvc.nodePushPlan(oldInbound)
  531. if perr != nil {
  532. return false, perr
  533. }
  534. if dirty {
  535. markDirty = true
  536. }
  537. }
  538. // Persist client stats + inbound atomically, serialized against the traffic
  539. // poll to avoid the cross-transaction lock-order deadlock (runSerializedTx).
  540. if txErr := runSerializedTx(func(tx *gorm.DB) error {
  541. if len(clients[0].Email) > 0 {
  542. if len(oldEmail) > 0 {
  543. emailUnchanged := strings.EqualFold(oldEmail, clients[0].Email)
  544. targetExists := int64(0)
  545. if !emailUnchanged {
  546. if e := tx.Model(xray.ClientTraffic{}).Where("email = ?", clients[0].Email).Count(&targetExists).Error; e != nil {
  547. return e
  548. }
  549. }
  550. if emailUnchanged || targetExists == 0 {
  551. if e := inboundSvc.UpdateClientStat(tx, oldEmail, &clients[0]); e != nil {
  552. return e
  553. }
  554. if e := inboundSvc.UpdateClientIPs(tx, oldEmail, clients[0].Email); e != nil {
  555. return e
  556. }
  557. } else {
  558. stillUsed, sErr := inboundSvc.emailUsedByOtherInbounds(oldEmail, data.Id)
  559. if sErr != nil {
  560. return sErr
  561. }
  562. if !stillUsed {
  563. if e := inboundSvc.DelClientStat(tx, oldEmail); e != nil {
  564. return e
  565. }
  566. if e := inboundSvc.DelClientIPs(tx, oldEmail); e != nil {
  567. return e
  568. }
  569. }
  570. if e := inboundSvc.UpdateClientStat(tx, clients[0].Email, &clients[0]); e != nil {
  571. return e
  572. }
  573. }
  574. } else {
  575. if e := inboundSvc.AddClientStat(tx, data.Id, &clients[0]); e != nil {
  576. return e
  577. }
  578. }
  579. } else {
  580. stillUsed, sErr := inboundSvc.emailUsedByOtherInbounds(oldEmail, data.Id)
  581. if sErr != nil {
  582. return sErr
  583. }
  584. if !stillUsed {
  585. if e := inboundSvc.DelClientStat(tx, oldEmail); e != nil {
  586. return e
  587. }
  588. if e := inboundSvc.DelClientIPs(tx, oldEmail); e != nil {
  589. return e
  590. }
  591. }
  592. }
  593. if e := tx.Save(oldInbound).Error; e != nil {
  594. return e
  595. }
  596. finalClients, gcErr := inboundSvc.GetClients(oldInbound)
  597. if gcErr != nil {
  598. return gcErr
  599. }
  600. return s.SyncInbound(tx, oldInbound.Id, finalClients)
  601. }); txErr != nil {
  602. return false, txErr
  603. }
  604. // Apply to the running runtime after the DB is committed — outside the
  605. // serialized writer so a slow node call can't stall traffic accounting.
  606. if len(oldEmail) > 0 {
  607. if oldInbound.NodeID == nil {
  608. if !push {
  609. needRestart = true
  610. } else {
  611. if oldClients[clientIndex].Enable {
  612. err1 := rt.RemoveUser(context.Background(), oldInbound, oldEmail)
  613. if err1 == nil {
  614. logger.Debug("Old client deleted on", rt.Name(), ":", oldEmail)
  615. } else if strings.Contains(err1.Error(), fmt.Sprintf("User %s not found.", oldEmail)) {
  616. logger.Debug("User is already deleted. Nothing to do more...")
  617. } else {
  618. logger.Debug("Error in deleting client on", rt.Name(), ":", err1)
  619. needRestart = true
  620. }
  621. }
  622. if clients[0].Enable {
  623. cipher := ""
  624. if oldInbound.Protocol == "shadowsocks" {
  625. cipher = oldSettings["method"].(string)
  626. }
  627. err1 := rt.AddUser(context.Background(), oldInbound, map[string]any{
  628. "email": clients[0].Email,
  629. "id": clients[0].ID,
  630. "security": clients[0].Security,
  631. "flow": clients[0].Flow,
  632. "auth": clients[0].Auth,
  633. "password": clients[0].Password,
  634. "cipher": cipher,
  635. })
  636. if err1 == nil {
  637. logger.Debug("Client edited on", rt.Name(), ":", clients[0].Email)
  638. } else {
  639. logger.Debug("Error in adding client on", rt.Name(), ":", err1)
  640. needRestart = true
  641. }
  642. }
  643. }
  644. } else if push {
  645. if err1 := rt.UpdateUser(context.Background(), oldInbound, oldEmail, clients[0]); err1 != nil {
  646. logger.Warning("Error in updating client on", rt.Name(), ":", err1)
  647. markDirty = true
  648. }
  649. }
  650. } else {
  651. logger.Debug("Client old email not found")
  652. needRestart = true
  653. }
  654. if markDirty && oldInbound.NodeID != nil {
  655. if dErr := (&NodeService{}).MarkNodeDirty(*oldInbound.NodeID); dErr != nil {
  656. logger.Warning("mark node dirty failed:", dErr)
  657. }
  658. }
  659. return needRestart, nil
  660. }
  661. func (s *ClientService) DelInboundClientByEmail(inboundSvc *InboundService, inboundId int, email string, keepTraffic bool) (bool, error) {
  662. defer lockInbound(inboundId).Unlock()
  663. oldInbound, err := inboundSvc.GetInbound(inboundId)
  664. if err != nil {
  665. logger.Error("Load Old Data Error")
  666. return false, err
  667. }
  668. var settings map[string]any
  669. if err := json.Unmarshal([]byte(oldInbound.Settings), &settings); err != nil {
  670. return false, err
  671. }
  672. interfaceClients, ok := settings["clients"].([]any)
  673. if !ok {
  674. return false, common.NewError("invalid clients format in inbound settings")
  675. }
  676. var newClients []any
  677. needApiDel := false
  678. found := false
  679. for _, client := range interfaceClients {
  680. c, ok := client.(map[string]any)
  681. if !ok {
  682. continue
  683. }
  684. if cEmail, ok := c["email"].(string); ok && cEmail == email {
  685. found = true
  686. needApiDel, _ = c["enable"].(bool)
  687. } else {
  688. newClients = append(newClients, client)
  689. }
  690. }
  691. if !found {
  692. return false, fmt.Errorf("%w for email: %s", ErrClientNotInInbound, email)
  693. }
  694. db := database.GetDB()
  695. newClients = compactOrphans(db, newClients)
  696. if newClients == nil {
  697. newClients = []any{}
  698. }
  699. settings["clients"] = newClients
  700. newSettings, err := json.MarshalIndent(settings, "", " ")
  701. if err != nil {
  702. return false, err
  703. }
  704. oldInbound.Settings = string(newSettings)
  705. emailShared, err := inboundSvc.emailUsedByOtherInbounds(email, inboundId)
  706. if err != nil {
  707. return false, err
  708. }
  709. needRestart := false
  710. markDirty := false
  711. // Decide what to delete and the push plan before the serialized DB write —
  712. // these are reads, and nodePushPlan failing should abort before committing.
  713. delStat := false
  714. if len(email) > 0 && !emailShared && !keepTraffic {
  715. traffic, tErr := inboundSvc.GetClientTrafficByEmail(email)
  716. if tErr != nil {
  717. return false, tErr
  718. }
  719. delStat = traffic != nil
  720. }
  721. // The runtime user is scoped to this inbound's tag + email, so the push plan
  722. // is resolved independently of emailShared — a sibling inbound still carrying
  723. // the email must not suppress removing the user from this inbound's Xray.
  724. var rt runtime.Runtime
  725. var push bool
  726. if len(email) > 0 && (oldInbound.NodeID != nil || needApiDel) {
  727. r, p, dirty, perr := inboundSvc.nodePushPlan(oldInbound)
  728. if perr != nil {
  729. return false, perr
  730. }
  731. rt, push = r, p
  732. if dirty {
  733. markDirty = true
  734. }
  735. }
  736. // Persist the deletion atomically, serialized against the traffic poll to
  737. // avoid the cross-transaction lock-order deadlock (runSerializedTx).
  738. if txErr := runSerializedTx(func(tx *gorm.DB) error {
  739. if !emailShared && !keepTraffic {
  740. if e := inboundSvc.DelClientIPs(tx, email); e != nil {
  741. logger.Error("Error in delete client IPs")
  742. return e
  743. }
  744. }
  745. if delStat {
  746. if e := inboundSvc.DelClientStat(tx, email); e != nil {
  747. logger.Error("Delete stats Data Error")
  748. return e
  749. }
  750. }
  751. if e := tx.Save(oldInbound).Error; e != nil {
  752. return e
  753. }
  754. finalClients, gcErr := inboundSvc.GetClients(oldInbound)
  755. if gcErr != nil {
  756. return gcErr
  757. }
  758. return s.SyncInbound(tx, inboundId, finalClients)
  759. }); txErr != nil {
  760. return false, txErr
  761. }
  762. // Apply the runtime delete after commit — outside the serialized writer so a
  763. // slow node call can't stall traffic accounting. Independent of emailShared:
  764. // Xray users are keyed by inbound tag, so the user must be removed from this
  765. // inbound's runtime even when the same email survives in another inbound.
  766. if len(email) > 0 {
  767. if oldInbound.NodeID == nil {
  768. // Local inbound: a disabled client isn't in the running Xray, so only
  769. // a live one (needApiDel) needs an API removal.
  770. if needApiDel {
  771. if !push {
  772. needRestart = true
  773. } else if err1 := rt.RemoveUser(context.Background(), oldInbound, email); err1 == nil {
  774. logger.Debug("Client deleted on", rt.Name(), ":", email)
  775. needRestart = false
  776. } else if strings.Contains(err1.Error(), fmt.Sprintf("User %s not found.", email)) {
  777. logger.Debug("User is already deleted. Nothing to do more...")
  778. } else {
  779. logger.Debug("Error in deleting client on", rt.Name(), ":", email)
  780. needRestart = true
  781. }
  782. }
  783. } else {
  784. // Node inbound: propagate the delete regardless of the enable flag —
  785. // the node's own DB still carries a disabled client and would
  786. // resurrect it on the next snapshot otherwise.
  787. if push {
  788. if err1 := rt.DeleteUser(context.Background(), oldInbound, email); err1 != nil {
  789. logger.Warning("Error in deleting client on", rt.Name(), ":", err1)
  790. markDirty = true
  791. }
  792. }
  793. }
  794. }
  795. if markDirty && oldInbound.NodeID != nil {
  796. if dErr := (&NodeService{}).MarkNodeDirty(*oldInbound.NodeID); dErr != nil {
  797. logger.Warning("mark node dirty failed:", dErr)
  798. }
  799. }
  800. return needRestart, nil
  801. }
  802. func (s *ClientService) SetClientTelegramUserID(inboundSvc *InboundService, trafficId int, tgId int64) (bool, error) {
  803. traffic, inbound, err := inboundSvc.GetClientInboundByTrafficID(trafficId)
  804. if err != nil {
  805. return false, err
  806. }
  807. if inbound == nil {
  808. return false, common.NewError("Inbound Not Found For Traffic ID:", trafficId)
  809. }
  810. clientEmail := traffic.Email
  811. oldClients, err := inboundSvc.GetClients(inbound)
  812. if err != nil {
  813. return false, err
  814. }
  815. found := false
  816. for _, oldClient := range oldClients {
  817. if oldClient.Email == clientEmail {
  818. found = true
  819. break
  820. }
  821. }
  822. if !found {
  823. return false, common.NewError("Client Not Found For Email:", clientEmail)
  824. }
  825. var settings map[string]any
  826. err = json.Unmarshal([]byte(inbound.Settings), &settings)
  827. if err != nil {
  828. return false, err
  829. }
  830. clients := settings["clients"].([]any)
  831. var newClients []any
  832. for client_index := range clients {
  833. c := clients[client_index].(map[string]any)
  834. if c["email"] == clientEmail {
  835. c["tgId"] = tgId
  836. c["updated_at"] = time.Now().Unix() * 1000
  837. newClients = append(newClients, any(c))
  838. }
  839. }
  840. settings["clients"] = newClients
  841. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  842. if err != nil {
  843. return false, err
  844. }
  845. inbound.Settings = string(modifiedSettings)
  846. needRestart, err := s.UpdateInboundClient(inboundSvc, inbound, clientEmail)
  847. return needRestart, err
  848. }
  849. func (s *ClientService) CheckIsEnabledByEmail(inboundSvc *InboundService, clientEmail string) (bool, error) {
  850. _, inbound, err := inboundSvc.GetClientInboundByEmail(clientEmail)
  851. if err != nil {
  852. return false, err
  853. }
  854. if inbound == nil {
  855. return false, common.NewError("Inbound Not Found For Email:", clientEmail)
  856. }
  857. clients, err := inboundSvc.GetClients(inbound)
  858. if err != nil {
  859. return false, err
  860. }
  861. isEnable := false
  862. for _, client := range clients {
  863. if client.Email == clientEmail {
  864. isEnable = client.Enable
  865. break
  866. }
  867. }
  868. return isEnable, err
  869. }
  870. func (s *ClientService) ToggleClientEnableByEmail(inboundSvc *InboundService, clientEmail string) (bool, bool, error) {
  871. _, inbound, err := inboundSvc.GetClientInboundByEmail(clientEmail)
  872. if err != nil {
  873. return false, false, err
  874. }
  875. if inbound == nil {
  876. return false, false, common.NewError("Inbound Not Found For Email:", clientEmail)
  877. }
  878. oldClients, err := inboundSvc.GetClients(inbound)
  879. if err != nil {
  880. return false, false, err
  881. }
  882. found := false
  883. clientOldEnabled := false
  884. for _, oldClient := range oldClients {
  885. if oldClient.Email == clientEmail {
  886. found = true
  887. clientOldEnabled = oldClient.Enable
  888. break
  889. }
  890. }
  891. if !found {
  892. return false, false, common.NewError("Client Not Found For Email:", clientEmail)
  893. }
  894. var settings map[string]any
  895. err = json.Unmarshal([]byte(inbound.Settings), &settings)
  896. if err != nil {
  897. return false, false, err
  898. }
  899. clients := settings["clients"].([]any)
  900. var newClients []any
  901. for client_index := range clients {
  902. c := clients[client_index].(map[string]any)
  903. if c["email"] == clientEmail {
  904. c["enable"] = !clientOldEnabled
  905. c["updated_at"] = time.Now().Unix() * 1000
  906. newClients = append(newClients, any(c))
  907. }
  908. }
  909. settings["clients"] = newClients
  910. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  911. if err != nil {
  912. return false, false, err
  913. }
  914. inbound.Settings = string(modifiedSettings)
  915. needRestart, err := s.UpdateInboundClient(inboundSvc, inbound, clientEmail)
  916. if err != nil {
  917. return false, needRestart, err
  918. }
  919. return !clientOldEnabled, needRestart, nil
  920. }
  921. func (s *ClientService) SetClientEnableByEmail(inboundSvc *InboundService, clientEmail string, enable bool) (bool, bool, error) {
  922. current, err := s.CheckIsEnabledByEmail(inboundSvc, clientEmail)
  923. if err != nil {
  924. return false, false, err
  925. }
  926. if current == enable {
  927. return false, false, nil
  928. }
  929. newEnabled, needRestart, err := s.ToggleClientEnableByEmail(inboundSvc, clientEmail)
  930. if err != nil {
  931. return false, needRestart, err
  932. }
  933. return newEnabled == enable, needRestart, nil
  934. }
  935. // applyClientFieldByEmail loads the inbound currently hosting clientEmail,
  936. // confirms the client exists, applies mutate to the matching client (plus a
  937. // refreshed updated_at), and hands a single-client update payload to
  938. // UpdateInboundClient. The rebuilt clients array intentionally contains only
  939. // the matched client — that is the input contract UpdateInboundClient expects
  940. // (clients[0] is the new data; clientEmail locates the row to replace). It
  941. // backs the single-field by-email setters below.
  942. // applyClientFieldByEmail mutates a client field on every inbound the email is
  943. // attached to. A multi-inbound client is one logical identity: patching only
  944. // the first inbound's JSON would leave the siblings stale, and the next
  945. // SyncInbound over a stale sibling would revert the edit in the normalized
  946. // records (#5039).
  947. func (s *ClientService) applyClientFieldByEmail(inboundSvc *InboundService, clientEmail string, mutate func(c map[string]any)) (bool, error) {
  948. inboundIds, err := s.GetInboundIdsForEmail(database.GetDB(), clientEmail)
  949. if err != nil {
  950. return false, err
  951. }
  952. if len(inboundIds) == 0 {
  953. // Legacy fallback for clients that only live in the inbound JSON and
  954. // were never normalized into client_inbounds.
  955. _, inbound, gErr := inboundSvc.GetClientInboundByEmail(clientEmail)
  956. if gErr != nil {
  957. return false, gErr
  958. }
  959. if inbound == nil {
  960. return false, common.NewError("Inbound Not Found For Email:", clientEmail)
  961. }
  962. inboundIds = []int{inbound.Id}
  963. }
  964. needRestart := false
  965. found := false
  966. for _, ibId := range inboundIds {
  967. inbound, gErr := inboundSvc.GetInbound(ibId)
  968. if gErr != nil {
  969. return needRestart, gErr
  970. }
  971. var settings map[string]any
  972. if uErr := json.Unmarshal([]byte(inbound.Settings), &settings); uErr != nil {
  973. return needRestart, uErr
  974. }
  975. clients, _ := settings["clients"].([]any)
  976. // UpdateInboundClient expects a single-client payload, so keep only the
  977. // matching entry in the scratch copy; it splices the result back into
  978. // the inbound's full client list itself.
  979. var newClients []any
  980. for client_index := range clients {
  981. c, ok := clients[client_index].(map[string]any)
  982. if !ok {
  983. continue
  984. }
  985. if c["email"] == clientEmail {
  986. mutate(c)
  987. c["updated_at"] = time.Now().Unix() * 1000
  988. newClients = append(newClients, any(c))
  989. }
  990. }
  991. if len(newClients) == 0 {
  992. continue
  993. }
  994. found = true
  995. settings["clients"] = newClients
  996. modifiedSettings, mErr := json.MarshalIndent(settings, "", " ")
  997. if mErr != nil {
  998. return needRestart, mErr
  999. }
  1000. inbound.Settings = string(modifiedSettings)
  1001. nr, uErr := s.UpdateInboundClient(inboundSvc, inbound, clientEmail)
  1002. if uErr != nil {
  1003. return needRestart, uErr
  1004. }
  1005. needRestart = needRestart || nr
  1006. }
  1007. if !found {
  1008. return needRestart, common.NewError("Client Not Found For Email:", clientEmail)
  1009. }
  1010. return needRestart, nil
  1011. }
  1012. func (s *ClientService) ResetClientIpLimitByEmail(inboundSvc *InboundService, clientEmail string, count int) (bool, error) {
  1013. return s.applyClientFieldByEmail(inboundSvc, clientEmail, func(c map[string]any) {
  1014. c["limitIp"] = count
  1015. })
  1016. }
  1017. func (s *ClientService) ResetClientExpiryTimeByEmail(inboundSvc *InboundService, clientEmail string, expiry_time int64) (bool, error) {
  1018. return s.applyClientFieldByEmail(inboundSvc, clientEmail, func(c map[string]any) {
  1019. c["expiryTime"] = expiry_time
  1020. })
  1021. }
  1022. func (s *ClientService) ResetClientTrafficLimitByEmail(inboundSvc *InboundService, clientEmail string, totalGB int) (bool, error) {
  1023. if totalGB < 0 {
  1024. return false, common.NewError("totalGB must be >= 0")
  1025. }
  1026. return s.applyClientFieldByEmail(inboundSvc, clientEmail, func(c map[string]any) {
  1027. c["totalGB"] = totalGB * 1024 * 1024 * 1024
  1028. })
  1029. }