Skip to content

Commit 23cc741

Browse files
committed
Check for sudo + cosmetic
1 parent c0b7cab commit 23cc741

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

main.go

+29-15
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"github.com/rivo/tview"
1414
)
1515

16+
var NUMBER_OF_V6_RULES = 0
17+
1618
func shellout(command string) (error, string, string) {
1719
var stdout bytes.Buffer
1820
var stderr bytes.Buffer
@@ -61,6 +63,10 @@ func (t *Tui) LoadInterfaces() ([]string, error) {
6163
func (t *Tui) LoadTableData() ([]string, error) {
6264
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/'")
6365

66+
r := regexp.MustCompile(`\(v6\)`)
67+
matches := r.FindAllStringSubmatch(out, -1)
68+
NUMBER_OF_V6_RULES = len(matches)
69+
6470
if err != nil {
6571
log.Printf("error: %v\n", err)
6672
}
@@ -150,7 +156,10 @@ func (t *Tui) CreateForm() {
150156
AddInputField("From", "", 20, nil, nil).
151157
AddInputField("Comment", "", 40, nil, nil).
152158
AddButton("Save", func() { t.CreateRule() }).
153-
AddButton("Cancel", t.Reset).
159+
AddButton("Cancel", func() {
160+
t.Reset()
161+
t.app.SetFocus(t.menu)
162+
}).
154163
SetButtonTextColor(tcell.ColorWhite).
155164
SetButtonBackgroundColor(tcell.ColorDarkCyan).
156165
SetFieldBackgroundColor(tcell.ColorDarkCyan).
@@ -276,20 +285,21 @@ func (t *Tui) EditForm() {
276285
AddInputField("Comment", comment, 40, nil, nil).
277286
AddButton("Save", func() {
278287
t.CreateRule(row)
279-
t.table.SetSelectable(false, false)
288+
t.app.SetFocus(t.table)
280289
}).
281290
AddButton("Cancel", func() {
282291
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)
284294
}).
285295
SetButtonTextColor(tcell.ColorWhite).
286296
SetButtonBackgroundColor(tcell.ColorDarkCyan).
287297
SetFieldBackgroundColor(tcell.ColorDarkCyan).
288298
SetLabelColor(tcell.ColorWhite)
289299

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\nPort, To and From fields respectively match any and Anywhere if left empty").
301+
SetTextColor(tcell.ColorDarkCyan).
302+
SetBorderPadding(0, 0, 1, 1)
293303

294304
t.app.SetFocus(t.form)
295305
})
@@ -306,20 +316,17 @@ func (t *Tui) CreateRule(position ...int) {
306316

307317
dryCmd := "ufw --dry-run "
308318
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 {
310321
dryCmd = fmt.Sprintf("ufw --dry-run insert %d ", position[0])
311322
baseCmd = fmt.Sprintf("ufw insert %d ", position[0])
312323
}
313324

314-
if port != "" && ninterface != "" {
325+
if ninterface != "" && (port != "" || proto != "") {
315326
return
316327
}
317328

318-
if port == "" && proto == "" && ninterface == "" {
319-
return
320-
}
321-
322-
if ninterface != "" && proto != "" {
329+
if port == "" && proto == "" && ninterface == "" && to == "" && from == "" {
323330
return
324331
}
325332

@@ -355,7 +362,6 @@ func (t *Tui) CreateRule(position ...int) {
355362
}
356363

357364
// Dry-run
358-
//fmt.Println(dryCmd + preCmd + cmd) // debugging
359365
err, _, _ := shellout(dryCmd + preCmd + cmd)
360366
if err == nil {
361367
// Delete first
@@ -410,6 +416,7 @@ func (t *Tui) CreateMenu() {
410416
AddItem("Edit a rule", "", 'e', func() {
411417
t.EditForm()
412418
t.app.SetFocus(t.table)
419+
t.help.SetText("Press <Esc> to go back to the menu selection").SetBorderPadding(1, 0, 1, 0)
413420
}).
414421
AddItem("Delete a rule", "", 'd', func() {
415422
t.RemoveRule()
@@ -457,7 +464,6 @@ func (t *Tui) Reset() {
457464
t.form.Clear(true)
458465
t.help.Clear()
459466
t.secondHelp.Clear()
460-
t.app.SetFocus(t.menu)
461467
}
462468

463469
func (t *Tui) CreateLayout() *tview.Pages {
@@ -483,6 +489,14 @@ func (t *Tui) CreateLayout() *tview.Pages {
483489
}
484490

485491
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+
486500
tui := CreateApplication()
487501
tui.Init()
488502
data, err := tui.LoadTableData()

0 commit comments

Comments
 (0)