From ee6b1bef61dce879c6fc13bebf58fbe2b0358eb0 Mon Sep 17 00:00:00 2001 From: xm1k3 Date: Wed, 1 Mar 2023 16:50:21 +0100 Subject: [PATCH] used retryablehttp for api requests --- v2/pkg/protocols/common/updatecheck/client.go | 37 +++---------------- 1 file changed, 5 insertions(+), 32 deletions(-) diff --git a/v2/pkg/protocols/common/updatecheck/client.go b/v2/pkg/protocols/common/updatecheck/client.go index d56682c7..3d4585c1 100644 --- a/v2/pkg/protocols/common/updatecheck/client.go +++ b/v2/pkg/protocols/common/updatecheck/client.go @@ -1,14 +1,10 @@ package updatecheck import ( - "context" "io" - "net/http" - "net/url" - "time" jsoniter "github.com/json-iterator/go" - "github.com/pkg/errors" + "github.com/projectdiscovery/retryablehttp-go" ) const ( @@ -32,14 +28,13 @@ func InitNucleiVersion(version string) { // GetLatestNucleiTemplatesVersion returns the latest version info for nuclei and templates repos func GetLatestNucleiTemplatesVersion() (*LatestVersion, error) { - body, err := callRegisterServer(VersionsCall) + resp, err := retryablehttp.DefaultClient().Get(RegisterServer + VersionsCall) if err != nil { return nil, err } - defer body.Close() data := make(map[string]string) - if err := jsoniter.NewDecoder(body).Decode(&data); err != nil { + if err := jsoniter.NewDecoder(resp.Body).Decode(&data); err != nil { return nil, err } return &LatestVersion{Nuclei: data["nuclei"], Templates: data["templates"], IgnoreHash: data["ignore-hash"]}, nil @@ -47,36 +42,14 @@ func GetLatestNucleiTemplatesVersion() (*LatestVersion, error) { // GetLatestIgnoreFile returns the latest version of nuclei ignore func GetLatestIgnoreFile() ([]byte, error) { - body, err := callRegisterServer(IgnoreCall) + resp, err := retryablehttp.DefaultClient().Get(RegisterServer + VersionsCall) if err != nil { return nil, err } - defer body.Close() - data, err := io.ReadAll(body) + data, err := io.ReadAll(resp.Body) if err != nil { return nil, err } return data, nil } - -// callRegisterServer makes a request to RegisterServer with a call. -func callRegisterServer(call string) (io.ReadCloser, error) { - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - - req, err := http.NewRequestWithContext(ctx, http.MethodGet, RegisterServer+call, nil) - if err != nil { - return nil, errors.Wrap(err, "could not make request") - } - if nucleiVersion != "" { - query := make(url.Values, 1) - query.Set("v", nucleiVersion) - req.URL.RawQuery = query.Encode() - } - resp, err := http.DefaultClient.Do(req) - if err != nil { - return nil, errors.Wrap(err, "could not do request") - } - return resp.Body, nil -}