Skip to content

Commit

Permalink
Add License
Browse files Browse the repository at this point in the history
  • Loading branch information
peltho committed Oct 18, 2022
1 parent f04bab8 commit 10de81b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 27 deletions.
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Oliver Kuederle

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
87 changes: 60 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ func shellout(command string) (error, string, string) {
}

type Tui struct {
app *tview.Application
form *tview.Form
table *tview.Table
menu *tview.Flex
help *tview.TextView
pages *tview.Pages
app *tview.Application
form *tview.Form
table *tview.Table
menu *tview.Flex
help *tview.TextView
secondHelp *tview.TextView
pages *tview.Pages
}

func CreateApplication() *Tui {
Expand All @@ -40,6 +41,7 @@ func (t *Tui) Init() {
t.form = tview.NewForm()
t.menu = tview.NewFlex()
t.help = tview.NewTextView()
t.secondHelp = tview.NewTextView()
t.pages = tview.NewPages()
}

Expand Down Expand Up @@ -106,13 +108,23 @@ func (t *Tui) CreateTable(rows []string) {
}
}).SetSelectedFunc(func(row int, column int) {
t.table.SetSelectable(false, false)
if row == 0 {
t.app.SetFocus(t.table)
return
}
t.CreateModal("Are you sure you want to remove this rule?",
func() {
shellout(fmt.Sprintf("ufw --force delete %d", row))
}, func() {
},
func() {
t.pages.HidePage("modal")
t.app.SetFocus(t.table)
},
func() {
t.pages.HidePage("modal")
t.app.SetFocus(t.table)
})
},
)
})
}

Expand All @@ -122,22 +134,20 @@ func (t *Tui) ReloadTable() {
t.CreateTable(data)
}

func (t *Tui) CreateModal(text string, action func(), finally func()) {
func (t *Tui) CreateModal(text string, confirm func(), cancel func(), finally func()) {
modal := tview.NewModal()
t.pages.AddPage("modal", modal.SetText(text).AddButtons([]string{"Confirm", "Cancel"}).SetDoneFunc(func(i int, label string) {
if label == "Confirm" {
action()
confirm()
t.ReloadTable()
} else {
cancel()
}
modal.ClearButtons()
finally()
}), true, true)
}

func (t *Tui) CreateHelp(text string) {
t.help.SetText(text).SetBorderPadding(1, 0, 1, 0)
}

func (t *Tui) CreateMenu() {
menuList := tview.NewList()
menuList.
Expand All @@ -147,49 +157,67 @@ func (t *Tui) CreateMenu() {
}).
AddItem("Remove a rule", "", 'd', func() {
t.app.SetFocus(t.table)
t.CreateHelp("Press <Esc> to go back to the menu selection")
t.help.SetText("Press <Esc> to go back to the menu selection").SetBorderPadding(1, 0, 1, 0)
}).
AddItem("Disable ufw", "", 's', func() {
t.CreateModal("Are you sure you want to disable ufw?",
func() {
shellout("ufw --force disable")
t.app.Stop()
},
func() {
t.app.Stop()
t.pages.RemovePage("modal")
t.app.SetFocus(t.menu)
},
func() {
t.app.SetFocus(t.menu)
},
)
}).
AddItem("Reset rules", "", 'r', func() {
t.CreateModal("Are you sure you want to reset all rules?",
func() {
shellout("ufw --force reset")
t.app.Stop()
},
func() {
t.app.Stop()
t.app.SetFocus(t.menu)
},
func() {
t.pages.RemovePage("modal")
t.app.SetFocus(t.menu)
},
)
}).
AddItem("Exit", "", 'q', func() { t.app.Stop() })
menuList.SetBorderPadding(1, 0, 1, 1)
menuList.SetShortcutColor(tcell.ColorDarkCyan).SetBorderPadding(1, 0, 1, 1)
t.menu.AddItem(menuList, 0, 1, true)
t.menu.SetBorder(true).SetTitle(" Menu ")
}

func (t *Tui) CreateForm() {
t.CreateHelp("Use <Tab> and <Enter> keys to navigate through the form")
t.form.AddInputField("To", "", 20, nil, nil).
t.help.SetText("Use <Tab> and <Enter> keys to navigate through the form").SetBorderPadding(1, 0, 1, 1)

t.form.AddInputField("To", "", 20, nil, nil).SetFieldTextColor(tcell.ColorWhite).
AddDropDown("Protocol", []string{"tcp", "udp"}, 0, nil).
AddDropDown("Action", []string{"ALLOW", "DENY", "REJECT", "LIMIT"}, 0, nil).
AddInputField("From", "", 20, nil, nil).
AddInputField("From *", "", 20, nil, nil).
AddInputField("Comment", "", 40, nil, nil).
AddButton("Save", t.CreateRule).
AddButton("Cancel", t.Cancel)
AddButton("Cancel", t.Cancel).
SetButtonTextColor(tcell.ColorWhite).
SetButtonBackgroundColor(tcell.ColorDarkCyan).
SetFieldBackgroundColor(tcell.ColorDarkCyan).
SetLabelColor(tcell.ColorWhite)

t.secondHelp.SetText("* Leave empty for Anywhere").SetTextColor(tcell.ColorDarkCyan).SetBorderPadding(1, 0, 1, 1)
}

func (t *Tui) Reset() {
t.pages.HidePage("form")
t.form.Clear(true)
t.help.Clear()
t.secondHelp.Clear()
t.app.SetFocus(t.menu)
}

Expand Down Expand Up @@ -222,19 +250,20 @@ func (t *Tui) CreateLayout() *tview.Pages {

base := tview.NewFlex().AddItem(
columns.
AddItem(t.menu, 0, 1, true).
AddItem(t.menu, 0, 2, true).
AddItem(t.table, 0, 4, false),
0, 1, true,
)

form := columns.AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(t.help, 0, 1, false).
AddItem(t.form, 0, 8, false),
AddItem(t.help, 0, 2, false).
AddItem(t.form, 0, 8, false).
AddItem(t.secondHelp, 0, 2, false),
0, 3, false,
)

t.pages.AddAndSwitchToPage("base", base, true)
t.pages.AddPage("form", form, false, false)
t.pages.AddPage("form", form, true, false)
return t.pages
}

Expand All @@ -254,11 +283,15 @@ func main() {
func() {
shellout("ufw --force enable")
},
func() {
tui.app.Stop()
},
func() {
tui.pages.HidePage("modal")
tui.pages.ShowPage("base")
tui.app.SetFocus(tui.menu)
})
},
)
}

tui.CreateTable(data)
Expand Down

0 comments on commit 10de81b

Please sign in to comment.