-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonreportwriter.go
52 lines (41 loc) · 1.05 KB
/
jsonreportwriter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package steps
import (
"bytes"
"fmt"
"github.com/s-rah/onionscan/report"
"log"
"os"
"time"
)
// JSONReportWriter is a sink Pipeline step used to output a JSON formated
// version of the OnionScan report.
type JSONReportWriter struct {
reportFile string
}
// Init sets up a JsonReportWriter
func (jrw *JSONReportWriter) Init(outputFile string) {
jrw.reportFile = outputFile
}
// Do runs the PipelineStep for JsonReportWriter to output the formatted report.
func (jrw *JSONReportWriter) Do(r *report.OnionScanReport) error {
jsonOut, err := r.Serialize()
if err != nil {
return err
}
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("%s\n", jsonOut))
if len(jrw.reportFile) > 0 {
reportFile := r.HiddenService + "." + jrw.reportFile
f, err := os.Create(reportFile)
for err != nil {
log.Printf("Cannot create report file: %s...trying again in 5 seconds...", err)
time.Sleep(time.Second * 5)
f, err = os.Create(reportFile)
}
defer f.Close()
f.WriteString(buffer.String())
} else {
fmt.Print(buffer.String())
}
return nil
}