Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/protocols/offlinehttp/find.go
2070 views
1
package offlinehttp
2
3
import (
4
"io/fs"
5
"os"
6
"path/filepath"
7
"strings"
8
9
"github.com/pkg/errors"
10
)
11
12
// getInputPaths parses the specified input paths and returns a compiled
13
// list of finished absolute paths to the files evaluating any allowlist, denylist,
14
// glob, file or folders, etc.
15
func (request *Request) getInputPaths(target string, callback func(string)) error {
16
processed := make(map[string]struct{})
17
18
// Template input includes a wildcard
19
if strings.Contains(target, "*") {
20
if err := request.findGlobPathMatches(target, processed, callback); err != nil {
21
return errors.Wrap(err, "could not find glob matches")
22
}
23
return nil
24
}
25
26
// Template input is either a file or a directory
27
file, err := request.findFileMatches(target, processed, callback)
28
if err != nil {
29
return errors.Wrap(err, "could not find file")
30
}
31
if file {
32
return nil
33
}
34
35
// Recursively walk down the Templates directory and run all
36
// the template file checks
37
if err := request.findDirectoryMatches(target, processed, callback); err != nil {
38
return errors.Wrap(err, "could not find directory matches")
39
}
40
return nil
41
}
42
43
// findGlobPathMatches returns the matched files from a glob path
44
func (request *Request) findGlobPathMatches(absPath string, processed map[string]struct{}, callback func(string)) error {
45
matches, err := filepath.Glob(absPath)
46
if err != nil {
47
return errors.Errorf("wildcard found, but unable to glob: %s\n", err)
48
}
49
for _, match := range matches {
50
if filepath.Ext(match) != ".txt" {
51
continue // only process .txt files
52
}
53
if _, ok := processed[match]; !ok {
54
processed[match] = struct{}{}
55
callback(match)
56
}
57
}
58
return nil
59
}
60
61
// findFileMatches finds if a path is an absolute file. If the path
62
// is a file, it returns true otherwise false with no errors.
63
func (request *Request) findFileMatches(absPath string, processed map[string]struct{}, callback func(string)) (bool, error) {
64
info, err := os.Stat(absPath)
65
if err != nil {
66
return false, err
67
}
68
if !info.Mode().IsRegular() {
69
return false, nil
70
}
71
if filepath.Ext(absPath) != ".txt" {
72
return false, nil // only process .txt files
73
}
74
if _, ok := processed[absPath]; !ok {
75
processed[absPath] = struct{}{}
76
callback(absPath)
77
}
78
return true, nil
79
}
80
81
// findDirectoryMatches finds matches for templates from a directory
82
func (request *Request) findDirectoryMatches(absPath string, processed map[string]struct{}, callback func(string)) error {
83
err := filepath.WalkDir(
84
absPath,
85
func(p string, d fs.DirEntry, err error) error {
86
// continue on errors
87
if err != nil {
88
return nil
89
}
90
if d.IsDir() {
91
return nil
92
}
93
if filepath.Ext(p) != ".txt" {
94
return nil // only process .txt files
95
}
96
if _, ok := processed[p]; !ok {
97
callback(p)
98
processed[p] = struct{}{}
99
}
100
return nil
101
},
102
)
103
return err
104
}
105
106