@@ -13,6 +13,8 @@ import (
13
13
"github.com/rivo/tview"
14
14
)
15
15
16
+ var NUMBER_OF_V6_RULES = 0
17
+
16
18
func shellout (command string ) (error , string , string ) {
17
19
var stdout bytes.Buffer
18
20
var stderr bytes.Buffer
@@ -61,6 +63,10 @@ func (t *Tui) LoadInterfaces() ([]string, error) {
61
63
func (t * Tui ) LoadTableData () ([]string , error ) {
62
64
err , out , _ := shellout ("ufw status numbered | sed '/^$/d' | awk '{$2=$2};1' | tail -n +4 | sed -r 's/(\\ [(\\ s)([0-9]+)\\ ])/\\ [\\ 3\\ ] /;s/(\\ [([0-9]+)\\ ])/\\ [\\ 2\\ ] /;s/\\ (out\\ )//;s/(\\ w)\\ s(\\ (v6\\ ))/\\ 1/;s/([A-Z]{2,})\\ s([A-Z]{2,3})/\\ 1-\\ 2/;s/^(.*)\\ s([A-Z]{2,}(-[A-Z]{2,3})?)\\ s(.*)\\ s(on)\\ s(.*)\\ s(#.*)?/\\ 1_\\ 5_\\ 6 - \\ 2 \\ 4 \\ 7/;s/([A-Z][a-z]+\\ /[a-z]{3})\\ s(([A-Z]+).*)/\\ 1 - \\ 2/;s/(\\ ]\\ s+)([0-9]{2,})\\ s([A-Z]{2,}(-[A-Z]{2,3})?)/\\ 1Anywhere \\ 2 \\ 3/;s/(\\ ]\\ s+)(([0-9]{1,3}\\ .){3}[0-9]{1,3}(\\ /[0-9]{1,2})?)\\ s([A-Z]{2,}-[A-Z]{2,3})/\\ 1\\ 2 - \\ 5/;s/([A-Z][a-z]+)\\ s(([A-Z]+).*)/\\ 1 - \\ 2/;s/(\\ ]\\ s+)(.*)\\ s([0-9]+)(\\ /[a-z]{3})/\\ 1\\ 2\\ 4 \\ 3/;s/(\\ ]\\ s+)\\ /([a-z]{3})\\ s/\\ 1\\ 2 /;s/^(.*)\\ s(on)\\ s(.*)\\ s([A-Z]{2,}(-[A-Z]{2,3})?)\\ s(.*)/\\ 1_\\ 2_\\ 3 - \\ 4 \\ 6/'" )
63
65
66
+ r := regexp .MustCompile (`\(v6\)` )
67
+ matches := r .FindAllStringSubmatch (out , - 1 )
68
+ NUMBER_OF_V6_RULES = len (matches )
69
+
64
70
if err != nil {
65
71
log .Printf ("error: %v\n " , err )
66
72
}
@@ -150,7 +156,10 @@ func (t *Tui) CreateForm() {
150
156
AddInputField ("From" , "" , 20 , nil , nil ).
151
157
AddInputField ("Comment" , "" , 40 , nil , nil ).
152
158
AddButton ("Save" , func () { t .CreateRule () }).
153
- AddButton ("Cancel" , t .Reset ).
159
+ AddButton ("Cancel" , func () {
160
+ t .Reset ()
161
+ t .app .SetFocus (t .menu )
162
+ }).
154
163
SetButtonTextColor (tcell .ColorWhite ).
155
164
SetButtonBackgroundColor (tcell .ColorDarkCyan ).
156
165
SetFieldBackgroundColor (tcell .ColorDarkCyan ).
@@ -276,20 +285,21 @@ func (t *Tui) EditForm() {
276
285
AddInputField ("Comment" , comment , 40 , nil , nil ).
277
286
AddButton ("Save" , func () {
278
287
t .CreateRule (row )
279
- t .table . SetSelectable ( false , false )
288
+ t .app . SetFocus ( t . table )
280
289
}).
281
290
AddButton ("Cancel" , func () {
282
291
t .Reset ()
283
- t .table .SetSelectable (false , false )
292
+ t .help .SetText ("Press <Esc> to go back to the menu selection" ).SetBorderPadding (1 , 0 , 1 , 0 )
293
+ t .app .SetFocus (t .table )
284
294
}).
285
295
SetButtonTextColor (tcell .ColorWhite ).
286
296
SetButtonBackgroundColor (tcell .ColorDarkCyan ).
287
297
SetFieldBackgroundColor (tcell .ColorDarkCyan ).
288
298
SetLabelColor (tcell .ColorWhite )
289
299
290
- /* t.secondHelp.SetText("* Mandatory field\n\nPort, To and From fields respectively match any and Anywhere if left empty").
291
- SetTextColor(tcell.ColorDarkCyan).
292
- SetBorderPadding(0, 0, 1, 1)*/
300
+ t .secondHelp .SetText ("* Mandatory field\n \n Port, To and From fields respectively match any and Anywhere if left empty" ).
301
+ SetTextColor (tcell .ColorDarkCyan ).
302
+ SetBorderPadding (0 , 0 , 1 , 1 )
293
303
294
304
t .app .SetFocus (t .form )
295
305
})
@@ -306,20 +316,17 @@ func (t *Tui) CreateRule(position ...int) {
306
316
307
317
dryCmd := "ufw --dry-run "
308
318
baseCmd := "ufw "
309
- if len (position ) > 0 && position [0 ] < t .table .GetRowCount ()- 1 {
319
+
320
+ if len (position ) > 0 && position [0 ] < t .table .GetRowCount ()- NUMBER_OF_V6_RULES - 1 {
310
321
dryCmd = fmt .Sprintf ("ufw --dry-run insert %d " , position [0 ])
311
322
baseCmd = fmt .Sprintf ("ufw insert %d " , position [0 ])
312
323
}
313
324
314
- if port != "" && ninterface != "" {
325
+ if ninterface != "" && ( port != "" || proto != "" ) {
315
326
return
316
327
}
317
328
318
- if port == "" && proto == "" && ninterface == "" {
319
- return
320
- }
321
-
322
- if ninterface != "" && proto != "" {
329
+ if port == "" && proto == "" && ninterface == "" && to == "" && from == "" {
323
330
return
324
331
}
325
332
@@ -355,7 +362,6 @@ func (t *Tui) CreateRule(position ...int) {
355
362
}
356
363
357
364
// Dry-run
358
- //fmt.Println(dryCmd + preCmd + cmd) // debugging
359
365
err , _ , _ := shellout (dryCmd + preCmd + cmd )
360
366
if err == nil {
361
367
// Delete first
@@ -410,6 +416,7 @@ func (t *Tui) CreateMenu() {
410
416
AddItem ("Edit a rule" , "" , 'e' , func () {
411
417
t .EditForm ()
412
418
t .app .SetFocus (t .table )
419
+ t .help .SetText ("Press <Esc> to go back to the menu selection" ).SetBorderPadding (1 , 0 , 1 , 0 )
413
420
}).
414
421
AddItem ("Delete a rule" , "" , 'd' , func () {
415
422
t .RemoveRule ()
@@ -457,7 +464,6 @@ func (t *Tui) Reset() {
457
464
t .form .Clear (true )
458
465
t .help .Clear ()
459
466
t .secondHelp .Clear ()
460
- t .app .SetFocus (t .menu )
461
467
}
462
468
463
469
func (t * Tui ) CreateLayout () * tview.Pages {
@@ -483,6 +489,14 @@ func (t *Tui) CreateLayout() *tview.Pages {
483
489
}
484
490
485
491
func main () {
492
+
493
+ cmd := exec .Command ("id" , "-u" )
494
+ output , err := cmd .Output ()
495
+ i , err := strconv .Atoi (string (output [:len (output )- 1 ]))
496
+ if i != 0 {
497
+ log .Fatal ("This program must be run as root! (sudo)" )
498
+ }
499
+
486
500
tui := CreateApplication ()
487
501
tui .Init ()
488
502
data , err := tui .LoadTableData ()
0 commit comments