1
0

inbound_tuic.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/tuic"
  8. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  9. )
  10. func (s *InboundService) DesiredTuicInstances() ([]tuic.Instance, error) {
  11. db := database.GetDB()
  12. var inbounds []*model.Inbound
  13. err := db.Model(model.Inbound{}).
  14. Where("protocol = ? AND enable = ? AND node_id IS NULL", model.TUIC, true).
  15. Find(&inbounds).Error
  16. if err != nil {
  17. return nil, err
  18. }
  19. if len(inbounds) == 0 {
  20. return nil, nil
  21. }
  22. ids := make([]int, 0, len(inbounds))
  23. for _, ib := range inbounds {
  24. ids = append(ids, ib.Id)
  25. }
  26. var disabledRows []xray.ClientTraffic
  27. err = db.Model(xray.ClientTraffic{}).
  28. Where("inbound_id IN ? AND enable = ?", ids, false).
  29. Select("inbound_id", "email").
  30. Find(&disabledRows).Error
  31. if err != nil {
  32. return nil, err
  33. }
  34. disabled := make(map[int]map[string]struct{}, len(disabledRows))
  35. for _, row := range disabledRows {
  36. if disabled[row.InboundId] == nil {
  37. disabled[row.InboundId] = map[string]struct{}{}
  38. }
  39. disabled[row.InboundId][row.Email] = struct{}{}
  40. }
  41. instances := make([]tuic.Instance, 0, len(inbounds))
  42. for _, ib := range inbounds {
  43. inst, ok := tuic.InstanceFromInbound(ib)
  44. if !ok {
  45. continue
  46. }
  47. if off := disabled[ib.Id]; len(off) > 0 {
  48. kept := make([]tuic.TuicClientSettings, 0, len(inst.Clients))
  49. for _, c := range inst.Clients {
  50. if _, skip := off[c.Email]; !skip {
  51. kept = append(kept, c)
  52. }
  53. }
  54. inst.Clients = kept
  55. }
  56. if len(inst.Clients) == 0 {
  57. continue
  58. }
  59. instances = append(instances, inst)
  60. }
  61. return instances, nil
  62. }
  63. func (s *InboundService) applyLocalTuic(inboundId int) {
  64. inbound, err := s.GetInbound(inboundId)
  65. if err != nil || inbound == nil || inbound.Protocol != model.TUIC || inbound.NodeID != nil {
  66. return
  67. }
  68. rt, err := s.runtimeFor(inbound)
  69. if err != nil {
  70. return
  71. }
  72. payload := inbound
  73. if inbound.Enable {
  74. if built, bErr := s.buildInboundForLocalRuntime(database.GetDB(), inbound); bErr == nil {
  75. payload = built
  76. }
  77. }
  78. if err := rt.UpdateInbound(context.Background(), inbound, payload); err != nil {
  79. logger.Debug("tuic: immediate client apply failed for inbound", inboundId, ":", err)
  80. }
  81. }