-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.go
811 lines (700 loc) · 23.6 KB
/
main.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
/*
###################
pexpo Demo Console.
console 130x45
###################
================================================================= Ctrl+S: Stop & Restart, Esc or Ctrl+C: Exit.
| o | Host | Response | Description | Now, Loss counting Per host.
================================================================= Hostname Loss(%) Loss(sum) Dead Now?
| o | 77.88.8.3 | 286.120512ms | Yandex.DNS | www.yahoo.com 0.000 0 loss
| o | 180.76.76.76 | 37.444591ms | Baidu DNS | 192.168.1.201 100.00 10 loss Dead Now!
| o | 114.114.114.114 | 288.835931ms | Baidu DNS | 2001:4860:4860:... 0.000 0 loss
| o | 80.80.80.80 | 74.98565ms | Freenom World | 8.8.8.8 0.000 0 loss
| o | 80.80.81.81 | 73.907249ms | Freenom World | 8.8.4.4 0.000 0 loss
| o | 8.26.56.26 | 213.717929ms | Comodo Secure DNS | 208.67.222.123 0.000 0 loss
| o | 8.20.247.20 | 209.27997ms | Comodo Secure DNS | 208.67.220.123 0.000 0 loss
| o | 106.186.17.181 | 74.254678ms | OpenNIC | 216.146.35.35 0.000 0 loss
| o | 106.185.41.36 | 66.059639ms | OpenNIC | 216.146.36.36 0.000 0 loss
| o | www.yahoo.com | 75.149983ms | Yahoo!!! | 77.88.8.8 0.000 0 loss
| x | 192.168.1.201 | ping...faild... | Host is Dead! | 77.88.8.1 0.000 0 loss
| o | 2001:4860:4860:...| 1.197034ms | Google_IPv6 | 77.88.8.88 0.000 0 loss
| o | 8.8.8.8 | 941.56µs | nandedaaaaaaaaa...| 77.88.8.2 0.000 0 loss
| o | 8.8.4.4 | 991.28µs | Google_IPv4 | 77.88.8.7 0.000 0 loss
| o | 208.67.222.123 | 944.168µs | OpenDNS | 77.88.8.3 0.000 0 loss
| o | 208.67.220.123 | 948.797µs | OpenDNS | 180.76.76.76 0.000 0 loss
| o | 216.146.35.35 | 954.225µs | Dyn Internet Guide| 114.114.114.114 0.000 0 loss
| o | 216.146.36.36 | 240.103655ms | Dyn Internet Guide| 80.80.80.80 0.000 0 loss
| o | 77.88.8.8 | 286.464099ms | Yandex.DNS | 80.80.81.81 0.000 0 loss
| o | 77.88.8.1 | 358.621804ms | Yandex.DNS | 8.26.56.26 0.000 0 loss
| o | 77.88.8.88 | 358.995054ms | Yandex.DNS | 8.20.247.20 0.000 0 loss
| o | 77.88.8.2 | 287.493195ms | Yandex.DNS | 106.186.17.181 0.000 0 loss
| o | 77.88.8.7 | 358.558967ms | Yandex.DNS | 106.185.41.36 0.000 0 loss
| o | 77.88.8.3 | 285.775403ms | Yandex.DNS |
| o | 180.76.76.76 | 37.389211ms | Baidu DNS |
| o | 114.114.114.114 | 288.735094ms | Baidu DNS |
| o | 80.80.80.80 | 75.043223ms | Freenom World |
| o | 80.80.81.81 | 73.908742ms | Freenom World |
| o | 8.26.56.26 | 213.743171ms | Comodo Secure DNS |
| o | 8.20.247.20 | 209.260693ms | Comodo Secure DNS |
| o | 106.186.17.181 | 74.223363ms | OpenNIC |
| o | 106.185.41.36 | 66.092089ms | OpenNIC |
| o | www.yahoo.com | 75.187195ms | Yahoo!!! |
| x | 192.168.1.201 | ping...faild... | Host is Dead! |
| o | 2001:4860:4860:...| 1.264779ms | Google_IPv6 |
| o | 8.8.8.8 | 977.278µs | nandedaaaaaaaaa...|
| o | 8.8.4.4 | 979.161µs | Google_IPv4 |
| o | 208.67.222.123 | 1.186372ms | OpenDNS |
| o | 208.67.220.123 | 951.586µs | OpenDNS |
| o | 216.146.35.35 | 930.594µs | Dyn Internet Guide|
| o | 216.146.36.36 | 240.093372ms | Dyn Internet Guide|
=================================================================
*/
package main
import (
"bufio"
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"math"
"net"
"net/http"
"os"
"os/exec"
"os/user"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/dariubs/percent"
"github.com/mattn/go-runewidth"
"github.com/nsf/termbox-go"
"github.com/tatsushid/go-fastping"
)
/*This global flag*/
var timeout = flag.Duration("t", time.Second*ICMP_TIMEOUT, "")
var interval = flag.Duration("i", time.Millisecond*ICMP_INTERVAL, "")
var pinglist = flag.String("f", PING_LIST, "")
var arpentries = flag.Bool("A", false, "")
var ver = flag.Bool("v", false, "")
var vi = flag.Bool("V", false, "")
var httping = flag.Bool("H", false, "")
var sslping = flag.Bool("S", false, "")
/*This Used by func flag.Usage()*/
var usage = `
Usage:
pexpo | pexpo.exe [-v] [-h] [-i interval] [-t timeout] [-f ping-list] [-A] [-H] [-S] [-V]
Examples:
./pexpo -i 500ms -t 1s -f /usr/local/ping-list.txt
pexpo.exe -i 500ms -t 1s -f C:\Users\arale\Desktop\ping-list.txt
Option:
-h Print this.
-v Print version.
-i Sending ICMP interval time(Default:500ms, should not be lower this).
You must not use "200" or "1" or..., must use "200ms" or "1s" or ... , so use with time's unit.
-t Sending ICMP timeout time(Default:3s)
You must not use "200" or "1" or..., must use "200ms" or "1s" or ... , so use with time's unit.
this "timeout" is Exact meaning, fastping.NewPinger() receives OnRecv struct value interval.
-f Using Filepath of ping-list(Default:current_dir/ping-list.txt).
-A If you want to write on ping-list -- such as Cisco's show ip arp -- ,
"Internet 10.0.0.1 0 ca01.18cc.0038 ARPA Ethernet2/0",
Ignoring string "Internet", So It is good as you copy&paste show ip arp line.
-V if you DON'T want to make file "ping-list", should use this option.
this option is run "vi", and make tmpfile...pexpo this file as ping-list.
Ignoring string "Internet", So It is good as you copy&paste show ip arp line.
<HTTP mode options!>
Examples:
./pexpo -H -i 500ms -t 1s -f /usr/local/curl-list.txt
pexpo.exe -S -i 500ms -t 1s -f C:\Users\arale\Desktop\curl-list.txt
(If you want to "Request, http and https", Using Both -H & -S.)
Option:
-H This optison is like "curl". So you Sending HTTP(:80) GET Request instead of the PING...!
-S This optison is like "curl". So you Sending HTTP"S"(:443) GET Request instead of the PING...!
-H or -S options HTTP/HTTPS GET Request instead of the PING.
(Just like, curl -LIs www.google.com -o /dev/null -w '%{http_code}\n')
This Request is ververy simple GET Request, Only Getting status code(No header, No form, No getting data.)
And, if http status code is "200", string color is Blue, else Red.
`
const (
/*Used by logging*/
DAY = "20060102"
DATE = "2006-01-02 15:04:05.000"
RESULT_DIR = ".pexpo"
/*Default ping-list*/
PING_LIST = "ping-list.txt"
/*This values disigning terminal*/
COLUMN = 17
//JUDGE_X = 3
HOST_X = 7
RTT_X = 27
DES_X = 47
START_X = 1
EDGE_X = 65
LIST_H_X = 70
LIST_P_X = 90
LIST_L_X = 100
LIST_D_X = 110
DRAW_UP_Y = 3
DRAW_DW_Y = 2
/*Sending ICMP Param*/
ICMP_INTERVAL = 500
ICMP_TIMEOUT = 3
)
var (
/*pexpo's version*/
version string
)
func fatal(err error) {
if err != nil {
log.Fatal(err)
}
}
func addog(text string, filename string) {
var writer *bufio.Writer
textData := []byte(text)
writeFile, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0755)
writer = bufio.NewWriter(writeFile)
writer.Write(textData)
writer.Flush()
fatal(err)
defer writeFile.Close()
}
func round(f float64, places int) float64 {
shift := math.Pow(10, float64(places))
return math.Floor(f*shift+.5) / shift
}
func keyEventLoop(killKey chan termbox.Key) {
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
killKey <- ev.Key
default:
}
}
}
func drawLine(x, y int, str string) {
color := termbox.ColorDefault
backgroundColor := termbox.ColorDefault
runes := []rune(str)
for n := 0; n < len(runes); n += 1 {
termbox.SetCell(x+n, y, runes[n], color, backgroundColor)
}
}
func drawLineColor(x, y int, str string, code termbox.Attribute) {
termbox.SetOutputMode(termbox.Output256)
color := code
backgroundColor := termbox.ColorDefault
runes := []rune(str)
for n := 0; n < len(runes); n += 1 {
termbox.SetCell(x+n, y, runes[n], color, backgroundColor)
}
}
func drawLineColorful(x, y int, str string, strcode, backcode termbox.Attribute) {
termbox.SetOutputMode(termbox.Output256)
color := strcode
backgroundColor := backcode
runes := []rune(str)
for n := 0; n < len(runes); n += 1 {
termbox.SetCell(x+n, y, runes[n], color, backgroundColor)
}
}
func drawFlag(x, y int, flag string) {
if flag == "o" || flag == "200" {
drawLineColor(x, y, fmt.Sprintf("%v", flag), termbox.ColorBlue)
} else {
drawLineColor(x, y, fmt.Sprintf("%v", flag), termbox.ColorRed)
}
}
func drawSeq(hx, rx, dx, y int, flag, r1, r2, des string) {
if flag == "o" || flag == "200" {
drawLine(hx, y, fmt.Sprintf("%v", runewidth.Truncate(r1, COLUMN, "")))
drawLine(rx, y, fmt.Sprintf("%v", runewidth.Truncate(r2, COLUMN, "")))
drawLine(dx, y, fmt.Sprintf("%v", runewidth.Truncate(des, COLUMN, "")))
} else {
drawLineColor(hx, y, fmt.Sprintf("%v", runewidth.Truncate(r1, COLUMN, "")), termbox.ColorRed)
drawLineColor(rx, y, fmt.Sprintf("%v", runewidth.Truncate(r2, COLUMN, "")), termbox.ColorRed)
drawLineColor(dx, y, fmt.Sprintf("%v", runewidth.Truncate(des, COLUMN, "")), termbox.ColorRed)
}
}
func fill(x, y, w, h int, cell termbox.Cell) {
for ly := 0; ly < h; ly++ {
for lx := 0; lx < w; lx++ {
termbox.SetCell(x+lx, y+ly, cell.Ch, cell.Fg, cell.Bg)
}
}
}
/*This Core of the sendig ICMP engine*/
func pinger(host string) (res []string) {
p := fastping.NewPinger()
/*Selecting IPv4 or IPv6*/
netProto := "ip4:icmp"
if strings.Index(host, ":") != -1 {
netProto = "ip6:ipv6-icmp"
}
ra, err := net.ResolveIPAddr(netProto, host)
if err != nil {
termbox.Close()
panic(err)
}
p.AddIPAddr(ra)
p.MaxRTT = *interval //icmp interval
out := make([]string, 0, 2) //out is success ping reult []string
res = make([]string, 0, 3) //res is for chanerizing the Ping result.
receiver := make(chan []string, 2) //suucess ping result -> receiver
parent, pCancel := context.WithTimeout(context.Background(), *timeout)
child, cCancel := context.WithCancel(parent)
defer cCancel()
/*Received value from fastping.NewPinger()*/
go func() {
p.OnRecv = func(addr *net.IPAddr, rtt time.Duration) {
out = append(out, host, rtt.String())
select {
case receiver <- out:
defer close(receiver)
return
case <-parent.Done():
pCancel()
return
}
}
}()
p.OnIdle = func() {
}
err = p.Run()
fatal(err)
select {
case res = <-receiver:
res = append([]string{"o"}, res...)
return
case <-child.Done():
res = append(res, "x", host, "ping...faild...")
return
}
}
/*https://golang.org/pkg/net/http/#Client*/
var client = http.Client{}
/*This http ping engine*/
func curlCheck(url string) (res []string) {
res = make([]string, 0, 3) // res is for chanerizing the HTTP Ping result.
/*syntax check*/
if *httping && *sslping {
if !strings.Contains(url, "https://") && !strings.Contains(url, "http://") {
url = "https://" + url
}
} else {
if *sslping {
if !strings.Contains(url, "https://") {
url = "https://" + url
}
} else if *httping {
if !strings.Contains(url, "http://") {
url = "http://" + url
}
}
}
timeStart := time.Now()
req, err := http.NewRequest("GET", url, nil)
fatal(err)
ctx, cancel := context.WithTimeout(context.Background(), *timeout)
defer cancel()
req = req.WithContext(ctx)
resp, err := client.Do(req)
if err != nil {
if *sslping {
res = append(res, "000", url, "ssl...no_response")
} else {
res = append(res, "000", url, "http...no_response")
}
return
}
defer resp.Body.Close()
res = append(res, strconv.Itoa(resp.StatusCode), url, time.Since(timeStart).String())
return
}
var scrCount int
/*HostCounter count per host Loss*/
type HostCounter struct {
Name string
Description string
Loss int
LossPercent float64
IsDead bool
}
/*HostList is slice of HostCounter*/
type HostList struct {
Hosts []HostCounter
}
var hostlist *HostList
func newHostList() (hl *HostList) {
h := make([]HostCounter, 0, 200)
hl = &HostList{
Hosts: h,
}
return
}
func (hl *HostList) registerVal(index int, host HostCounter) []HostCounter {
hl.Hosts[index] = host
return hl.Hosts
}
func drawHostlist(maxX, maxY int) {
fill(LIST_H_X, DRAW_UP_Y, COLUMN+2, maxY-4, termbox.Cell{Ch: ' '})
fill(LIST_P_X, DRAW_UP_Y, COLUMN+2, maxY-4, termbox.Cell{Ch: ' '})
fill(LIST_L_X, DRAW_UP_Y, COLUMN+2, maxY-4, termbox.Cell{Ch: ' '})
for n, h := range hostlist.Hosts {
if n < scrCount {
continue
}
drawLineColor(LIST_H_X, n-scrCount+DRAW_UP_Y, fmt.Sprintf("%v", runewidth.Truncate(h.Name, COLUMN, "")), termbox.ColorGreen)
drawLineColor(LIST_P_X, n-scrCount+DRAW_UP_Y, fmt.Sprintf("%.2f", h.LossPercent), termbox.ColorGreen)
drawLineColor(LIST_L_X, n-scrCount+DRAW_UP_Y, fmt.Sprintf("%v loss", h.Loss), termbox.ColorGreen)
if h.IsDead {
drawLineColor(LIST_D_X, n-scrCount+DRAW_UP_Y, fmt.Sprintf("%v", "Dead Now!"), termbox.ColorRed)
}
if n > maxY-6+scrCount {
break
}
}
}
/*Pauser give pause implementation*/
type Pauser struct {
Stop chan struct{}
MainMux sync.Mutex
DrawMux sync.Mutex
}
/*This is Main loop*/
func drawLoop(maxX, maxY int, pauser *Pauser) {
/***************************
Initilizing part here /(^o^)\
***************************/
var i int // "i" is all pings count.
var j int // "j" is all pings "per host" count.
var k int // "k" is scroll counter
pbfAry := make([]string, 0, 200) // pbfAry is ping-list(textfile -> buffer).
rbfAry := make([]string, 0, 200) // rbfAry is ping result list.
/*select mode*/
var JUDGE_X int
if *httping || *sslping {
JUDGE_X = 2
} else {
JUDGE_X = 3
}
/*reading ping-list*/
pl, err := os.Open(*pinglist)
if err != nil {
termbox.Close()
panic(err)
}
defer pl.Close()
plscanner := bufio.NewScanner(pl)
for plscanner.Scan() {
s := plscanner.Text()
/*If blank line, transform to "#"*/
if s == "" {
s = "#" + s
}
/*# is comment out line*/
if !strings.HasPrefix(s, "#") {
/*Deleting "\t"...tab key*/
for {
if strings.Contains(s, "\t") {
sAry := strings.SplitN(s, "\t", 2)
s = sAry[0] + " " + sAry[1]
} else {
break
}
}
/*Deleting consecutive white space "head"
" 8.8.8.8 google.com" */
for {
if strings.HasPrefix(s, " ") {
sAry := strings.SplitN(s, " ", 2)
s = sAry[1]
} else {
break
}
}
/*No description, Put in "noname_host"
If not this statement, After, Will read blank array,
So, occurring panic error*/
if !strings.Contains(s, " ") {
s = s + " noname_host"
} else {
/*Deleting consecutive white space "between"
"8.8.8.8 google.com" */
sAry := strings.SplitN(s, " ", 2)
sAry[1] = strings.TrimSpace(sAry[1])
s = sAry[0] + " " + sAry[1]
/*if "8.8.8.8 "
pus noname_host*/
if len(strings.TrimSpace(s)) == len(sAry[0]) {
s = s + "noname_host"
}
/*For -A option
Ignoring string, "Internet"*/
if *arpentries && strings.HasPrefix(s, "Internet") {
sAry := strings.SplitN(s, " ", 2)
s = sAry[1]
}
}
if !*httping || !*sslping {
sAry := strings.SplitN(s, " ", 2)
if *httping && strings.Contains(sAry[0], "https://") {
termbox.Close()
fmt.Printf("Sorry, %v is not http protocol.\n", sAry[0])
fmt.Printf("Please, Check your %v.\n", *pinglist)
os.Exit(1)
} else if *sslping && strings.Contains(sAry[0], "http://") {
termbox.Close()
fmt.Printf("Sorry, %v is not https protocol.\n", sAry[0])
fmt.Printf("Please, Check your %v.\n", *pinglist)
os.Exit(1)
}
}
/*ping-list -> pbf*/
pbfAry = append(pbfAry, s)
}
if err := plscanner.Err(); err != nil {
panic(err)
}
}
/*draw init loss counter*/
for _, pres := range pbfAry {
sAry := strings.SplitN(pres, " ", 2)
//s := sAry[0]
var h HostCounter
h.Name = sAry[0]
h.Description = sAry[1]
hostlist.Hosts = append(hostlist.Hosts, h)
}
drawHostlist(maxX, maxY)
/*making logging file*/
day := time.Now()
formatingDay := day.Format(DAY)
result := "result_" + formatingDay + ".txt"
u, err := user.Current()
fatal(err)
rfile := filepath.Join(u.HomeDir, RESULT_DIR, result)
/*drawing column*/
drawLine(maxX-44, 0, "Ctrl+S: Stop & Restart, Esc or Ctrl+C: Exit.")
drawLine(maxX-9, maxY-1, fmt.Sprintf("ver. %v", version))
drawLineColorful(LIST_H_X-1, 1, fmt.Sprintf("%v", " Now, Loss counting Per host. "), termbox.ColorDefault, termbox.ColorMagenta)
drawLineColor(LIST_H_X, 2, fmt.Sprintf("%v", "Hostname"), termbox.ColorWhite)
drawLineColor(LIST_P_X, 2, fmt.Sprintf("%v", "Loss(%)"), termbox.ColorWhite)
drawLineColor(LIST_L_X, 2, fmt.Sprintf("%v", "Loss(sum)"), termbox.ColorWhite)
drawLineColor(LIST_D_X, 2, fmt.Sprintf("%v", "Dead Now?"), termbox.ColorWhite)
drawLineColor(120, DRAW_UP_Y, "↑", termbox.ColorDefault)
drawLineColor(120, maxY-2, "↓", termbox.ColorDefault)
drawLine(HOST_X, 1, fmt.Sprintf("%v", "Host"))
drawLine(RTT_X, 1, fmt.Sprintf("%v", "Response"))
drawLine(DES_X, 1, fmt.Sprintf("%v", "Description"))
fill(START_X, 0, EDGE_X, 1, termbox.Cell{Ch: '='})
fill(START_X, 2, EDGE_X, 1, termbox.Cell{Ch: '='})
fill(START_X, maxY-1, EDGE_X, 1, termbox.Cell{Ch: '='})
fill(START_X, 3, 1, maxY-4, termbox.Cell{Ch: '|'})
fill(START_X, 1, 1, 1, termbox.Cell{Ch: '|'})
fill(HOST_X-2, 3, 1, maxY-4, termbox.Cell{Ch: '|'})
fill(HOST_X-2, 1, 1, 1, termbox.Cell{Ch: '|'})
fill(RTT_X-2, 3, 1, maxY-4, termbox.Cell{Ch: '|'})
fill(RTT_X-2, 1, 1, 1, termbox.Cell{Ch: '|'})
fill(DES_X-2, 3, 1, maxY-4, termbox.Cell{Ch: '|'})
fill(DES_X-2, 1, 1, 1, termbox.Cell{Ch: '|'})
fill(EDGE_X, 3, 1, maxY-4, termbox.Cell{Ch: '|'})
fill(EDGE_X, 1, 1, 1, termbox.Cell{Ch: '|'})
/***************************
Starting MAIN LOOP!!!\(@w@)/
***************************/
for {
/*Counting per running This function*/
j++
/*Userd by hbf, index must be Reinitialize in per loop*/
for index, host := range hostlist.Hosts {
/*For Stop & Restart*/
select {
case <-pauser.Stop:
pauser.MainMux.Unlock()
pauser.DrawMux.Lock()
pauser.MainMux.Unlock()
/*Default behavior*/
default:
}
resAry := make([]string, 0, 3)
if *httping || *sslping {
time.Sleep(*interval)
resAry = curlCheck(host.Name)
} else {
resAry = pinger(host.Name)
}
if resAry[0] != "o" && resAry[0] != "200" {
host.Loss++
}
/*Before Scrolling To the bottom*/
if maxY > i+DRAW_UP_Y+1 {
drawFlag(JUDGE_X, i+DRAW_UP_Y, resAry[0])
drawFlag(JUDGE_X, 1, resAry[0])
drawSeq(HOST_X, RTT_X, DES_X, i+DRAW_UP_Y, resAry[0], resAry[1], resAry[2], host.Description)
/*After Scrolling To the bottom*/
} else {
/*ping-list clear*/
fill(JUDGE_X+1, DRAW_UP_Y, 1, maxY-4, termbox.Cell{Ch: ' '})
fill(HOST_X+1, DRAW_UP_Y, COLUMN-1, maxY-4, termbox.Cell{Ch: ' '})
fill(RTT_X+1, DRAW_UP_Y, COLUMN-1, maxY-4, termbox.Cell{Ch: ' '})
fill(DES_X+1, DRAW_UP_Y, COLUMN-1, maxY-4, termbox.Cell{Ch: ' '})
drawFlag(JUDGE_X, maxY-DRAW_DW_Y, resAry[0])
drawFlag(JUDGE_X, 1, resAry[0])
drawSeq(HOST_X, RTT_X, DES_X, maxY-DRAW_DW_Y, resAry[0], resAry[1], resAry[2], host.Description)
/*rc is count Reading rbf After Scrolling To the bottom*/
var rc int
//var tmpAry []string
tmpAry := make([]string, 0, 200)
/*"rc" -"k" -> "All Result" - "Line of Don't want to see" */
rc = rc - k
for _, rs := range rbfAry {
if rc > 0 {
rsAry := strings.SplitN(rs, " ", 4)
drawFlag(JUDGE_X, rc+2, rsAry[0])
drawSeq(HOST_X, RTT_X, DES_X, rc+2, rsAry[0], rsAry[1], rsAry[2], rsAry[3])
tmpAry = append(tmpAry, rs)
}
rc++
}
k++
copy(tmpAry, rbfAry)
}
/*finish Reading Ping-list & Drawing Result.
After, Logging, & Drawing Loss Counter*/
//var pres []string
pres := make([]string, 0, 4)
pres = append(pres, resAry[0], resAry[1], resAry[2], host.Description)
/*Logging rbf -> This buffer Called by Next Drawing*/
rbfAry = append(rbfAry, strings.Join(pres, " "))
/*Logging All Result with time stamp*/
date := time.Now()
formatingDate := date.Format(DATE)
log := "[" + formatingDate + "]" + " " + strings.Join(pres, " ") + "\n"
addog(log, rfile)
host.LossPercent = round(percent.PercentOf(host.Loss, j), 2)
/*Judge the Dead*/
if resAry[0] == "o" || resAry[0] == "200" {
host.IsDead = false
} else {
host.IsDead = true
}
hostlist.Hosts = hostlist.registerVal(index, host)
drawHostlist(maxX, maxY)
/*Drawing Done*/
termbox.Flush()
/*All couting per sending ICMP*/
i++
}
}
}
func init() {
flag.Usage = func() {
fmt.Printf(usage)
}
flag.Parse()
if *ver {
fmt.Println("version: ", version)
os.Exit(0)
}
u, err := user.Current()
fatal(err)
rdir := filepath.Join(u.HomeDir, RESULT_DIR)
err = os.MkdirAll(rdir, 0755)
fatal(err)
hostlist = newHostList()
if *vi {
edited := make(chan struct{}, 0)
go func() {
tmplist, err := ioutil.TempFile("/tmp", "pexpotmp")
defer tmplist.Close()
if err != nil {
panic(err)
}
cmd := exec.Command(`vi`, tmplist.Name())
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
runErr := cmd.Run()
if runErr != nil {
panic(runErr)
}
*pinglist = tmplist.Name()
edited <- struct{}{}
}()
<-edited
}
}
func main() {
if *vi {
defer func() {
if err := os.RemoveAll(*pinglist); err != nil {
fmt.Println(err)
}
}()
}
/*termbox start*/
err := termbox.Init()
fatal(err)
defer termbox.Close()
maxX, maxY := termbox.Size()
/*killKey channel is received HW key interrupt*/
killKey := make(chan termbox.Key)
/*sleep flag*/
sleep := false
/*pauser give pause implementation*/
pauser := &Pauser{
Stop: make(chan struct{}, 0),
}
pauser.MainMux.Lock()
pauser.DrawMux.Lock()
go keyEventLoop(killKey)
go drawLoop(maxX, maxY, pauser)
for {
select {
case wait := <-killKey:
switch wait {
case termbox.KeyEsc, termbox.KeyCtrlC:
return
case termbox.KeyCtrlS:
if sleep == false {
fill(maxX-44, 0, 45, 1, termbox.Cell{Ch: ' '})
drawLineColor(maxX-48, 0, "Stop Now!! Crtl+S: Restart, Esc or Ctrl+C: Exit.", termbox.ColorYellow)
pauser.Stop <- struct{}{}
pauser.MainMux.Lock()
sleep = true
} else if sleep == true {
fill(maxX-48, 0, 49, 1, termbox.Cell{Ch: ' '})
drawLine(maxX-44, 0, "Ctrl+S: Stop & Restart, Esc or Ctrl+C: Exit.")
pauser.DrawMux.Unlock()
pauser.MainMux.Lock()
sleep = false
}
//case termbox.KeyArrowUp, termbox.KeyCtrlA:
case termbox.KeyArrowDown, termbox.KeyCtrlZ:
if len(hostlist.Hosts) >= scrCount+maxY-3 {
scrCount++
drawLineColor(120, maxY-2, "↓", termbox.ColorCyan)
drawHostlist(maxX, maxY)
} else {
drawLineColor(120, maxY-2, "↓", termbox.ColorRed)
}
drawLineColor(120, DRAW_UP_Y, "↑", termbox.ColorDefault)
termbox.Flush()
//case termbox.KeyArrowDown, termbox.KeyCtrlZ:
case termbox.KeyArrowUp, termbox.KeyCtrlA:
if scrCount != 0 {
scrCount--
drawLineColor(120, DRAW_UP_Y, "↑", termbox.ColorCyan)
drawHostlist(maxX, maxY)
} else {
drawLineColor(120, DRAW_UP_Y, "↑", termbox.ColorRed)
}
drawLineColor(120, maxY-2, "↓", termbox.ColorDefault)
termbox.Flush()
}
}
}
}