mirror of https://github.com/daffainfo/nuclei.git
moved tracefile to pkg/output + misC
parent
5c79319af5
commit
e1bbb9d93d
|
@ -1,74 +0,0 @@
|
|||
package bufwriter
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Writer is a mutex protected buffered writer
|
||||
type Writer struct {
|
||||
file *os.File
|
||||
writer *bufio.Writer
|
||||
mutex *sync.Mutex
|
||||
}
|
||||
|
||||
// New creates a new mutex protected buffered writer for a file
|
||||
func New(file string) (*Writer, error) {
|
||||
output, err := os.Create(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Writer{file: output, writer: bufio.NewWriter(output), mutex: &sync.Mutex{}}, nil
|
||||
}
|
||||
|
||||
// Write writes a byte slice to the underlying file
|
||||
//
|
||||
// It also writes a newline if the last byte isn't a newline character.
|
||||
func (w *Writer) Write(data []byte) error {
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
w.mutex.Lock()
|
||||
defer w.mutex.Unlock()
|
||||
|
||||
_, err := w.writer.Write(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if data[len(data)-1] != '\n' {
|
||||
_, err = w.writer.WriteRune('\n')
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// WriteString writes a string to the underlying file
|
||||
//
|
||||
// It also writes a newline if the last byte isn't a newline character.
|
||||
func (w *Writer) WriteString(data string) error {
|
||||
if data == "" {
|
||||
return nil
|
||||
}
|
||||
w.mutex.Lock()
|
||||
defer w.mutex.Unlock()
|
||||
|
||||
_, err := w.writer.WriteString(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if data[len(data)-1] != '\n' {
|
||||
_, err = w.writer.WriteRune('\n')
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Close closes the underlying writer flushing everything to disk
|
||||
func (w *Writer) Close() error {
|
||||
w.mutex.Lock()
|
||||
defer w.mutex.Unlock()
|
||||
|
||||
w.writer.Flush()
|
||||
//nolint:errcheck // we don't care whether sync failed or succeeded.
|
||||
w.file.Sync()
|
||||
return w.file.Close()
|
||||
}
|
|
@ -1,76 +0,0 @@
|
|||
package tracelog
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
// Log is an interface for logging trace log of all the requests
|
||||
type Log interface {
|
||||
// Close closes the log interface flushing data
|
||||
Close()
|
||||
// Request writes a log the requests trace log
|
||||
Request(templateID, url, requestType string, err error)
|
||||
}
|
||||
|
||||
// NoopLogger is a noop logger that simply does nothing
|
||||
type NoopLogger struct{}
|
||||
|
||||
// Close closes the log interface flushing data
|
||||
func (n *NoopLogger) Close() {}
|
||||
|
||||
// Request writes a log the requests trace log
|
||||
func (n *NoopLogger) Request(templateID, url, requestType string, err error) {}
|
||||
|
||||
// FileLogger is a trace logger that writes request logs to a file.
|
||||
type FileLogger struct {
|
||||
encoder *jsoniter.Encoder
|
||||
file *os.File
|
||||
mutex *sync.Mutex
|
||||
}
|
||||
|
||||
// NewFileLogger creates a new file logger structure
|
||||
func NewFileLogger(path string) (*FileLogger, error) {
|
||||
file, err := os.Create(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &FileLogger{file: file, encoder: jsoniter.NewEncoder(file), mutex: &sync.Mutex{}}, nil
|
||||
}
|
||||
|
||||
// Close closes the log interface flushing data
|
||||
func (f *FileLogger) Close() {
|
||||
f.mutex.Lock()
|
||||
defer f.mutex.Unlock()
|
||||
|
||||
f.file.Close()
|
||||
}
|
||||
|
||||
// JSONRequest is a trace log request written to file
|
||||
type JSONRequest struct {
|
||||
ID string `json:"id"`
|
||||
URL string `json:"url"`
|
||||
Error string `json:"error"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
// Request writes a log the requests trace log
|
||||
func (f *FileLogger) Request(templateID, url, requestType string, err error) {
|
||||
request := &JSONRequest{
|
||||
ID: templateID,
|
||||
URL: url,
|
||||
Type: requestType,
|
||||
}
|
||||
if err != nil {
|
||||
request.Error = err.Error()
|
||||
} else {
|
||||
request.Error = "none"
|
||||
}
|
||||
|
||||
f.mutex.Lock()
|
||||
defer f.mutex.Unlock()
|
||||
//nolint:errcheck // We don't need to do anything here
|
||||
f.encoder.Encode(request)
|
||||
}
|
|
@ -26,9 +26,7 @@ func (w *fileWriter) Write(data []byte) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if data[len(data)-1] != '\n' {
|
||||
_, err = w.writer.WriteRune('\n')
|
||||
}
|
||||
_, err = w.writer.WriteRune('\n')
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"os"
|
||||
"sync"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/logrusorgru/aurora"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
@ -14,6 +15,8 @@ type Writer interface {
|
|||
Close()
|
||||
// Write writes the event to file and/or screen.
|
||||
Write(Event) error
|
||||
// Request writes a log the requests trace log
|
||||
Request(templateID, url, requestType string, err error)
|
||||
}
|
||||
|
||||
// StandardWriter is a writer writing output to file and screen for results.
|
||||
|
@ -22,7 +25,9 @@ type StandardWriter struct {
|
|||
noMetadata bool
|
||||
aurora aurora.Aurora
|
||||
outputFile *fileWriter
|
||||
mutex *sync.Mutex
|
||||
outputMutex *sync.Mutex
|
||||
traceFile *fileWriter
|
||||
traceMutex *sync.Mutex
|
||||
severityMap map[string]string
|
||||
}
|
||||
|
||||
|
@ -35,7 +40,7 @@ const (
|
|||
type Event map[string]interface{}
|
||||
|
||||
// NewStandardWriter creates a new output writer based on user configurations
|
||||
func NewStandardWriter(colors, noMetadata, json bool, file string) (*StandardWriter, error) {
|
||||
func NewStandardWriter(colors, noMetadata, json bool, file, traceFile string) (*StandardWriter, error) {
|
||||
colorizer := aurora.NewAurora(colors)
|
||||
|
||||
var outputFile *fileWriter
|
||||
|
@ -46,6 +51,14 @@ func NewStandardWriter(colors, noMetadata, json bool, file string) (*StandardWri
|
|||
}
|
||||
outputFile = output
|
||||
}
|
||||
var traceOutput *fileWriter
|
||||
if traceFile != "" {
|
||||
output, err := newFileOutputWriter(traceFile)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not create output file")
|
||||
}
|
||||
traceOutput = output
|
||||
}
|
||||
severityMap := map[string]string{
|
||||
"info": colorizer.Blue("info").String(),
|
||||
"low": colorizer.Green("low").String(),
|
||||
|
@ -58,8 +71,10 @@ func NewStandardWriter(colors, noMetadata, json bool, file string) (*StandardWri
|
|||
noMetadata: noMetadata,
|
||||
severityMap: severityMap,
|
||||
aurora: colorizer,
|
||||
mutex: &sync.Mutex{},
|
||||
outputFile: outputFile,
|
||||
outputMutex: &sync.Mutex{},
|
||||
traceFile: traceOutput,
|
||||
traceMutex: &sync.Mutex{},
|
||||
}
|
||||
return writer, nil
|
||||
}
|
||||
|
@ -86,7 +101,46 @@ func (w *StandardWriter) Write(event Event) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// JSONTraceRequest is a trace log request written to file
|
||||
type JSONTraceRequest struct {
|
||||
ID string `json:"id"`
|
||||
URL string `json:"url"`
|
||||
Error string `json:"error"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
// Request writes a log the requests trace log
|
||||
func (w *StandardWriter) Request(templateID, url, requestType string, err error) {
|
||||
if w.traceFile == nil {
|
||||
return
|
||||
}
|
||||
request := &JSONTraceRequest{
|
||||
ID: templateID,
|
||||
URL: url,
|
||||
Type: requestType,
|
||||
}
|
||||
if err != nil {
|
||||
request.Error = err.Error()
|
||||
} else {
|
||||
request.Error = "none"
|
||||
}
|
||||
|
||||
data, err := jsoniter.Marshal(request)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
w.traceMutex.Lock()
|
||||
//nolint:errcheck // We don't need to do anything here
|
||||
_ = w.traceFile.Write(data)
|
||||
w.traceMutex.Unlock()
|
||||
}
|
||||
|
||||
// Close closes the output writing interface
|
||||
func (w *StandardWriter) Close() {
|
||||
w.outputFile.Close()
|
||||
if w.outputFile != nil {
|
||||
w.outputFile.Close()
|
||||
}
|
||||
if w.traceFile != nil {
|
||||
w.traceFile.Close()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue