From 9cf4320e84f86c9bbb0c48c8e4c325a9104ff4dd Mon Sep 17 00:00:00 2001 From: "(inc0gbyt3)" <53656948+incogbyte@users.noreply.github.com> Date: Sat, 5 Oct 2024 08:53:31 -0300 Subject: [PATCH] Update api.go --- apishodan/api.go | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/apishodan/api.go b/apishodan/api.go index 0b0106c..0ad857f 100644 --- a/apishodan/api.go +++ b/apishodan/api.go @@ -41,23 +41,31 @@ func New(key string) *API { } func (s *API) InfoAccount() (*JsonData, error) { - - res, err := http.Get(fmt.Sprintf("%s/api-info?key=%s", URL, s.apiKey)) - - if err != nil { - fmt.Println(">> Something went wrong") - panic(err) - } - - defer res.Body.Close() - - var ret JsonData - - if err := json.NewDecoder(res.Body).Decode(&ret); err != nil { - return nil, err - } - return &ret, nil - + res, err := http.Get(fmt.Sprintf("%s/api-info?key=%s", URL, s.apiKey)) + if err != nil { + return nil, fmt.Errorf("failed to make request ( Info Account Shodan ): %v", err) + } + defer res.Body.Close() + + if res.StatusCode == http.StatusUnauthorized { + return nil, fmt.Errorf("authorization error: invalid Shodan API key (HTTP 401)") + } + + if res.StatusCode != http.StatusOK { + return nil, fmt.Errorf("unexpected status code: %d", res.StatusCode) + } + + body, err := io.ReadAll(res.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %v", err) + } + + var ret JsonData + if err := json.Unmarshal(body, &ret); err != nil { + return nil, fmt.Errorf("failed to decode JSON response: %v", err) + } + + return &ret, nil } func (s *API) GetSubdomain(domain string) (*JsonSubDomain, error) {