Просмотр исходного кода

fix: propagate inbound traffic reset to nodes (#5103)

Co-authored-by: Rqzbeh <[email protected]>
Rouzbeh† 6 часов назад
Родитель
Сommit
be8bd4e22c
4 измененных файлов с 38 добавлено и 2 удалено
  1. 4 0
      web/runtime/local.go
  2. 5 0
      web/runtime/remote.go
  3. 1 0
      web/runtime/runtime.go
  4. 28 2
      web/service/inbound.go

+ 4 - 0
web/runtime/local.go

@@ -159,3 +159,7 @@ func (l *Local) ResetClientTraffic(_ context.Context, _ *model.Inbound, _ string
 func (l *Local) ResetAllTraffics(_ context.Context) error {
 func (l *Local) ResetAllTraffics(_ context.Context) error {
 	return nil
 	return nil
 }
 }
+
+func (l *Local) ResetInboundTraffic(_ context.Context, _ *model.Inbound) error {
+	return nil
+}

+ 5 - 0
web/runtime/remote.go

@@ -399,6 +399,11 @@ func (r *Remote) ResetAllTraffics(ctx context.Context) error {
 	return err
 	return err
 }
 }
 
 
+func (r *Remote) ResetInboundTraffic(ctx context.Context, ib *model.Inbound) error {
+	_, err := r.do(ctx, http.MethodPost, fmt.Sprintf("panel/api/inbounds/%d/resetTraffic", ib.Id), nil)
+	return err
+}
+
 type TrafficSnapshot struct {
 type TrafficSnapshot struct {
 	Inbounds     []*model.Inbound
 	Inbounds     []*model.Inbound
 	OnlineEmails []string
 	OnlineEmails []string

+ 1 - 0
web/runtime/runtime.go

@@ -26,5 +26,6 @@ type Runtime interface {
 	RestartXray(ctx context.Context) error
 	RestartXray(ctx context.Context) error
 
 
 	ResetClientTraffic(ctx context.Context, ib *model.Inbound, email string) error
 	ResetClientTraffic(ctx context.Context, ib *model.Inbound, email string) error
+	ResetInboundTraffic(ctx context.Context, ib *model.Inbound) error
 	ResetAllTraffics(ctx context.Context) error
 	ResetAllTraffics(ctx context.Context) error
 }
 }

+ 28 - 2
web/service/inbound.go

@@ -3142,15 +3142,41 @@ func (s *InboundService) resetAllTrafficsLocked() error {
 		return err
 		return err
 	}
 	}
 
 
+	nodes, err := (&NodeService{}).GetAll()
+	if err == nil {
+		for _, node := range nodes {
+			if rt, err := runtime.GetManager().RuntimeFor(&node.Id); err == nil {
+				if e := rt.ResetAllTraffics(context.Background()); e != nil {
+					logger.Warning("ResetAllTraffics: remote propagation to", rt.Name(), "failed:", e)
+				}
+			}
+		}
+	}
+
 	return nil
 	return nil
 }
 }
 
 
 func (s *InboundService) ResetInboundTraffic(id int) error {
 func (s *InboundService) ResetInboundTraffic(id int) error {
 	return submitTrafficWrite(func() error {
 	return submitTrafficWrite(func() error {
 		db := database.GetDB()
 		db := database.GetDB()
-		return db.Model(model.Inbound{}).
+		if err := db.Model(model.Inbound{}).
 			Where("id = ?", id).
 			Where("id = ?", id).
-			Updates(map[string]any{"up": 0, "down": 0}).Error
+			Updates(map[string]any{"up": 0, "down": 0}).Error; err != nil {
+			return err
+		}
+
+		inbound, err := s.GetInbound(id)
+		if err == nil && inbound != nil && inbound.NodeID != nil {
+			if rt, rterr := s.runtimeFor(inbound); rterr == nil {
+				if e := rt.ResetInboundTraffic(context.Background(), inbound); e != nil {
+					logger.Warning("ResetInboundTraffic: remote propagation to", rt.Name(), "failed:", e)
+				}
+			} else {
+				logger.Warning("ResetInboundTraffic: runtime lookup failed:", rterr)
+			}
+		}
+
+		return nil
 	})
 	})
 }
 }