forked from vulncheck-oss/sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.go
200 lines (183 loc) · 5.96 KB
/
index.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package sdk
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
type IndexQueryParameters struct {
// INDEX RELATED
Cve string `json:"cve"`
Alias string `json:"alias"`
Iava string `json:"iava"`
LastModStartDate string `json:"lastModStartDate"`
LastModEndDate string `json:"lastModEndDate"`
PubStartDate string `json:"pubStartDate"`
PubEndDate string `json:"pubEndDate"`
ThreatActor string `json:"threat_actor"`
MitreId string `json:"mitre_id"`
MispId string `json:"misp_id"`
Ransomware string `json:"ransomware"`
Botnet string `json:"botnet"`
Hostname string `json:"hostname"`
ID string `json:"id"`
Kind string `json:"kind"`
Country string `json:"country"`
CountryCode string `json:"country_code"`
Asn string `json:"asn"`
Cidr string `json:"cidr"`
Ilvn string `json:"ilvn"`
Jvndb string `json:"jvndb"`
// PAGINATION RELATED
Limit int `json:"limit"`
Sort string `json:"sort"`
Order string `json:"order"`
Page int `json:"page"`
StartCursor bool `json:"start_cursor"`
Cursor string `json:"cursor"`
}
type IndexMeta struct {
Timestamp string `json:"timestamp"`
Index string `json:"index"`
Limit int `json:"limit"`
TotalDocuments int `json:"total_documents"`
Sort string `json:"sort"`
Parameters []IndexMetaParameters `json:"parameters"`
Order string `json:"order"`
Page int `json:"page"`
TotalPages int `json:"total_pages"`
MaxPages int `json:"max_pages"`
FirstItem int `json:"first_item"`
LastItem int `json:"last_item"`
NextCursor string `json:"next_cursor"`
}
type IndexMetaParameters struct {
Name string `json:"name"`
Format string `json:"format"`
}
type IndexResponse struct {
Benchmark float64 `json:"_benchmark"`
Meta IndexMeta `json:"_meta"`
Data []json.RawMessage `json:"data"`
}
// add method to set query parameters
func setIndexQueryParameters(query url.Values, queryParameters ...IndexQueryParameters) {
for _, queryParameter := range queryParameters {
// INDEX RELATED
if queryParameter.Cve != "" {
query.Add("cve", FormatCVE(queryParameter.Cve))
}
if queryParameter.Alias != "" {
query.Add("alias", queryParameter.Alias)
}
if queryParameter.Iava != "" {
query.Add("iava", queryParameter.Iava)
}
if queryParameter.LastModStartDate != "" {
query.Add("lastModStartDate", queryParameter.LastModStartDate)
}
if queryParameter.LastModEndDate != "" {
query.Add("lastModEndDate", queryParameter.LastModEndDate)
}
if queryParameter.PubStartDate != "" {
query.Add("pubStartDate", queryParameter.PubStartDate)
}
if queryParameter.PubEndDate != "" {
query.Add("pubEndDate", queryParameter.PubEndDate)
}
if queryParameter.ThreatActor != "" {
query.Add("threat_actor", queryParameter.ThreatActor)
}
if queryParameter.MitreId != "" {
query.Add("mitre_id", queryParameter.MitreId)
}
if queryParameter.MispId != "" {
query.Add("misp_id", queryParameter.MispId)
}
if queryParameter.Ransomware != "" {
query.Add("ransomware", queryParameter.Ransomware)
}
if queryParameter.Botnet != "" {
query.Add("botnet", queryParameter.Botnet)
}
if queryParameter.Hostname != "" {
query.Add("hostname", queryParameter.Hostname)
}
if queryParameter.ID != "" {
query.Add("id", queryParameter.ID)
}
if queryParameter.Kind != "" {
query.Add("kind", queryParameter.Kind)
}
if queryParameter.Country != "" {
query.Add("country", queryParameter.Country)
}
if queryParameter.CountryCode != "" {
query.Add("country_code", queryParameter.CountryCode)
}
if queryParameter.Asn != "" {
query.Add("asn", queryParameter.Asn)
}
if queryParameter.Cidr != "" {
query.Add("cidr", queryParameter.Cidr)
}
if queryParameter.Ilvn != "" {
query.Add("ilvn", queryParameter.Ilvn)
}
if queryParameter.Jvndb != "" {
query.Add("jvndb", queryParameter.Jvndb)
}
// PAGINATION RELATED
if queryParameter.Limit != 0 {
query.Add("limit", fmt.Sprintf("%d", queryParameter.Limit))
}
if queryParameter.Sort != "" {
query.Add("sort", queryParameter.Sort)
}
if queryParameter.Order != "" {
query.Add("order", queryParameter.Order)
}
if queryParameter.Page != 0 {
query.Add("page", fmt.Sprintf("%d", queryParameter.Page))
}
if queryParameter.StartCursor {
query.Add("start_cursor", fmt.Sprintf("%t", queryParameter.StartCursor))
}
if queryParameter.Cursor != "" {
query.Add("cursor", queryParameter.Cursor)
query.Del("start_cursor") // cursor and start_cursor are mutually exclusive
}
}
}
// https://docs.vulncheck.com/api/indice
func (c *Client) GetIndex(index string, queryParameters ...IndexQueryParameters) (responseJSON *IndexResponse, err error) {
client := &http.Client{}
req, err := http.NewRequest("GET", c.GetUrl()+"/v3/index/"+url.QueryEscape(index), nil)
if err != nil {
panic(err)
}
c.SetAuthHeader(req)
query := req.URL.Query()
setIndexQueryParameters(query, queryParameters...)
req.URL.RawQuery = query.Encode()
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
var metaError MetaError
_ = json.NewDecoder(resp.Body).Decode(&metaError)
return nil, fmt.Errorf("error: %v", metaError.Errors)
}
_ = json.NewDecoder(resp.Body).Decode(&responseJSON)
return responseJSON, nil
}
// Strings representation of the response
func (r IndexResponse) String() string {
return fmt.Sprintf("Benchmark: %f\nMeta: %v\nData: %v\n", r.Benchmark, r.Meta, r.Data)
}
// GetData - Returns the data from the response
func (r IndexResponse) GetData() []json.RawMessage {
return r.Data
}