-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
592 lines (535 loc) · 16.5 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
package main
import (
"log"
"net/http"
"github.com/gorilla/websocket"
)
var clients_settings = make(map[*websocket.Conn]bool) // connected clients
var clients_overview = make(map[*websocket.Conn]bool) // connected clients
var clients_jailscontainers = make(map[*websocket.Conn]bool) // connected clients
var clients_instance_jail = make(map[*websocket.Conn]bool) // connected clients
var clients_bhyvevms = make(map[*websocket.Conn]bool) // connected clients
var clients_vm_packages = make(map[*websocket.Conn]bool) // connected clients
var clients_nodes = make(map[*websocket.Conn]bool) // connected clients
var clients_vpnet = make(map[*websocket.Conn]bool) // connected clients
var clients_authkey = make(map[*websocket.Conn]bool) // connected clients
var clients_media = make(map[*websocket.Conn]bool) // connected clients
var clients_repo = make(map[*websocket.Conn]bool) // connected clients
var clients_bases = make(map[*websocket.Conn]bool) // connected clients
var clients_sources = make(map[*websocket.Conn]bool) // connected clients
var clients_jail_marketplace = make(map[*websocket.Conn]bool) // connected clients
var clients_bhyve_marketplace = make(map[*websocket.Conn]bool) // connected clients
var clients_tasklog = make(map[*websocket.Conn]bool) // connected clients
var clients_users = make(map[*websocket.Conn]bool) // connected clients
var clients_imported = make(map[*websocket.Conn]bool) // connected imported
var clients_k8s = make(map[*websocket.Conn]bool) // connected k8s
var broadcast_settings = make(chan []byte) // broadcast channel
var broadcast_overview = make(chan []byte) // broadcast channel
var broadcast_jailscontainers = make(chan []byte) // broadcast channel
var broadcast_instance_jail = make(chan []byte) // broadcast channel
var broadcast_bhyvevms = make(chan []byte) // broadcast channel
var broadcast_vm_packages = make(chan []byte) // broadcast channel
var broadcast_nodes = make(chan []byte) // broadcast channel
var broadcast_vpnet = make(chan []byte) // broadcast channel
var broadcast_authkey = make(chan []byte) // broadcast channel
var broadcast_media = make(chan []byte) // broadcast channel
var broadcast_repo = make(chan []byte) // broadcast channel
var broadcast_bases = make(chan []byte) // broadcast channel
var broadcast_sources = make(chan []byte) // broadcast channel
var broadcast_jail_marketplace = make(chan []byte) // broadcast channel
var broadcast_bhyve_marketplace = make(chan []byte) // broadcast channel
var broadcast_tasklog = make(chan []byte) // broadcast channel
var broadcast_users = make(chan []byte) // broadcast channel
var broadcast_imported = make(chan []byte) // broadcast channel
var broadcast_k8s = make(chan []byte) // broadcast channel
// Configure the upgrader
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func main() {
// Configure websocket route
http.HandleFunc("/clonos/settings/", handleConnections)
http.HandleFunc("/clonos/overview/", handleConnections)
http.HandleFunc("/clonos/jailscontainers/", handleConnections)
http.HandleFunc("/clonos/instance_jail/", handleConnections)
http.HandleFunc("/clonos/bhyvevms/", handleConnections)
http.HandleFunc("/clonos/vm_packages/", handleConnections)
http.HandleFunc("/clonos/nodes/", handleConnections)
http.HandleFunc("/clonos/vpnet/", handleConnections)
http.HandleFunc("/clonos/authkey/", handleConnections)
http.HandleFunc("/clonos/media/", handleConnections)
http.HandleFunc("/clonos/repo/", handleConnections)
http.HandleFunc("/clonos/bases/", handleConnections)
http.HandleFunc("/clonos/sources/", handleConnections)
http.HandleFunc("/clonos/jail_marketplace/", handleConnections)
http.HandleFunc("/clonos/bhyve_marketplace/", handleConnections)
http.HandleFunc("/clonos/tasklog/", handleConnections)
http.HandleFunc("/clonos/users/", handleConnections)
http.HandleFunc("/clonos/imported/", handleConnections)
http.HandleFunc("/clonos/k8s/", handleConnections)
// Start listening for incoming chat messages
go handleMessages_overview()
go handleMessages_settings()
go handleMessages_jailscontainers()
go handleMessages_instance_jail()
go handleMessages_bhyvevms()
go handleMessages_vm_packages()
go handleMessages_nodes()
go handleMessages_vpnet()
go handleMessages_authkey()
go handleMessages_media()
go handleMessages_repo()
go handleMessages_bases()
go handleMessages_sources()
go handleMessages_jail_marketplace()
go handleMessages_bhyve_marketplace()
go handleMessages_tasklog()
go handleMessages_users()
go handleMessages_imported()
go handleMessages_k8s()
// Start the server on localhost port 8000 and log any errors
log.Println("http server started on :8023")
err := http.ListenAndServe(":8023", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
// TODO: need to get pattern and use it as channel route
func handleConnections(w http.ResponseWriter, r *http.Request) {
// Upgrade initial GET request to a websocket
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Fatal(err)
}
// Make sure we close the connection when the function returns
defer ws.Close()
log.Println("Wake up on URL: ",r.URL.Path)
//todo: we need arrays if clients[r.URL.Path][ws]
// enum for { channel1, channel2 } ?
switch r.URL.Path {
case "/clonos/settings/":
clients_settings[ws] = true
case "/clonos/overview/":
clients_overview[ws] = true
case "/clonos/jailscontainers/":
clients_jailscontainers[ws] = true
case "/clonos/instance_jail/":
clients_instance_jail[ws] = true
case "/clonos/bhyvevms/":
clients_bhyvevms[ws] = true
case "/clonos/vm_packages/":
clients_vm_packages[ws] = true
case "/clonos/nodes/":
clients_nodes[ws] = true
case "/clonos/vpnet/":
clients_vpnet[ws] = true
case "/clonos/authkey/":
clients_authkey[ws] = true
case "/clonos/media/":
clients_media[ws] = true
case "/clonos/repo/":
clients_repo[ws] = true
case "/clonos/bases/":
clients_bases[ws] = true
case "/clonos/sources/":
clients_sources[ws] = true
case "/clonos/jail_marketplace/":
clients_jail_marketplace[ws] = true
case "/clonos/bhyve_marketplace/":
clients_bhyve_marketplace[ws] = true
case "/clonos/tasklog/":
clients_tasklog[ws] = true
case "/clonos/users/":
clients_users[ws] = true
case "/clonos/imported/":
clients_imported[ws] = true
case "/clonos/k8s/":
clients_k8s[ws] = true
default:
log.Println("Urouted URL: ",r.URL.Path)
return
}
for {
// Read in a new message as JSON and map it to a Message object
_, amsg, err := ws.ReadMessage()
log.Printf("Read /: [%s]",amsg)
if err != nil {
log.Printf("error: %v", err)
switch r.URL.Path {
case "/clonos/overview/":
delete(clients_overview,ws)
case "/clonos/settings/":
delete(clients_settings,ws)
case "/clonos/jailscontainers/":
delete(clients_jailscontainers, ws)
case "/clonos/instance_jail/":
delete(clients_instance_jail, ws)
case "/clonos/bhyvevms/":
delete(clients_bhyvevms, ws)
case "/clonos/vm_packages/":
delete(clients_vm_packages, ws)
case "/clonos/nodes/":
delete(clients_nodes, ws)
case "/clonos/vpnet/":
delete(clients_vpnet, ws)
case "/clonos/authkey/":
delete(clients_authkey, ws)
case "/clonos/media/":
delete(clients_media, ws)
case "/clonos/repo/":
delete(clients_repo, ws)
case "/clonos/bases/":
delete(clients_bases, ws)
case "/clonos/sources/":
delete(clients_sources, ws)
case "/clonos/jail_marketplace/":
delete(clients_jail_marketplace, ws)
case "/clonos/bhyve_marketplace/":
delete(clients_bhyve_marketplace, ws)
case "/clonos/tasklog/":
delete(clients_tasklog, ws)
case "/clonos/users/":
delete(clients_users, ws)
case "/clonos/imported/":
delete(clients_imported, ws)
case "/clonos/k8s/":
delete(clients_k8s, ws)
default:
log.Println("Urouted URL: ",r.URL.Path)
return
}
break
}
// Send the newly received message to the broadcast channel
switch r.URL.Path {
case "/clonos/settings/":
broadcast_settings <- amsg
case "/clonos/overview/":
broadcast_overview <- amsg
case "/clonos/jailscontainers/":
broadcast_jailscontainers <- amsg
case "/clonos/instance_jail/":
broadcast_instance_jail <- amsg
case "/clonos/bhyvevms/":
broadcast_bhyvevms <- amsg
case "/clonos/vm_packages/":
broadcast_vm_packages <- amsg
case "/clonos/nodes/":
broadcast_nodes <- amsg
case "/clonos/vpnet/":
broadcast_vpnet <- amsg
case "/clonos/authkey/":
broadcast_authkey <- amsg
case "/clonos/media/":
broadcast_media <- amsg
case "/clonos/repo/":
broadcast_repo <- amsg
case "/clonos/bases/":
broadcast_bases <- amsg
case "/clonos/sources/":
broadcast_sources <- amsg
case "/clonos/jail_marketplace/":
broadcast_jail_marketplace <- amsg
case "/clonos/bhyve_marketplace/":
broadcast_bhyve_marketplace <- amsg
case "/clonos/tasklog/":
broadcast_tasklog <- amsg
case "/clonos/users/":
broadcast_users <- amsg
case "/clonos/imported/":
broadcast_imported <- amsg
case "/clonos/k8s/":
broadcast_k8s <- amsg
default:
log.Println("Urouted URL: ",r.URL.Path)
return
}
}
}
func handleMessages_settings() {
for {
// Grab the next message from the broadcast channel
msg := <-broadcast_settings
// Send it out to every client that is currently connected
for client := range clients_settings {
err := client.WriteMessage(1, msg)
if err != nil {
log.Printf("error: %v", err)
client.Close()
delete(clients_settings , client)
}
}
}
}
func handleMessages_overview() {
for {
// Grab the next message from the broadcast channel
msg := <-broadcast_overview
// Send it out to every client that is currently connected
for client := range clients_overview {
err := client.WriteMessage(1, msg)
if err != nil {
log.Printf("error: %v", err)
client.Close()
delete(clients_overview , client)
}
}
}
}
func handleMessages_jailscontainers() {
for {
// Grab the next message from the broadcast channel
msg := <-broadcast_jailscontainers
// Send it out to every client that is currently connected
for client := range clients_jailscontainers {
err := client.WriteMessage(1, msg)
if err != nil {
log.Printf("error: %v", err)
client.Close()
delete(clients_jailscontainers, client)
}
}
}
}
func handleMessages_instance_jail() {
for {
// Grab the next message from the broadcast channel
msg := <-broadcast_instance_jail
// Send it out to every client that is currently connected
for client := range clients_instance_jail {
err := client.WriteMessage(1, msg)
if err != nil {
log.Printf("error: %v", err)
client.Close()
delete(clients_instance_jail,client)
}
}
}
}
func handleMessages_bhyvevms() {
for {
// Grab the next message from the broadcast channel
msg := <-broadcast_bhyvevms
// Send it out to every client that is currently connected
for client := range clients_bhyvevms {
err := client.WriteMessage(1, msg)
if err != nil {
log.Printf("error: %v", err)
client.Close()
delete(clients_bhyvevms,client)
}
}
}
}
func handleMessages_vm_packages() {
for {
// Grab the next message from the broadcast channel
msg := <-broadcast_vm_packages
// Send it out to every client that is currently connected
for client := range clients_vm_packages {
err := client.WriteMessage(1, msg)
if err != nil {
log.Printf("error: %v", err)
client.Close()
delete(clients_vm_packages,client)
}
}
}
}
func handleMessages_nodes() {
for {
// Grab the next message from the broadcast channel
msg := <-broadcast_nodes
// Send it out to every client that is currently connected
for client := range clients_nodes {
err := client.WriteMessage(1, msg)
if err != nil {
log.Printf("error: %v", err)
client.Close()
delete(clients_nodes,client)
}
}
}
}
func handleMessages_vpnet() {
for {
// Grab the next message from the broadcast channel
msg := <-broadcast_vpnet
// Send it out to every client that is currently connected
for client := range clients_vpnet {
err := client.WriteMessage(1, msg)
if err != nil {
log.Printf("error: %v", err)
client.Close()
delete(clients_vpnet,client)
}
}
}
}
func handleMessages_authkey() {
for {
// Grab the next message from the broadcast channel
msg := <-broadcast_authkey
// Send it out to every client that is currently connected
for client := range clients_authkey {
err := client.WriteMessage(1, msg)
if err != nil {
log.Printf("error: %v", err)
client.Close()
delete(clients_authkey,client)
}
}
}
}
func handleMessages_media() {
for {
// Grab the next message from the broadcast channel
msg := <-broadcast_media
// Send it out to every client that is currently connected
for client := range clients_media {
err := client.WriteMessage(1, msg)
if err != nil {
log.Printf("error: %v", err)
client.Close()
delete(clients_media,client)
}
}
}
}
func handleMessages_repo() {
for {
// Grab the next message from the broadcast channel
msg := <-broadcast_repo
// Send it out to every client that is currently connected
for client := range clients_repo {
err := client.WriteMessage(1, msg)
if err != nil {
log.Printf("error: %v", err)
client.Close()
delete(clients_repo,client)
}
}
}
}
func handleMessages_bases() {
for {
// Grab the next message from the broadcast channel
msg := <-broadcast_bases
// Send it out to every client that is currently connected
for client := range clients_bases {
err := client.WriteMessage(1, msg)
if err != nil {
log.Printf("error: %v", err)
client.Close()
delete(clients_bases,client)
}
}
}
}
func handleMessages_sources() {
for {
// Grab the next message from the broadcast channel
msg := <-broadcast_sources
// Send it out to every client that is currently connected
for client := range clients_sources {
err := client.WriteMessage(1, msg)
if err != nil {
log.Printf("error: %v", err)
client.Close()
delete(clients_sources,client)
}
}
}
}
func handleMessages_jail_marketplace() {
for {
// Grab the next message from the broadcast channel
msg := <-broadcast_jail_marketplace
// Send it out to every client that is currently connected
for client := range clients_jail_marketplace {
err := client.WriteMessage(1, msg)
if err != nil {
log.Printf("error: %v", err)
client.Close()
delete(clients_jail_marketplace,client)
}
}
}
}
func handleMessages_bhyve_marketplace() {
for {
// Grab the next message from the broadcast channel
msg := <-broadcast_bhyve_marketplace
// Send it out to every client that is currently connected
for client := range clients_bhyve_marketplace {
err := client.WriteMessage(1, msg)
if err != nil {
log.Printf("error: %v", err)
client.Close()
delete(clients_bhyve_marketplace,client)
}
}
}
}
func handleMessages_tasklog() {
for {
// Grab the next message from the broadcast channel
msg := <-broadcast_tasklog
// Send it out to every client that is currently connected
for client := range clients_tasklog {
err := client.WriteMessage(1, msg)
if err != nil {
log.Printf("error: %v", err)
client.Close()
delete(clients_tasklog,client)
}
}
}
}
func handleMessages_users() {
for {
// Grab the next message from the broadcast channel
msg := <-broadcast_users
// Send it out to every client that is currently connected
for client := range clients_users {
err := client.WriteMessage(1, msg)
if err != nil {
log.Printf("error: %v", err)
client.Close()
delete(clients_users,client)
}
}
}
}
func handleMessages_imported() {
for {
// Grab the next message from the broadcast channel
msg := <-broadcast_imported
// Send it out to every client that is currently connected
for client := range clients_imported {
err := client.WriteMessage(1, msg)
if err != nil {
log.Printf("error: %v", err)
client.Close()
delete(clients_imported,client)
}
}
}
}
func handleMessages_k8s() {
for {
// Grab the next message from the broadcast channel
msg := <-broadcast_k8s
// Send it out to every client that is currently connected
for client := range clients_k8s {
err := client.WriteMessage(1, msg)
if err != nil {
log.Printf("error: %v", err)
client.Close()
delete(clients_k8s,client)
}
}
}
}