Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/tmplexec/interface.go
2070 views
1
package tmplexec
2
3
import (
4
"errors"
5
"regexp"
6
"strings"
7
8
"github.com/projectdiscovery/nuclei/v3/pkg/scan"
9
"github.com/projectdiscovery/nuclei/v3/pkg/tmplexec/flow"
10
"github.com/projectdiscovery/nuclei/v3/pkg/tmplexec/generic"
11
"github.com/projectdiscovery/nuclei/v3/pkg/tmplexec/multiproto"
12
"github.com/projectdiscovery/utils/errkit"
13
)
14
15
var (
16
_ TemplateEngine = &generic.Generic{}
17
_ TemplateEngine = &flow.FlowExecutor{}
18
_ TemplateEngine = &multiproto.MultiProtocol{}
19
)
20
21
// TemplateEngine is a template executor with different functionality
22
// Ex:
23
// 1. generic => executes all protocol requests one after another (Done)
24
// 2. flow => executes protocol requests based on how they are defined in flow (Done)
25
// 3. multiprotocol => executes multiple protocols in parallel (Done)
26
type TemplateEngine interface {
27
// Note: below methods only need to implement extra / engine specific functionality
28
// basic request compilation , callbacks , cli output callback etc are handled by `TemplateExecuter` and no need to do it again
29
// Extra Compilation (if any)
30
Compile() error
31
32
// ExecuteWithResults executes the template and returns results
33
ExecuteWithResults(ctx *scan.ScanContext) error
34
35
// Name returns name of template engine
36
Name() string
37
}
38
39
var (
40
// A temporary fix to remove errKind from error message
41
// this is because errkit is not used everywhere yet
42
reNoKind = regexp.MustCompile(`([\[][^][]+[\]]|errKind=[^ ]+) `)
43
)
44
45
// parseScanError parses given scan error and only returning the cause
46
// instead of inefficient one
47
func parseScanError(msg string) string {
48
if msg == "" {
49
return ""
50
}
51
if strings.HasPrefix(msg, "ReadStatusLine:") {
52
// last index is actual error (from rawhttp)
53
parts := strings.Split(msg, ":")
54
msg = strings.TrimSpace(parts[len(parts)-1])
55
}
56
if strings.Contains(msg, "read ") {
57
// same here
58
parts := strings.Split(msg, ":")
59
msg = strings.TrimSpace(parts[len(parts)-1])
60
}
61
e := errkit.FromError(errors.New(msg))
62
for _, err := range e.Errors() {
63
if err != nil && strings.Contains(err.Error(), "context deadline exceeded") {
64
continue
65
}
66
msg = reNoKind.ReplaceAllString(err.Error(), "")
67
return msg
68
}
69
wrapped := errkit.Append(errkit.New("failed to get error cause"), e).Error()
70
return reNoKind.ReplaceAllString(wrapped, "")
71
}
72
73