Fix reset network

This commit is contained in:
Gavin Luo
2026-08-10 23:10:24 +08:00
committed by GitHub
parent 65380457b8
commit 7ac8e5c0ab
7 changed files with 25 additions and 19 deletions
+4 -4
View File
@@ -18,10 +18,10 @@ import (
"github.com/gofrs/uuid/v5"
)
func connectionRouter(ctx context.Context, router adapter.Router, trafficManager *trafficontrol.Manager) http.Handler {
func connectionRouter(ctx context.Context, network adapter.NetworkManager, trafficManager *trafficontrol.Manager) http.Handler {
r := chi.NewRouter()
r.Get("/", getConnections(ctx, trafficManager))
r.Delete("/", closeAllConnections(router, trafficManager))
r.Delete("/", closeAllConnections(network, trafficManager))
r.Delete("/{id}", closeConnection(trafficManager))
return r
}
@@ -96,13 +96,13 @@ func closeConnection(trafficManager *trafficontrol.Manager) func(w http.Response
}
}
func closeAllConnections(router adapter.Router, trafficManager *trafficontrol.Manager) func(w http.ResponseWriter, r *http.Request) {
func closeAllConnections(network adapter.NetworkManager, trafficManager *trafficontrol.Manager) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
snapshot := trafficManager.Snapshot()
for _, c := range snapshot.Connections {
c.Close()
}
router.ResetNetwork()
network.ResetNetwork()
render.NoContent(w, r)
}
}
+3 -1
View File
@@ -42,6 +42,7 @@ var _ adapter.ClashServer = (*Server)(nil)
type Server struct {
ctx context.Context
network adapter.NetworkManager
router adapter.Router
dnsRouter adapter.DNSRouter
outbound adapter.OutboundManager
@@ -67,6 +68,7 @@ func NewServer(ctx context.Context, logFactory log.ObservableFactory, options op
chiRouter := chi.NewRouter()
s := &Server{
ctx: ctx,
network: service.FromContext[adapter.NetworkManager](ctx),
router: service.FromContext[adapter.Router](ctx),
dnsRouter: service.FromContext[adapter.DNSRouter](ctx),
outbound: service.FromContext[adapter.OutboundManager](ctx),
@@ -121,7 +123,7 @@ func NewServer(ctx context.Context, logFactory log.ObservableFactory, options op
r.Mount("/configs", configRouter(s, logFactory))
r.Mount("/proxies", proxyRouter(s, s.router))
r.Mount("/rules", ruleRouter(s.router))
r.Mount("/connections", connectionRouter(s.ctx, s.router, trafficManager))
r.Mount("/connections", connectionRouter(s.ctx, s.network, trafficManager))
r.Mount("/providers/proxies", proxyProviderRouter())
r.Mount("/providers/rules", ruleProviderRouter())
r.Mount("/script", scriptRouter())