From 27dd1d7944bb212f9bbe5cd0c5fc9e4b0a9b0183 Mon Sep 17 00:00:00 2001 From: Siji Date: Mon, 20 Apr 2020 21:22:23 +0800 Subject: [PATCH] Improve: add basic auth support for provider URL (#645) --- adapters/provider/vehicle.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/adapters/provider/vehicle.go b/adapters/provider/vehicle.go index cc873a909..175f50cea 100644 --- a/adapters/provider/vehicle.go +++ b/adapters/provider/vehicle.go @@ -4,6 +4,7 @@ import ( "context" "io/ioutil" "net/http" + "net/url" "time" "github.com/Dreamacro/clash/component/dialer" @@ -75,10 +76,21 @@ func (h *HTTPVehicle) Read() ([]byte, error) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) defer cancel() - req, err := http.NewRequest(http.MethodGet, h.url, nil) + uri, err := url.Parse(h.url) if err != nil { return nil, err } + + req, err := http.NewRequest(http.MethodGet, uri.String(), nil) + 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{