-
Notifications
You must be signed in to change notification settings - Fork 0
/
results.go
121 lines (107 loc) · 2.39 KB
/
results.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"fmt"
"os"
"strings"
"sync"
"github.com/olekukonko/tablewriter"
)
type Results struct {
sync.Mutex
Attempts int
FailedAttempts int
AcceptedAttempts int
Failed *EntrySet
Accepted *EntrySet
Ips []string
entries map[string]*Entry
order OrderBy
jsonOutput bool
ipOutput bool
failedOutput bool
acceptedOutput bool
}
func newResults(order OrderBy) *Results {
return &Results{
Failed: newEntrySet(order),
Accepted: newEntrySet(order),
entries: map[string]*Entry{},
Ips: []string{},
order: order,
acceptedOutput: true,
failedOutput: true,
ipOutput: false,
jsonOutput: false,
}
}
func (r *Results) AddEntry(entry *Entry) {
r.Lock()
defer r.Unlock()
ip := entry.Ip
existingEntry, exists := r.entries[ip]
if exists {
existingEntry.Attempts++
if entry.Authenticated == existingEntry.Authenticated {
return
} else {
entry.Attempts = existingEntry.Attempts
}
} else {
r.entries[ip] = entry
}
if entry.Authenticated {
r.Accepted.Append(entry)
} else {
r.Failed.Append(entry)
}
}
func (r *Results) writeRows(t *tablewriter.Table, es *EntrySet) {
for _, e := range es.Entries {
t.Append(e.Row())
}
}
func (r *Results) renderJSON() {
fmt.Println("JSON")
}
func (r *Results) renderIps() {
if r.acceptedOutput && r.failedOutput {
fmt.Printf("\nAccepted: \n\n%s\n", strings.Join(r.Accepted.Ips(), "\n"))
fmt.Printf("\nFailed: \n\n%s\n", strings.Join(r.Failed.Ips(), "\n"))
}
var ips []string
if r.acceptedOutput {
ips = r.Accepted.Ips()
} else {
ips = r.Failed.Ips()
}
fmt.Println(strings.Join(ips, "\n"))
}
func (r *Results) renderTable() {
headers := []string{"Date", "Ip", "Attempts", "User", "Auth", "Protocol", "Port", "Server"}
if r.acceptedOutput {
acceptedTable := tablewriter.NewWriter(os.Stdout)
acceptedTable.SetHeader(headers)
r.writeRows(acceptedTable, r.Accepted)
fmt.Println("Accepted: \n")
acceptedTable.Render()
}
if r.failedOutput {
failedTable := tablewriter.NewWriter(os.Stdout)
failedTable.SetHeader(headers)
r.writeRows(failedTable, r.Failed)
fmt.Println("Failed: \n")
failedTable.Render()
}
}
func (r *Results) Render() {
r.Accepted.Sort()
r.Failed.Sort()
switch {
case r.ipOutput:
r.renderIps()
case r.jsonOutput:
r.renderJSON()
default:
r.renderTable()
}
}