used retryablehttp for api requests

dev
xm1k3 2023-03-01 16:50:21 +01:00
parent 3ea87e2186
commit ee6b1bef61
1 changed files with 5 additions and 32 deletions

View File

@ -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
}