Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/js/libs/fs/fs.go
2070 views
1
package fs
2
3
import (
4
"context"
5
"os"
6
7
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/protocolstate"
8
)
9
10
// ListDir lists itemType values within a directory
11
// depending on the itemType provided
12
// itemType can be any one of ['file','dir',”]
13
// @example
14
// ```javascript
15
// const fs = require('nuclei/fs');
16
// // this will only return files in /tmp directory
17
// const files = fs.ListDir('/tmp', 'file');
18
// ```
19
// @example
20
// ```javascript
21
// const fs = require('nuclei/fs');
22
// // this will only return directories in /tmp directory
23
// const dirs = fs.ListDir('/tmp', 'dir');
24
// ```
25
// @example
26
// ```javascript
27
// const fs = require('nuclei/fs');
28
// // when no itemType is provided, it will return both files and directories
29
// const items = fs.ListDir('/tmp');
30
// ```
31
func ListDir(ctx context.Context, path string, itemType string) ([]string, error) {
32
executionId := ctx.Value("executionId").(string)
33
finalPath, err := protocolstate.NormalizePathWithExecutionId(executionId, path)
34
if err != nil {
35
return nil, err
36
}
37
values, err := os.ReadDir(finalPath)
38
if err != nil {
39
return nil, err
40
}
41
var results []string
42
for _, value := range values {
43
if itemType == "file" && value.IsDir() {
44
continue
45
}
46
if itemType == "dir" && !value.IsDir() {
47
continue
48
}
49
results = append(results, value.Name())
50
}
51
return results, nil
52
}
53
54
// ReadFile reads file contents within permitted paths
55
// and returns content as byte array
56
// @example
57
// ```javascript
58
// const fs = require('nuclei/fs');
59
// // here permitted directories are $HOME/nuclei-templates/*
60
// const content = fs.ReadFile('helpers/usernames.txt');
61
// ```
62
func ReadFile(ctx context.Context, path string) ([]byte, error) {
63
executionId := ctx.Value("executionId").(string)
64
finalPath, err := protocolstate.NormalizePathWithExecutionId(executionId, path)
65
if err != nil {
66
return nil, err
67
}
68
bin, err := os.ReadFile(finalPath)
69
return bin, err
70
}
71
72
// ReadFileAsString reads file contents within permitted paths
73
// and returns content as string
74
// @example
75
// ```javascript
76
// const fs = require('nuclei/fs');
77
// // here permitted directories are $HOME/nuclei-templates/*
78
// const content = fs.ReadFileAsString('helpers/usernames.txt');
79
// ```
80
func ReadFileAsString(ctx context.Context, path string) (string, error) {
81
bin, err := ReadFile(ctx, path)
82
if err != nil {
83
return "", err
84
}
85
return string(bin), nil
86
}
87
88
// ReadFilesFromDir reads all files from a directory
89
// and returns a string array with file contents of all files
90
// @example
91
// ```javascript
92
// const fs = require('nuclei/fs');
93
// // here permitted directories are $HOME/nuclei-templates/*
94
// const contents = fs.ReadFilesFromDir('helpers/ssh-keys');
95
// log(contents);
96
// ```
97
func ReadFilesFromDir(ctx context.Context, dir string) ([]string, error) {
98
files, err := ListDir(ctx, dir, "file")
99
if err != nil {
100
return nil, err
101
}
102
var results []string
103
for _, file := range files {
104
content, err := ReadFileAsString(ctx, dir+"/"+file)
105
if err != nil {
106
return nil, err
107
}
108
results = append(results, content)
109
}
110
return results, nil
111
}
112
113