-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
376 lines (314 loc) · 7.31 KB
/
Copy pathmain.go
File metadata and controls
376 lines (314 loc) · 7.31 KB
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
package main
import (
"fmt"
"net"
"strconv"
"strings"
"sync"
"time"
mc "github.com/authapon/mcryptzero"
tg "gopkg.in/telegram-bot-api.v4"
)
type (
host struct {
name string
htype string
state byte
alert int64
}
user struct {
name string
id int64
}
)
var (
botUP *tg.BotAPI
botDOWN *tg.BotAPI
mutex = &sync.Mutex{}
coreChan = make(chan string)
hosts = make([]host, 0)
)
func makeBot(token string, btype string) {
for {
bot, err := tg.NewBotAPI(token)
if err != nil {
time.Sleep(5 * time.Second)
continue
}
switch btype {
case "up":
botUP = bot
default:
botDOWN = bot
}
go func() {
u := tg.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
if err != nil {
fmt.Printf("Error to get Channel")
return
}
for update := range updates {
if update.Message == nil {
continue
}
txt := strings.TrimSpace(update.Message.Text)
txtlow := strings.ToLower(txt)
go func(txtdat, bbtype string, uid int64) {
switch txtdat {
case "myid":
SendMsg(uid, fmt.Sprintf("Your ID is %d", uid), bbtype)
case "status":
coreChan <- "show status " + fmt.Sprintf("%d %s", uid, bbtype)
case "up":
coreChan <- "show up " + fmt.Sprintf("%d %s", uid, bbtype)
case "down":
coreChan <- "show down " + fmt.Sprintf("%d %s", uid, bbtype)
case "ping":
coreChan <- "show ping " + fmt.Sprintf("%d %s", uid, bbtype)
case "mysql":
coreChan <- "show mysql " + fmt.Sprintf("%d %s", uid, bbtype)
case "web":
coreChan <- "show web " + fmt.Sprintf("%d %s", uid, bbtype)
case "ldap":
coreChan <- "show ldap " + fmt.Sprintf("%d %s", uid, bbtype)
case "dns":
coreChan <- "show dns " + fmt.Sprintf("%d %s", uid, bbtype)
case "watch":
coreChan <- "show watch " + fmt.Sprintf("%d %s", uid, bbtype)
default:
for i := range users {
if users[i].id == uid {
go SendMsg(uid, "Sorry! I don't understand the command.", bbtype)
}
}
}
}(txtlow, btype, int64(update.Message.From.ID))
}
}()
break
}
SendMsgAll("NetAlertBot is *working* now.", btype)
}
func SendMsg(user int64, txt string, btype string) {
mutex.Lock()
msg := tg.NewMessage(user, txt)
msg.ParseMode = "Markdown"
switch btype {
case "up":
botUP.Send(msg)
default:
botDOWN.Send(msg)
}
mutex.Unlock()
}
func SendMsgAll(txt string, btype string) {
for k, _ := range users {
SendMsg(users[k].id, txt, btype)
}
}
func getAllHostType() []string {
ht := make(map[string]bool)
for i := range hosts {
ht[hosts[i].htype] = true
}
htype := make([]string, 0)
for k, _ := range ht {
htype = append(htype, k)
}
return htype
}
func timeTXT() string {
thisTime := time.Now().UTC().Add(7 * time.Hour)
return fmt.Sprintf("%d-%02d-%02d %02d:%02d:%02d", thisTime.Year(), int(thisTime.Month()), thisTime.Day(), thisTime.Hour(), thisTime.Minute(), thisTime.Second())
}
func alertDown(index int) {
SendMsgAll(fmt.Sprintf("%s\n*DOWN* -> %s\nService: *%s*", timeTXT(), hosts[index].name, strings.ToUpper(hosts[index].htype)), "down")
}
func alertUp(index int) {
SendMsgAll(fmt.Sprintf("%s\n*UP* -> %s\nService: *%s*", timeTXT(), hosts[index].name, strings.ToUpper(hosts[index].htype)), "up")
}
func checkingHOST() {
t := time.Now().Unix()
for i := range hosts {
if t > hosts[i].alert {
go alertDown(i)
hosts[i].state = 1
hosts[i].alert = hosts[i].alert + repeatDuration
}
}
}
func uptime(htype, hostname string) {
t := time.Now().Unix()
found := false
for i := range hosts {
if hosts[i].htype == htype && hosts[i].name == hostname {
if hosts[i].state == 1 {
go alertUp(i)
}
hosts[i].state = 0
hosts[i].alert = t + downDuration
found = true
}
}
if !found {
hostx := host{
htype: htype,
name: hostname,
state: 0,
alert: t + downDuration,
}
hosts = append(hosts, hostx)
}
}
func startHost(htype, hostname string) {
t := time.Now().Unix()
found := false
for i := range hosts {
if hosts[i].htype == htype && hosts[i].name == hostname {
found = true
}
}
if !found {
hostx := host{
htype: htype,
name: hostname,
state: 0,
alert: t + downDuration,
}
hosts = append(hosts, hostx)
}
}
func ShowService(uid int64, service, bot string) {
hserviceTXT := ""
for i := range hosts {
if hosts[i].htype == service {
if hosts[i].state == 0 {
hserviceTXT += "UP -> " + hosts[i].name + "\n"
} else {
hserviceTXT += "*DOWN* -> " + hosts[i].name + "\n"
}
}
}
if hserviceTXT == "" {
hserviceTXT = "no service\n"
}
go SendMsg(uid, fmt.Sprintf("%s\nService: *%s*\n\n%s", timeTXT(), service, hserviceTXT), bot)
}
func ShowServiceState(uid int64, service, bot string, state byte) {
hserviceTXT := ""
stateTXT := "UP"
if state == 1 {
stateTXT = "*DOWN*"
}
for i := range hosts {
if hosts[i].htype == service {
if hosts[i].state == state {
hserviceTXT += stateTXT + " -> " + hosts[i].name + "\n"
}
}
}
if hserviceTXT == "" {
hserviceTXT = "nothing\n"
}
go SendMsg(uid, fmt.Sprintf("%s\nService: *%s*\n\n%s", timeTXT(), service, hserviceTXT), bot)
}
func coreLoop() {
for {
data := <-coreChan
arg := strings.Split(data, " ")
switch arg[0] {
case "show":
go func(arg1, arg2, arg3 string) {
uid, _ := strconv.ParseInt(arg2, 10, 64)
switch arg1 {
case "status":
allHtype := getAllHostType()
if len(allHtype) == 0 {
SendMsg(uid, "No any service", arg3)
} else {
for i := range allHtype {
go ShowService(uid, allHtype[i], arg3)
}
}
case "up":
allHtype := getAllHostType()
if len(allHtype) == 0 {
SendMsg(uid, "No any service", arg3)
} else {
for i := range allHtype {
go ShowServiceState(uid, allHtype[i], arg3, byte(0))
}
}
case "down":
allHtype := getAllHostType()
if len(allHtype) == 0 {
SendMsg(uid, "No any service", arg3)
} else {
for i := range allHtype {
go ShowServiceState(uid, allHtype[i], arg3, byte(1))
}
}
default:
go ShowService(uid, arg1, arg3)
}
}(arg[1], arg[2], arg[3])
case "checking":
fmt.Printf("Checking\n")
go checkingHOST()
default:
datax := strings.SplitN(data, " ", 3)
switch datax[0] {
case "up":
uptime(datax[1], datax[2])
case "start":
startHost(datax[1], datax[2])
}
}
}
}
func UDPserver() {
udpAddr, err := net.ResolveUDPAddr("udp", port)
if err != nil {
fmt.Printf("Error in UDP server!!!\n")
panic(err)
}
conUDP, err := net.ListenUDP("udp", udpAddr)
if err != nil {
fmt.Printf("Error to listen UDP!!!\n")
panic(err)
}
for {
buffer := make([]byte, 1024)
n, _, err := conUDP.ReadFromUDP(buffer)
if err != nil {
continue
}
dcrypt := string(buffer[:n])
dcryptsplit := strings.SplitN(dcrypt, ":", 2)
if len(dcryptsplit) != 2 {
fmt.Printf("Got wrong UDP data\n")
continue
}
data := string(mc.Decrypt([]byte(dcryptsplit[1]), []byte(dcryptsplit[0]+secret+dcryptsplit[0])))
go func(datax string) {
coreChan <- datax
fmt.Printf("%s\n", datax)
}(data)
}
}
func checkLoop() {
c := time.Tick(checkingLoopDuration)
for _ = range c {
coreChan <- "checking"
}
}
func main() {
configProcess()
go coreLoop()
go UDPserver()
go makeBot(tokenTGup, "up")
go makeBot(tokenTGdown, "down")
checkLoop()
}