-
Notifications
You must be signed in to change notification settings - Fork 45
/
cpe_analyzer.go
80 lines (69 loc) · 1.64 KB
/
cpe_analyzer.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
package webfingerprint
import (
"github.com/pkg/errors"
"sync"
)
type CPEAnalyzer struct {
WebsiteURLs []string
cpeOriginMap *sync.Map
cpeMap *sync.Map
}
func NewCPEAnalyzer(urls ...string) *CPEAnalyzer {
return &CPEAnalyzer{
WebsiteURLs: urls,
cpeMap: new(sync.Map),
cpeOriginMap: new(sync.Map),
}
}
func (c *CPEAnalyzer) Feed(url string, cpes ...*CPE) {
for _, cpe := range cpes {
c.cpeMap.Store(cpe.String(), cpe)
if _, ok := c.cpeOriginMap.Load(cpe.String()); !ok {
c.cpeOriginMap.Store(cpe.String(), url)
}
}
}
func (c *CPEAnalyzer) AvailableCPE() []*CPE {
var cpes []*CPE
c.cpeMap.Range(func(key, value interface{}) bool {
cpes = append(cpes, value.(*CPE))
return true
})
return cpes
}
func (c *CPEAnalyzer) IsProductExisted(product string) bool {
for _, cpe := range c.AvailableCPE() {
if cpe.Product == product {
return true
}
}
return false
}
func (c *CPEAnalyzer) GetCPEsByProduct(product string) []*CPE {
var cpes []*CPE
for _, cpe := range c.AvailableCPE() {
if cpe.Product == product {
cpes = append(cpes, cpe)
}
}
return cpes
}
func (c *CPEAnalyzer) GetVersionByProduct(product string) (string, error) {
cpes := c.GetCPEsByProduct(product)
if len(cpes) > 1 {
return "", errors.Errorf("failed: %s", "multi version")
} else if len(cpes) <= 0 {
return "", errors.New("no product cpe")
}
return cpes[0].Version, nil
}
func (c *CPEAnalyzer) QueryOrigins(cpes ...*CPE) map[*CPE]string {
results := map[*CPE]string{}
for _, value := range cpes {
if raw, ok := c.cpeOriginMap.Load(value.String()); ok {
origin := raw.(string)
results[value] = origin
}
}
return results
}