From b498d2dda26f0c64186713e3ba1e506d2ecd5edd Mon Sep 17 00:00:00 2001 From: MetaCubeX Date: Wed, 27 Apr 2022 22:54:12 +0800 Subject: [PATCH 1/5] chore: system err log --- listener/tun/ipstack/system/mars/nat/nat.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/listener/tun/ipstack/system/mars/nat/nat.go b/listener/tun/ipstack/system/mars/nat/nat.go index 4ff95db7..b8b197c5 100644 --- a/listener/tun/ipstack/system/mars/nat/nat.go +++ b/listener/tun/ipstack/system/mars/nat/nat.go @@ -1,6 +1,7 @@ package nat import ( + log "github.com/sirupsen/logrus" "io" "net" "net/netip" @@ -43,6 +44,7 @@ func Start(device io.ReadWriter, gateway, portal, broadcast netip.Addr) (*TCP, * for { n, err := device.Read(buf) if err != nil { + log.Warnln("system error:%s", err.Error()) continue } From 30eaa8add60bde3927e027cef95642a4e76d29a1 Mon Sep 17 00:00:00 2001 From: Skyxim Date: Thu, 28 Apr 2022 08:54:33 +0800 Subject: [PATCH 2/5] fix: count error --- rule/provider/classical_strategy.go | 4 +++- rule/provider/domain_strategy.go | 4 +++- rule/provider/ipcidr_strategy.go | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/rule/provider/classical_strategy.go b/rule/provider/classical_strategy.go index aba2a4e4..1c2cba4f 100644 --- a/rule/provider/classical_strategy.go +++ b/rule/provider/classical_strategy.go @@ -32,6 +32,7 @@ func (c *classicalStrategy) ShouldResolveIP() bool { func (c *classicalStrategy) OnUpdate(rules []string) { var classicalRules []C.Rule shouldResolveIP := false + count := 0 for _, rawRule := range rules { ruleType, rule, params := ruleParse(rawRule) r, err := parseRule(ruleType, rule, "", params) @@ -43,11 +44,12 @@ func (c *classicalStrategy) OnUpdate(rules []string) { } classicalRules = append(classicalRules, r) - c.count++ + count++ } } c.rules = classicalRules + c.count = count } func NewClassicalStrategy() *classicalStrategy { diff --git a/rule/provider/domain_strategy.go b/rule/provider/domain_strategy.go index 0288c8aa..81979658 100644 --- a/rule/provider/domain_strategy.go +++ b/rule/provider/domain_strategy.go @@ -27,16 +27,18 @@ func (d *domainStrategy) ShouldResolveIP() bool { func (d *domainStrategy) OnUpdate(rules []string) { domainTrie := trie.New[bool]() + count := 0 for _, rule := range rules { err := domainTrie.Insert(rule, true) if err != nil { log.Warnln("invalid domain:[%s]", rule) } else { - d.count++ + count++ } } d.domainRules = domainTrie + d.count = count } func ruleParse(ruleRaw string) (string, string, []string) { diff --git a/rule/provider/ipcidr_strategy.go b/rule/provider/ipcidr_strategy.go index 9853f47e..bba9ddc5 100644 --- a/rule/provider/ipcidr_strategy.go +++ b/rule/provider/ipcidr_strategy.go @@ -26,16 +26,18 @@ func (i *ipcidrStrategy) ShouldResolveIP() bool { func (i *ipcidrStrategy) OnUpdate(rules []string) { ipCidrTrie := trie.NewIpCidrTrie() + count := 0 for _, rule := range rules { err := ipCidrTrie.AddIpCidrForString(rule) if err != nil { log.Warnln("invalid Ipcidr:[%s]", rule) } else { - i.count++ + count++ } } i.trie = ipCidrTrie + i.count = count i.shouldResolveIP = i.count > 0 } From 2e74986fe409c280f89ca08ea913ffea05fcfe00 Mon Sep 17 00:00:00 2001 From: Skyxim Date: Thu, 28 Apr 2022 08:55:45 +0800 Subject: [PATCH 3/5] refactor: adjust provider loading order, remove meaningless pointers --- config/config.go | 8 +++--- hub/executor/executor.go | 53 +++++++++++++++++++++------------------- tunnel/tunnel.go | 6 ++--- 3 files changed, 35 insertions(+), 32 deletions(-) diff --git a/config/config.go b/config/config.go index b4874175..c6069c69 100644 --- a/config/config.go +++ b/config/config.go @@ -151,7 +151,7 @@ type Config struct { Users []auth.AuthUser Proxies map[string]C.Proxy Providers map[string]providerTypes.ProxyProvider - RuleProviders map[string]*providerTypes.RuleProvider + RuleProviders map[string]providerTypes.RuleProvider Sniffer *Sniffer } @@ -523,8 +523,8 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[ return proxies, providersMap, nil } -func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, map[string]*providerTypes.RuleProvider, error) { - ruleProviders := map[string]*providerTypes.RuleProvider{} +func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, map[string]providerTypes.RuleProvider, error) { + ruleProviders := map[string]providerTypes.RuleProvider{} log.Infoln("Geodata Loader mode: %s", geodata.LoaderName()) // parse rule provider for name, mapping := range cfg.RuleProvider { @@ -533,7 +533,7 @@ func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, map[strin return nil, nil, err } - ruleProviders[name] = &rp + ruleProviders[name] = rp RP.SetRuleProvider(rp) } diff --git a/hub/executor/executor.go b/hub/executor/executor.go index 404e7fe4..75476527 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -82,8 +82,9 @@ func ApplyConfig(cfg *config.Config, force bool) { updateTun(cfg.Tun, cfg.DNS) updateExperimental(cfg) updateHosts(cfg.Hosts) - loadProvider(cfg.RuleProviders, cfg.Providers) + loadProxyProvider(cfg.Providers) updateProfile(cfg) + loadRuleProvider(cfg.RuleProviders) log.SetLevel(cfg.General.LogLevel) } @@ -181,39 +182,41 @@ func updateProxies(proxies map[string]C.Proxy, providers map[string]provider.Pro tunnel.UpdateProxies(proxies, providers) } -func updateRules(rules []C.Rule, ruleProviders map[string]*provider.RuleProvider) { +func updateRules(rules []C.Rule, ruleProviders map[string]provider.RuleProvider) { tunnel.UpdateRules(rules, ruleProviders) } -func loadProvider(ruleProviders map[string]*provider.RuleProvider, proxyProviders map[string]provider.ProxyProvider) { - load := func(pv provider.Provider) { - if pv.VehicleType() == provider.Compatible { - log.Infoln("Start initial compatible provider %s", pv.Name()) - } else { - log.Infoln("Start initial provider %s", (pv).Name()) - } - - if err := (pv).Initial(); err != nil { - switch pv.Type() { - case provider.Proxy: - { - log.Warnln("initial proxy provider %s error: %v", (pv).Name(), err) - } - case provider.Rule: - { - log.Warnln("initial rule provider %s error: %v", (pv).Name(), err) - } +func loadProvider(pv provider.Provider) { + if pv.VehicleType() == provider.Compatible { + log.Infoln("Start initial compatible provider %s", pv.Name()) + } else { + log.Infoln("Start initial provider %s", (pv).Name()) + } + if err := (pv).Initial(); err != nil { + switch pv.Type() { + case provider.Proxy: + { + log.Warnln("initial proxy provider %s error: %v", (pv).Name(), err) } + case provider.Rule: + { + log.Warnln("initial rule provider %s error: %v", (pv).Name(), err) + } + } } +} - for _, proxyProvider := range proxyProviders { - load(proxyProvider) - } - +func loadRuleProvider(ruleProviders map[string]provider.RuleProvider) { for _, ruleProvider := range ruleProviders { - load(*ruleProvider) + loadProvider(ruleProvider) + } +} + +func loadProxyProvider(ruleProviders map[string]provider.ProxyProvider) { + for _, ruleProvider := range ruleProviders { + loadProvider(ruleProvider) } } diff --git a/tunnel/tunnel.go b/tunnel/tunnel.go index 6d0265d6..2108e814 100644 --- a/tunnel/tunnel.go +++ b/tunnel/tunnel.go @@ -30,7 +30,7 @@ var ( rules []C.Rule proxies = make(map[string]C.Proxy) providers map[string]provider.ProxyProvider - ruleProviders map[string]*provider.RuleProvider + ruleProviders map[string]provider.RuleProvider configMux sync.RWMutex // Outbound Rule @@ -62,7 +62,7 @@ func Rules() []C.Rule { } // UpdateRules handle update rules -func UpdateRules(newRules []C.Rule, rp map[string]*provider.RuleProvider) { +func UpdateRules(newRules []C.Rule, rp map[string]provider.RuleProvider) { configMux.Lock() rules = newRules ruleProviders = rp @@ -80,7 +80,7 @@ func Providers() map[string]provider.ProxyProvider { } // RuleProviders return all loaded rule providers -func RuleProviders() map[string]*provider.RuleProvider { +func RuleProviders() map[string]provider.RuleProvider { return ruleProviders } From 573be855f74105bdaa0e690f1d930e569c7a5153 Mon Sep 17 00:00:00 2001 From: Skyxim Date: Thu, 28 Apr 2022 09:07:58 +0800 Subject: [PATCH 4/5] fix: file not change modify time when updated rule --- rule/provider/fetcher.go | 1 + 1 file changed, 1 insertion(+) diff --git a/rule/provider/fetcher.go b/rule/provider/fetcher.go index c8204dae..c7ab9141 100644 --- a/rule/provider/fetcher.go +++ b/rule/provider/fetcher.go @@ -100,6 +100,7 @@ func (f *fetcher) Update() (interface{}, bool, error) { hash := md5.Sum(buf) if bytes.Equal(f.hash[:], hash[:]) { f.updatedAt = &now + os.Chtimes(f.vehicle.Path(), now, now) return nil, true, nil } From c161d5e6bee42473c75d929f94a64ee47fc57452 Mon Sep 17 00:00:00 2001 From: Skyxim Date: Thu, 28 Apr 2022 09:24:40 +0800 Subject: [PATCH 5/5] fix: inner request error --- adapter/inbound/socket.go | 9 ++------- adapter/provider/vehicle.go | 5 ----- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/adapter/inbound/socket.go b/adapter/inbound/socket.go index dea100e1..5aa90034 100644 --- a/adapter/inbound/socket.go +++ b/adapter/inbound/socket.go @@ -33,15 +33,10 @@ func NewInner(conn net.Conn, dst string, host string) *context.ConnContext { metadata.Host = host metadata.AddrType = C.AtypDomainName metadata.Process = C.ClashName - if ip, port, err := parseAddr(dst); err == nil { + if h, port, err := net.SplitHostPort(dst); err == nil { metadata.DstPort = port if host == "" { - metadata.DstIP = ip - if ip.Is4() { - metadata.AddrType = C.AtypIPv6 - } else { - metadata.AddrType = C.AtypIPv4 - } + metadata.Host = h } } diff --git a/adapter/provider/vehicle.go b/adapter/provider/vehicle.go index eba8423c..30282d82 100644 --- a/adapter/provider/vehicle.go +++ b/adapter/provider/vehicle.go @@ -2,7 +2,6 @@ package provider import ( "context" - "github.com/Dreamacro/clash/component/dialer" "github.com/Dreamacro/clash/listener/inner" "io" "net" @@ -86,10 +85,6 @@ func (h *HTTPVehicle) Read() ([]byte, error) { client := http.Client{Transport: transport} resp, err := client.Do(req) if err != nil { - transport.DialContext = func(ctx context.Context, network, address string) (net.Conn, error) { - return dialer.DialContext(ctx, network, address) - } - resp, err = client.Do(req) if err != nil { return nil, err }