From 149b4b5b4356c6506b7442d878af3e0d95439e1f Mon Sep 17 00:00:00 2001 From: adlyq Date: Tue, 24 May 2022 12:43:26 +0800 Subject: [PATCH] feat: RESTful API support disable sniffer --- config/config.go | 3 ++- hub/executor/executor.go | 2 ++ hub/route/configs.go | 5 +++++ listener/listener.go | 9 +++------ tunnel/tunnel.go | 32 +++++++++++++++++++++++--------- 5 files changed, 35 insertions(+), 16 deletions(-) diff --git a/config/config.go b/config/config.go index 5142210b..a8c5be9d 100644 --- a/config/config.go +++ b/config/config.go @@ -47,13 +47,14 @@ type General struct { UnifiedDelay bool LogLevel log.LogLevel `json:"log-level"` IPv6 bool `json:"ipv6"` - Interface string `json:"-"` + Interface string `json:"interface-name"` RoutingMark int `json:"-"` GeodataMode bool `json:"geodata-mode"` GeodataLoader string `json:"geodata-loader"` TCPConcurrent bool `json:"tcp-concurrent"` EnableProcess bool `json:"enable-process"` Tun Tun `json:"tun"` + Sniffing bool `json:"sniffing"` } // Inbound config diff --git a/hub/executor/executor.go b/hub/executor/executor.go index 6e53d567..7d83698b 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -119,6 +119,8 @@ func GetGeneral() *config.General { IPv6: !resolver.DisableIPv6, GeodataLoader: G.LoaderName(), Tun: P.GetTunConf(), + Interface: dialer.DefaultInterface.Load(), + Sniffing: tunnel.IsSniffing(), } return general diff --git a/hub/route/configs.go b/hub/route/configs.go index 3e36c054..78927cc9 100644 --- a/hub/route/configs.go +++ b/hub/route/configs.go @@ -36,6 +36,7 @@ type configSchema struct { Mode *tunnel.TunnelMode `json:"mode"` LogLevel *log.LogLevel `json:"log-level"` IPv6 *bool `json:"ipv6"` + Sniffing *bool `json:"sniffing"` } func getConfigs(w http.ResponseWriter, r *http.Request) { @@ -67,6 +68,10 @@ func patchConfigs(w http.ResponseWriter, r *http.Request) { P.SetBindAddress(*general.BindAddress) } + if general.Sniffing != nil { + tunnel.SetSniffing(*general.Sniffing) + } + ports := P.GetPorts() tcpIn := tunnel.TCPIn() diff --git a/listener/listener.go b/listener/listener.go index cbac01a2..80429dd7 100644 --- a/listener/listener.go +++ b/listener/listener.go @@ -6,7 +6,6 @@ import ( "github.com/Dreamacro/clash/listener/inner" "github.com/Dreamacro/clash/listener/tun/ipstack/commons" "net" - "net/netip" "runtime" "sort" "strconv" @@ -26,10 +25,9 @@ import ( ) var ( - allowLan = false - bindAddress = "*" - lastTunConf *config.Tun - lastTunAddressPrefix *netip.Prefix + allowLan = false + bindAddress = "*" + lastTunConf *config.Tun socksListener *socks.Listener socksUDPListener *socks.UDPListener @@ -467,5 +465,4 @@ func Cleanup(wait bool) { } tunStackListener = nil lastTunConf = nil - lastTunAddressPrefix = nil } diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index ae4c1fca..ddc0670a 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -25,14 +25,15 @@ import ( ) var ( - tcpQueue = make(chan C.ConnContext, 200) - udpQueue = make(chan *inbound.PacketAdapter, 200) - natTable = nat.New() - rules []C.Rule - proxies = make(map[string]C.Proxy) - providers map[string]provider.ProxyProvider - ruleProviders map[string]provider.RuleProvider - configMux sync.RWMutex + tcpQueue = make(chan C.ConnContext, 200) + udpQueue = make(chan *inbound.PacketAdapter, 200) + natTable = nat.New() + rules []C.Rule + proxies = make(map[string]C.Proxy) + providers map[string]provider.ProxyProvider + ruleProviders map[string]provider.RuleProvider + sniffingEnable bool + configMux sync.RWMutex // Outbound Rule mode = Rule @@ -43,6 +44,18 @@ var ( failTotal int ) +func SetSniffing(b bool) { + if sniffer.Dispatcher.Enable() { + configMux.Lock() + sniffingEnable = b + configMux.Unlock() + } +} + +func IsSniffing() bool { + return sniffingEnable +} + func init() { go process() } @@ -96,6 +109,7 @@ func UpdateProxies(newProxies map[string]C.Proxy, newProviders map[string]provid func UpdateSniffer(dispatcher *sniffer.SnifferDispatcher) { configMux.Lock() sniffer.Dispatcher = *dispatcher + sniffingEnable = true configMux.Unlock() } @@ -325,7 +339,7 @@ func handleTCPConn(connCtx C.ConnContext) { return } - if sniffer.Dispatcher.Enable() { + if sniffer.Dispatcher.Enable() && sniffingEnable { sniffer.Dispatcher.TCPSniff(connCtx.Conn(), metadata) }