-
Notifications
You must be signed in to change notification settings - Fork 0
/
grif.go
354 lines (320 loc) · 7.56 KB
/
grif.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package main
import (
"bufio"
"crypto/tls"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
"unicode/utf8"
"github.com/faiface/beep"
"github.com/faiface/beep/mp3"
"github.com/faiface/beep/speaker"
"github.com/gen2brain/beeep"
"github.com/getlantern/systray"
"github.com/sparrc/go-ping"
)
// Length in seconds between each cycle of checks
var wait time.Duration = 3
var checking = false
var ready = false
var numOfChecksRan uint32 = 0
func main() {
fmt.Println("Grif v0.1 - https://github.com/d4rkd0s/grif (c) 2019 d4rkd0s")
if !checkForAdmin() {
os.Exit(126)
} else {
fmt.Println("Administrator command prompt achieved")
}
checkAndCreateHostsFile()
// Code to ask amount of delay
//
//fmt.Print("Enter number of seconds to wait between checking all hosts: ")
//var i int
//_, err := fmt.Scanf("%d", &i)
//if err != nil {
// log.Fatal(err)
//}
//wait = time.Duration(i)
systray.Run(onReady, onExit)
}
func onReady() {
systray.SetIcon(getIcon("assets/grif.ico"))
systray.SetTitle("Grif")
systray.SetTooltip("Grif is running")
mQuit := systray.AddMenuItem("Quit", "Quits Grif")
go func() {
for {
select {
case <-mQuit.ClickedCh:
systray.Quit()
return
}
}
}()
go func() {
for range time.Tick(wait * time.Second) {
if !ready {
go notify("Grif is now running")
bark()
}
ready = true
if !checking {
go checkHosts()
} else {
fmt.Println("Busy checking hosts, waiting another cycle")
}
}
}()
}
func onExit() {
// Cleaning stuff here.
}
func getIcon(s string) []byte {
b, err := ioutil.ReadFile(s)
if err != nil {
fmt.Print(err)
}
return b
}
func checkHosts() {
checking = true
fmt.Println("Checking hosts...")
hosts, err := getHosts()
fmt.Println(hosts)
if err != nil {
log.Fatal(err)
}
for _, host := range hosts {
numOfChecksRan++
if strings.Contains(host, "http://") || strings.Contains(host, "https://") {
checkHTTP(host)
}
if strings.Contains(host, "icmp://") {
fmt.Println("ICMP unsupported currently")
// checkICMP(host)
}
}
systray.SetTooltip(fmt.Sprintf("%s checks ran, %d hosts up, %d hosts down", strconv.Itoa(int(numOfChecksRan)), getCountOfUpHosts(), getCountOfDownHosts()))
fmt.Println("Done checking hosts")
checking = false
}
func getHosts() ([]string, error) {
// getHosts reads a 'hosts' file into memory
// and returns a slice of its hosts
file, err := os.Open("hosts")
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func getCountOfUpHosts() uint32 {
var numOfUpHosts uint32 = 0
var hosts, err = getHosts()
if err != nil {
log.Fatal(err)
}
for _, host := range hosts {
if !strings.HasPrefix(host, "#") {
numOfUpHosts++
}
}
return numOfUpHosts
}
func getCountOfDownHosts() uint32 {
var numOfDownHosts uint32 = 0
var hosts, err = getHosts()
if err != nil {
log.Fatal(err)
}
for _, host := range hosts {
if strings.HasPrefix(host, "#") {
numOfDownHosts++
}
}
return numOfDownHosts
}
func checkIfHostWasUp(host string) bool {
hosts, err := getHosts()
if err != nil {
log.Fatal(err)
}
for _, hostFromFile := range hosts {
if strings.Contains(hostFromFile, host) {
if strings.HasPrefix(hostFromFile, "#") {
return false
} else {
return true
}
}
}
return false
}
func markHostDown(host string) {
hosts, err := getHosts()
if err != nil {
log.Fatal(err)
}
for i, hostFromFile := range hosts {
if strings.Contains(hostFromFile, host) {
if !strings.HasPrefix(hostFromFile, "#") {
hosts[i] = fmt.Sprintf("#%s", hostFromFile)
output := strings.Join(hosts, "\n")
err = ioutil.WriteFile("hosts", []byte(output), 0644)
if err != nil {
log.Fatalln(err)
}
}
}
}
return
}
func markHostUp(host string) {
hosts, err := getHosts()
if err != nil {
log.Fatal(err)
}
for i, hostFromFile := range hosts {
if strings.Contains(hostFromFile, host) {
if strings.HasPrefix(hostFromFile, "#") {
hosts[i] = fmt.Sprintf("%s", host)
output := strings.Join(hosts, "\n")
err = ioutil.WriteFile("hosts", []byte(output), 0644)
if err != nil {
log.Fatalln(err)
}
}
}
}
return
}
func notify(message string) {
// If the host is commented out, don't alert
err := beeep.Notify("Grif", message, "assets/grif.png")
if err != nil {
panic(err)
}
// Wait 2 seconds before continuing to let the notifications pool up slowly
time.Sleep(2 * time.Second)
return
}
func alert(message string, host string) {
// If the host is commented out, don't alert
if checkIfHostWasUp(host) {
err := beeep.Alert("Grif", message, "assets/grif.png")
if err != nil {
panic(err)
}
bark()
markHostDown(host)
// TODO: Add downed hosts to list in right click menu
// If a user clicked a downed host, Grif will recheck (ie remove #)
}
// Wait 2 seconds before continuing to let the notifications pool up slowly
time.Sleep(2 * time.Second)
return
}
func checkHTTP(host string) {
if strings.HasPrefix(host, "#") {
host = trimFirstRune(host)
}
//resp, err := http.Get(host)
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
resp, err := client.Get(host)
if err != nil {
fmt.Println(fmt.Sprintf("%s", err), host)
alert(fmt.Sprintf("%s", err), host)
} else {
if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
fmt.Println(fmt.Sprintf("%s replied with a 2xx", host))
markHostUp(host)
} else {
alert(fmt.Sprintf("%s returned a non 2xx HTTP response", host), host)
}
}
}
func trimFirstRune(s string) string {
_, i := utf8.DecodeRuneInString(s)
return s[i:]
}
func checkICMP(host string) {
if strings.HasPrefix(host, "#") {
host = trimFirstRune(host)
}
host = strings.TrimPrefix(host, "icmp://")
pinger, err := ping.NewPinger(host)
if err != nil {
fmt.Println(fmt.Sprintf("%s", err), host)
alert(fmt.Sprintf("%s", err), host)
}
pinger.Count = 1
pinger.Run() // blocks until finished
//if err != nil {
//
//} else {
// if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
// fmt.Println(fmt.Sprintf("%s replied with a 2xx", host))
// markHostUp(host)
// } else {
// alert(fmt.Sprintf("%s returned a non 2xx HTTP response", host), host)
// }
//}
}
func bark() {
// Open first sample File
f, err := os.Open("assets/bark.mp3")
// Check for errors when opening the file
if err != nil {
log.Fatal(err)
}
// Decode the .mp3 File, if you have a .wav file, use wav.Decode(f)
s, format, _ := mp3.Decode(f)
// Init the Speaker with the SampleRate of the format and a buffer size of 1/10s
err2 := speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
if err2 != nil {
panic(err2)
}
// Channel, which will signal the end of the playback.
playing := make(chan struct{})
// Now we Play our Streamer on the Speaker
speaker.Play(beep.Seq(s, beep.Callback(func() {
// Callback after the stream Ends
close(playing)
})))
<-playing
}
func checkAndCreateHostsFile() bool {
_, err := os.Open("hosts")
if err != nil {
_, err2 := os.OpenFile("hosts", os.O_RDONLY|os.O_CREATE, 0666)
if err2 != nil {
fmt.Println("Unable to create hosts file in current directory, we have admin rights, so something must be wrong.")
log.Fatal(err2)
} else {
fmt.Println("Created hosts file in current directory")
}
} else {
fmt.Println("Detected hosts file in current directory")
}
return true
}
func checkForAdmin() bool {
_, err := os.Open("\\\\.\\PHYSICALDRIVE0")
if err != nil {
return false
}
return true
}