inbound.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. // Package service provides business logic services for the 3x-ui web panel,
  2. // including inbound/outbound management, user administration, settings, and Xray integration.
  3. package service
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "sort"
  9. "strings"
  10. "time"
  11. "github.com/mhsanaei/3x-ui/v3/internal/database"
  12. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  13. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  14. "github.com/mhsanaei/3x-ui/v3/internal/util/common"
  15. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  16. "gorm.io/gorm"
  17. )
  18. type InboundService struct {
  19. xrayApi xray.XrayAPI
  20. clientService ClientService
  21. fallbackService FallbackService
  22. }
  23. // GetInbounds retrieves all inbounds for a specific user with client stats.
  24. func (s *InboundService) GetInbounds(userId int) ([]*model.Inbound, error) {
  25. db := database.GetDB()
  26. var inbounds []*model.Inbound
  27. err := db.Model(model.Inbound{}).Preload("ClientStats").Where("user_id = ?", userId).Order("id ASC").Find(&inbounds).Error
  28. if err != nil && err != gorm.ErrRecordNotFound {
  29. return nil, err
  30. }
  31. s.enrichClientStats(db, inbounds)
  32. s.annotateFallbackParents(db, inbounds)
  33. s.annotateLocalOriginGuid(inbounds)
  34. return inbounds, nil
  35. }
  36. // annotateLocalOriginGuid fills OriginNodeGuid for this panel's OWN inbounds
  37. // (NodeID == nil) with the panel's stable GUID; inbounds synced from a node
  38. // already carry the originating node's GUID. Read-time only (not persisted) so
  39. // the per-inbound online view can scope by GUID uniformly across a chain of
  40. // nodes (#4983).
  41. func (s *InboundService) annotateLocalOriginGuid(inbounds []*model.Inbound) {
  42. if len(inbounds) == 0 {
  43. return
  44. }
  45. guid := s.panelGuid()
  46. if guid == "" {
  47. return
  48. }
  49. for _, ib := range inbounds {
  50. if ib.OriginNodeGuid == "" && ib.NodeID == nil {
  51. ib.OriginNodeGuid = guid
  52. }
  53. }
  54. }
  55. // GetInboundsSlim returns the same list of inbounds as GetInbounds but
  56. // strips every per-client field other than email / enable / comment from
  57. // settings.clients and skips UUID/SubId enrichment on ClientStats. The
  58. // inbounds page only needs those three to roll up client counts and
  59. // render badges, so this trims tens of bytes per client (UUID, password,
  60. // flow, security, totalGB, expiryTime, limitIp, tgId, ...) which adds
  61. // up fast on installs with thousands of clients.
  62. //
  63. // Full client data is still available through GET /panel/api/inbounds/get/:id
  64. // for the edit/info/qr/export/clone flows that need it.
  65. func (s *InboundService) GetInboundsSlim(userId int) ([]*model.Inbound, error) {
  66. db := database.GetDB()
  67. var inbounds []*model.Inbound
  68. err := db.Model(model.Inbound{}).Preload("ClientStats").Where("user_id = ?", userId).Order("id ASC").Find(&inbounds).Error
  69. if err != nil && err != gorm.ErrRecordNotFound {
  70. return nil, err
  71. }
  72. s.annotateFallbackParents(db, inbounds)
  73. s.annotateLocalOriginGuid(inbounds)
  74. for _, ib := range inbounds {
  75. ib.Settings = slimSettingsClients(ib.Settings)
  76. }
  77. return inbounds, nil
  78. }
  79. // slimSettingsClients rewrites the inbound settings JSON so settings.clients[]
  80. // keeps only the fields the list view actually reads. Returns the input
  81. // unchanged when the JSON can't be parsed or has no clients array.
  82. func slimSettingsClients(settings string) string {
  83. if settings == "" {
  84. return settings
  85. }
  86. var raw map[string]any
  87. if err := json.Unmarshal([]byte(settings), &raw); err != nil {
  88. return settings
  89. }
  90. clients, ok := raw["clients"].([]any)
  91. if !ok || len(clients) == 0 {
  92. return settings
  93. }
  94. slim := make([]any, 0, len(clients))
  95. for _, entry := range clients {
  96. c, ok := entry.(map[string]any)
  97. if !ok {
  98. continue
  99. }
  100. row := make(map[string]any, 3)
  101. if v, ok := c["email"]; ok {
  102. row["email"] = v
  103. }
  104. if v, ok := c["enable"]; ok {
  105. row["enable"] = v
  106. }
  107. if v, ok := c["comment"]; ok && v != "" {
  108. row["comment"] = v
  109. }
  110. slim = append(slim, row)
  111. }
  112. raw["clients"] = slim
  113. out, err := json.Marshal(raw)
  114. if err != nil {
  115. return settings
  116. }
  117. return string(out)
  118. }
  119. // annotateFallbackParents fills FallbackParent on each inbound that is
  120. // the child side of a fallback rule. One DB round-trip serves the full
  121. // list — the frontend needs this to rewrite the child's client-share
  122. // link so it points at the master's reachable endpoint.
  123. func (s *InboundService) annotateFallbackParents(db *gorm.DB, inbounds []*model.Inbound) {
  124. if len(inbounds) == 0 {
  125. return
  126. }
  127. childIds := make([]int, 0, len(inbounds))
  128. for _, ib := range inbounds {
  129. childIds = append(childIds, ib.Id)
  130. }
  131. var rows []model.InboundFallback
  132. if err := db.Where("child_id IN ?", childIds).
  133. Order("sort_order ASC, id ASC").
  134. Find(&rows).Error; err != nil {
  135. return
  136. }
  137. first := make(map[int]model.InboundFallback, len(rows))
  138. for _, r := range rows {
  139. if _, ok := first[r.ChildId]; !ok {
  140. first[r.ChildId] = r
  141. }
  142. }
  143. for _, ib := range inbounds {
  144. if r, ok := first[ib.Id]; ok {
  145. ib.FallbackParent = &model.FallbackParentInfo{
  146. MasterId: r.MasterId,
  147. Path: r.Path,
  148. }
  149. }
  150. }
  151. }
  152. type InboundOption struct {
  153. Id int `json:"id" example:"1"`
  154. Remark string `json:"remark" example:"VLESS-443"`
  155. Tag string `json:"tag" example:"in-443-tcp"`
  156. Protocol string `json:"protocol" example:"vless"`
  157. Port int `json:"port" example:"443"`
  158. TlsFlowCapable bool `json:"tlsFlowCapable" example:"true"`
  159. SsMethod string `json:"ssMethod"`
  160. }
  161. func (s *InboundService) GetInboundOptions(userId int) ([]InboundOption, error) {
  162. db := database.GetDB()
  163. var rows []struct {
  164. Id int `gorm:"column:id"`
  165. Remark string `gorm:"column:remark"`
  166. Tag string `gorm:"column:tag"`
  167. Protocol string `gorm:"column:protocol"`
  168. Port int `gorm:"column:port"`
  169. StreamSettings string `gorm:"column:stream_settings"`
  170. Settings string `gorm:"column:settings"`
  171. }
  172. err := db.Table("inbounds").
  173. Select("id, remark, tag, protocol, port, stream_settings, settings").
  174. Where("user_id = ?", userId).
  175. Order("id ASC").
  176. Scan(&rows).Error
  177. if err != nil && err != gorm.ErrRecordNotFound {
  178. return nil, err
  179. }
  180. out := make([]InboundOption, 0, len(rows))
  181. for _, r := range rows {
  182. out = append(out, InboundOption{
  183. Id: r.Id,
  184. Remark: r.Remark,
  185. Tag: r.Tag,
  186. Protocol: r.Protocol,
  187. Port: r.Port,
  188. TlsFlowCapable: inboundCanEnableTlsFlow(r.Protocol, r.StreamSettings),
  189. SsMethod: inboundShadowsocksMethod(r.Protocol, r.Settings),
  190. })
  191. }
  192. return out, nil
  193. }
  194. // GetAllInbounds retrieves all inbounds with client stats.
  195. func (s *InboundService) GetAllInbounds() ([]*model.Inbound, error) {
  196. db := database.GetDB()
  197. var inbounds []*model.Inbound
  198. err := db.Model(model.Inbound{}).Preload("ClientStats").Find(&inbounds).Error
  199. if err != nil && err != gorm.ErrRecordNotFound {
  200. return nil, err
  201. }
  202. s.enrichClientStats(db, inbounds)
  203. return inbounds, nil
  204. }
  205. func (s *InboundService) GetInboundsByTrafficReset(period string) ([]*model.Inbound, error) {
  206. db := database.GetDB()
  207. var inbounds []*model.Inbound
  208. err := db.Model(model.Inbound{}).Where("traffic_reset = ?", period).Find(&inbounds).Error
  209. if err != nil && err != gorm.ErrRecordNotFound {
  210. return nil, err
  211. }
  212. return inbounds, nil
  213. }
  214. func (s *InboundService) GetClients(inbound *model.Inbound) ([]model.Client, error) {
  215. settings := map[string][]model.Client{}
  216. json.Unmarshal([]byte(inbound.Settings), &settings)
  217. if settings == nil {
  218. return nil, fmt.Errorf("setting is null")
  219. }
  220. clients := settings["clients"]
  221. if clients == nil {
  222. return nil, nil
  223. }
  224. return clients, nil
  225. }
  226. func (s *InboundService) GetAllEmails() ([]string, error) {
  227. db := database.GetDB()
  228. var emails []string
  229. query := fmt.Sprintf(
  230. "SELECT DISTINCT %s %s",
  231. database.JSONFieldText("client.value", "email"),
  232. database.JSONClientsFromInbound(),
  233. )
  234. if err := db.Raw(query).Scan(&emails).Error; err != nil {
  235. return nil, err
  236. }
  237. return emails, nil
  238. }
  239. // getAllEmailSubIDs returns email→subId. An email seen with two different
  240. // non-empty subIds is locked (mapped to "") so neither identity can claim it.
  241. func (s *InboundService) getAllEmailSubIDs() (map[string]string, error) {
  242. db := database.GetDB()
  243. var rows []struct {
  244. Email string
  245. SubID string
  246. }
  247. query := fmt.Sprintf(
  248. "SELECT %s AS email, %s AS sub_id %s",
  249. database.JSONFieldText("client.value", "email"),
  250. database.JSONFieldText("client.value", "subId"),
  251. database.JSONClientsFromInbound(),
  252. )
  253. if err := db.Raw(query).Scan(&rows).Error; err != nil {
  254. return nil, err
  255. }
  256. result := make(map[string]string, len(rows))
  257. for _, r := range rows {
  258. email := strings.ToLower(r.Email)
  259. if email == "" {
  260. continue
  261. }
  262. subID := r.SubID
  263. if existing, ok := result[email]; ok {
  264. if existing != subID {
  265. result[email] = ""
  266. }
  267. continue
  268. }
  269. result[email] = subID
  270. }
  271. return result, nil
  272. }
  273. // normalizeStreamSettings clears StreamSettings for protocols that don't use it.
  274. // Only vmess, vless, trojan, shadowsocks, hysteria, and wireguard protocols use
  275. // streamSettings (wireguard for finalmask UDP masks and sockopt on its listener).
  276. func (s *InboundService) normalizeStreamSettings(inbound *model.Inbound) {
  277. protocolsWithStream := map[model.Protocol]bool{
  278. model.VMESS: true,
  279. model.VLESS: true,
  280. model.Trojan: true,
  281. model.Shadowsocks: true,
  282. model.Hysteria: true,
  283. model.WireGuard: true,
  284. }
  285. if !protocolsWithStream[inbound.Protocol] {
  286. inbound.StreamSettings = ""
  287. }
  288. }
  289. // normalizeMtprotoSecret rebuilds an mtproto inbound's FakeTLS secret so it is
  290. // always valid and matches the configured domain before the row is persisted.
  291. func (s *InboundService) normalizeMtprotoSecret(inbound *model.Inbound) {
  292. if inbound.Protocol != model.MTProto {
  293. return
  294. }
  295. if healed, ok := model.HealMtprotoSecret(inbound.Settings); ok {
  296. inbound.Settings = healed
  297. }
  298. }
  299. // AddInbound creates a new inbound configuration.
  300. // It validates port uniqueness, client email uniqueness, and required fields,
  301. // then saves the inbound to the database and optionally adds it to the running Xray instance.
  302. // Returns the created inbound, whether Xray needs restart, and any error.
  303. func (s *InboundService) AddInbound(inbound *model.Inbound) (*model.Inbound, bool, error) {
  304. // Normalize streamSettings based on protocol
  305. s.normalizeStreamSettings(inbound)
  306. s.normalizeMtprotoSecret(inbound)
  307. conflict, err := s.checkPortConflict(inbound, 0)
  308. if err != nil {
  309. return inbound, false, err
  310. }
  311. if conflict != nil {
  312. return inbound, false, common.NewError(conflict.String())
  313. }
  314. inbound.Tag, err = s.resolveInboundTag(inbound, 0)
  315. if err != nil {
  316. return inbound, false, err
  317. }
  318. clients, err := s.GetClients(inbound)
  319. if err != nil {
  320. return inbound, false, err
  321. }
  322. existEmail, err := s.clientService.checkEmailsExistForClients(s, clients, nil)
  323. if err != nil {
  324. return inbound, false, err
  325. }
  326. if existEmail != "" {
  327. return inbound, false, common.NewError("Duplicate email:", existEmail)
  328. }
  329. // Ensure created_at and updated_at on clients in settings
  330. if len(clients) > 0 {
  331. var settings map[string]any
  332. if err2 := json.Unmarshal([]byte(inbound.Settings), &settings); err2 == nil && settings != nil {
  333. now := time.Now().Unix() * 1000
  334. updatedClients := make([]model.Client, 0, len(clients))
  335. for _, c := range clients {
  336. if c.CreatedAt == 0 {
  337. c.CreatedAt = now
  338. }
  339. c.UpdatedAt = now
  340. updatedClients = append(updatedClients, c)
  341. }
  342. settings["clients"] = updatedClients
  343. if bs, err3 := json.MarshalIndent(settings, "", " "); err3 == nil {
  344. inbound.Settings = string(bs)
  345. } else {
  346. logger.Debug("Unable to marshal inbound settings with timestamps:", err3)
  347. }
  348. } else if err2 != nil {
  349. logger.Debug("Unable to parse inbound settings for timestamps:", err2)
  350. }
  351. }
  352. // Secure client ID
  353. for _, client := range clients {
  354. switch inbound.Protocol {
  355. case "trojan":
  356. if client.Password == "" {
  357. return inbound, false, common.NewError("empty client ID")
  358. }
  359. case "shadowsocks":
  360. if client.Email == "" {
  361. return inbound, false, common.NewError("empty client ID")
  362. }
  363. case "hysteria":
  364. if client.Auth == "" {
  365. return inbound, false, common.NewError("empty client ID")
  366. }
  367. default:
  368. if client.ID == "" {
  369. return inbound, false, common.NewError("empty client ID")
  370. }
  371. }
  372. }
  373. db := database.GetDB()
  374. tx := db.Begin()
  375. markDirty := false
  376. defer func() {
  377. if err != nil {
  378. tx.Rollback()
  379. return
  380. }
  381. tx.Commit()
  382. if markDirty && inbound.NodeID != nil {
  383. if dErr := (&NodeService{}).MarkNodeDirty(*inbound.NodeID); dErr != nil {
  384. logger.Warning("mark node dirty failed:", dErr)
  385. }
  386. }
  387. }()
  388. err = tx.Save(inbound).Error
  389. if err == nil {
  390. if len(inbound.ClientStats) == 0 {
  391. for _, client := range clients {
  392. s.AddClientStat(tx, inbound.Id, &client)
  393. }
  394. }
  395. } else {
  396. return inbound, false, err
  397. }
  398. if err = s.clientService.SyncInbound(tx, inbound.Id, clients); err != nil {
  399. return inbound, false, err
  400. }
  401. needRestart := false
  402. if inbound.Enable {
  403. rt, push, dirty, perr := s.nodePushPlan(inbound)
  404. if perr != nil {
  405. err = perr
  406. return inbound, false, err
  407. }
  408. if dirty {
  409. markDirty = true
  410. }
  411. if push {
  412. if err1 := rt.AddInbound(context.Background(), inbound); err1 == nil {
  413. logger.Debug("New inbound added on", rt.Name(), ":", inbound.Tag)
  414. } else {
  415. logger.Debug("Unable to add inbound on", rt.Name(), ":", err1)
  416. if inbound.NodeID != nil {
  417. markDirty = true
  418. } else {
  419. needRestart = true
  420. }
  421. }
  422. }
  423. }
  424. return inbound, needRestart, err
  425. }
  426. func (s *InboundService) DelInbound(id int) (bool, error) {
  427. db := database.GetDB()
  428. needRestart := false
  429. markDirty := false
  430. var ib model.Inbound
  431. loadErr := db.Model(model.Inbound{}).Where("id = ?", id).First(&ib).Error
  432. if loadErr == nil {
  433. shouldPushToRuntime := ib.NodeID != nil || ib.Enable
  434. if shouldPushToRuntime {
  435. rt, push, dirty, perr := s.nodePushPlan(&ib)
  436. if perr != nil {
  437. logger.Warning("DelInbound: node lookup failed, deleting central row anyway:", perr)
  438. markDirty = true
  439. } else if push {
  440. if err1 := rt.DelInbound(context.Background(), &ib); err1 == nil {
  441. logger.Debug("Inbound deleted on", rt.Name(), ":", ib.Tag)
  442. } else {
  443. logger.Warning("DelInbound on", rt.Name(), "failed, deleting central row anyway:", err1)
  444. if ib.NodeID == nil {
  445. needRestart = true
  446. } else {
  447. markDirty = true
  448. }
  449. }
  450. } else if ib.NodeID == nil {
  451. needRestart = true
  452. } else if dirty {
  453. markDirty = true
  454. }
  455. } else {
  456. logger.Debug("DelInbound: skipping runtime push for disabled local inbound id:", id)
  457. }
  458. } else {
  459. logger.Debug("DelInbound: inbound not found, id:", id)
  460. }
  461. if err := s.clientService.DetachInbound(db, id); err != nil {
  462. return false, err
  463. }
  464. if err := db.Delete(model.Inbound{}, id).Error; err != nil {
  465. return needRestart, err
  466. }
  467. if markDirty && ib.NodeID != nil {
  468. if dErr := (&NodeService{}).MarkNodeDirty(*ib.NodeID); dErr != nil {
  469. logger.Warning("mark node dirty failed:", dErr)
  470. }
  471. }
  472. if !database.IsPostgres() {
  473. var count int64
  474. if err := db.Model(&model.Inbound{}).Count(&count).Error; err != nil {
  475. return needRestart, err
  476. }
  477. if count == 0 {
  478. if err := db.Exec("DELETE FROM sqlite_sequence WHERE name = ?", "inbounds").Error; err != nil {
  479. return needRestart, err
  480. }
  481. }
  482. }
  483. return needRestart, nil
  484. }
  485. type BulkDelInboundResult struct {
  486. Deleted int `json:"deleted"`
  487. Skipped []BulkDelInboundReport `json:"skipped,omitempty"`
  488. }
  489. type BulkDelInboundReport struct {
  490. Id int `json:"id"`
  491. Reason string `json:"reason"`
  492. }
  493. // DelInbounds removes every inbound in the list, reusing the single-delete
  494. // path per id. Failures are recorded in Skipped and processing continues for
  495. // the rest; the aggregated needRestart is returned so the caller restarts
  496. // xray at most once.
  497. func (s *InboundService) DelInbounds(ids []int) (BulkDelInboundResult, bool, error) {
  498. result := BulkDelInboundResult{}
  499. needRestart := false
  500. for _, id := range ids {
  501. r, err := s.DelInbound(id)
  502. if err != nil {
  503. result.Skipped = append(result.Skipped, BulkDelInboundReport{Id: id, Reason: err.Error()})
  504. continue
  505. }
  506. result.Deleted++
  507. if r {
  508. needRestart = true
  509. }
  510. }
  511. return result, needRestart, nil
  512. }
  513. func (s *InboundService) GetInbound(id int) (*model.Inbound, error) {
  514. db := database.GetDB()
  515. inbound := &model.Inbound{}
  516. err := db.Model(model.Inbound{}).First(inbound, id).Error
  517. if err != nil {
  518. return nil, err
  519. }
  520. return inbound, nil
  521. }
  522. func (s *InboundService) GetInboundDetail(id int) (*model.Inbound, error) {
  523. db := database.GetDB()
  524. inbound := &model.Inbound{}
  525. err := db.Model(model.Inbound{}).Preload("ClientStats").First(inbound, id).Error
  526. if err != nil {
  527. return nil, err
  528. }
  529. s.enrichClientStats(db, []*model.Inbound{inbound})
  530. return inbound, nil
  531. }
  532. func (s *InboundService) SetInboundEnable(id int, enable bool) (bool, error) {
  533. inbound, err := s.GetInbound(id)
  534. if err != nil {
  535. return false, err
  536. }
  537. if inbound.Enable == enable {
  538. return false, nil
  539. }
  540. db := database.GetDB()
  541. if err := db.Model(model.Inbound{}).Where("id = ?", id).
  542. Update("enable", enable).Error; err != nil {
  543. return false, err
  544. }
  545. inbound.Enable = enable
  546. needRestart := false
  547. rt, push, dirty, perr := s.nodePushPlan(inbound)
  548. if perr != nil {
  549. return false, perr
  550. }
  551. // Remote nodes interpret DelInbound as a real row delete (it hits
  552. // panel/api/inbounds/del/:id on the remote), so toggling the enable
  553. // switch on a remote inbound used to wipe the row entirely (#4402).
  554. // PATCH the remote row via UpdateInbound instead — preserves the
  555. // settings/client history and just flips the enable flag.
  556. if inbound.NodeID != nil {
  557. if push {
  558. if err := rt.UpdateInbound(context.Background(), inbound, inbound); err != nil {
  559. logger.Warning("SetInboundEnable: remote UpdateInbound on", rt.Name(), "failed:", err)
  560. dirty = true
  561. }
  562. }
  563. if dirty {
  564. if dErr := (&NodeService{}).MarkNodeDirty(*inbound.NodeID); dErr != nil {
  565. logger.Warning("mark node dirty failed:", dErr)
  566. }
  567. }
  568. return false, nil
  569. }
  570. if !push {
  571. return true, nil
  572. }
  573. if err := rt.DelInbound(context.Background(), inbound); err != nil &&
  574. !strings.Contains(err.Error(), "not found") {
  575. logger.Debug("SetInboundEnable: DelInbound on", rt.Name(), "failed:", err)
  576. needRestart = true
  577. }
  578. if !enable {
  579. return needRestart, nil
  580. }
  581. runtimeInbound, err := s.buildRuntimeInboundForAPI(db, inbound)
  582. if err != nil {
  583. logger.Debug("SetInboundEnable: build runtime config failed:", err)
  584. return true, nil
  585. }
  586. if err := rt.AddInbound(context.Background(), runtimeInbound); err != nil {
  587. logger.Debug("SetInboundEnable: AddInbound on", rt.Name(), "failed:", err)
  588. needRestart = true
  589. }
  590. return needRestart, nil
  591. }
  592. func (s *InboundService) UpdateInbound(inbound *model.Inbound) (*model.Inbound, bool, error) {
  593. // Normalize streamSettings based on protocol
  594. s.normalizeStreamSettings(inbound)
  595. s.normalizeMtprotoSecret(inbound)
  596. conflict, err := s.checkPortConflict(inbound, inbound.Id)
  597. if err != nil {
  598. return inbound, false, err
  599. }
  600. if conflict != nil {
  601. return inbound, false, common.NewError(conflict.String())
  602. }
  603. oldInbound, err := s.GetInbound(inbound.Id)
  604. if err != nil {
  605. return inbound, false, err
  606. }
  607. inbound.NodeID = oldInbound.NodeID
  608. tag := oldInbound.Tag
  609. oldBits := inboundTransports(oldInbound.Protocol, oldInbound.StreamSettings, oldInbound.Settings)
  610. oldTagWasAuto := isAutoGeneratedTag(tag, oldInbound.Port, oldInbound.NodeID, oldBits)
  611. db := database.GetDB()
  612. tx := db.Begin()
  613. markDirty := false
  614. defer func() {
  615. if err != nil {
  616. tx.Rollback()
  617. return
  618. }
  619. tx.Commit()
  620. if markDirty && oldInbound.NodeID != nil {
  621. if dErr := (&NodeService{}).MarkNodeDirty(*oldInbound.NodeID); dErr != nil {
  622. logger.Warning("mark node dirty failed:", dErr)
  623. }
  624. }
  625. }()
  626. err = s.updateClientTraffics(tx, oldInbound, inbound)
  627. if err != nil {
  628. return inbound, false, err
  629. }
  630. // Ensure created_at and updated_at exist in inbound.Settings clients
  631. {
  632. var oldSettings map[string]any
  633. _ = json.Unmarshal([]byte(oldInbound.Settings), &oldSettings)
  634. emailToCreated := map[string]int64{}
  635. emailToUpdated := map[string]int64{}
  636. if oldSettings != nil {
  637. if oc, ok := oldSettings["clients"].([]any); ok {
  638. for _, it := range oc {
  639. if m, ok2 := it.(map[string]any); ok2 {
  640. if email, ok3 := m["email"].(string); ok3 {
  641. switch v := m["created_at"].(type) {
  642. case float64:
  643. emailToCreated[email] = int64(v)
  644. case int64:
  645. emailToCreated[email] = v
  646. }
  647. switch v := m["updated_at"].(type) {
  648. case float64:
  649. emailToUpdated[email] = int64(v)
  650. case int64:
  651. emailToUpdated[email] = v
  652. }
  653. }
  654. }
  655. }
  656. }
  657. }
  658. var newSettings map[string]any
  659. if err2 := json.Unmarshal([]byte(inbound.Settings), &newSettings); err2 == nil && newSettings != nil {
  660. now := time.Now().Unix() * 1000
  661. if nSlice, ok := newSettings["clients"].([]any); ok {
  662. for i := range nSlice {
  663. if m, ok2 := nSlice[i].(map[string]any); ok2 {
  664. email, _ := m["email"].(string)
  665. if _, ok3 := m["created_at"]; !ok3 {
  666. if v, ok4 := emailToCreated[email]; ok4 && v > 0 {
  667. m["created_at"] = v
  668. } else {
  669. m["created_at"] = now
  670. }
  671. }
  672. // Preserve client's updated_at if present; do not bump on parent inbound update
  673. if _, hasUpdated := m["updated_at"]; !hasUpdated {
  674. if v, ok4 := emailToUpdated[email]; ok4 && v > 0 {
  675. m["updated_at"] = v
  676. }
  677. }
  678. nSlice[i] = m
  679. }
  680. }
  681. newSettings["clients"] = nSlice
  682. if bs, err3 := json.MarshalIndent(newSettings, "", " "); err3 == nil {
  683. inbound.Settings = string(bs)
  684. }
  685. }
  686. }
  687. }
  688. oldInbound.Total = inbound.Total
  689. oldInbound.Remark = inbound.Remark
  690. oldInbound.Enable = inbound.Enable
  691. oldInbound.ExpiryTime = inbound.ExpiryTime
  692. oldInbound.TrafficReset = inbound.TrafficReset
  693. oldInbound.Listen = inbound.Listen
  694. oldInbound.Port = inbound.Port
  695. oldInbound.Protocol = inbound.Protocol
  696. oldInbound.Settings = inbound.Settings
  697. oldInbound.StreamSettings = inbound.StreamSettings
  698. oldInbound.Sniffing = inbound.Sniffing
  699. if oldTagWasAuto && inbound.Tag == tag {
  700. inbound.Tag = ""
  701. }
  702. oldInbound.Tag, err = s.resolveInboundTag(inbound, inbound.Id)
  703. if err != nil {
  704. return inbound, false, err
  705. }
  706. inbound.Tag = oldInbound.Tag
  707. needRestart := false
  708. rt, push, dirty, perr := s.nodePushPlan(oldInbound)
  709. if perr != nil {
  710. err = perr
  711. return inbound, false, err
  712. }
  713. if dirty {
  714. markDirty = true
  715. }
  716. if oldInbound.NodeID == nil {
  717. if !push {
  718. needRestart = true
  719. } else {
  720. oldSnapshot := *oldInbound
  721. oldSnapshot.Tag = tag
  722. if err2 := rt.DelInbound(context.Background(), &oldSnapshot); err2 == nil {
  723. logger.Debug("Old inbound deleted on", rt.Name(), ":", tag)
  724. }
  725. if inbound.Enable {
  726. runtimeInbound, err2 := s.buildRuntimeInboundForAPI(tx, oldInbound)
  727. if err2 != nil {
  728. logger.Debug("Unable to prepare runtime inbound config:", err2)
  729. needRestart = true
  730. } else if err2 := rt.AddInbound(context.Background(), runtimeInbound); err2 == nil {
  731. logger.Debug("Updated inbound added on", rt.Name(), ":", oldInbound.Tag)
  732. } else {
  733. logger.Debug("Unable to update inbound on", rt.Name(), ":", err2)
  734. needRestart = true
  735. }
  736. }
  737. }
  738. } else if push {
  739. oldSnapshot := *oldInbound
  740. oldSnapshot.Tag = tag
  741. if !inbound.Enable {
  742. if err2 := rt.DelInbound(context.Background(), &oldSnapshot); err2 != nil {
  743. logger.Warning("Unable to disable inbound on", rt.Name(), ":", err2)
  744. markDirty = true
  745. }
  746. } else if err2 := rt.UpdateInbound(context.Background(), &oldSnapshot, oldInbound); err2 != nil {
  747. logger.Warning("Unable to update inbound on", rt.Name(), ":", err2)
  748. markDirty = true
  749. }
  750. }
  751. if err = tx.Save(oldInbound).Error; err != nil {
  752. return inbound, false, err
  753. }
  754. newClients, gcErr := s.GetClients(oldInbound)
  755. if gcErr != nil {
  756. err = gcErr
  757. return inbound, false, err
  758. }
  759. if err = s.clientService.SyncInbound(tx, oldInbound.Id, newClients); err != nil {
  760. return inbound, false, err
  761. }
  762. return inbound, needRestart, nil
  763. }
  764. func (s *InboundService) buildRuntimeInboundForAPI(tx *gorm.DB, inbound *model.Inbound) (*model.Inbound, error) {
  765. if inbound == nil {
  766. return nil, fmt.Errorf("inbound is nil")
  767. }
  768. runtimeInbound := *inbound
  769. settings := map[string]any{}
  770. if err := json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
  771. return nil, err
  772. }
  773. clients, ok := settings["clients"].([]any)
  774. if !ok {
  775. return &runtimeInbound, nil
  776. }
  777. var clientStats []xray.ClientTraffic
  778. err := tx.Model(xray.ClientTraffic{}).
  779. Where("inbound_id = ?", inbound.Id).
  780. Select("email", "enable").
  781. Find(&clientStats).Error
  782. if err != nil {
  783. return nil, err
  784. }
  785. enableMap := make(map[string]bool, len(clientStats))
  786. for _, clientTraffic := range clientStats {
  787. enableMap[clientTraffic.Email] = clientTraffic.Enable
  788. }
  789. finalClients := make([]any, 0, len(clients))
  790. for _, client := range clients {
  791. c, ok := client.(map[string]any)
  792. if !ok {
  793. continue
  794. }
  795. email, _ := c["email"].(string)
  796. if enable, exists := enableMap[email]; exists && !enable {
  797. continue
  798. }
  799. if manualEnable, ok := c["enable"].(bool); ok && !manualEnable {
  800. continue
  801. }
  802. finalClients = append(finalClients, c)
  803. }
  804. settings["clients"] = finalClients
  805. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  806. if err != nil {
  807. return nil, err
  808. }
  809. runtimeInbound.Settings = string(modifiedSettings)
  810. return &runtimeInbound, nil
  811. }
  812. // updateClientTraffics syncs the ClientTraffic rows with the inbound's clients
  813. // list: removes rows for emails that disappeared, inserts rows for newly-added
  814. // emails. Uses sets for O(N) lookup — the previous nested-loop implementation
  815. // was O(N²) and degraded into multi-second pauses on inbounds with thousands
  816. // of clients (toggling, saving, or deleting any such inbound felt frozen).
  817. func (s *InboundService) updateClientTraffics(tx *gorm.DB, oldInbound *model.Inbound, newInbound *model.Inbound) error {
  818. oldClients, err := s.GetClients(oldInbound)
  819. if err != nil {
  820. return err
  821. }
  822. newClients, err := s.GetClients(newInbound)
  823. if err != nil {
  824. return err
  825. }
  826. // Email is the unique key for ClientTraffic rows. Clients without an
  827. // email have no stats row to sync — skip them on both sides instead of
  828. // risking a unique-constraint hit or accidental delete of an unrelated row.
  829. oldEmails := make(map[string]struct{}, len(oldClients))
  830. for i := range oldClients {
  831. if oldClients[i].Email == "" {
  832. continue
  833. }
  834. oldEmails[oldClients[i].Email] = struct{}{}
  835. }
  836. newEmails := make(map[string]struct{}, len(newClients))
  837. for i := range newClients {
  838. if newClients[i].Email == "" {
  839. continue
  840. }
  841. newEmails[newClients[i].Email] = struct{}{}
  842. }
  843. // Drop stats rows for removed emails — but not when a sibling inbound
  844. // still references the email, since the row is the shared accumulator.
  845. for i := range oldClients {
  846. email := oldClients[i].Email
  847. if email == "" {
  848. continue
  849. }
  850. if _, kept := newEmails[email]; kept {
  851. continue
  852. }
  853. stillUsed, err := s.emailUsedByOtherInbounds(email, oldInbound.Id)
  854. if err != nil {
  855. return err
  856. }
  857. if stillUsed {
  858. continue
  859. }
  860. if err := s.DelClientStat(tx, email); err != nil {
  861. return err
  862. }
  863. // Keep inbound_client_ips in sync when the inbound edit drops an
  864. // email, so the IP-limit job doesn't keep a ghost tracking row (#4963).
  865. if err := s.DelClientIPs(tx, email); err != nil {
  866. return err
  867. }
  868. }
  869. for i := range newClients {
  870. email := newClients[i].Email
  871. if email == "" {
  872. continue
  873. }
  874. if _, existed := oldEmails[email]; existed {
  875. if err := s.UpdateClientStat(tx, email, &newClients[i]); err != nil {
  876. return err
  877. }
  878. continue
  879. }
  880. if err := s.AddClientStat(tx, oldInbound.Id, &newClients[i]); err != nil {
  881. return err
  882. }
  883. }
  884. return nil
  885. }
  886. func (s *InboundService) GetInboundTags() (string, error) {
  887. db := database.GetDB()
  888. var inboundTags []string
  889. err := db.Model(model.Inbound{}).Select("tag").Find(&inboundTags).Error
  890. if err != nil && err != gorm.ErrRecordNotFound {
  891. return "", err
  892. }
  893. tags, _ := json.Marshal(inboundTags)
  894. return string(tags), nil
  895. }
  896. func (s *InboundService) GetClientReverseTags() (string, error) {
  897. db := database.GetDB()
  898. var inbounds []model.Inbound
  899. err := db.Model(model.Inbound{}).Select("settings").Where("protocol = ?", "vless").Find(&inbounds).Error
  900. if err != nil && err != gorm.ErrRecordNotFound {
  901. return "[]", err
  902. }
  903. tagSet := make(map[string]struct{})
  904. for _, inbound := range inbounds {
  905. var settings map[string]any
  906. if err := json.Unmarshal([]byte(inbound.Settings), &settings); err != nil {
  907. continue
  908. }
  909. clients, ok := settings["clients"].([]any)
  910. if !ok {
  911. continue
  912. }
  913. for _, client := range clients {
  914. clientMap, ok := client.(map[string]any)
  915. if !ok {
  916. continue
  917. }
  918. reverse, ok := clientMap["reverse"].(map[string]any)
  919. if !ok {
  920. continue
  921. }
  922. tag, _ := reverse["tag"].(string)
  923. tag = strings.TrimSpace(tag)
  924. if tag != "" {
  925. tagSet[tag] = struct{}{}
  926. }
  927. }
  928. }
  929. rawTags := make([]string, 0, len(tagSet))
  930. for tag := range tagSet {
  931. rawTags = append(rawTags, tag)
  932. }
  933. sort.Strings(rawTags)
  934. result, _ := json.Marshal(rawTags)
  935. return string(result), nil
  936. }
  937. func (s *InboundService) SearchInbounds(query string) ([]*model.Inbound, error) {
  938. db := database.GetDB()
  939. var inbounds []*model.Inbound
  940. err := db.Model(model.Inbound{}).Preload("ClientStats").Where("remark like ?", "%"+query+"%").Find(&inbounds).Error
  941. if err != nil && err != gorm.ErrRecordNotFound {
  942. return nil, err
  943. }
  944. return inbounds, nil
  945. }