From daee0db7bb24ffb94d5ee1d56da6260931e3547d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Mon, 24 Apr 2023 10:05:13 +0800 Subject: [PATCH] clash-api: Reset outbounds in DELETE /connections --- adapter/router.go | 2 ++ experimental/clashapi/connections.go | 8 +++++--- experimental/clashapi/server.go | 2 +- route/router.go | 19 ++++++++++++++++--- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/adapter/router.go b/adapter/router.go index 29b157f0..3cf9e6d4 100644 --- a/adapter/router.go +++ b/adapter/router.go @@ -51,6 +51,8 @@ type Router interface { V2RayServer() V2RayServer SetV2RayServer(server V2RayServer) + + ResetNetwork() error } type routerContextKey struct{} diff --git a/experimental/clashapi/connections.go b/experimental/clashapi/connections.go index 8d1e5f1a..94cfb9a3 100644 --- a/experimental/clashapi/connections.go +++ b/experimental/clashapi/connections.go @@ -6,6 +6,7 @@ import ( "strconv" "time" + "github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/common/json" "github.com/sagernet/sing-box/experimental/clashapi/trafficontrol" "github.com/sagernet/websocket" @@ -14,10 +15,10 @@ import ( "github.com/go-chi/render" ) -func connectionRouter(trafficManager *trafficontrol.Manager) http.Handler { +func connectionRouter(router adapter.Router, trafficManager *trafficontrol.Manager) http.Handler { r := chi.NewRouter() r.Get("/", getConnections(trafficManager)) - r.Delete("/", closeAllConnections(trafficManager)) + r.Delete("/", closeAllConnections(router, trafficManager)) r.Delete("/{id}", closeConnection(trafficManager)) return r } @@ -86,12 +87,13 @@ func closeConnection(trafficManager *trafficontrol.Manager) func(w http.Response } } -func closeAllConnections(trafficManager *trafficontrol.Manager) func(w http.ResponseWriter, r *http.Request) { +func closeAllConnections(router adapter.Router, 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() render.NoContent(w, r) } } diff --git a/experimental/clashapi/server.go b/experimental/clashapi/server.go index 4ba61d2d..6a5fb458 100644 --- a/experimental/clashapi/server.go +++ b/experimental/clashapi/server.go @@ -105,7 +105,7 @@ func NewServer(ctx context.Context, router adapter.Router, logFactory log.Observ r.Mount("/configs", configRouter(server, logFactory, server.logger)) r.Mount("/proxies", proxyRouter(server, router)) r.Mount("/rules", ruleRouter(router)) - r.Mount("/connections", connectionRouter(trafficManager)) + r.Mount("/connections", connectionRouter(router, trafficManager)) r.Mount("/providers/proxies", proxyProviderRouter()) r.Mount("/providers/rules", ruleProviderRouter()) r.Mount("/script", scriptRouter()) diff --git a/route/router.go b/route/router.go index cb0a60ea..95e417da 100644 --- a/route/router.go +++ b/route/router.go @@ -974,9 +974,22 @@ func (r *Router) notifyNetworkUpdate(int) error { r.logger.Info("updated default interface ", r.interfaceMonitor.DefaultInterfaceName(netip.IPv4Unspecified()), ", index ", r.interfaceMonitor.DefaultInterfaceIndex(netip.IPv4Unspecified())) } - if conntrack.Enabled { - conntrack.Close() - } + conntrack.Close() + + for _, outbound := range r.outbounds { + listener, isListener := outbound.(adapter.InterfaceUpdateListener) + if isListener { + err := listener.InterfaceUpdated() + if err != nil { + return err + } + } + } + return nil +} + +func (r *Router) ResetNetwork() error { + conntrack.Close() for _, outbound := range r.outbounds { listener, isListener := outbound.(adapter.InterfaceUpdateListener)