1
|
|
|
package summarizer |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"fmt" |
5
|
|
|
"net/http" |
6
|
|
|
"sort" |
7
|
|
|
"sync" |
8
|
|
|
|
9
|
|
|
"github.com/stefanoj3/dirstalk/pkg/scan/summarizer/tree" |
10
|
|
|
|
11
|
|
|
"github.com/sirupsen/logrus" |
12
|
|
|
"github.com/stefanoj3/dirstalk/pkg/scan" |
13
|
|
|
) |
14
|
|
|
|
15
|
|
|
const ( |
16
|
|
|
breakingText = "Found something breaking" |
17
|
|
|
foundText = "Found" |
18
|
|
|
) |
19
|
|
|
|
20
|
|
|
func NewResultSummarizer(treePrinter ResultTreePrinter, logger *logrus.Logger) *ResultSummarizer { |
|
|
|
|
21
|
|
|
return &ResultSummarizer{ |
22
|
|
|
treePrinter: treePrinter, |
23
|
|
|
logger: logger, |
24
|
|
|
resultMap: make(map[string]struct{}), |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
type ResultSummarizer struct { |
|
|
|
|
29
|
|
|
treePrinter ResultTreePrinter |
30
|
|
|
logger *logrus.Logger |
31
|
|
|
results []scan.Result |
32
|
|
|
resultMap map[string]struct{} |
33
|
|
|
mux sync.RWMutex |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
func (s *ResultSummarizer) Add(result scan.Result) { |
|
|
|
|
37
|
|
|
s.mux.Lock() |
38
|
|
|
defer s.mux.Unlock() |
39
|
|
|
|
40
|
|
|
key := keyForResult(result) |
41
|
|
|
_, found := s.resultMap[key] |
42
|
|
|
if found { |
43
|
|
|
return |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
s.log(result) |
47
|
|
|
|
48
|
|
|
s.resultMap[key] = struct{}{} |
49
|
|
|
|
50
|
|
|
s.results = append(s.results, result) |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
func (s *ResultSummarizer) Summarize() { |
|
|
|
|
54
|
|
|
s.mux.Lock() |
55
|
|
|
defer s.mux.Unlock() |
56
|
|
|
|
57
|
|
|
sort.Slice(s.results, func(i, j int) bool { |
58
|
|
|
return s.results[i].Target.Path < s.results[j].Target.Path |
59
|
|
|
}) |
60
|
|
|
|
61
|
|
|
s.printSummary() |
62
|
|
|
s.printTree() |
63
|
|
|
|
64
|
|
|
for _, r := range s.results { |
65
|
|
|
_, _ = fmt.Fprintln( |
66
|
|
|
s.logger.Out, |
67
|
|
|
fmt.Sprintf( |
68
|
|
|
"%s [%d] [%s]", |
69
|
|
|
r.URL.String(), |
70
|
|
|
r.StatusCode, |
71
|
|
|
r.Target.Method, |
72
|
|
|
), |
73
|
|
|
) |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
func (s *ResultSummarizer) printSummary() { |
78
|
|
|
_, _ = fmt.Fprintln( |
79
|
|
|
s.logger.Out, |
80
|
|
|
fmt.Sprintf("%d results found", len(s.results)), |
81
|
|
|
) |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
func (s *ResultSummarizer) printTree() { |
85
|
|
|
tree.NewResultTreePrinter().Print(s.results, s.logger.Out) |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
func (s *ResultSummarizer) log(result scan.Result) { |
89
|
|
|
statusCode := result.StatusCode |
90
|
|
|
|
91
|
|
|
l := s.logger.WithFields(logrus.Fields{ |
92
|
|
|
"status-code": statusCode, |
93
|
|
|
"method": result.Target.Method, |
94
|
|
|
"url": result.URL.String(), |
95
|
|
|
}) |
96
|
|
|
|
97
|
|
|
if statusCode >= http.StatusInternalServerError { |
98
|
|
|
l.Warn(breakingText) |
99
|
|
|
} else { |
100
|
|
|
l.Info(foundText) |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
func keyForResult(result scan.Result) string { |
105
|
|
|
return fmt.Sprintf("%s~%s", result.URL.String(), result.Target.Method) |
106
|
|
|
} |
107
|
|
|
|