Files
Sanaei f727d04f65
CI / go-test (push) Canceled after 0s
CI / postgres-durable-first (push) Canceled after 0s
CI / codegen (push) Canceled after 0s
CI / govulncheck (push) Canceled after 0s
CI / race (push) Canceled after 0s
CI / fuzz-smoke (push) Canceled after 0s
CI / golangci (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CodeQL Advanced / Analyze (go) (push) Canceled after 0s
CodeQL Advanced / Analyze (actions) (push) Canceled after 0s
CodeQL Advanced / Analyze (javascript-typescript) (push) Canceled after 0s
Docs CI / build (push) Canceled after 0s
Docs Deploy (GitHub Pages) / build (push) Canceled after 0s
Release 3X-UI / build (386) (push) Canceled after 0s
Release 3X-UI / build (amd64) (push) Canceled after 0s
Release 3X-UI / build (arm64) (push) Canceled after 0s
Release 3X-UI / build (armv5) (push) Canceled after 0s
Release 3X-UI / build (armv6) (push) Canceled after 0s
Release 3X-UI / build (armv7) (push) Canceled after 0s
Release 3X-UI / build (s390x) (push) Canceled after 0s
Release 3X-UI / Build for Windows (amd64) (push) Canceled after 0s
Deploy Smoke Tests / noninteractive-install (ubuntu-24.04-arm) (push) Canceled after 0s
Deploy Smoke Tests / noninteractive-install (ubuntu-latest) (push) Canceled after 0s
Deploy Smoke Tests / release-tag-install (ubuntu-24.04-arm) (push) Canceled after 0s
Deploy Smoke Tests / release-tag-install (ubuntu-latest) (push) Canceled after 0s
Docs Deploy (GitHub Pages) / deploy (push) Canceled after 0s
Release 3X-UI / Publish rolling dev release (push) Canceled after 0s
v3.7.0
2026-08-24 15:07:15 +02:00

110 lines
2.5 KiB
Go

package service
import (
"context"
"strings"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
"github.com/mhsanaei/3x-ui/v3/internal/logger"
"github.com/mhsanaei/3x-ui/v3/internal/xray"
"gorm.io/gorm"
)
type trafficLocalApplyAction uint8
const (
trafficAddUser trafficLocalApplyAction = iota + 1
trafficRemoveUser
trafficDisableInbound
)
type trafficLocalApplyPlan struct {
action trafficLocalApplyAction
inbound model.Inbound
client map[string]any
email string
}
type trafficMutationBatch struct {
localPlans []trafficLocalApplyPlan
remotePlans []trafficInboundUpdatePlan
nodeIDs map[int]struct{}
}
type trafficInboundUpdatePlan struct{ oldInbound, newInbound model.Inbound }
func newTrafficMutationBatch() *trafficMutationBatch {
return &trafficMutationBatch{nodeIDs: make(map[int]struct{})}
}
func (b *trafficMutationBatch) addNode(nodeID int) {
if nodeID > 0 {
b.nodeIDs[nodeID] = struct{}{}
}
}
func (b *trafficMutationBatch) markNodesTx(tx *gorm.DB) error {
if b == nil {
return nil
}
nodeSvc := NodeService{}
for nodeID := range b.nodeIDs {
if err := nodeSvc.MarkNodeDirtyTx(tx, nodeID); err != nil {
return err
}
}
return nil
}
func (s *InboundService) applyTrafficMutationBatch(b *trafficMutationBatch) bool {
if b == nil {
return false
}
needRestart := false
for i := range b.remotePlans {
plan := &b.remotePlans[i]
rt, err := s.runtimeFor(&plan.newInbound)
if err == nil {
err = rt.UpdateInbound(context.Background(), &plan.oldInbound, &plan.newInbound)
}
if err != nil {
logger.Debug("traffic post-commit remote apply failed:", err)
needRestart = true
}
}
for i := range b.localPlans {
plan := &b.localPlans[i]
if plan.inbound.Protocol == model.MTProto {
s.applyLocalMtproto(plan.inbound.Id)
continue
}
if plan.inbound.Protocol == model.AmneziaWG {
s.applyLocalAmneziaWG(plan.inbound.Id)
continue
}
rt, err := s.runtimeFor(&plan.inbound)
if err == nil {
switch plan.action {
case trafficAddUser:
err = rt.AddUser(context.Background(), &plan.inbound, plan.client)
case trafficRemoveUser:
err = rt.RemoveUser(context.Background(), &plan.inbound, plan.email)
if err != nil && strings.Contains(err.Error(), "not found") {
err = nil
}
case trafficDisableInbound:
err = rt.DelInbound(context.Background(), &plan.inbound)
if xray.IsMissingHandlerErr(err) {
err = nil
}
}
}
if err != nil {
logger.Debug("traffic post-commit runtime apply failed:", err)
needRestart = true
}
}
return needRestart
}