forked from ggrpmnn/shar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.go
104 lines (93 loc) · 2.44 KB
/
entry.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
package main
import (
"encoding/json"
"fmt"
"strings"
"github.com/fatih/color"
)
// tracks the login attempts (per-IP, single day)
type authEntry struct {
IP string `json:"ip"`
Count int `json:"count"`
Users []string `json:"usernames"`
Location ipAPIResponse `json:"location"`
}
// associates authEntryList objects with a particular date
type datedAuthEntries struct {
Date string `json:"date"`
Entries []authEntry `json:"entries"`
}
// slice for containing all dated entries
type allEntries []datedAuthEntries
// adds the username to the list for the given IP AuthEntry struct
func (ae *authEntry) addUser(user string) {
ae.Count++
for _, un := range ae.Users {
if un == user {
return
}
}
ae.Users = append(ae.Users, user)
}
// returns true if the IP string exists in the given map
func (dae *datedAuthEntries) exists(ip string) (int, bool) {
for idx, ae := range dae.Entries {
if ae.IP == ip {
return idx, true
}
}
return 0, false
}
// maps a function to the entries underneath a datedAuthEntries struct
func (dae *datedAuthEntries) apply(f func(authEntry) authEntry) []authEntry {
applied := make([]authEntry, 0)
for _, ae := range dae.Entries {
applied = append(applied, f(ae))
}
return applied
}
// filters the entries based on certain functional criteria
func (dae *datedAuthEntries) filter(f func(authEntry) bool) []authEntry {
filtered := make([]authEntry, 0)
for _, ae := range dae.Entries {
if f(ae) {
filtered = append(filtered, ae)
}
}
return filtered
}
func (ae allEntries) print() {
for idx, dae := range ae {
// don't print the date if there are no entries
if len(dae.Entries) > 0 {
color.Set(color.FgGreen, color.Bold)
fmt.Println("Date: " + dae.Date)
color.Unset()
for _, ae := range dae.Entries {
color.Set(color.FgBlue, color.Bold)
fmt.Printf("IP: %s\n", ae.IP)
color.Unset()
color.Set(color.FgYellow)
fmt.Print("Location: ")
color.Unset()
fmt.Println(ae.Location.composeLocationString())
color.Set(color.FgYellow)
fmt.Print("Attempts: ")
color.Unset()
fmt.Println(ae.Count)
color.Set(color.FgYellow)
fmt.Print("Usernames: ")
color.Unset()
fmt.Println(strings.Join(ae.Users, ", "))
}
// don't print a newline after the last date
if idx != len(ae)-1 {
fmt.Println()
}
}
}
}
func (ae allEntries) printJSON() {
bytes, _ := json.MarshalIndent(ae, "", " ")
fmt.Println(string(bytes))
}