forked from projectdiscovery/nuclei
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.go
88 lines (71 loc) · 1.77 KB
/
project.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
package projectfile
import (
"net/http"
"regexp"
"github.com/pkg/errors"
"github.com/projectdiscovery/hmap/store/hybrid"
)
var (
ErrNotFound = errors.New("not found")
regexUserAgent = regexp.MustCompile(`(?mi)\r\nUser-Agent: .+\r\n`)
regexDefaultInteract = regexp.MustCompile(`(?mi)[a-zA-Z1-9%.]+interact.sh`)
)
type Options struct {
Path string
Cleanup bool
}
type ProjectFile struct {
Path string
hm *hybrid.HybridMap
}
func New(options *Options) (*ProjectFile, error) {
var p ProjectFile
hOptions := hybrid.DefaultDiskOptions
hOptions.Path = options.Path
hOptions.Cleanup = options.Cleanup
var err error
p.hm, err = hybrid.New(hOptions)
if err != nil {
return nil, err
}
return &p, nil
}
func (pf *ProjectFile) cleanupData(data []byte) []byte {
// ignore all user agents
data = regexUserAgent.ReplaceAll(data, []byte("\r\n"))
// ignore interact markers
return regexDefaultInteract.ReplaceAll(data, []byte(""))
}
func (pf *ProjectFile) Get(req []byte) (*http.Response, error) {
reqHash, err := hash(pf.cleanupData(req))
if err != nil {
return nil, err
}
data, ok := pf.hm.Get(reqHash)
if !ok {
return nil, ErrNotFound
}
var httpRecord HTTPRecord
httpRecord.Response = newInternalResponse()
if err := unmarshal(data, &httpRecord); err != nil {
return nil, err
}
return fromInternalResponse(httpRecord.Response), nil
}
func (pf *ProjectFile) Set(req []byte, resp *http.Response, data []byte) error {
reqHash, err := hash(pf.cleanupData(req))
if err != nil {
return err
}
var httpRecord HTTPRecord
httpRecord.Request = req
httpRecord.Response = toInternalResponse(resp, data)
data, err = marshal(httpRecord)
if err != nil {
return err
}
return pf.hm.Set(reqHash, data)
}
func (pf *ProjectFile) Close() {
pf.hm.Close()
}