From c52e689d0d8e056a56e81f96ba0422bcb3d9b28a Mon Sep 17 00:00:00 2001 From: Skyxim Date: Sat, 4 Jun 2022 19:12:50 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20classical=20rule-set=20=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E6=9C=AA=E6=B8=85=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rule/provider/classical_strategy.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/rule/provider/classical_strategy.go b/rule/provider/classical_strategy.go index 4ce69389..297ad32f 100644 --- a/rule/provider/classical_strategy.go +++ b/rule/provider/classical_strategy.go @@ -30,20 +30,24 @@ func (c *classicalStrategy) ShouldResolveIP() bool { } func (c *classicalStrategy) OnUpdate(rules []string) { + var classicalRules []C.Rule + shouldResolveIP := false for _, rawRule := range rules { ruleType, rule, params := ruleParse(rawRule) r, err := parseRule(ruleType, rule, "", params) if err != nil { log.Warnln("parse rule error:[%s]", err.Error()) } else { - if !c.shouldResolveIP { - c.shouldResolveIP = r.ShouldResolveIP() + if !shouldResolveIP { + shouldResolveIP = r.ShouldResolveIP() } - c.rules = append(c.rules, r) - c.count++ + classicalRules = append(classicalRules, r) } } + + c.rules = classicalRules + c.count = len(classicalRules) } func NewClassicalStrategy() *classicalStrategy { From 3827e00b548575544af541e572cc56ea3a27ca20 Mon Sep 17 00:00:00 2001 From: Skyxim Date: Sat, 4 Jun 2022 19:14:39 +0800 Subject: [PATCH 2/3] =?UTF-8?q?refactor:=20=E6=8A=BD=E7=A6=BBhttp=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- adapter/provider/vehicle.go | 46 ++------------------------ common/net/http.go | 5 --- component/http/http.go | 64 +++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 48 deletions(-) delete mode 100644 common/net/http.go create mode 100644 component/http/http.go diff --git a/adapter/provider/vehicle.go b/adapter/provider/vehicle.go index 289ab3a0..1eb5df83 100644 --- a/adapter/provider/vehicle.go +++ b/adapter/provider/vehicle.go @@ -2,16 +2,12 @@ package provider import ( "context" - "github.com/Dreamacro/clash/listener/inner" + netHttp "github.com/Dreamacro/clash/component/http" + types "github.com/Dreamacro/clash/constant/provider" "io" - "net" "net/http" - "net/url" "os" "time" - - netHttp "github.com/Dreamacro/clash/common/net" - types "github.com/Dreamacro/clash/constant/provider" ) type FileVehicle struct { @@ -50,52 +46,16 @@ func (h *HTTPVehicle) Path() string { func (h *HTTPVehicle) Read() ([]byte, error) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) defer cancel() - - uri, err := url.Parse(h.url) + resp, err := netHttp.HttpRequest(ctx, h.url, http.MethodGet, nil, nil) if err != nil { return nil, err } - req, err := http.NewRequest(http.MethodGet, uri.String(), nil) - req.Header.Set("User-Agent", netHttp.UA) - - if err != nil { - return nil, err - } - - if user := uri.User; user != nil { - password, _ := user.Password() - req.SetBasicAuth(user.Username(), password) - } - - req = req.WithContext(ctx) - - transport := &http.Transport{ - // from http.DefaultTransport - MaxIdleConns: 100, - IdleConnTimeout: 30 * time.Second, - TLSHandshakeTimeout: 10 * time.Second, - ExpectContinueTimeout: 1 * time.Second, - DialContext: func(ctx context.Context, network, address string) (net.Conn, error) { - conn := inner.HandleTcp(address, uri.Hostname()) - return conn, nil - }, - } - - client := http.Client{Transport: transport} - resp, err := client.Do(req) - if err != nil { - if err != nil { - return nil, err - } - } defer resp.Body.Close() - buf, err := io.ReadAll(resp.Body) if err != nil { return nil, err } - return buf, nil } diff --git a/common/net/http.go b/common/net/http.go deleted file mode 100644 index ae10f7fc..00000000 --- a/common/net/http.go +++ /dev/null @@ -1,5 +0,0 @@ -package net - -const ( - UA = "Clash" -) diff --git a/component/http/http.go b/component/http/http.go new file mode 100644 index 00000000..c534b2ee --- /dev/null +++ b/component/http/http.go @@ -0,0 +1,64 @@ +package http + +import ( + "context" + "github.com/Dreamacro/clash/listener/inner" + "github.com/Dreamacro/clash/log" + "io" + "net" + "net/http" + URL "net/url" + "strings" + "time" +) + +const ( + UA = "Clash" +) + +func HttpRequest(ctx context.Context, url, method string, header map[string][]string, body io.Reader) (*http.Response, error) { + method = strings.ToUpper(method) + urlRes, err := URL.Parse(url) + if err != nil { + return nil, err + } + + req, err := http.NewRequest(method, urlRes.String(), body) + for k, v := range header { + for _, v := range v { + req.Header.Add(k, v) + } + } + + if _, ok := header["User-Agent"]; !ok { + req.Header.Set("User-Agent", UA) + } + + if err != nil { + return nil, err + } + + if user := urlRes.User; user != nil { + password, _ := user.Password() + req.SetBasicAuth(user.Username(), password) + } + + req = req.WithContext(ctx) + + transport := &http.Transport{ + // from http.DefaultTransport + MaxIdleConns: 100, + IdleConnTimeout: 30 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + DialContext: func(ctx context.Context, network, address string) (net.Conn, error) { + log.Infoln(urlRes.String()) + conn := inner.HandleTcp(address, urlRes.Hostname()) + return conn, nil + }, + } + + client := http.Client{Transport: transport} + return client.Do(req) + +} From 8ff7e180a490ec31b53293df27fa12622f31fa45 Mon Sep 17 00:00:00 2001 From: Skyxim Date: Sat, 4 Jun 2022 19:15:30 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=E5=BD=93=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E6=97=B6=EF=BC=8C=E5=AE=9A=E6=97=B6=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=A4=B1=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- adapter/provider/provider.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adapter/provider/provider.go b/adapter/provider/provider.go index 1a3850a6..4fc30a64 100644 --- a/adapter/provider/provider.go +++ b/adapter/provider/provider.go @@ -75,7 +75,7 @@ func (pp *proxySetProvider) Initial() error { pp.onUpdate(elm) if pp.healthCheck.auto() { - go pp.healthCheck.process() + defer func() { go pp.healthCheck.process() }() } return nil