client_inbound_apply.go 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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. var rt runtime.Runtime
  722. var push bool
  723. if len(email) > 0 && !emailShared && (oldInbound.NodeID != nil || needApiDel) {
  724. r, p, dirty, perr := inboundSvc.nodePushPlan(oldInbound)
  725. if perr != nil {
  726. return false, perr
  727. }
  728. rt, push = r, p
  729. if dirty {
  730. markDirty = true
  731. }
  732. }
  733. // Persist the deletion atomically, serialized against the traffic poll to
  734. // avoid the cross-transaction lock-order deadlock (runSerializedTx).
  735. if txErr := runSerializedTx(func(tx *gorm.DB) error {
  736. if !emailShared && !keepTraffic {
  737. if e := inboundSvc.DelClientIPs(tx, email); e != nil {
  738. logger.Error("Error in delete client IPs")
  739. return e
  740. }
  741. }
  742. if delStat {
  743. if e := inboundSvc.DelClientStat(tx, email); e != nil {
  744. logger.Error("Delete stats Data Error")
  745. return e
  746. }
  747. }
  748. if e := tx.Save(oldInbound).Error; e != nil {
  749. return e
  750. }
  751. finalClients, gcErr := inboundSvc.GetClients(oldInbound)
  752. if gcErr != nil {
  753. return gcErr
  754. }
  755. return s.SyncInbound(tx, inboundId, finalClients)
  756. }); txErr != nil {
  757. return false, txErr
  758. }
  759. // Apply the runtime delete after commit — outside the serialized writer so a
  760. // slow node call can't stall traffic accounting.
  761. if len(email) > 0 && !emailShared {
  762. if oldInbound.NodeID == nil {
  763. // Local inbound: a disabled client isn't in the running Xray, so only
  764. // a live one (needApiDel) needs an API removal.
  765. if needApiDel {
  766. if !push {
  767. needRestart = true
  768. } else if err1 := rt.RemoveUser(context.Background(), oldInbound, email); err1 == nil {
  769. logger.Debug("Client deleted on", rt.Name(), ":", email)
  770. needRestart = false
  771. } else if strings.Contains(err1.Error(), fmt.Sprintf("User %s not found.", email)) {
  772. logger.Debug("User is already deleted. Nothing to do more...")
  773. } else {
  774. logger.Debug("Error in deleting client on", rt.Name(), ":", email)
  775. needRestart = true
  776. }
  777. }
  778. } else {
  779. // Node inbound: propagate the delete regardless of the enable flag —
  780. // the node's own DB still carries a disabled client and would
  781. // resurrect it on the next snapshot otherwise.
  782. if push {
  783. if err1 := rt.DeleteUser(context.Background(), oldInbound, email); err1 != nil {
  784. logger.Warning("Error in deleting client on", rt.Name(), ":", err1)
  785. markDirty = true
  786. }
  787. }
  788. }
  789. }
  790. if markDirty && oldInbound.NodeID != nil {
  791. if dErr := (&NodeService{}).MarkNodeDirty(*oldInbound.NodeID); dErr != nil {
  792. logger.Warning("mark node dirty failed:", dErr)
  793. }
  794. }
  795. return needRestart, nil
  796. }
  797. func (s *ClientService) SetClientTelegramUserID(inboundSvc *InboundService, trafficId int, tgId int64) (bool, error) {
  798. traffic, inbound, err := inboundSvc.GetClientInboundByTrafficID(trafficId)
  799. if err != nil {
  800. return false, err
  801. }
  802. if inbound == nil {
  803. return false, common.NewError("Inbound Not Found For Traffic ID:", trafficId)
  804. }
  805. clientEmail := traffic.Email
  806. oldClients, err := inboundSvc.GetClients(inbound)
  807. if err != nil {
  808. return false, err
  809. }
  810. found := false
  811. for _, oldClient := range oldClients {
  812. if oldClient.Email == clientEmail {
  813. found = true
  814. break
  815. }
  816. }
  817. if !found {
  818. return false, common.NewError("Client Not Found For Email:", clientEmail)
  819. }
  820. var settings map[string]any
  821. err = json.Unmarshal([]byte(inbound.Settings), &settings)
  822. if err != nil {
  823. return false, err
  824. }
  825. clients := settings["clients"].([]any)
  826. var newClients []any
  827. for client_index := range clients {
  828. c := clients[client_index].(map[string]any)
  829. if c["email"] == clientEmail {
  830. c["tgId"] = tgId
  831. c["updated_at"] = time.Now().Unix() * 1000
  832. newClients = append(newClients, any(c))
  833. }
  834. }
  835. settings["clients"] = newClients
  836. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  837. if err != nil {
  838. return false, err
  839. }
  840. inbound.Settings = string(modifiedSettings)
  841. needRestart, err := s.UpdateInboundClient(inboundSvc, inbound, clientEmail)
  842. return needRestart, err
  843. }
  844. func (s *ClientService) CheckIsEnabledByEmail(inboundSvc *InboundService, clientEmail string) (bool, error) {
  845. _, inbound, err := inboundSvc.GetClientInboundByEmail(clientEmail)
  846. if err != nil {
  847. return false, err
  848. }
  849. if inbound == nil {
  850. return false, common.NewError("Inbound Not Found For Email:", clientEmail)
  851. }
  852. clients, err := inboundSvc.GetClients(inbound)
  853. if err != nil {
  854. return false, err
  855. }
  856. isEnable := false
  857. for _, client := range clients {
  858. if client.Email == clientEmail {
  859. isEnable = client.Enable
  860. break
  861. }
  862. }
  863. return isEnable, err
  864. }
  865. func (s *ClientService) ToggleClientEnableByEmail(inboundSvc *InboundService, clientEmail string) (bool, bool, error) {
  866. _, inbound, err := inboundSvc.GetClientInboundByEmail(clientEmail)
  867. if err != nil {
  868. return false, false, err
  869. }
  870. if inbound == nil {
  871. return false, false, common.NewError("Inbound Not Found For Email:", clientEmail)
  872. }
  873. oldClients, err := inboundSvc.GetClients(inbound)
  874. if err != nil {
  875. return false, false, err
  876. }
  877. found := false
  878. clientOldEnabled := false
  879. for _, oldClient := range oldClients {
  880. if oldClient.Email == clientEmail {
  881. found = true
  882. clientOldEnabled = oldClient.Enable
  883. break
  884. }
  885. }
  886. if !found {
  887. return false, false, common.NewError("Client Not Found For Email:", clientEmail)
  888. }
  889. var settings map[string]any
  890. err = json.Unmarshal([]byte(inbound.Settings), &settings)
  891. if err != nil {
  892. return false, false, err
  893. }
  894. clients := settings["clients"].([]any)
  895. var newClients []any
  896. for client_index := range clients {
  897. c := clients[client_index].(map[string]any)
  898. if c["email"] == clientEmail {
  899. c["enable"] = !clientOldEnabled
  900. c["updated_at"] = time.Now().Unix() * 1000
  901. newClients = append(newClients, any(c))
  902. }
  903. }
  904. settings["clients"] = newClients
  905. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  906. if err != nil {
  907. return false, false, err
  908. }
  909. inbound.Settings = string(modifiedSettings)
  910. needRestart, err := s.UpdateInboundClient(inboundSvc, inbound, clientEmail)
  911. if err != nil {
  912. return false, needRestart, err
  913. }
  914. return !clientOldEnabled, needRestart, nil
  915. }
  916. func (s *ClientService) SetClientEnableByEmail(inboundSvc *InboundService, clientEmail string, enable bool) (bool, bool, error) {
  917. current, err := s.CheckIsEnabledByEmail(inboundSvc, clientEmail)
  918. if err != nil {
  919. return false, false, err
  920. }
  921. if current == enable {
  922. return false, false, nil
  923. }
  924. newEnabled, needRestart, err := s.ToggleClientEnableByEmail(inboundSvc, clientEmail)
  925. if err != nil {
  926. return false, needRestart, err
  927. }
  928. return newEnabled == enable, needRestart, nil
  929. }
  930. // applyClientFieldByEmail loads the inbound currently hosting clientEmail,
  931. // confirms the client exists, applies mutate to the matching client (plus a
  932. // refreshed updated_at), and hands a single-client update payload to
  933. // UpdateInboundClient. The rebuilt clients array intentionally contains only
  934. // the matched client — that is the input contract UpdateInboundClient expects
  935. // (clients[0] is the new data; clientEmail locates the row to replace). It
  936. // backs the single-field by-email setters below.
  937. // applyClientFieldByEmail mutates a client field on every inbound the email is
  938. // attached to. A multi-inbound client is one logical identity: patching only
  939. // the first inbound's JSON would leave the siblings stale, and the next
  940. // SyncInbound over a stale sibling would revert the edit in the normalized
  941. // records (#5039).
  942. func (s *ClientService) applyClientFieldByEmail(inboundSvc *InboundService, clientEmail string, mutate func(c map[string]any)) (bool, error) {
  943. inboundIds, err := s.GetInboundIdsForEmail(database.GetDB(), clientEmail)
  944. if err != nil {
  945. return false, err
  946. }
  947. if len(inboundIds) == 0 {
  948. // Legacy fallback for clients that only live in the inbound JSON and
  949. // were never normalized into client_inbounds.
  950. _, inbound, gErr := inboundSvc.GetClientInboundByEmail(clientEmail)
  951. if gErr != nil {
  952. return false, gErr
  953. }
  954. if inbound == nil {
  955. return false, common.NewError("Inbound Not Found For Email:", clientEmail)
  956. }
  957. inboundIds = []int{inbound.Id}
  958. }
  959. needRestart := false
  960. found := false
  961. for _, ibId := range inboundIds {
  962. inbound, gErr := inboundSvc.GetInbound(ibId)
  963. if gErr != nil {
  964. return needRestart, gErr
  965. }
  966. var settings map[string]any
  967. if uErr := json.Unmarshal([]byte(inbound.Settings), &settings); uErr != nil {
  968. return needRestart, uErr
  969. }
  970. clients, _ := settings["clients"].([]any)
  971. // UpdateInboundClient expects a single-client payload, so keep only the
  972. // matching entry in the scratch copy; it splices the result back into
  973. // the inbound's full client list itself.
  974. var newClients []any
  975. for client_index := range clients {
  976. c, ok := clients[client_index].(map[string]any)
  977. if !ok {
  978. continue
  979. }
  980. if c["email"] == clientEmail {
  981. mutate(c)
  982. c["updated_at"] = time.Now().Unix() * 1000
  983. newClients = append(newClients, any(c))
  984. }
  985. }
  986. if len(newClients) == 0 {
  987. continue
  988. }
  989. found = true
  990. settings["clients"] = newClients
  991. modifiedSettings, mErr := json.MarshalIndent(settings, "", " ")
  992. if mErr != nil {
  993. return needRestart, mErr
  994. }
  995. inbound.Settings = string(modifiedSettings)
  996. nr, uErr := s.UpdateInboundClient(inboundSvc, inbound, clientEmail)
  997. if uErr != nil {
  998. return needRestart, uErr
  999. }
  1000. needRestart = needRestart || nr
  1001. }
  1002. if !found {
  1003. return needRestart, common.NewError("Client Not Found For Email:", clientEmail)
  1004. }
  1005. return needRestart, nil
  1006. }
  1007. func (s *ClientService) ResetClientIpLimitByEmail(inboundSvc *InboundService, clientEmail string, count int) (bool, error) {
  1008. return s.applyClientFieldByEmail(inboundSvc, clientEmail, func(c map[string]any) {
  1009. c["limitIp"] = count
  1010. })
  1011. }
  1012. func (s *ClientService) ResetClientExpiryTimeByEmail(inboundSvc *InboundService, clientEmail string, expiry_time int64) (bool, error) {
  1013. return s.applyClientFieldByEmail(inboundSvc, clientEmail, func(c map[string]any) {
  1014. c["expiryTime"] = expiry_time
  1015. })
  1016. }
  1017. func (s *ClientService) ResetClientTrafficLimitByEmail(inboundSvc *InboundService, clientEmail string, totalGB int) (bool, error) {
  1018. if totalGB < 0 {
  1019. return false, common.NewError("totalGB must be >= 0")
  1020. }
  1021. return s.applyClientFieldByEmail(inboundSvc, clientEmail, func(c map[string]any) {
  1022. c["totalGB"] = totalGB * 1024 * 1024 * 1024
  1023. })
  1024. }