mirror of
https://github.com/anotherhadi/usbguard-tui.git
synced 2026-05-11 22:02:34 +02:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ecd12f18e0 | |||
| 787d4ac0f1 | |||
| 8c250389b3 | |||
| b19739b0a6 | |||
| 20f9b7cf89 | |||
| abe6b5dde5 | |||
| c7f42c1a12 | |||
| 3731160024 | |||
| 62cba43e15 | |||
| f1dd37cfc6 |
Binary file not shown.
|
After Width: | Height: | Size: 295 KiB |
@@ -0,0 +1,27 @@
|
|||||||
|
Output ./assets/demo.gif
|
||||||
|
Require usbguard-tui
|
||||||
|
|
||||||
|
Set Shell "zsh"
|
||||||
|
Set FontSize 32
|
||||||
|
Set Width 1500
|
||||||
|
Set Height 1000
|
||||||
|
|
||||||
|
Type "usbguard-tui"
|
||||||
|
Sleep 500ms
|
||||||
|
Enter
|
||||||
|
Sleep 1s
|
||||||
|
|
||||||
|
Down
|
||||||
|
Sleep 200ms
|
||||||
|
Down
|
||||||
|
Sleep 200ms
|
||||||
|
Enter
|
||||||
|
Sleep 1s
|
||||||
|
|
||||||
|
Down Sleep 200ms
|
||||||
|
Down Sleep 200ms
|
||||||
|
Sleep 1s
|
||||||
|
Enter
|
||||||
|
Sleep 1s
|
||||||
|
|
||||||
|
Type "Q"
|
||||||
@@ -15,7 +15,18 @@ A terminal UI for managing USB devices via [usbguard](https://usbguard.github.io
|
|||||||
|
|
||||||
USBGuard is a software framework for implementing a USB device authorization policy (allowlisting/blocklisting). It protects your system against rogue USB devices by scanning them and checking their parameters against a set of rules.
|
USBGuard is a software framework for implementing a USB device authorization policy (allowlisting/blocklisting). It protects your system against rogue USB devices by scanning them and checking their parameters against a set of rules.
|
||||||
|
|
||||||
Built with [bubbletea](https://github.com/charmbracelet/bubbletea) & Goland!
|
<img alt="USBGuard-tui demo" src="./.github/assets/demo.gif" width="600" />
|
||||||
|
|
||||||
|
Built with [bubbletea](https://github.com/charmbracelet/bubbletea) & Golang!
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- List all connected USB devices with their current status (allowed, blocked, rejected)
|
||||||
|
- Allow, block, or reject devices: temporarily or permanently
|
||||||
|
- Action popup with mouse support for quick device management
|
||||||
|
- Filter devices by name with `/`
|
||||||
|
- Auto-refresh
|
||||||
|
- Keyboard shortcuts for all actions (`a`/`A`, `b`/`B`, `e`/`E`)
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
|
|||||||
@@ -14,12 +14,12 @@
|
|||||||
(system: f system (import nixpkgs {inherit system;}));
|
(system: f system (import nixpkgs {inherit system;}));
|
||||||
|
|
||||||
pname = "usbguard-tui";
|
pname = "usbguard-tui";
|
||||||
version = "1.0.0";
|
version = "1.0.1";
|
||||||
|
|
||||||
ldflags = ["-s" "-w" "-X main.version=${version}"];
|
ldflags = ["-s" "-w" "-X main.version=${version}"];
|
||||||
in {
|
in {
|
||||||
packages = forAllSystems (system: pkgs: {
|
packages = forAllSystems (system: pkgs: let
|
||||||
"${pname}" = pkgs.buildGoModule {
|
pkg = pkgs.buildGoModule {
|
||||||
inherit pname version ldflags;
|
inherit pname version ldflags;
|
||||||
|
|
||||||
src = ./.;
|
src = ./.;
|
||||||
@@ -33,9 +33,9 @@
|
|||||||
platforms = platforms.unix;
|
platforms = platforms.unix;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
in {
|
||||||
|
"${pname}" = pkg;
|
||||||
|
default = pkg;
|
||||||
});
|
});
|
||||||
|
|
||||||
defaultPackage =
|
|
||||||
forAllSystems (system: pkgs: self.packages.${system}.${pname});
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package guard
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -86,6 +87,42 @@ func wrapExecError(err error) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsRulesManaged() bool {
|
||||||
|
out, err := exec.Command("systemctl", "cat", "usbguard").Output()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
configPath := extractConfigPath(string(out))
|
||||||
|
if configPath == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
ruleFile := parseRuleFilePath(configPath)
|
||||||
|
return strings.HasPrefix(ruleFile, "/nix/store/")
|
||||||
|
}
|
||||||
|
|
||||||
|
func extractConfigPath(s string) string {
|
||||||
|
fields := strings.Fields(s)
|
||||||
|
for i, f := range fields {
|
||||||
|
if f == "-c" && i+1 < len(fields) {
|
||||||
|
return fields[i+1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseRuleFilePath(configPath string) string {
|
||||||
|
data, err := os.ReadFile(configPath)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
for _, line := range strings.Split(string(data), "\n") {
|
||||||
|
if after, ok := strings.CutPrefix(line, "RuleFile="); ok {
|
||||||
|
return strings.TrimSpace(after)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func classifyError(output string) error {
|
func classifyError(output string) error {
|
||||||
lower := strings.ToLower(output)
|
lower := strings.ToLower(output)
|
||||||
switch {
|
switch {
|
||||||
|
|||||||
@@ -5,5 +5,5 @@ import "errors"
|
|||||||
var (
|
var (
|
||||||
ErrNotFound = errors.New("usbguard not found in PATH")
|
ErrNotFound = errors.New("usbguard not found in PATH")
|
||||||
ErrPermission = errors.New("insufficient permissions to manage devices")
|
ErrPermission = errors.New("insufficient permissions to manage devices")
|
||||||
ErrReadOnly = errors.New("rules file is read-only")
|
ErrReadOnly = errors.New("rules file is not writable")
|
||||||
)
|
)
|
||||||
|
|||||||
+4
-14
@@ -2,7 +2,6 @@ package ui
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"image/color"
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"charm.land/bubbles/v2/list"
|
"charm.land/bubbles/v2/list"
|
||||||
@@ -11,7 +10,6 @@ import (
|
|||||||
"github.com/anotherhadi/usbguard-tui/internal/guard"
|
"github.com/anotherhadi/usbguard-tui/internal/guard"
|
||||||
)
|
)
|
||||||
|
|
||||||
// deviceDelegate renders device list items with status colors.
|
|
||||||
type deviceDelegate struct{}
|
type deviceDelegate struct{}
|
||||||
|
|
||||||
func (d deviceDelegate) Height() int { return 2 }
|
func (d deviceDelegate) Height() int { return 2 }
|
||||||
@@ -26,20 +24,14 @@ func (d deviceDelegate) Render(w io.Writer, m list.Model, index int, item list.I
|
|||||||
|
|
||||||
selected := index == m.Index()
|
selected := index == m.Index()
|
||||||
|
|
||||||
var clr color.Color
|
colorMap := statusColors
|
||||||
if selected {
|
if selected {
|
||||||
var ok bool
|
colorMap = statusColorsSelected
|
||||||
clr, ok = statusColorsSelected[dev.Status]
|
}
|
||||||
|
clr, ok := colorMap[dev.Status]
|
||||||
if !ok {
|
if !ok {
|
||||||
clr = colorMuted
|
clr = colorMuted
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
var ok bool
|
|
||||||
clr, ok = statusColors[dev.Status]
|
|
||||||
if !ok {
|
|
||||||
clr = colorMuted
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var nameStyle, descStyle lipgloss.Style
|
var nameStyle, descStyle lipgloss.Style
|
||||||
if selected {
|
if selected {
|
||||||
@@ -69,7 +61,6 @@ func (d deviceDelegate) Render(w io.Writer, m list.Model, index int, item list.I
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// actionItem represents a device policy action in the select popup.
|
|
||||||
type actionItem struct {
|
type actionItem struct {
|
||||||
label string
|
label string
|
||||||
fn func(int, bool) error
|
fn func(int, bool) error
|
||||||
@@ -81,7 +72,6 @@ func (a actionItem) Title() string { return a.label }
|
|||||||
func (a actionItem) Description() string { return "" }
|
func (a actionItem) Description() string { return "" }
|
||||||
func (a actionItem) FilterValue() string { return a.label }
|
func (a actionItem) FilterValue() string { return a.label }
|
||||||
|
|
||||||
// actionDelegate renders single-line action items.
|
|
||||||
type actionDelegate struct{}
|
type actionDelegate struct{}
|
||||||
|
|
||||||
func (d actionDelegate) Height() int { return 1 }
|
func (d actionDelegate) Height() int { return 1 }
|
||||||
|
|||||||
+92
-25
@@ -37,6 +37,7 @@ type Model struct {
|
|||||||
height int
|
height int
|
||||||
notice string
|
notice string
|
||||||
selectedDev *guard.Device
|
selectedDev *guard.Device
|
||||||
|
rulesManaged bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() Model {
|
func New() Model {
|
||||||
@@ -58,16 +59,35 @@ func New() Model {
|
|||||||
h := help.New()
|
h := help.New()
|
||||||
h.Styles = help.DefaultStyles(true)
|
h.Styles = help.DefaultStyles(true)
|
||||||
|
|
||||||
|
rulesManaged := guard.IsRulesManaged()
|
||||||
|
notice := ""
|
||||||
|
if rulesManaged {
|
||||||
|
notice = "Rules managed by NixOS config: permanent actions not available."
|
||||||
|
listKeys.AllowPerm.SetEnabled(false)
|
||||||
|
listKeys.BlockPerm.SetEnabled(false)
|
||||||
|
listKeys.RejectPerm.SetEnabled(false)
|
||||||
|
}
|
||||||
|
|
||||||
return Model{
|
return Model{
|
||||||
state: stateList,
|
state: stateList,
|
||||||
list: l,
|
list: l,
|
||||||
actionList: makeActionList(),
|
actionList: makeActionList(rulesManaged),
|
||||||
help: h,
|
help: h,
|
||||||
|
rulesManaged: rulesManaged,
|
||||||
|
notice: notice,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeActionList() list.Model {
|
func makeActionList(rulesManaged bool) list.Model {
|
||||||
items := []list.Item{
|
var items []list.Item
|
||||||
|
if rulesManaged {
|
||||||
|
items = []list.Item{
|
||||||
|
actionItem{"allow", guard.AllowDevice, false, guard.Allowed},
|
||||||
|
actionItem{"block", guard.BlockDevice, false, guard.Blocked},
|
||||||
|
actionItem{"reject", guard.RejectDevice, false, guard.Rejected},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
items = []list.Item{
|
||||||
actionItem{"allow", guard.AllowDevice, false, guard.Allowed},
|
actionItem{"allow", guard.AllowDevice, false, guard.Allowed},
|
||||||
actionItem{"allow (permanent)", guard.AllowDevice, true, guard.Allowed},
|
actionItem{"allow (permanent)", guard.AllowDevice, true, guard.Allowed},
|
||||||
actionItem{"block", guard.BlockDevice, false, guard.Blocked},
|
actionItem{"block", guard.BlockDevice, false, guard.Blocked},
|
||||||
@@ -75,7 +95,8 @@ func makeActionList() list.Model {
|
|||||||
actionItem{"reject", guard.RejectDevice, false, guard.Rejected},
|
actionItem{"reject", guard.RejectDevice, false, guard.Rejected},
|
||||||
actionItem{"reject (permanent)", guard.RejectDevice, true, guard.Rejected},
|
actionItem{"reject (permanent)", guard.RejectDevice, true, guard.Rejected},
|
||||||
}
|
}
|
||||||
l := list.New(items, actionDelegate{}, 24, 6)
|
}
|
||||||
|
l := list.New(items, actionDelegate{}, 24, len(items))
|
||||||
l.SetShowHelp(false)
|
l.SetShowHelp(false)
|
||||||
l.SetShowTitle(false)
|
l.SetShowTitle(false)
|
||||||
l.SetShowStatusBar(false)
|
l.SetShowStatusBar(false)
|
||||||
@@ -120,14 +141,14 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
if msg.err != nil {
|
if msg.err != nil {
|
||||||
switch msg.err {
|
switch msg.err {
|
||||||
case guard.ErrReadOnly:
|
case guard.ErrReadOnly:
|
||||||
m.notice = "Read-only rules: applied temporarily. Add the rule to your config for persistence."
|
m.notice = "Rules file is not writable: permanent changes are not supported."
|
||||||
case guard.ErrPermission:
|
case guard.ErrPermission:
|
||||||
m.notice = "Permission denied. Run with appropriate privileges."
|
m.notice = "Permission denied. Run with appropriate privileges."
|
||||||
default:
|
default:
|
||||||
m.notice = msg.err.Error()
|
m.notice = msg.err.Error()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
m.notice = ""
|
m.notice = m.defaultNotice()
|
||||||
}
|
}
|
||||||
return m, fetchDevices
|
return m, fetchDevices
|
||||||
|
|
||||||
@@ -145,11 +166,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case tea.MouseWheelMsg:
|
case tea.MouseWheelMsg:
|
||||||
if m.state == statePopup {
|
return m.updateMouseWheel(msg)
|
||||||
var cmd tea.Cmd
|
|
||||||
m.actionList, cmd = m.actionList.Update(msg)
|
|
||||||
return m, cmd
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.state == stateList {
|
if m.state == stateList {
|
||||||
@@ -171,7 +188,7 @@ func (m Model) updateList(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
|
|||||||
case key.Matches(msg, listKeys.Quit):
|
case key.Matches(msg, listKeys.Quit):
|
||||||
return m, tea.Quit
|
return m, tea.Quit
|
||||||
case key.Matches(msg, listKeys.Refresh):
|
case key.Matches(msg, listKeys.Refresh):
|
||||||
m.notice = ""
|
m.notice = m.defaultNotice()
|
||||||
return m, tea.Batch(fetchDevices, fetchDaemonStatus)
|
return m, tea.Batch(fetchDevices, fetchDaemonStatus)
|
||||||
case key.Matches(msg, listKeys.Help):
|
case key.Matches(msg, listKeys.Help):
|
||||||
m.help.ShowAll = !m.help.ShowAll
|
m.help.ShowAll = !m.help.ShowAll
|
||||||
@@ -186,18 +203,11 @@ func (m Model) updateList(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
|
|||||||
m.state = statePopup
|
m.state = statePopup
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
case id >= 0 && key.Matches(msg, listKeys.Allow):
|
}
|
||||||
return m, doAction(id, guard.AllowDevice, false)
|
if id >= 0 {
|
||||||
case id >= 0 && key.Matches(msg, listKeys.AllowPerm):
|
if cmd := m.deviceActionCmd(msg, id); cmd != nil {
|
||||||
return m, doAction(id, guard.AllowDevice, true)
|
return m, cmd
|
||||||
case id >= 0 && key.Matches(msg, listKeys.Block):
|
}
|
||||||
return m, doAction(id, guard.BlockDevice, false)
|
|
||||||
case id >= 0 && key.Matches(msg, listKeys.BlockPerm):
|
|
||||||
return m, doAction(id, guard.BlockDevice, true)
|
|
||||||
case id >= 0 && key.Matches(msg, listKeys.Reject):
|
|
||||||
return m, doAction(id, guard.RejectDevice, false)
|
|
||||||
case id >= 0 && key.Matches(msg, listKeys.RejectPerm):
|
|
||||||
return m, doAction(id, guard.RejectDevice, true)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var cmd tea.Cmd
|
var cmd tea.Cmd
|
||||||
@@ -290,12 +300,26 @@ func (m Model) actionListInnerWidth() int {
|
|||||||
return m.popupOuterWidth() - 8 // border(2) + padding_h(6)
|
return m.popupOuterWidth() - 8 // border(2) + padding_h(6)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m Model) defaultNotice() string {
|
||||||
|
if m.rulesManaged {
|
||||||
|
return "Rules managed by NixOS config: permanent actions not available."
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m Model) actionItemCount() int {
|
||||||
|
if m.rulesManaged {
|
||||||
|
return 3
|
||||||
|
}
|
||||||
|
return 6
|
||||||
|
}
|
||||||
|
|
||||||
// updateActionListSize sizes the action list and toggles pagination based on available space.
|
// updateActionListSize sizes the action list and toggles pagination based on available space.
|
||||||
// When there is enough room for all items: pagination is hidden and height is set exactly,
|
// When there is enough room for all items: pagination is hidden and height is set exactly,
|
||||||
// avoiding the phantom line that bubbles/list reserves when showPagination=true.
|
// avoiding the phantom line that bubbles/list reserves when showPagination=true.
|
||||||
// When space is limited: pagination is shown naturally by bubbles/list.
|
// When space is limited: pagination is shown naturally by bubbles/list.
|
||||||
func (m *Model) updateActionListSize() {
|
func (m *Model) updateActionListSize() {
|
||||||
const items = 6
|
items := m.actionItemCount()
|
||||||
innerW := m.actionListInnerWidth()
|
innerW := m.actionListInnerWidth()
|
||||||
// popup overhead: border(2) + padding_v(2) + title(1) + blank(1) + hint(1) = 7
|
// popup overhead: border(2) + padding_v(2) + title(1) + blank(1) + hint(1) = 7
|
||||||
available := m.height - 7 - 2 // 2 lines margin
|
available := m.height - 7 - 2 // 2 lines margin
|
||||||
@@ -347,3 +371,46 @@ func doAction(id int, fn func(int, bool) error, permanent bool) tea.Cmd {
|
|||||||
return actionMsg{err: fn(id, permanent)}
|
return actionMsg{err: fn(id, permanent)}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type actionBinding struct {
|
||||||
|
binding key.Binding
|
||||||
|
fn func(int, bool) error
|
||||||
|
perm bool
|
||||||
|
needsWritable bool
|
||||||
|
}
|
||||||
|
|
||||||
|
var deviceActionBindings = []actionBinding{
|
||||||
|
{listKeys.Allow, guard.AllowDevice, false, false},
|
||||||
|
{listKeys.AllowPerm, guard.AllowDevice, true, true},
|
||||||
|
{listKeys.Block, guard.BlockDevice, false, false},
|
||||||
|
{listKeys.BlockPerm, guard.BlockDevice, true, true},
|
||||||
|
{listKeys.Reject, guard.RejectDevice, false, false},
|
||||||
|
{listKeys.RejectPerm, guard.RejectDevice, true, true},
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m Model) deviceActionCmd(msg tea.KeyPressMsg, id int) tea.Cmd {
|
||||||
|
for _, b := range deviceActionBindings {
|
||||||
|
if (!b.needsWritable || !m.rulesManaged) && key.Matches(msg, b.binding) {
|
||||||
|
return doAction(id, b.fn, b.perm)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m Model) updateMouseWheel(msg tea.MouseWheelMsg) (tea.Model, tea.Cmd) {
|
||||||
|
switch msg.Button {
|
||||||
|
case tea.MouseWheelUp:
|
||||||
|
if m.state == statePopup {
|
||||||
|
m.actionList.CursorUp()
|
||||||
|
} else {
|
||||||
|
m.list.CursorUp()
|
||||||
|
}
|
||||||
|
case tea.MouseWheelDown:
|
||||||
|
if m.state == statePopup {
|
||||||
|
m.actionList.CursorDown()
|
||||||
|
} else {
|
||||||
|
m.list.CursorDown()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -48,7 +48,5 @@ var (
|
|||||||
|
|
||||||
popupTitleStyle = lipgloss.NewStyle().Bold(true).MarginBottom(1)
|
popupTitleStyle = lipgloss.NewStyle().Bold(true).MarginBottom(1)
|
||||||
|
|
||||||
keyHintStyle = lipgloss.NewStyle().Foreground(colorAccent).Bold(true)
|
|
||||||
warnStyle = lipgloss.NewStyle().Foreground(colorRejected)
|
warnStyle = lipgloss.NewStyle().Foreground(colorRejected)
|
||||||
errStyle = lipgloss.NewStyle().Foreground(colorBlocked).Bold(true)
|
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user