6
6
"log"
7
7
"os/exec"
8
8
"regexp"
9
+ "strconv"
9
10
"strings"
10
11
11
12
"github.com/gdamore/tcell/v2"
@@ -47,11 +48,11 @@ func (t *Tui) Init() {
47
48
}
48
49
49
50
func (t * Tui ) LoadTableData () ([]string , error ) {
50
- err , out , _ := shellout ("ufw status | sed '/^$/d' | awk '{$2=$2};1' | tail -n +4 | sed -r 's/^(([0-9]{1,3}\\ .){3}[0-9])\\ s(.*)(\\ /[a-z]{3})/\\ 1\\ 4 \\ 3/;s/^(([0-9]*)\\ /([a-z]{3}))/\\ 3 \\ 2/;s/(\\ w)\\ s(\\ (v6\\ ))$/\\ 1\\ 2/;s/\\ (v6\\ )//'" )
51
+ err , out , _ := shellout ("ufw status | sed '/^$/d' | awk '{$2=$2};1' | tail -n +4 | sed -r 's/^(([0-9]{1,3}\\ .){3}[0-9]{1,3})\\ s(.*)(\\ /[a-z]{3})/\\ 1\\ 4 \\ 3/;s/^(([0-9]*)\\ /([a-z]{3}))/\\ 3 \\ 2/;s/(\\ w)\\ s(\\ (v6\\ ))$/\\ 1\\ 2/;s/\\ (v6\\ )//'" )
52
+
51
53
if err != nil {
52
54
log .Printf ("error: %v\n " , err )
53
55
}
54
-
55
56
rows := strings .Split (out , "\n " )
56
57
57
58
return rows , nil
@@ -78,18 +79,14 @@ func (t *Tui) CreateTable(rows []string) {
78
79
cols := strings .Fields (row )
79
80
80
81
value := ""
81
- if len (cols ) < len (columns ) && c >= len (cols ) {
82
- t .table .SetCell (r + 1 , c + 1 , tview .NewTableCell (value ).SetTextColor (tcell .ColorWhite ).SetAlign (tview .AlignCenter ).SetExpansion (1 ))
83
- } else {
84
- // Conditional statement for displaying Comments if any
85
- if c >= 4 {
86
- value = strings .Join (cols [c :], " " )
87
- } else {
88
- value = cols [c ]
89
- }
90
-
91
- t .table .SetCell (r + 1 , c + 1 , tview .NewTableCell (value ).SetTextColor (tcell .ColorWhite ).SetAlign (tview .AlignCenter ).SetExpansion (1 ))
82
+ switch {
83
+ case c >= 4 :
84
+ value = strings .Join (cols [c :], " " )
85
+ default :
86
+ value = cols [c ]
92
87
}
88
+
89
+ t .table .SetCell (r + 1 , c + 1 , tview .NewTableCell (value ).SetTextColor (tcell .ColorWhite ).SetAlign (tview .AlignCenter ).SetExpansion (1 ))
93
90
}
94
91
}
95
92
@@ -203,15 +200,25 @@ func (t *Tui) EditForm() {
203
200
t .help .SetText ("Use <Tab> and <Enter> keys to navigate through the form" ).SetBorderPadding (1 , 0 , 1 , 1 )
204
201
205
202
to := t .table .GetCell (row , 1 ).Text
206
- re := regexp .MustCompile (`((([0-9]{1,3}\.){3}[0-9]{1,3})/)?([a-z]{3})` )
207
- match := re .FindStringSubmatch (to )
203
+ rip := regexp .MustCompile (`(([0-9]{1,3}\.){3}[0-9]{1,3})(/[0-9]{1,2})?` )
204
+ rproto := regexp .MustCompile (`/?([a-z]{3})` )
205
+ matchIP := rip .FindStringSubmatch (to )
206
+ matchProto := rproto .FindStringSubmatch (to )
208
207
209
208
toValue := ""
210
- proto := match [0 ]
211
- if match [4 ] != "" {
212
- toValue = match [2 ]
213
- proto = match [4 ]
209
+ proto := ""
210
+ if len (matchIP ) > 0 {
211
+ toValue = matchIP [0 ]
214
212
}
213
+ if len (matchProto ) > 1 {
214
+ proto = matchProto [1 ]
215
+ }
216
+
217
+ portValue := ""
218
+ port := t .table .GetCell (row , 2 ).Text
219
+ rport := regexp .MustCompile (`([0-9]*)(/[a-z]{3})?` )
220
+ matchPort := rport .FindStringSubmatch (port )
221
+ portValue = matchPort [1 ]
215
222
216
223
protocolOptionIndex := 1
217
224
if proto == "tcp" {
@@ -238,13 +245,13 @@ func (t *Tui) EditForm() {
238
245
comment := strings .ReplaceAll (t .table .GetCell (row , 5 ).Text , "# " , "" )
239
246
240
247
t .form .AddInputField ("To" , toValue , 20 , nil , nil ).SetFieldTextColor (tcell .ColorWhite ).
241
- AddInputField ("Port *" , t . table . GetCell ( row , 2 ). Text , 20 , nil , nil ).SetFieldTextColor (tcell .ColorWhite ).
248
+ AddInputField ("Port *" , portValue , 20 , validatePort , nil ).SetFieldTextColor (tcell .ColorWhite ).
242
249
AddDropDown ("Protocol *" , []string {"tcp" , "udp" }, protocolOptionIndex , nil ).
243
250
AddDropDown ("Action *" , []string {"ALLOW" , "DENY" , "REJECT" , "LIMIT" }, actionOptionIndex , nil ).
244
251
AddInputField ("From" , fromValue , 20 , nil , nil ).
245
252
AddInputField ("Comment" , comment , 40 , nil , nil ).
246
253
AddButton ("Save" , func () { t .CreateRule (row ); t .table .SetSelectable (false , false ) }).
247
- AddButton ("Cancel" , func () { t .Cancel (); t .table .SetSelectable (false , false ) }).
254
+ AddButton ("Cancel" , func () { t .CreateRule ( row ); t . Reset (); t .table .SetSelectable (false , false ) }).
248
255
SetButtonTextColor (tcell .ColorWhite ).
249
256
SetButtonBackgroundColor (tcell .ColorDarkCyan ).
250
257
SetFieldBackgroundColor (tcell .ColorDarkCyan ).
@@ -316,13 +323,13 @@ func (t *Tui) CreateForm() {
316
323
t .help .SetText ("Use <Tab> and <Enter> keys to navigate through the form" ).SetBorderPadding (1 , 0 , 1 , 1 )
317
324
318
325
t .form .AddInputField ("To" , "" , 20 , nil , nil ).SetFieldTextColor (tcell .ColorWhite ).
319
- AddInputField ("Port *" , "" , 20 , nil , nil ).SetFieldTextColor (tcell .ColorWhite ).
326
+ AddInputField ("Port *" , "" , 20 , validatePort , nil ).SetFieldTextColor (tcell .ColorWhite ).
320
327
AddDropDown ("Protocol *" , []string {"tcp" , "udp" }, 0 , nil ).
321
328
AddDropDown ("Action *" , []string {"ALLOW" , "DENY" , "REJECT" , "LIMIT" }, 0 , nil ).
322
329
AddInputField ("From" , "" , 20 , nil , nil ).
323
330
AddInputField ("Comment" , "" , 40 , nil , nil ).
324
331
AddButton ("Save" , func () { t .CreateRule () }).
325
- AddButton ("Cancel" , t .Cancel ).
332
+ AddButton ("Cancel" , t .Reset ).
326
333
SetButtonTextColor (tcell .ColorWhite ).
327
334
SetButtonBackgroundColor (tcell .ColorDarkCyan ).
328
335
SetFieldBackgroundColor (tcell .ColorDarkCyan ).
@@ -331,6 +338,11 @@ func (t *Tui) CreateForm() {
331
338
t .secondHelp .SetText ("* Mandatory field\n \n To and From fields match any and Anywhere if left empty" ).SetTextColor (tcell .ColorDarkCyan ).SetBorderPadding (0 , 0 , 1 , 1 )
332
339
}
333
340
341
+ func validatePort (text string , ch rune ) bool {
342
+ _ , err := strconv .Atoi (text )
343
+ return err == nil
344
+ }
345
+
334
346
func (t * Tui ) Reset () {
335
347
t .pages .HidePage ("form" )
336
348
t .form .Clear (true )
@@ -339,10 +351,6 @@ func (t *Tui) Reset() {
339
351
t .app .SetFocus (t .menu )
340
352
}
341
353
342
- func (t * Tui ) Cancel () {
343
- t .Reset ()
344
- }
345
-
346
354
func (t * Tui ) CreateLayout () * tview.Pages {
347
355
columns := tview .NewFlex ().SetDirection (tview .FlexColumn )
348
356
@@ -377,7 +385,7 @@ func main() {
377
385
378
386
if len (data ) <= 1 {
379
387
tui .pages .HidePage ("base" )
380
- tui .CreateModal ("ufw is disabled. Do you want to enable it?" ,
388
+ tui .CreateModal ("ufw is disabled.\n Do you want to enable it?" ,
381
389
func () {
382
390
shellout ("ufw --force enable" )
383
391
},
0 commit comments