inbound_tuic.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. inst, ok := tuic.InstanceFromInbound(ib)
  24. if !ok {
  25. continue
  26. }
  27. instances = append(instances, inst)
  28. }
  29. emails := make([]string, 0)
  30. for _, inst := range instances {
  31. for _, e := range inst.Clients {
  32. emails = append(emails, e.Email)
  33. }
  34. }
  35. disabled, err := trafficDisabledEmails(db, emails)
  36. if err != nil {
  37. return nil, err
  38. }
  39. served := instances[:0]
  40. for _, inst := range instances {
  41. kept := make([]tuic.TuicClientSettings, 0, len(inst.Clients))
  42. for _, e := range inst.Clients {
  43. if _, off := disabled[e.Email]; !off {
  44. kept = append(kept, e)
  45. }
  46. }
  47. inst.Clients = kept
  48. if len(kept) > 0 {
  49. served = append(served, inst)
  50. }
  51. }
  52. return served, nil
  53. }
  54. func (s *InboundService) applyLocalTuic(inboundId int) {
  55. inbound, err := s.GetInbound(inboundId)
  56. if err != nil || inbound == nil || inbound.Protocol != model.TUIC || inbound.NodeID != nil {
  57. return
  58. }
  59. rt, err := s.runtimeFor(inbound)
  60. if err != nil {
  61. return
  62. }
  63. payload := inbound
  64. if inbound.Enable {
  65. if built, bErr := s.buildInboundForLocalRuntime(database.GetDB(), inbound); bErr == nil {
  66. payload = built
  67. }
  68. }
  69. if err := rt.UpdateInbound(context.Background(), inbound, payload); err != nil {
  70. logger.Debug("tuic: immediate client apply failed for inbound", inboundId, ":", err)
  71. }
  72. }