-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·260 lines (231 loc) · 5.29 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
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"time"
"github.com/prometheus-community/pro-bing"
flag "github.com/spf13/pflag"
)
var (
projectName string = "Pingo"
firstTry bool = true
isConnected bool = false
mode string = "term"
// term : (default) full terminal text
// termMin : minimal terminal text
// ico : icon mode
// notify : notification mode
noTrail bool
inPolybar bool
noneStop bool
reCheckingDelay uint
reTryingDelay uint
timeout uint
leftAttempts uint8
red string
green string
blue string
none string
pingIP string
)
const (
attempts uint8 = 3
IPdefault string = "google.com"
redB string = "\033[38;5;9m"
greenB string = "\033[38;5;10m"
blueB string = "\033[38;5;33m"
noneB string = "\033[0m"
redP string = "%{F#c55555}"
greenP string = "%{F#a9c474}"
blueP string = "%{F#0b79fe}"
noneP string = "%{F-}"
)
func main() {
args()
env()
colorSetup()
leftAttempts = attempts
for leftAttempts != 0 {
// try
isConnected, stats := pinger()
if isConnected {
if firstTry && !noneStop {
break
}
// Connected but need recheck
if !noneStop {
recheckMSG(stats)
leftAttempts--
} else {
conectedMSG(stats)
}
time.Sleep(time.Duration(reCheckingDelay) * time.Second)
} else {
// not Connected
notconectedMSG()
leftAttempts = attempts
time.Sleep(time.Duration(reTryingDelay) * time.Second)
}
firstTry = false
}
// all GOOD :)
stableMSG()
}
func sendNotification(amount, duration int, text string, isCritical bool) {
cmd := []string{projectName, text, fmt.Sprintf("--expire-time=%d", duration)}
if isCritical {
cmd = append(cmd, "--urgency=critical")
}
for i := 1; i <= amount; i++ {
exec.Command("notify-send", cmd...).Run()
}
}
func pinger() (bool, *probing.Statistics) {
pinger, err := probing.NewPinger(pingIP)
if err != nil {
return false, &probing.Statistics{}
}
pinger.Timeout = time.Millisecond * time.Duration(timeout)
pinger.Count = 1
err = pinger.Run()
if err != nil {
return false, &probing.Statistics{}
}
stats := pinger.Statistics()
return stats.PacketsRecv >= 1, &*stats
}
func stableMSG() {
switch mode {
case "term":
RLL()
if firstTry {
fmt.Printf(
" %s✓%s - Conection is stable - %sConnected form the first try%s\n",
green, none,
green, none,
)
} else {
fmt.Printf(
" %s✓%s - Conection is stable - %d/%d \n",
green, none,
attempts, attempts,
)
}
break
case "termMin":
RLL()
fmt.Printf(" %s✓%s - Conection is stable \n", green, none)
break
case "ico":
RLL()
fmt.Printf(" %s●%s \n", green, none)
break
case "notify":
sendNotification(1, 5000, "Conection is stable", false)
break
}
}
func recheckMSG(stats *probing.Statistics) {
switch mode {
case "term":
RLL()
fmt.Printf(
" %s◎%s - Connected re-checking - %dms - %d/%d ",
blue, none,
int64(stats.AvgRtt.Milliseconds()),
attempts-leftAttempts, attempts,
)
break
case "termMin":
RLL()
fmt.Printf(" %s◎%s - Connected re-checking ", blue, none)
break
case "ico":
RLL()
fmt.Printf(" %s◎%s ", blue, none)
break
case "notify":
sendNotification(1, 5000, "Connected re-checking", false)
break
}
}
func notconectedMSG() {
switch mode {
case "term":
RLL()
fmt.Printf(
" %s●%s - Not connected - %sReset%s ",
red, none, red, none,
)
break
case "termMin":
RLL()
fmt.Printf(" %s●%s - Not connected ", red, none)
break
case "ico":
RLL()
fmt.Printf(" %s●%s ", red, none)
break
case "notify":
sendNotification(1, 1000, "Not connected", true)
break
}
}
func conectedMSG(stats *probing.Statistics) {
switch mode {
case "term":
RLL()
fmt.Printf(
" %s●%s - Connected - %dms ",
green, none,
int64(stats.AvgRtt.Milliseconds()),
)
break
case "termMin":
RLL()
fmt.Printf(" %s●%s - Connected ", green, none)
break
case "ico":
RLL()
fmt.Printf(" %s●%s ", green, none)
break
case "notify":
sendNotification(1, 1000, "Not connected", true)
break
}
}
func RLL() {
// HACK: I KNOW THIS IS SO FUCKING DUMB but its working
if !noTrail {
fmt.Printf("\r" + strings.Repeat(" ", 50))
fmt.Printf("\r")
} else if !firstTry {
fmt.Print("\n")
}
}
func args() {
flag.StringVarP(&mode, "mode", "m", "term", "Mode (term, termMin, ico, notify)")
flag.BoolVarP(&noneStop, "nonestop", "n", false,
"turn on noneStop (use '$ killall pingo' for stop)")
flag.BoolVar(&noTrail, "no-trail", false, "no trail (no replacing last line)")
flag.BoolVarP(&inPolybar, "polybar", "p", false, "polybar colors")
flag.UintVar(&timeout, "timeout", 300, "ping timeout in Milliseconds")
flag.UintVar(&reCheckingDelay, "recheck-delay", 8, "delay between rechecks in Seconds")
flag.UintVar(&reTryingDelay, "retry-delay", 1, "delay between retrys in Seconds")
flag.Parse()
}
func env() {
pingIP = os.Getenv("PINGOIP")
if pingIP == "" {
pingIP = IPdefault
}
}
func colorSetup() {
if inPolybar {
red, blue, green, none = redP, blueP, greenP, noneP
} else {
red, blue, green, none = redB, blueB, greenB, noneB
}
}