Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/catalog/disk/catalog.go
2070 views
1
package disk
2
3
import (
4
"io"
5
"io/fs"
6
"os"
7
8
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
9
)
10
11
// DiskCatalog is a template catalog helper implementation based on disk
12
type DiskCatalog struct {
13
templatesDirectory string
14
templatesFS fs.FS // Due to issues with how Go has implemented fs.FS, we'll have to also implement normal os operations, as well. See: https://github.com/golang/go/issues/44279
15
}
16
17
// NewCatalog creates a new Catalog structure using provided input items
18
// using disk based items
19
func NewCatalog(directory string) *DiskCatalog {
20
catalog := &DiskCatalog{templatesDirectory: directory}
21
if directory == "" {
22
catalog.templatesDirectory = config.DefaultConfig.GetTemplateDir()
23
}
24
return catalog
25
}
26
27
// NewFSCatalog creates a new Catalog structure using provided input items
28
// using the fs.FS as its filesystem.
29
func NewFSCatalog(fs fs.FS, directory string) *DiskCatalog {
30
catalog := &DiskCatalog{
31
templatesDirectory: directory,
32
templatesFS: fs,
33
}
34
return catalog
35
}
36
37
// OpenFile opens a file and returns an io.ReadCloser to the file.
38
// It is used to read template and payload files based on catalog responses.
39
func (d *DiskCatalog) OpenFile(filename string) (io.ReadCloser, error) {
40
if d.templatesFS == nil {
41
file, err := os.Open(filename)
42
if err != nil {
43
if file, errx := os.Open(BackwardsCompatiblePaths(d.templatesDirectory, filename)); errx == nil {
44
return file, nil
45
}
46
}
47
return file, err
48
}
49
50
return d.templatesFS.Open(filename)
51
}
52
53