Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/output/standard_writer.go
2070 views
1
package output
2
3
import (
4
"io"
5
"os"
6
"sync"
7
8
"github.com/logrusorgru/aurora"
9
"github.com/projectdiscovery/nuclei/v3/pkg/model/types/severity"
10
fileutil "github.com/projectdiscovery/utils/file"
11
)
12
13
// WriterOptions contains configuration options for a writer
14
type WriterOptions func(s *StandardWriter) error
15
16
// WithJson writes output in json format
17
func WithJson(json bool, dumpReqResp bool) WriterOptions {
18
return func(s *StandardWriter) error {
19
s.json = json
20
s.jsonReqResp = dumpReqResp
21
return nil
22
}
23
}
24
25
// WithTimestamp writes output with timestamp
26
func WithTimestamp(timestamp bool) WriterOptions {
27
return func(s *StandardWriter) error {
28
s.timestamp = timestamp
29
return nil
30
}
31
}
32
33
// WithNoMetadata disables metadata output
34
func WithNoMetadata(noMetadata bool) WriterOptions {
35
return func(s *StandardWriter) error {
36
s.noMetadata = noMetadata
37
return nil
38
}
39
}
40
41
// WithMatcherStatus writes output with matcher status
42
func WithMatcherStatus(matcherStatus bool) WriterOptions {
43
return func(s *StandardWriter) error {
44
s.matcherStatus = matcherStatus
45
return nil
46
}
47
}
48
49
// WithAurora sets the aurora instance for the writer
50
func WithAurora(aurora aurora.Aurora) WriterOptions {
51
return func(s *StandardWriter) error {
52
s.aurora = aurora
53
return nil
54
}
55
}
56
57
// WithWriter sets the writer for the writer
58
func WithWriter(outputFile io.WriteCloser) WriterOptions {
59
return func(s *StandardWriter) error {
60
s.outputFile = outputFile
61
return nil
62
}
63
}
64
65
// WithTraceSink sets the writer where trace output is written
66
func WithTraceSink(traceFile io.WriteCloser) WriterOptions {
67
return func(s *StandardWriter) error {
68
s.traceFile = traceFile
69
return nil
70
}
71
}
72
73
// WithErrorSink sets the writer where error output is written
74
func WithErrorSink(errorFile io.WriteCloser) WriterOptions {
75
return func(s *StandardWriter) error {
76
s.errorFile = errorFile
77
return nil
78
}
79
}
80
81
// WithSeverityColors sets the color function for severity
82
func WithSeverityColors(severityColors func(severity.Severity) string) WriterOptions {
83
return func(s *StandardWriter) error {
84
s.severityColors = severityColors
85
return nil
86
}
87
}
88
89
// WithStoreResponse sets the store response option
90
func WithStoreResponse(storeResponse bool, respDir string) WriterOptions {
91
return func(s *StandardWriter) error {
92
s.storeResponse = storeResponse
93
s.storeResponseDir = respDir
94
return nil
95
}
96
}
97
98
// NewWriter creates a new output writer
99
// if no writer is specified it writes to stdout
100
func NewWriter(opts ...WriterOptions) (*StandardWriter, error) {
101
s := &StandardWriter{
102
mutex: &sync.Mutex{},
103
DisableStdout: true,
104
AddNewLinesOutputFile: true,
105
}
106
for _, opt := range opts {
107
if err := opt(s); err != nil {
108
return nil, err
109
}
110
}
111
if s.aurora == nil {
112
s.aurora = aurora.NewAurora(false)
113
}
114
if s.outputFile == nil {
115
s.outputFile = os.Stdout
116
}
117
// Try to create output folder if it doesn't exist
118
if s.storeResponse && !fileutil.FolderExists(s.storeResponseDir) {
119
if err := fileutil.CreateFolder(s.storeResponseDir); err != nil {
120
return nil, err
121
}
122
}
123
return s, nil
124
}
125
126