-
Notifications
You must be signed in to change notification settings - Fork 18
/
types.go
75 lines (66 loc) · 1.84 KB
/
types.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
package awesomesearchqueries
import (
"encoding/json"
"errors"
"fmt"
"os"
)
// ====== NOTE: Update https://github.com/projectdiscovery/cvemap.git before planning any breaking changes ====
// Custom type that can be either a string or a slice of strings
type StringOrSlice struct {
Value []string
}
// UnmarshalJSON customizes the unmarshaling of the JSON
func (s *StringOrSlice) UnmarshalJSON(data []byte) error {
// Try to unmarshal as a string slice first
var sliceValue []string
if err := json.Unmarshal(data, &sliceValue); err == nil {
s.Value = sliceValue
return nil
}
// If slice unmarshaling fails, try as a single string
var stringValue string
if err := json.Unmarshal(data, &stringValue); err == nil {
s.Value = []string{stringValue}
return nil
}
// If both fail, return an error
return fmt.Errorf("value is neither a string nor a slice of strings")
}
type Query struct {
Name string `json:"name"`
Vendor StringOrSlice `json:"vendor"`
Type string `json:"type"`
Engines []Engine `json:"engines"`
}
type Engine struct {
Platform string `json:"platform"`
Queries []string `json:"queries"`
}
// LoadShodanQueries only loads queries that are for shodan
func LoadShodanQueries(filePath string) ([]Query, error) {
var queries []Query
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
err = json.NewDecoder(file).Decode(&queries)
if err != nil {
return nil, errors.Join(err, fmt.Errorf("failed to load shodan queries from %s", filePath))
}
filteredQueries := []Query{}
for _, query := range queries {
eng := []Engine{}
for _, engine := range query.Engines {
if engine.Platform == "shodan" {
eng = append(eng, engine)
}
}
if len(eng) > 0 {
query.Engines = eng
filteredQueries = append(filteredQueries, query)
}
}
return filteredQueries, nil
}