Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/reporting/exporters/jsonexporter/jsonexporter.go
2070 views
1
package jsonexporter
2
3
import (
4
"os"
5
"sync"
6
7
"github.com/pkg/errors"
8
"github.com/projectdiscovery/nuclei/v3/pkg/output"
9
"github.com/projectdiscovery/nuclei/v3/pkg/utils/json"
10
)
11
12
type Exporter struct {
13
options *Options
14
mutex *sync.Mutex
15
rows []output.ResultEvent
16
}
17
18
// Options contains the configuration options for JSON exporter client
19
type Options struct {
20
// File is the file to export found JSON result to
21
File string `yaml:"file"`
22
OmitRaw bool `yaml:"omit-raw"`
23
}
24
25
// New creates a new JSON exporter integration client based on options.
26
func New(options *Options) (*Exporter, error) {
27
exporter := &Exporter{
28
mutex: &sync.Mutex{},
29
options: options,
30
rows: []output.ResultEvent{},
31
}
32
return exporter, nil
33
}
34
35
// Export appends the passed result event to the list of objects to be exported to
36
// the resulting JSON file
37
func (exporter *Exporter) Export(event *output.ResultEvent) error {
38
exporter.mutex.Lock()
39
defer exporter.mutex.Unlock()
40
41
if exporter.options.OmitRaw {
42
event.Request = ""
43
event.Response = ""
44
}
45
46
// Add the event to the rows
47
exporter.rows = append(exporter.rows, *event)
48
49
return nil
50
}
51
52
// Close writes the in-memory data to the JSON file specified by options.JSONExport
53
// and closes the exporter after operation
54
func (exporter *Exporter) Close() error {
55
exporter.mutex.Lock()
56
defer exporter.mutex.Unlock()
57
58
// Convert the rows to JSON byte array
59
obj, err := json.Marshal(exporter.rows)
60
if err != nil {
61
return errors.Wrap(err, "failed to generate JSON report")
62
}
63
64
// Attempt to write the JSON to file specified in options.JSONExport
65
if err := os.WriteFile(exporter.options.File, obj, 0644); err != nil {
66
return errors.Wrap(err, "failed to create JSON file")
67
}
68
69
return nil
70
}
71
72