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

Update NetworksDB API queries #639

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
78 changes: 24 additions & 54 deletions datasrcs/networksdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

const (
networksdbBaseURL = "https://networksdb.io"
networksdbAPIPATH = "/api/v1"
networksdbAPIPATH = "/api"
)

var (
Expand Down Expand Up @@ -372,35 +372,28 @@ func (n *NetworksDB) apiIPQuery(ctx context.Context, addr string) (string, strin
}

var m struct {
Error string `json:"error"`
Total int `json:"total"`
Results []struct {
Org struct {
ID string `json:"id"`
} `json:"organisation"`
Network struct {
CIDR string `json:"cidr"`
} `json:"network"`
} `json:"results"`
Org struct {
ID string `json:"id"`
} `json:"organisation"`
Network struct {
CIDR string `json:"cidr"`
} `json:"network"`
}
if err := json.Unmarshal([]byte(page), &m); err != nil {
bus.Publish(requests.LogTopic, eventbus.PriorityHigh, fmt.Sprintf("%s: %s: %v", n.String(), u, err))
return "", ""
} else if m.Error != "" {
bus.Publish(requests.LogTopic, eventbus.PriorityHigh, fmt.Sprintf("%s: %s: %s", n.String(), u, m.Error))
return "", ""
} else if m.Total == 0 || len(m.Results) == 0 {
} else if m.Network.CIDR == "N/A" {
bus.Publish(requests.LogTopic, eventbus.PriorityHigh,
fmt.Sprintf("%s: %s: The request returned zero results", n.String(), u),
)
return "", ""
}

return m.Results[0].Network.CIDR, m.Results[0].Org.ID
return m.Network.CIDR, m.Org.ID
}

func (n *NetworksDB) getAPIIPURL() string {
return networksdbBaseURL + networksdbAPIPATH + "/ip/info"
return networksdbBaseURL + networksdbAPIPATH + "/ip-info"
}

func (n *NetworksDB) apiOrgInfoQuery(ctx context.Context, id string) []int {
Expand All @@ -420,30 +413,23 @@ func (n *NetworksDB) apiOrgInfoQuery(ctx context.Context, id string) []int {
}

var m struct {
Error string `json:"error"`
Total int `json:"total"`
Results []struct {
ASNs []int `json:"asns"`
} `json:"results"`
ASNs []int `json:"asns"`
}
if err := json.Unmarshal([]byte(page), &m); err != nil {
bus.Publish(requests.LogTopic, eventbus.PriorityHigh, fmt.Sprintf("%s: %s: %v", n.String(), u, err))
return []int{}
} else if m.Error != "" {
bus.Publish(requests.LogTopic, eventbus.PriorityHigh, fmt.Sprintf("%s: %s: %s", n.String(), u, m.Error))
return []int{}
} else if m.Total == 0 || len(m.Results[0].ASNs) == 0 {
} else if len(m.ASNs) == 0 {
bus.Publish(requests.LogTopic, eventbus.PriorityHigh,
fmt.Sprintf("%s: %s: The request returned zero results", n.String(), u),
)
return []int{}
}

return m.Results[0].ASNs
return m.ASNs
}

func (n *NetworksDB) getAPIOrgInfoURL() string {
return networksdbBaseURL + networksdbAPIPATH + "/org/info"
return networksdbBaseURL + networksdbAPIPATH + "/org-info"
}

func (n *NetworksDB) apiASNInfoQuery(ctx context.Context, asn int) *requests.ASNRequest {
Expand All @@ -463,40 +449,28 @@ func (n *NetworksDB) apiASNInfoQuery(ctx context.Context, asn int) *requests.ASN
}

var m struct {
Error string `json:"error"`
Total int `json:"total"`
Results []struct {
ASN int `json:"asn"`
ASName string `json:"as_name"`
Description string `json:"description"`
CountryCode string `json:"countrycode"`
Country string `json:"country"`
} `json:"results"`
ASN int `json:"asn"`
ASName string `json:"as_name"`
Description string `json:"description"`
CountryCode string `json:"countrycode"`
Country string `json:"country"`
}
if err := json.Unmarshal([]byte(page), &m); err != nil {
bus.Publish(requests.LogTopic, eventbus.PriorityHigh, fmt.Sprintf("%s: %s: %v", n.String(), u, err))
return nil
} else if m.Error != "" {
bus.Publish(requests.LogTopic, eventbus.PriorityHigh, fmt.Sprintf("%s: %s: %s", n.String(), u, m.Error))
return nil
} else if m.Total == 0 || len(m.Results) == 0 {
bus.Publish(requests.LogTopic, eventbus.PriorityHigh,
fmt.Sprintf("%s: %s: The request returned zero results", n.String(), u),
)
return nil
}

return &requests.ASNRequest{
ASN: m.Results[0].ASN,
CC: m.Results[0].CountryCode,
Description: m.Results[0].Description + ", " + m.Results[0].CountryCode,
ASN: m.ASN,
CC: m.CountryCode,
Description: m.Description + ", " + m.CountryCode,
Tag: n.SourceType,
Source: n.String(),
}
}

func (n *NetworksDB) getAPIASNInfoURL() string {
return networksdbBaseURL + networksdbAPIPATH + "/as/info"
return networksdbBaseURL + networksdbAPIPATH + "/asn-info"
}

func (n *NetworksDB) apiNetblocksQuery(ctx context.Context, asn int) stringset.Set {
Expand All @@ -518,7 +492,6 @@ func (n *NetworksDB) apiNetblocksQuery(ctx context.Context, asn int) stringset.S
}

var m struct {
Error string `json:"error"`
Total int `json:"total"`
Results []struct {
CIDR string `json:"cidr"`
Expand All @@ -527,9 +500,6 @@ func (n *NetworksDB) apiNetblocksQuery(ctx context.Context, asn int) stringset.S
if err := json.Unmarshal([]byte(page), &m); err != nil {
bus.Publish(requests.LogTopic, eventbus.PriorityHigh, fmt.Sprintf("%s: %s: %v", n.String(), u, err))
return netblocks
} else if m.Error != "" {
bus.Publish(requests.LogTopic, eventbus.PriorityHigh, fmt.Sprintf("%s: %s: %s", n.String(), u, m.Error))
return netblocks
} else if m.Total == 0 || len(m.Results) == 0 {
bus.Publish(requests.LogTopic, eventbus.PriorityHigh,
fmt.Sprintf("%s: %s: The request returned zero results", n.String(), u),
Expand All @@ -544,7 +514,7 @@ func (n *NetworksDB) apiNetblocksQuery(ctx context.Context, asn int) stringset.S
}

func (n *NetworksDB) getAPINetblocksURL() string {
return networksdbBaseURL + networksdbAPIPATH + "/as/networks"
return networksdbBaseURL + networksdbAPIPATH + "/asn-networks"
}

func (n *NetworksDB) getHeaders() map[string]string {
Expand Down