forked from 415ALS/onionscanv3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplereportwriter.go
56 lines (48 loc) · 1.19 KB
/
simplereportwriter.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
53
54
55
56
package steps
import (
"fmt"
"github.com/415ALS/onionscanv3/report"
"log"
"os"
"time"
)
// SimpleReportWriter is a PipelineStep which outputs a human friendly summary
// of an OnionScanReport.
type SimpleReportWriter struct {
reportFile string
asJSON bool
width int
}
// Init sets up a SimpleReportWriter.
func (srw *SimpleReportWriter) Init(outputFile string, asJSON bool, width int) {
srw.reportFile = outputFile
srw.asJSON = asJSON
srw.width = width
}
// Do runs the PipelineStep for SimpleReportWriter to output the formatted report.
func (srw *SimpleReportWriter) Do(r *report.OnionScanReport) error {
var reportStr string
var err error
if srw.asJSON {
reportStr, err = r.SimpleReport.Serialize()
} else {
reportStr, err = r.SimpleReport.Format(srw.width)
}
if err != nil {
return err
}
if len(srw.reportFile) > 0 {
reportFile := r.HiddenService + "." + srw.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(reportStr)
} else {
fmt.Print(reportStr)
}
return nil
}