feat: RESTful API support disable sniffer

This commit is contained in:
adlyq 2022-05-24 12:43:26 +08:00
parent c0eb9aac1c
commit 149b4b5b43
5 changed files with 35 additions and 16 deletions

View file

@ -47,13 +47,14 @@ type General struct {
UnifiedDelay bool UnifiedDelay bool
LogLevel log.LogLevel `json:"log-level"` LogLevel log.LogLevel `json:"log-level"`
IPv6 bool `json:"ipv6"` IPv6 bool `json:"ipv6"`
Interface string `json:"-"` Interface string `json:"interface-name"`
RoutingMark int `json:"-"` RoutingMark int `json:"-"`
GeodataMode bool `json:"geodata-mode"` GeodataMode bool `json:"geodata-mode"`
GeodataLoader string `json:"geodata-loader"` GeodataLoader string `json:"geodata-loader"`
TCPConcurrent bool `json:"tcp-concurrent"` TCPConcurrent bool `json:"tcp-concurrent"`
EnableProcess bool `json:"enable-process"` EnableProcess bool `json:"enable-process"`
Tun Tun `json:"tun"` Tun Tun `json:"tun"`
Sniffing bool `json:"sniffing"`
} }
// Inbound config // Inbound config

View file

@ -119,6 +119,8 @@ func GetGeneral() *config.General {
IPv6: !resolver.DisableIPv6, IPv6: !resolver.DisableIPv6,
GeodataLoader: G.LoaderName(), GeodataLoader: G.LoaderName(),
Tun: P.GetTunConf(), Tun: P.GetTunConf(),
Interface: dialer.DefaultInterface.Load(),
Sniffing: tunnel.IsSniffing(),
} }
return general return general

View file

@ -36,6 +36,7 @@ type configSchema struct {
Mode *tunnel.TunnelMode `json:"mode"` Mode *tunnel.TunnelMode `json:"mode"`
LogLevel *log.LogLevel `json:"log-level"` LogLevel *log.LogLevel `json:"log-level"`
IPv6 *bool `json:"ipv6"` IPv6 *bool `json:"ipv6"`
Sniffing *bool `json:"sniffing"`
} }
func getConfigs(w http.ResponseWriter, r *http.Request) { func getConfigs(w http.ResponseWriter, r *http.Request) {
@ -67,6 +68,10 @@ func patchConfigs(w http.ResponseWriter, r *http.Request) {
P.SetBindAddress(*general.BindAddress) P.SetBindAddress(*general.BindAddress)
} }
if general.Sniffing != nil {
tunnel.SetSniffing(*general.Sniffing)
}
ports := P.GetPorts() ports := P.GetPorts()
tcpIn := tunnel.TCPIn() tcpIn := tunnel.TCPIn()

View file

@ -6,7 +6,6 @@ import (
"github.com/Dreamacro/clash/listener/inner" "github.com/Dreamacro/clash/listener/inner"
"github.com/Dreamacro/clash/listener/tun/ipstack/commons" "github.com/Dreamacro/clash/listener/tun/ipstack/commons"
"net" "net"
"net/netip"
"runtime" "runtime"
"sort" "sort"
"strconv" "strconv"
@ -29,7 +28,6 @@ var (
allowLan = false allowLan = false
bindAddress = "*" bindAddress = "*"
lastTunConf *config.Tun lastTunConf *config.Tun
lastTunAddressPrefix *netip.Prefix
socksListener *socks.Listener socksListener *socks.Listener
socksUDPListener *socks.UDPListener socksUDPListener *socks.UDPListener
@ -467,5 +465,4 @@ func Cleanup(wait bool) {
} }
tunStackListener = nil tunStackListener = nil
lastTunConf = nil lastTunConf = nil
lastTunAddressPrefix = nil
} }

View file

@ -32,6 +32,7 @@ var (
proxies = make(map[string]C.Proxy) proxies = make(map[string]C.Proxy)
providers map[string]provider.ProxyProvider providers map[string]provider.ProxyProvider
ruleProviders map[string]provider.RuleProvider ruleProviders map[string]provider.RuleProvider
sniffingEnable bool
configMux sync.RWMutex configMux sync.RWMutex
// Outbound Rule // Outbound Rule
@ -43,6 +44,18 @@ var (
failTotal int failTotal int
) )
func SetSniffing(b bool) {
if sniffer.Dispatcher.Enable() {
configMux.Lock()
sniffingEnable = b
configMux.Unlock()
}
}
func IsSniffing() bool {
return sniffingEnable
}
func init() { func init() {
go process() go process()
} }
@ -96,6 +109,7 @@ func UpdateProxies(newProxies map[string]C.Proxy, newProviders map[string]provid
func UpdateSniffer(dispatcher *sniffer.SnifferDispatcher) { func UpdateSniffer(dispatcher *sniffer.SnifferDispatcher) {
configMux.Lock() configMux.Lock()
sniffer.Dispatcher = *dispatcher sniffer.Dispatcher = *dispatcher
sniffingEnable = true
configMux.Unlock() configMux.Unlock()
} }
@ -325,7 +339,7 @@ func handleTCPConn(connCtx C.ConnContext) {
return return
} }
if sniffer.Dispatcher.Enable() { if sniffer.Dispatcher.Enable() && sniffingEnable {
sniffer.Dispatcher.TCPSniff(connCtx.Conn(), metadata) sniffer.Dispatcher.TCPSniff(connCtx.Conn(), metadata)
} }