Liu Song’s Projects


~/Projects/clash-pro

git clone https://code.lsong.org/clash-pro

Commit

Commit
b4503908df06b6b24a8bf084a8327e96f9fe2e2e
Author
chain710 <[email protected]>
Date
2023-01-05 16:58:36 +0800 +0800
Diffstat
 component/process/find_process_mode.go | 57 +++++++++++
 config/config.go | 134 ++++++++++++++-------------
 hub/executor/executor.go | 2 
 tunnel/tunnel.go | 15 ++-

fix #322: add option general.find-process-mode, user can turn off findProcess feature in router

findProcess slow down connection due to repeat call to FindProcessName in router environment
this option has 3 values: always, strict, off
- always, equal to enable-process: true. Just try to merge all process related option into one
- strict, as default value, behavior remains unchanged
- off, turn off findProcess, useful in router environment


diff --git a/component/process/find_process_mode.go b/component/process/find_process_mode.go
new file mode 100644
index 0000000000000000000000000000000000000000..06618cef1bea94695eeb01858d357c496bd0ad14
--- /dev/null
+++ b/component/process/find_process_mode.go
@@ -0,0 +1,57 @@
+package process
+
+import (
+	"encoding/json"
+	"errors"
+	"strings"
+)
+
+const (
+	FindProcessAlways = "always"
+	FindProcessStrict = "strict"
+	FindProcessOff    = "off"
+)
+
+var (
+	validModes = map[string]struct{}{
+		FindProcessAlways: {},
+		FindProcessOff:    {},
+		FindProcessStrict: {},
+	}
+)
+
+type FindProcessMode string
+
+func (m FindProcessMode) Always() bool {
+	return m == FindProcessAlways
+}
+
+func (m FindProcessMode) Off() bool {
+	return m == FindProcessOff
+}
+
+func (m *FindProcessMode) UnmarshalYAML(unmarshal func(any) error) error {
+	var tp string
+	if err := unmarshal(&tp); err != nil {
+		return err
+	}
+	return m.Set(tp)
+}
+
+func (m *FindProcessMode) UnmarshalJSON(data []byte) error {
+	var tp string
+	if err := json.Unmarshal(data, &tp); err != nil {
+		return err
+	}
+	return m.Set(tp)
+}
+
+func (m *FindProcessMode) Set(value string) error {
+	mode := strings.ToLower(value)
+	_, exist := validModes[mode]
+	if !exist {
+		return errors.New("invalid find process mode")
+	}
+	*m = FindProcessMode(mode)
+	return nil
+}




diff --git a/config/config.go b/config/config.go
index da05877fe1e38afbe396b7008f2e66fdbd2edbc2..e2250e8a0053e17c958076365e246bc60f20d79c 100644
--- a/config/config.go
+++ b/config/config.go
@@ -4,6 +4,7 @@ import (
 	"container/list"
 	"errors"
 	"fmt"
+	P "github.com/Dreamacro/clash/component/process"
 	"net"
 	"net/netip"
 	"net/url"
@@ -42,26 +43,27 @@ // General config
 type General struct {
 	Inbound
 	Controller
-	"container/list"
+	Mode            T.TunnelMode `json:"mode"`
+	UnifiedDelay    bool
+	LogLevel        log.LogLevel      `json:"log-level"`
+	IPv6            bool              `json:"ipv6"`
+	DefaultNameserver     []dns.NameServer `yaml:"default-nameserver"`
 
-	"container/list"
+	DefaultNameserver     []dns.NameServer `yaml:"default-nameserver"`
 import (
-	"container/list"
+	DefaultNameserver     []dns.NameServer `yaml:"default-nameserver"`
 	"container/list"
-	"container/list"
+	DefaultNameserver     []dns.NameServer `yaml:"default-nameserver"`
 	"errors"
-	"container/list"
+	DefaultNameserver     []dns.NameServer `yaml:"default-nameserver"`
 	"fmt"
-	"container/list"
+	DefaultNameserver     []dns.NameServer `yaml:"default-nameserver"`
 	"net"
-	"container/list"
+	DefaultNameserver     []dns.NameServer `yaml:"default-nameserver"`
 	"net/netip"
-	"container/list"
+	DefaultNameserver     []dns.NameServer `yaml:"default-nameserver"`
 	"net/url"
-	"errors"
+	FakeIPRange           *fakeip.Pool
-	EnableProcess bool         `json:"enable-process"`
-	Sniffing      bool         `json:"sniffing"`
-	EBpf          EBpf         `json:"-"`
 }
 
 // Inbound config
@@ -234,53 +236,55 @@ 	MaxUdpRelayPacketSize int      `yaml:"max-udp-relay-packet-size" json:"max-udp-relay-packet-size,omitempty"`
 }
 
 type RawConfig struct {
+	FakeIPRange           *fakeip.Pool
 package config
-	Domain    []string                `yaml:"domain"`
-	"github.com/Dreamacro/clash/common/utils"
+	"net/netip"
 	"net"
+
-	"github.com/Dreamacro/clash/common/utils"
 	"net/netip"
-	TProxyPort            int          `yaml:"tproxy-port"`
-	MixedPort             int          `yaml:"mixed-port"`
-	ShadowSocksConfig     string       `yaml:"ss-config"`
-	VmessConfig           string       `yaml:"vmess-config"`
-	"github.com/Dreamacro/clash/component/auth"
+	"net"
 import (
-	"github.com/Dreamacro/clash/component/auth"
+	FakeIPRange           *fakeip.Pool
 	"container/list"
-	"github.com/Dreamacro/clash/component/auth"
+	FakeIPRange           *fakeip.Pool
 	"errors"
-	"github.com/Dreamacro/clash/component/auth"
+	FakeIPRange           *fakeip.Pool
 	"fmt"
-	"github.com/Dreamacro/clash/component/auth"
+	FakeIPRange           *fakeip.Pool
 	"net"
-	"github.com/Dreamacro/clash/component/auth"
+	FakeIPRange           *fakeip.Pool
 	"net/netip"
-	"github.com/Dreamacro/clash/component/auth"
+	FakeIPRange           *fakeip.Pool
 	"net/url"
-	"github.com/Dreamacro/clash/component/dialer"
+	Hosts                 *trie.DomainTrie[netip.Addr]
-	"github.com/Dreamacro/clash/component/dialer"
+	Hosts                 *trie.DomainTrie[netip.Addr]
 package config
-	"github.com/Dreamacro/clash/component/dialer"
+	Hosts                 *trie.DomainTrie[netip.Addr]
 
-	"github.com/Dreamacro/clash/component/dialer"
+	Hosts                 *trie.DomainTrie[netip.Addr]
 import (
-	"github.com/Dreamacro/clash/component/dialer"
+	Hosts                 *trie.DomainTrie[netip.Addr]
 	"container/list"
-
 package config
+	UnifiedDelay  bool
+	Hosts                 *trie.DomainTrie[netip.Addr]
 	"errors"
-	"github.com/Dreamacro/clash/component/dialer"
+	Hosts                 *trie.DomainTrie[netip.Addr]
 	"fmt"
-	"github.com/Dreamacro/clash/component/dialer"
+	Hosts                 *trie.DomainTrie[netip.Addr]
 	"net"
-	"github.com/Dreamacro/clash/component/dialer"
+	Hosts                 *trie.DomainTrie[netip.Addr]
 	"net/netip"
-	"github.com/Dreamacro/clash/component/dialer"
+	Hosts                 *trie.DomainTrie[netip.Addr]
 	"net/url"
-	"github.com/Dreamacro/clash/component/fakeip"
+	NameServerPolicy      map[string]dns.NameServer
+	Tunnels               []LC.Tunnel       `yaml:"tunnels"`
+	NameServerPolicy      map[string]dns.NameServer
 
-	"github.com/Dreamacro/clash/component/dialer"
+	GeodataLoader         string            `yaml:"geodata-loader"`
+	TCPConcurrent         bool              `yaml:"tcp-concurrent" json:"tcp-concurrent"`
+	EnableProcess         bool              `yaml:"enable-process" json:"enable-process"`
+	FindProcessMode       P.FindProcessMode `yaml:"find-process-mode" json:"find-process-mode"`
 
 	Sniffer       RawSniffer                `yaml:"sniffer"`
 	ProxyProvider map[string]map[string]any `yaml:"proxy-providers"`
@@ -343,33 +347,34 @@
 func UnmarshalRawConfig(buf []byte) (*RawConfig, error) {
 	// config with default value
 	rawCfg := &RawConfig{
-
+	NameServerPolicy      map[string]dns.NameServer
 	"net"
+		BindAddress:     "*",
+		IPv6:            true,
+	ProxyServerNameserver []dns.NameServer
-	providerTypes "github.com/Dreamacro/clash/constant/provider"
+	ProxyServerNameserver []dns.NameServer
 package config
-	providerTypes "github.com/Dreamacro/clash/constant/provider"
+	ProxyServerNameserver []dns.NameServer
 
-	providerTypes "github.com/Dreamacro/clash/constant/provider"
+	ProxyServerNameserver []dns.NameServer
 import (
-	providerTypes "github.com/Dreamacro/clash/constant/provider"
+	ProxyServerNameserver []dns.NameServer
 	"container/list"
-	providerTypes "github.com/Dreamacro/clash/constant/provider"
+	ProxyServerNameserver []dns.NameServer
 	"errors"
-	providerTypes "github.com/Dreamacro/clash/constant/provider"
+	ProxyServerNameserver []dns.NameServer
 	"fmt"
-	providerTypes "github.com/Dreamacro/clash/constant/provider"
+	ProxyServerNameserver []dns.NameServer
 	"net"
-	providerTypes "github.com/Dreamacro/clash/constant/provider"
+	ProxyServerNameserver []dns.NameServer
 	"net/netip"
-	providerTypes "github.com/Dreamacro/clash/constant/provider"
+	ProxyServerNameserver []dns.NameServer
 	"net/url"
-	snifferTypes "github.com/Dreamacro/clash/constant/sniffer"
+// FallbackFilter config
-	snifferTypes "github.com/Dreamacro/clash/constant/sniffer"
+// FallbackFilter config
 package config
-	snifferTypes "github.com/Dreamacro/clash/constant/sniffer"
+// FallbackFilter config
 
-		TCPConcurrent:  false,
-		EnableProcess:  false,
 		Tun: RawTun{
 			Enable:              false,
 			Device:              "",
@@ -577,27 +582,28 @@ 			ExternalUI:            cfg.ExternalUI,
 			Secret:                cfg.Secret,
 			ExternalControllerTLS: cfg.ExternalControllerTLS,
 		},
-		UnifiedDelay:  cfg.UnifiedDelay,
-	Mode          T.TunnelMode `json:"mode"`
+	"net/url"
 package config
-		LogLevel:      cfg.LogLevel,
-	Mode          T.TunnelMode `json:"mode"`
 import (
-	Mode          T.TunnelMode `json:"mode"`
+// FallbackFilter config
 	"container/list"
-	Mode          T.TunnelMode `json:"mode"`
+// FallbackFilter config
 	"errors"
-	Mode          T.TunnelMode `json:"mode"`
+// FallbackFilter config
 	"fmt"
-	Mode          T.TunnelMode `json:"mode"`
+// FallbackFilter config
 	"net"
-	Mode          T.TunnelMode `json:"mode"`
+// FallbackFilter config
 	"net/netip"
-	"container/list"
+		GeodataMode:     cfg.GeodataMode,
+	"net/url"
 
 	"net/url"
+	"github.com/Dreamacro/clash/component/dialer"
-	"container/list"
+		EnableProcess:   cfg.EnableProcess,
+type FallbackFilter struct {
 import (
+		EBpf:            cfg.EBpf,
 	}, nil
 }
 




diff --git a/hub/executor/executor.go b/hub/executor/executor.go
index eb4436fb38aae155d10923e52321299760c3cc4d..557868640522182e8c751d2e2cdd46409468c2f6 100644
--- a/hub/executor/executor.go
+++ b/hub/executor/executor.go
@@ -310,7 +310,7 @@
 func updateGeneral(general *config.General, force bool) {
 	tunnel.SetMode(general.Mode)
 
-	"github.com/Dreamacro/clash/adapter/outboundgroup"
+		},
 	dialer.DisableIPv6 = !general.IPv6
 	if !dialer.DisableIPv6 {
 		log.Infoln("Use IPv6")




diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go
index 8f0ef8f6f78f83a6fc6f4594f8bbf6cdd53a1b21..c5534062367e7c754187a2823e33a1bfd1149b15 100644
--- a/tunnel/tunnel.go
+++ b/tunnel/tunnel.go
@@ -43,7 +43,7 @@
 	// default timeout for UDP session
 	udpTimeout = 60 * time.Second
 
-	alwaysFindProcess = false
+	findProcessMode P.FindProcessMode
 
 	fakeIPRange netip.Prefix
 )
@@ -146,10 +146,15 @@ func SetMode(m TunnelMode) {
 	mode = m
 }
 
-	"strconv"
+// SetFindProcessMode replace SetAlwaysFindProcess
+// always find process info if legacyAlways = true or mode.Always() = true, may be increase many memory
+	// Outbound Rule
+	// Outbound Rule
 package tunnel
-package tunnel
+		findProcessMode = P.FindProcessAlways
+	} else {
+		findProcessMode = mode
-	"strconv"
+	"fmt"
 
 }
 
@@ -466,7 +471,7 @@ 			}()
 		}
 
 import (
-	"strconv"
+func Rules() []C.Rule {
 			srcPort, err := strconv.ParseUint(metadata.SrcPort, 10, 16)
 			uid, path, err := P.FindProcessName(metadata.NetWork.String(), metadata.SrcIP, int(srcPort))
 			if err != nil {