Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/types/resume.go
2070 views
1
package types
2
3
import (
4
"fmt"
5
"math"
6
"path/filepath"
7
"sync"
8
9
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
10
"github.com/rs/xid"
11
)
12
13
// Default resume file
14
const DefaultResumeFileName = "resume-%s.cfg"
15
16
func DefaultResumeFilePath() string {
17
configDir := config.DefaultConfig.GetCacheDir()
18
resumeFile := filepath.Join(configDir, fmt.Sprintf(DefaultResumeFileName, xid.New().String()))
19
return resumeFile
20
}
21
22
// ResumeCfg contains the scan progression
23
type ResumeCfg struct {
24
sync.RWMutex
25
ResumeFrom map[string]*ResumeInfo `json:"resumeFrom"`
26
Current map[string]*ResumeInfo `json:"-"`
27
}
28
29
type ResumeInfo struct {
30
sync.RWMutex
31
Completed bool `json:"completed"`
32
InFlight map[uint32]struct{} `json:"inFlight"`
33
SkipUnder uint32 `json:"-"`
34
Repeat map[uint32]struct{} `json:"-"`
35
DoAbove uint32 `json:"-"`
36
}
37
38
// Clone the ResumeInfo structure
39
func (resumeInfo *ResumeInfo) Clone() *ResumeInfo {
40
resumeInfo.Lock()
41
defer resumeInfo.Unlock()
42
43
inFlight := make(map[uint32]struct{})
44
for u := range resumeInfo.InFlight {
45
inFlight[u] = struct{}{}
46
}
47
repeat := make(map[uint32]struct{})
48
for u := range resumeInfo.Repeat {
49
repeat[u] = struct{}{}
50
}
51
52
return &ResumeInfo{
53
Completed: resumeInfo.Completed,
54
InFlight: inFlight,
55
SkipUnder: resumeInfo.SkipUnder,
56
Repeat: repeat,
57
DoAbove: resumeInfo.DoAbove,
58
}
59
}
60
61
// NewResumeCfg creates a new scan progression structure
62
func NewResumeCfg() *ResumeCfg {
63
return &ResumeCfg{
64
ResumeFrom: make(map[string]*ResumeInfo),
65
Current: make(map[string]*ResumeInfo),
66
}
67
}
68
69
// Clone the resume structure
70
func (resumeCfg *ResumeCfg) Clone() *ResumeCfg {
71
resumeCfg.Lock()
72
defer resumeCfg.Unlock()
73
74
resumeFrom := make(map[string]*ResumeInfo)
75
for id, resumeInfo := range resumeCfg.ResumeFrom {
76
resumeFrom[id] = resumeInfo.Clone()
77
}
78
current := make(map[string]*ResumeInfo)
79
for id, resumeInfo := range resumeCfg.Current {
80
current[id] = resumeInfo.Clone()
81
}
82
83
return &ResumeCfg{
84
ResumeFrom: resumeFrom,
85
Current: current,
86
}
87
}
88
89
// Clone the resume structure
90
func (resumeCfg *ResumeCfg) Compile() {
91
resumeCfg.Lock()
92
defer resumeCfg.Unlock()
93
94
for _, resumeInfo := range resumeCfg.ResumeFrom {
95
if resumeInfo.Completed && len(resumeInfo.InFlight) > 0 {
96
resumeInfo.InFlight = make(map[uint32]struct{})
97
}
98
min := uint32(math.MaxUint32)
99
max := uint32(0)
100
for index := range resumeInfo.InFlight {
101
if index < min {
102
min = index
103
}
104
if index > max {
105
max = index
106
}
107
}
108
// maybe redundant but ensures we track the indexes to be repeated
109
resumeInfo.Repeat = map[uint32]struct{}{}
110
for index := range resumeInfo.InFlight {
111
resumeInfo.Repeat[index] = struct{}{}
112
}
113
resumeInfo.SkipUnder = min
114
resumeInfo.DoAbove = max
115
}
116
}
117
118