inbound_mtproto.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package service
  2. import (
  3. "context"
  4. "github.com/mhsanaei/3x-ui/v3/internal/database"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  6. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  7. "github.com/mhsanaei/3x-ui/v3/internal/mtproto"
  8. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  9. )
  10. // DesiredMtprotoInstances derives the mtg sidecar configs this panel should be
  11. // running: one instance per enabled local mtproto inbound, serving only the
  12. // secrets of clients that are both enabled in the inbound settings and not
  13. // depletion-disabled in client_traffics. That is the same effective client set
  14. // buildRuntimeInboundForAPI pushes on interactive edits, so the reconcile job
  15. // and the push paths agree on one fingerprint — a disagreement would surface
  16. // as a needless mtg restart, and a job that read only the raw settings would
  17. // keep serving depleted clients until an unrelated restart. Inbounds whose
  18. // every secret is filtered away are omitted so Reconcile stops their sidecar.
  19. func (s *InboundService) DesiredMtprotoInstances() ([]mtproto.Instance, error) {
  20. db := database.GetDB()
  21. var inbounds []*model.Inbound
  22. err := db.Model(model.Inbound{}).
  23. Where("protocol = ? AND enable = ? AND node_id IS NULL", model.MTProto, true).
  24. Find(&inbounds).Error
  25. if err != nil {
  26. return nil, err
  27. }
  28. if len(inbounds) == 0 {
  29. return nil, nil
  30. }
  31. ids := make([]int, 0, len(inbounds))
  32. for _, ib := range inbounds {
  33. ids = append(ids, ib.Id)
  34. }
  35. var disabledRows []xray.ClientTraffic
  36. err = db.Model(xray.ClientTraffic{}).
  37. Where("inbound_id IN ? AND enable = ?", ids, false).
  38. Select("inbound_id", "email").
  39. Find(&disabledRows).Error
  40. if err != nil {
  41. return nil, err
  42. }
  43. disabled := make(map[int]map[string]struct{}, len(disabledRows))
  44. for _, row := range disabledRows {
  45. if disabled[row.InboundId] == nil {
  46. disabled[row.InboundId] = map[string]struct{}{}
  47. }
  48. disabled[row.InboundId][row.Email] = struct{}{}
  49. }
  50. instances := make([]mtproto.Instance, 0, len(inbounds))
  51. for _, ib := range inbounds {
  52. inst, ok := mtproto.InstanceFromInbound(ib)
  53. if !ok {
  54. continue
  55. }
  56. if off := disabled[ib.Id]; len(off) > 0 {
  57. kept := make([]mtproto.SecretEntry, 0, len(inst.Secrets))
  58. for _, sec := range inst.Secrets {
  59. if _, skip := off[sec.Name]; !skip {
  60. kept = append(kept, sec)
  61. }
  62. }
  63. inst.Secrets = kept
  64. }
  65. if len(inst.Secrets) == 0 {
  66. continue
  67. }
  68. instances = append(instances, inst)
  69. }
  70. return instances, nil
  71. }
  72. // applyLocalMtproto pushes a single local mtproto inbound's current client set
  73. // to its mtg sidecar right after a client edit commits, so an add, removal,
  74. // re-key or enable-toggle takes effect immediately instead of waiting up to
  75. // 10s for the reconcile job. With a reload-capable mtg the change is applied in
  76. // place without dropping other clients; older binaries fall back to a restart
  77. // inside the manager. It re-reads the inbound so it sees the committed settings,
  78. // filters depleted clients exactly like the reconcile job, and is a no-op for
  79. // node-owned or non-mtproto inbounds. Failures are logged and swallowed: the
  80. // reconcile job is the backstop, and an xray restart cannot help the sidecar.
  81. func (s *InboundService) applyLocalMtproto(inboundId int) {
  82. inbound, err := s.GetInbound(inboundId)
  83. if err != nil || inbound == nil || inbound.Protocol != model.MTProto || inbound.NodeID != nil {
  84. return
  85. }
  86. rt, err := s.runtimeFor(inbound)
  87. if err != nil {
  88. return
  89. }
  90. payload := inbound
  91. if inbound.Enable {
  92. if built, bErr := s.buildRuntimeInboundForAPI(database.GetDB(), inbound); bErr == nil {
  93. payload = built
  94. }
  95. }
  96. if err := rt.UpdateInbound(context.Background(), inbound, payload); err != nil {
  97. logger.Debug("mtproto: immediate client apply failed for inbound", inboundId, ":", err)
  98. }
  99. }