-
Notifications
You must be signed in to change notification settings - Fork 16
/
scanFiles.go
76 lines (66 loc) · 1.72 KB
/
scanFiles.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
package nipejs
import (
"io"
"io/ioutil"
"os"
"path/filepath"
log "github.com/projectdiscovery/gologger"
)
func createTMPfile(filename string, strings2write []string) io.Reader {
file, err := os.Create(filename)
if err != nil {
log.Fatal().Msg("Unable to create file in /tmp directory")
}
defer file.Close()
for _, str := range strings2write {
_, err := file.WriteString(str + "\n")
if err != nil {
log.Fatal().Msg("Unable to write in file on /tmp directory")
}
}
tmpfile, _ := os.Open(filename)
return tmpfile
}
func scanFolder(tmpfilename string, foldername string) (io.Reader, int) {
var relativePaths []string
err := filepath.Walk(foldername, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if path != foldername {
relativePath, err := filepath.Rel(foldername, path)
if err != nil {
return err
}
if foldername[len(foldername)-1] == '/' {
relativePath = foldername + relativePath
relativePaths = append(relativePaths, relativePath)
} else {
relativePath = foldername + "/" + relativePath
relativePaths = append(relativePaths, relativePath)
}
}
return nil
})
log.Debug().Msgf("Total Files: %s", len(relativePaths))
if err != nil {
log.Error().Msgf("%s", err)
}
r := createTMPfile(tmpfilename, relativePaths)
return r, len(relativePaths)
}
func ReadFiles(results chan Results, files chan string) {
for file := range files {
regexfile, _ := ioutil.ReadFile(*regexf)
jsprefile, err := os.Open(file)
if err != nil {
log.Error().Msgf("Unable to open file: %s", file)
wg.Done()
continue
}
jsfile, _ := io.ReadAll(jsprefile)
matchRegex(string(jsfile), file, results, regexfile)
jsprefile.Close()
wg.Done()
}
}