| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- package service
- import (
- "context"
- "github.com/mhsanaei/3x-ui/v3/internal/database"
- "github.com/mhsanaei/3x-ui/v3/internal/database/model"
- "github.com/mhsanaei/3x-ui/v3/internal/logger"
- "github.com/mhsanaei/3x-ui/v3/internal/tuic"
- )
- func (s *InboundService) DesiredTuicInstances() ([]tuic.Instance, error) {
- db := database.GetDB()
- var inbounds []*model.Inbound
- err := db.Model(model.Inbound{}).
- Where("protocol = ? AND enable = ? AND node_id IS NULL", model.TUIC, true).
- Find(&inbounds).Error
- if err != nil {
- return nil, err
- }
- if len(inbounds) == 0 {
- return nil, nil
- }
- instances := make([]tuic.Instance, 0, len(inbounds))
- for _, ib := range inbounds {
- built, err := s.buildInboundForLocalRuntime(db, ib)
- if err != nil {
- return nil, err
- }
- inst, ok := tuic.InstanceFromInbound(built)
- if !ok {
- continue
- }
- instances = append(instances, inst)
- }
- emails := make([]string, 0)
- for _, inst := range instances {
- for _, e := range inst.Clients {
- emails = append(emails, e.Email)
- }
- }
- disabled, err := trafficDisabledEmails(db, emails)
- if err != nil {
- return nil, err
- }
- served := instances[:0]
- for _, inst := range instances {
- kept := make([]tuic.TuicClientSettings, 0, len(inst.Clients))
- for _, e := range inst.Clients {
- if _, off := disabled[e.Email]; !off {
- kept = append(kept, e)
- }
- }
- inst.Clients = kept
- if len(kept) > 0 {
- served = append(served, inst)
- }
- }
- return served, nil
- }
- func (s *InboundService) applyLocalTuic(inboundId int) {
- inbound, err := s.GetInbound(inboundId)
- if err != nil || inbound == nil || inbound.Protocol != model.TUIC || inbound.NodeID != nil {
- return
- }
- rt, err := s.runtimeFor(inbound)
- if err != nil {
- return
- }
- payload := inbound
- if inbound.Enable {
- if built, bErr := s.buildInboundForLocalRuntime(database.GetDB(), inbound); bErr == nil {
- payload = built
- }
- }
- if err := rt.UpdateInbound(context.Background(), inbound, payload); err != nil {
- logger.Debug("tuic: immediate client apply failed for inbound", inboundId, ":", err)
- }
- }
|