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) + +}