inbound_tuic.go 2.0 KB

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