Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(potential) SIGV err #175

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions libs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const serverURL = "https://api.asnmap.sh/"

type Client struct {
url *url.URL
http http.Client
http *http.Client
}

// generatefullURL creates the complete URL with path, scheme, and host
Expand All @@ -34,7 +34,7 @@ func generateFullURL(host string) (*url.URL, error) {
}

if !stringsutil.EqualFoldAny(rawURL.Scheme, "http", "https") {
return nil, errors.New("Host should start with http or https.")
return nil, errors.New("host should start with http or https")
}

rawURL.Path = "api/v1/asnmap"
Expand Down Expand Up @@ -63,7 +63,7 @@ func NewClient() (*Client, error) {

client := Client{
url: URL,
http: http.Client{Transport: transCfg},
http: &http.Client{Transport: transCfg},
}
return &client, nil
}
Expand Down Expand Up @@ -149,14 +149,24 @@ func generateRawQuery(query, value string) string {
}

func (c Client) makeRequest() ([]byte, error) {
req, _ := http.NewRequest(http.MethodGet, c.url.String(), nil)
if c.http == nil {
return nil, errors.New("http client is not initialized")
}

req, err := http.NewRequest(http.MethodGet, c.url.String(), nil)
if err != nil {
return nil, err
}
res, err := c.http.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()

resBody, _ := io.ReadAll(res.Body)
resBody, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
return resBody, nil
}

Expand Down
14 changes: 8 additions & 6 deletions runner/runner_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runner

import (
"fmt"
"testing"

asnmap "github.com/projectdiscovery/asnmap/libs"
Expand All @@ -21,12 +22,12 @@ func TestRunner(t *testing.T) {
},
expectedOutput: []*asnmap.Response{
{
FirstIp: "102.129.206.0",
LastIp: "127.255.255.255",
FirstIp: "104.16.0.0",
LastIp: "104.21.127.255",
Input: "104.16.99.52",
ASN: 9498,
Country: "IN",
Org: "BBIL-AP BHARTI Airtel Ltd."},
ASN: 13335,
Country: "US",
Org: "CLOUDFLARENET"},
},
},
{
Expand Down Expand Up @@ -69,8 +70,9 @@ func TestRunner(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fmt.Println(tt.name)
tt.options.OnResult = func(o []*asnmap.Response) {
require.Equal(t, o, tt.expectedOutput)
require.Equal(t, tt.expectedOutput, o)
}
r, err := New(tt.options)
require.Nil(t, err)
Expand Down
Loading