Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/protocols/common/generators/options.go
2846 views
1
package generators
2
3
import (
4
"sync"
5
6
"github.com/projectdiscovery/nuclei/v3/pkg/types"
7
)
8
9
// optionsPayloadMap caches the result of BuildPayloadFromOptions per options
10
// pointer. This supports multiple SDK instances with different options running
11
// concurrently.
12
var optionsPayloadMap sync.Map // map[*types.Options]map[string]interface{}
13
14
// BuildPayloadFromOptions returns a map with the payloads provided via CLI.
15
//
16
// The result is cached per options pointer since options don't change during a run.
17
// Returns a copy of the cached map to prevent concurrent modification issues.
18
// Safe for concurrent use with multiple SDK instances.
19
func BuildPayloadFromOptions(options *types.Options) map[string]interface{} {
20
if options == nil {
21
return make(map[string]interface{})
22
}
23
24
if cached, ok := optionsPayloadMap.Load(options); ok {
25
return CopyMap(cached.(map[string]interface{}))
26
}
27
28
m := make(map[string]interface{})
29
30
// merge with vars
31
if !options.Vars.IsEmpty() {
32
m = MergeMaps(m, options.Vars.AsMap())
33
}
34
35
// merge with env vars
36
if options.EnvironmentVariables {
37
m = MergeMaps(EnvVars(), m)
38
}
39
40
actual, _ := optionsPayloadMap.LoadOrStore(options, m)
41
42
// Return a copy to prevent concurrent writes to the cached map
43
return CopyMap(actual.(map[string]interface{}))
44
}
45
46
// ClearOptionsPayloadMap clears the cached options payload.
47
// SDK users should call this when disposing of a NucleiEngine instance
48
// to prevent memory leaks if creating many short-lived instances.
49
func ClearOptionsPayloadMap(options *types.Options) {
50
if options != nil {
51
optionsPayloadMap.Delete(options)
52
}
53
}
54
55