From ba7bcce8959cc10c7acd1ece12a2a6ba2de69da8 Mon Sep 17 00:00:00 2001 From: yaling888 <73897884+yaling888@users.noreply.github.com> Date: Sat, 5 Mar 2022 18:04:04 +0800 Subject: [PATCH] Chore: code style --- README.md | 20 ++++++-------------- hub/executor/executor.go | 22 +++++++++++----------- hub/hub.go | 4 ++-- listener/listener.go | 4 ++-- main.go | 6 +++--- tunnel/tunnel.go | 9 +++++---- 6 files changed, 29 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index fcbdd513..7b83192e 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ On Windows, you should download the [Wintun](https://www.wintun.net) driver and # Enable the TUN listener tun: enable: true - stack: system # system or gvisor + stack: gvisor # system or gvisor dns-listen: 0.0.0.0:53 # additional dns server listen on TUN auto-route: true # auto set global route ``` @@ -93,7 +93,7 @@ rules: - DOMAIN-SUFFIX,example.com,DIRECT,tcp - DOMAIN-SUFFIX,example.com,REJECT,udp - # process(add 'P:' prefix) condition for all rules + # process condition for all rules (add 'P:' prefix) - DOMAIN-SUFFIX,example.com,REJECT,P:Google Chrome Helper # multiport condition for rules SRC-PORT and DST-PORT @@ -108,19 +108,14 @@ rules: - GEOSITE,facebook,PROXY - GEOSITE,youtube,PROXY - GEOSITE,geolocation-cn,DIRECT - - GEOSITE,gfw,PROXY - - GEOSITE,greatfire,PROXY - #- GEOSITE,geolocation-!cn,PROXY + - GEOSITE,geolocation-!cn,PROXY + # source IPCIDR condition for all rules in gateway proxy + #- GEOSITE,geolocation-!cn,REJECT,192.168.1.88/32,192.168.1.99/32 + - GEOIP,telegram,PROXY,no-resolve - GEOIP,private,DIRECT,no-resolve - GEOIP,cn,DIRECT - - # Not match condition for rule GEOIP - #- GEOIP,!cn,PROXY - - # source IPCIDR condition for all rules in gateway proxy - #- GEOSITE,geolocation-!cn,REJECT,192.168.1.88/32,192.168.1.99/32 - MATCH,PROXY ``` @@ -184,9 +179,6 @@ Add field `Process` to `Metadata` and prepare to get process name for Restful AP To display process name in GUI please use https://yaling888.github.io/yacd/. -## Premium Release -[Release](https://github.com/Dreamacro/clash/releases/tag/premium) - ## Development If you want to build an application that uses clash as a library, check out the the [GitHub Wiki](https://github.com/Dreamacro/clash/wiki/use-clash-as-a-library) diff --git a/hub/executor/executor.go b/hub/executor/executor.go index 3252602a..a26d93ec 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -6,7 +6,6 @@ import ( "os" "runtime" "strconv" - "strings" "sync" "github.com/Dreamacro/clash/adapter" @@ -73,6 +72,8 @@ func ApplyConfig(cfg *config.Config, force bool) { mux.Lock() defer mux.Unlock() + log.SetLevel(log.DEBUG) + updateUsers(cfg.Users) updateProxies(cfg.Proxies, cfg.Providers) updateRules(cfg.Rules) @@ -82,6 +83,8 @@ func ApplyConfig(cfg *config.Config, force bool) { updateDNS(cfg.DNS, cfg.General) updateGeneral(cfg.General, force) updateExperimental(cfg) + + log.SetLevel(cfg.General.LogLevel) } func GetGeneral() *config.General { @@ -152,9 +155,7 @@ func updateDNS(c *config.DNS, general *config.General) { resolver.DefaultResolver = r resolver.MainResolver = mr resolver.DefaultHostMapper = m - if general.Tun.Enable && strings.EqualFold(general.Tun.Stack, "system") { - resolver.DefaultLocalServer = dns.NewLocalServer(r, m) - } + resolver.DefaultLocalServer = dns.NewLocalServer(r, m) dns.ReCreateServer(c.Listen, r, m) } @@ -172,7 +173,6 @@ func updateRules(rules []C.Rule) { } func updateGeneral(general *config.General, force bool) { - log.SetLevel(log.DEBUG) tunnel.SetMode(general.Mode) resolver.DisableIPv6 = !general.IPv6 @@ -200,7 +200,6 @@ func updateGeneral(general *config.General, force bool) { iface.FlushCache() if !force { - log.SetLevel(general.LogLevel) return } @@ -219,8 +218,6 @@ func updateGeneral(general *config.General, force bool) { P.ReCreateTProxy(general.TProxyPort, tcpIn, udpIn) P.ReCreateMixed(general.MixedPort, tcpIn, udpIn) P.ReCreateTun(general.Tun, tcpIn, udpIn) - - log.SetLevel(general.LogLevel) } func updateUsers(users []auth.AuthUser) { @@ -283,7 +280,10 @@ func updateIPTables(dns *config.DNS, general *config.General) { tproxy.CleanUpTProxyLinuxIPTables() - dialer.DefaultRoutingMark.Store(2158) + if dialer.DefaultRoutingMark.Load() == 0 { + dialer.DefaultRoutingMark.Store(2158) + } + err = tproxy.SetTProxyLinuxIPTables(general.Interface, general.TProxyPort, dnsPort) if err != nil { @@ -292,8 +292,8 @@ func updateIPTables(dns *config.DNS, general *config.General) { } } -func CleanUp() { - P.CleanUp() +func Cleanup() { + P.Cleanup() if runtime.GOOS == "linux" { tproxy.CleanUpTProxyLinuxIPTables() diff --git a/hub/hub.go b/hub/hub.go index 5b80ab66..cde0bb57 100644 --- a/hub/hub.go +++ b/hub/hub.go @@ -49,6 +49,6 @@ func Parse(options ...Option) error { return nil } -func CleanUp() { - executor.CleanUp() +func Cleanup() { + executor.Cleanup() } diff --git a/listener/listener.go b/listener/listener.go index 9ec69b14..c477ed4b 100644 --- a/listener/listener.go +++ b/listener/listener.go @@ -403,8 +403,8 @@ func genAddr(host string, port int, allowLan bool) string { return fmt.Sprintf("127.0.0.1:%d", port) } -// CleanUp clean up something -func CleanUp() { +// Cleanup clean up something +func Cleanup() { if runtime.GOOS == "windows" { if tunAdapter != nil { tunAdapter.Close() diff --git a/main.go b/main.go index e4481f13..82c79f3b 100644 --- a/main.go +++ b/main.go @@ -104,9 +104,9 @@ func main() { signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM) <-sigCh - // clean up - log.Warnln("Clash clean up") - hub.CleanUp() + // cleanup + log.Warnln("Clash cleanup") + hub.Cleanup() log.Warnln("Clash shutting down") } diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index 907d06bd..964cb5fe 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -34,7 +34,8 @@ var ( // default timeout for UDP session udpTimeout = 60 * time.Second - preProcessCacheFinder, _ = R.NewProcess("", "", nil) + // a default rule type of process, use to pre resolve process name + defaultProcessRule, _ = R.NewProcess("", "", nil) ) func init() { @@ -149,6 +150,9 @@ func preHandleMetadata(metadata *C.Metadata) error { } } + // pre resolve process name + defaultProcessRule.Match(metadata) + return nil } @@ -318,9 +322,6 @@ func match(metadata *C.Metadata) (C.Proxy, C.Rule, error) { resolved = true } - // preset process name and cache it - preProcessCacheFinder.Match(metadata) - for _, rule := range rules { if !resolved && shouldResolveIP(rule, metadata) { ip, err := resolver.ResolveIP(metadata.Host)