mirror of
https://github.com/anotherhadi/usbguard-tui.git
synced 2026-05-11 22:02:34 +02:00
Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 64b36e716c | |||
| 85184dafca | |||
| 6db3a32758 | |||
| 1ac92a5ace | |||
| 2c54df832c | |||
| ecd12f18e0 | |||
| 787d4ac0f1 | |||
| 8c250389b3 | |||
| b19739b0a6 | |||
| 20f9b7cf89 | |||
| abe6b5dde5 | |||
| c7f42c1a12 | |||
| 3731160024 | |||
| 62cba43e15 | |||
| f1dd37cfc6 | |||
| 6267bc6087 | |||
| 1661ec4f57 | |||
| 6e3beb44e1 | |||
| 6811a1c7fd | |||
| e67b259cfb | |||
| dfa9a30586 | |||
| d181eae077 |
@@ -0,0 +1 @@
|
|||||||
|
ko_fi: anotherhadi
|
||||||
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"
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
name: Release
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- "v*"
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
release:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- uses: actions/setup-go@v5
|
||||||
|
with:
|
||||||
|
go-version-file: go.mod
|
||||||
|
|
||||||
|
- uses: goreleaser/goreleaser-action@v6
|
||||||
|
with:
|
||||||
|
version: "~> v2"
|
||||||
|
args: release --clean
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
version: 2
|
||||||
|
|
||||||
|
before:
|
||||||
|
hooks:
|
||||||
|
- go mod tidy
|
||||||
|
|
||||||
|
builds:
|
||||||
|
- binary: usbguard-tui
|
||||||
|
goos:
|
||||||
|
- linux
|
||||||
|
goarch:
|
||||||
|
- amd64
|
||||||
|
- arm64
|
||||||
|
ldflags:
|
||||||
|
- -s -w -X main.version={{.Version}}
|
||||||
|
|
||||||
|
archives:
|
||||||
|
- formats:
|
||||||
|
- tar.gz
|
||||||
|
name_template: "{{ .ProjectName }}_{{ .Os }}_{{ .Arch }}"
|
||||||
|
|
||||||
|
checksum:
|
||||||
|
name_template: checksums.txt
|
||||||
|
|
||||||
|
changelog:
|
||||||
|
sort: asc
|
||||||
|
filters:
|
||||||
|
exclude:
|
||||||
|
- "^docs:"
|
||||||
|
- "^test:"
|
||||||
|
- "^ci:"
|
||||||
@@ -2,13 +2,31 @@
|
|||||||
▖▖▄▖▄ ▄▖ ▌ ▄▖▖▖▄▖
|
▖▖▄▖▄ ▄▖ ▌ ▄▖▖▖▄▖
|
||||||
▌▌▚ ▙▘▌ ▌▌▀▌▛▘▛▌ ▐ ▌▌▐
|
▌▌▚ ▙▘▌ ▌▌▀▌▛▘▛▌ ▐ ▌▌▐
|
||||||
▙▌▄▌▙▘▙▌▙▌█▌▌ ▙▌ ▐ ▙▌▟▖
|
▙▌▄▌▙▘▙▌▙▌█▌▌ ▙▌ ▐ ▙▌▟▖
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
[](go.mod)
|
||||||
|
[](https://github.com/anotherhadi/usbguard-tui/releases)
|
||||||
|
[](LICENSE)
|
||||||
|
[](https://goreportcard.com/report/github.com/anotherhadi/usbguard-tui)
|
||||||
|
|
||||||
# USBGuard TUI
|
# USBGuard TUI
|
||||||
|
|
||||||
A terminal UI for managing USB devices via [usbguard](https://usbguard.github.io/).
|
A terminal UI for managing USB devices via [usbguard](https://usbguard.github.io/).
|
||||||
Built with [bubbletea](https://github.com/charmbracelet/bubbletea) & Goland!
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
<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,18 +14,18 @@
|
|||||||
(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"];
|
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 = ./.;
|
||||||
outputs = ["out"];
|
outputs = ["out"];
|
||||||
|
|
||||||
vendorHash = "sha256-SMhllO87YlmySHroKfPq1pHb67CwHaZ3XMp3t983etc=";
|
vendorHash = "sha256-NfmNdQaISob3ZguQnwgfXHUDUFION988ucp18q996pM=";
|
||||||
|
|
||||||
meta = with pkgs.lib; {
|
meta = with pkgs.lib; {
|
||||||
description = "A terminal UI for managing USB devices via usbguard.";
|
description = "A terminal UI for managing USB devices via usbguard.";
|
||||||
@@ -33,9 +33,9 @@
|
|||||||
platforms = platforms.unix;
|
platforms = platforms.unix;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
in {
|
||||||
|
"${pname}" = pkg;
|
||||||
|
default = pkg;
|
||||||
});
|
});
|
||||||
|
|
||||||
defaultPackage =
|
|
||||||
forAllSystems (system: pkgs: self.packages.${system}.${pname});
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,32 +3,27 @@ module github.com/anotherhadi/usbguard-tui
|
|||||||
go 1.26.2
|
go 1.26.2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/charmbracelet/bubbles v1.0.0
|
charm.land/bubbles/v2 v2.1.0
|
||||||
github.com/charmbracelet/bubbletea v1.3.10
|
charm.land/bubbletea/v2 v2.0.6
|
||||||
github.com/charmbracelet/lipgloss v1.1.0
|
charm.land/lipgloss/v2 v2.0.3
|
||||||
github.com/charmbracelet/x/ansi v0.11.6
|
github.com/charmbracelet/x/ansi v0.11.7
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/atotto/clipboard v0.1.4 // indirect
|
github.com/atotto/clipboard v0.1.4 // indirect
|
||||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
github.com/charmbracelet/colorprofile v0.4.3 // indirect
|
||||||
github.com/charmbracelet/colorprofile v0.4.1 // indirect
|
github.com/charmbracelet/ultraviolet v0.0.0-20260416155717-489999b90468 // indirect
|
||||||
github.com/charmbracelet/x/cellbuf v0.0.15 // indirect
|
|
||||||
github.com/charmbracelet/x/term v0.2.2 // indirect
|
github.com/charmbracelet/x/term v0.2.2 // indirect
|
||||||
github.com/clipperhouse/displaywidth v0.9.0 // indirect
|
github.com/charmbracelet/x/termios v0.1.1 // indirect
|
||||||
github.com/clipperhouse/stringish v0.1.1 // indirect
|
github.com/charmbracelet/x/windows v0.2.2 // indirect
|
||||||
github.com/clipperhouse/uax29/v2 v2.5.0 // indirect
|
github.com/clipperhouse/displaywidth v0.11.0 // indirect
|
||||||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
|
github.com/clipperhouse/uax29/v2 v2.7.0 // indirect
|
||||||
github.com/lucasb-eyer/go-colorful v1.3.0 // indirect
|
github.com/lucasb-eyer/go-colorful v1.4.0 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-runewidth v0.0.23 // indirect
|
||||||
github.com/mattn/go-localereader v0.0.1 // indirect
|
|
||||||
github.com/mattn/go-runewidth v0.0.19 // indirect
|
|
||||||
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
|
|
||||||
github.com/muesli/cancelreader v0.2.2 // indirect
|
github.com/muesli/cancelreader v0.2.2 // indirect
|
||||||
github.com/muesli/termenv v0.16.0 // indirect
|
|
||||||
github.com/rivo/uniseg v0.4.7 // indirect
|
github.com/rivo/uniseg v0.4.7 // indirect
|
||||||
github.com/sahilm/fuzzy v0.1.1 // indirect
|
github.com/sahilm/fuzzy v0.1.1 // indirect
|
||||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
||||||
golang.org/x/sys v0.38.0 // indirect
|
golang.org/x/sync v0.20.0 // indirect
|
||||||
golang.org/x/text v0.3.8 // indirect
|
golang.org/x/sys v0.43.0 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,49 +1,39 @@
|
|||||||
|
charm.land/bubbles/v2 v2.1.0 h1:YSnNh5cPYlYjPxRrzs5VEn3vwhtEn3jVGRBT3M7/I0g=
|
||||||
|
charm.land/bubbles/v2 v2.1.0/go.mod h1:l97h4hym2hvWBVfmJDtrEHHCtkIKeTEb3TTJ4ZOB3wY=
|
||||||
|
charm.land/bubbletea/v2 v2.0.6 h1:UHN/91OyuhaOFGSrBXQ/hMZD8IO1Uc4BvHlgHXL2WJo=
|
||||||
|
charm.land/bubbletea/v2 v2.0.6/go.mod h1:MH/D8ZLlN3op37vQvijKuU29g3rqTp+aQapURFonF9g=
|
||||||
|
charm.land/lipgloss/v2 v2.0.3 h1:yM2zJ4Cf5Y51b7RHIwioil4ApI/aypFXXVHSwlM6RzU=
|
||||||
|
charm.land/lipgloss/v2 v2.0.3/go.mod h1:7myLU9iG/3xluAWzpY/fSxYYHCgoKTie7laxk6ATwXA=
|
||||||
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
|
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
|
||||||
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
|
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
|
||||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
|
github.com/aymanbagabas/go-udiff v0.4.1 h1:OEIrQ8maEeDBXQDoGCbbTTXYJMYRCRO1fnodZ12Gv5o=
|
||||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
|
github.com/aymanbagabas/go-udiff v0.4.1/go.mod h1:0L9PGwj20lrtmEMeyw4WKJ/TMyDtvAoK9bf2u/mNo3w=
|
||||||
github.com/aymanbagabas/go-udiff v0.3.1 h1:LV+qyBQ2pqe0u42ZsUEtPiCaUoqgA9gYRDs3vj1nolY=
|
github.com/charmbracelet/colorprofile v0.4.3 h1:QPa1IWkYI+AOB+fE+mg/5/4HRMZcaXex9t5KX76i20Q=
|
||||||
github.com/aymanbagabas/go-udiff v0.3.1/go.mod h1:G0fsKmG+P6ylD0r6N/KgQD/nWzgfnl8ZBcNLgcbrw8E=
|
github.com/charmbracelet/colorprofile v0.4.3/go.mod h1:/zT4BhpD5aGFpqQQqw7a+VtHCzu+zrQtt1zhMt9mR4Q=
|
||||||
github.com/charmbracelet/bubbles v1.0.0 h1:12J8/ak/uCZEMQ6KU7pcfwceyjLlWsDLAxB5fXonfvc=
|
github.com/charmbracelet/ultraviolet v0.0.0-20260416155717-489999b90468 h1:Q9fO0y1Zo5KB/5Vu8JZoLGm1N3RzF9bNj3Ao3xoR+Ac=
|
||||||
github.com/charmbracelet/bubbles v1.0.0/go.mod h1:9d/Zd5GdnauMI5ivUIVisuEm3ave1XwXtD1ckyV6r3E=
|
github.com/charmbracelet/ultraviolet v0.0.0-20260416155717-489999b90468/go.mod h1:bAAz7dh/FTYfC+oiHavL4mX1tOIBZ0ZwYjSi3qE6ivM=
|
||||||
github.com/charmbracelet/bubbletea v1.3.10 h1:otUDHWMMzQSB0Pkc87rm691KZ3SWa4KUlvF9nRvCICw=
|
github.com/charmbracelet/x/ansi v0.11.7 h1:kzv1kJvjg2S3r9KHo8hDdHFQLEqn4RBCb39dAYC84jI=
|
||||||
github.com/charmbracelet/bubbletea v1.3.10/go.mod h1:ORQfo0fk8U+po9VaNvnV95UPWA1BitP1E0N6xJPlHr4=
|
github.com/charmbracelet/x/ansi v0.11.7/go.mod h1:9qGpnAVYz+8ACONkZBUWPtL7lulP9No6p1epAihUZwQ=
|
||||||
github.com/charmbracelet/colorprofile v0.4.1 h1:a1lO03qTrSIRaK8c3JRxJDZOvhvIeSco3ej+ngLk1kk=
|
github.com/charmbracelet/x/exp/golden v0.0.0-20250806222409-83e3a29d542f h1:pk6gmGpCE7F3FcjaOEKYriCvpmIN4+6OS/RD0vm4uIA=
|
||||||
github.com/charmbracelet/colorprofile v0.4.1/go.mod h1:U1d9Dljmdf9DLegaJ0nGZNJvoXAhayhmidOdcBwAvKk=
|
github.com/charmbracelet/x/exp/golden v0.0.0-20250806222409-83e3a29d542f/go.mod h1:IfZAMTHB6XkZSeXUqriemErjAWCCzT0LwjKFYCZyw0I=
|
||||||
github.com/charmbracelet/lipgloss v1.1.0 h1:vYXsiLHVkK7fp74RkV7b2kq9+zDLoEU4MZoFqR/noCY=
|
|
||||||
github.com/charmbracelet/lipgloss v1.1.0/go.mod h1:/6Q8FR2o+kj8rz4Dq0zQc3vYf7X+B0binUUBwA0aL30=
|
|
||||||
github.com/charmbracelet/x/ansi v0.11.6 h1:GhV21SiDz/45W9AnV2R61xZMRri5NlLnl6CVF7ihZW8=
|
|
||||||
github.com/charmbracelet/x/ansi v0.11.6/go.mod h1:2JNYLgQUsyqaiLovhU2Rv/pb8r6ydXKS3NIttu3VGZQ=
|
|
||||||
github.com/charmbracelet/x/cellbuf v0.0.15 h1:ur3pZy0o6z/R7EylET877CBxaiE1Sp1GMxoFPAIztPI=
|
|
||||||
github.com/charmbracelet/x/cellbuf v0.0.15/go.mod h1:J1YVbR7MUuEGIFPCaaZ96KDl5NoS0DAWkskup+mOY+Q=
|
|
||||||
github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91 h1:payRxjMjKgx2PaCWLZ4p3ro9y97+TVLZNaRZgJwSVDQ=
|
|
||||||
github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=
|
|
||||||
github.com/charmbracelet/x/term v0.2.2 h1:xVRT/S2ZcKdhhOuSP4t5cLi5o+JxklsoEObBSgfgZRk=
|
github.com/charmbracelet/x/term v0.2.2 h1:xVRT/S2ZcKdhhOuSP4t5cLi5o+JxklsoEObBSgfgZRk=
|
||||||
github.com/charmbracelet/x/term v0.2.2/go.mod h1:kF8CY5RddLWrsgVwpw4kAa6TESp6EB5y3uxGLeCqzAI=
|
github.com/charmbracelet/x/term v0.2.2/go.mod h1:kF8CY5RddLWrsgVwpw4kAa6TESp6EB5y3uxGLeCqzAI=
|
||||||
github.com/clipperhouse/displaywidth v0.9.0 h1:Qb4KOhYwRiN3viMv1v/3cTBlz3AcAZX3+y9OLhMtAtA=
|
github.com/charmbracelet/x/termios v0.1.1 h1:o3Q2bT8eqzGnGPOYheoYS8eEleT5ZVNYNy8JawjaNZY=
|
||||||
github.com/clipperhouse/displaywidth v0.9.0/go.mod h1:aCAAqTlh4GIVkhQnJpbL0T/WfcrJXHcj8C0yjYcjOZA=
|
github.com/charmbracelet/x/termios v0.1.1/go.mod h1:rB7fnv1TgOPOyyKRJ9o+AsTU/vK5WHJ2ivHeut/Pcwo=
|
||||||
github.com/clipperhouse/stringish v0.1.1 h1:+NSqMOr3GR6k1FdRhhnXrLfztGzuG+VuFDfatpWHKCs=
|
github.com/charmbracelet/x/windows v0.2.2 h1:IofanmuvaxnKHuV04sC0eBy/smG6kIKrWG2/jYn2GuM=
|
||||||
github.com/clipperhouse/stringish v0.1.1/go.mod h1:v/WhFtE1q0ovMta2+m+UbpZ+2/HEXNWYXQgCt4hdOzA=
|
github.com/charmbracelet/x/windows v0.2.2/go.mod h1:/8XtdKZzedat74NQFn0NGlGL4soHB0YQZrETF96h75k=
|
||||||
github.com/clipperhouse/uax29/v2 v2.5.0 h1:x7T0T4eTHDONxFJsL94uKNKPHrclyFI0lm7+w94cO8U=
|
github.com/clipperhouse/displaywidth v0.11.0 h1:lBc6kY44VFw+TDx4I8opi/EtL9m20WSEFgwIwO+UVM8=
|
||||||
github.com/clipperhouse/uax29/v2 v2.5.0/go.mod h1:Wn1g7MK6OoeDT0vL+Q0SQLDz/KpfsVRgg6W7ihQeh4g=
|
github.com/clipperhouse/displaywidth v0.11.0/go.mod h1:bkrFNkf81G8HyVqmKGxsPufD3JhNl3dSqnGhOoSD/o0=
|
||||||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
|
github.com/clipperhouse/uax29/v2 v2.7.0 h1:+gs4oBZ2gPfVrKPthwbMzWZDaAFPGYK72F0NJv2v7Vk=
|
||||||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
|
github.com/clipperhouse/uax29/v2 v2.7.0/go.mod h1:EFJ2TJMRUaplDxHKj1qAEhCtQPW2tJSwu5BF98AuoVM=
|
||||||
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||||
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||||
github.com/lucasb-eyer/go-colorful v1.3.0 h1:2/yBRLdWBZKrf7gB40FoiKfAWYQ0lqNcbuQwVHXptag=
|
github.com/lucasb-eyer/go-colorful v1.4.0 h1:UtrWVfLdarDgc44HcS7pYloGHJUjHV/4FwW4TvVgFr4=
|
||||||
github.com/lucasb-eyer/go-colorful v1.3.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
github.com/lucasb-eyer/go-colorful v1.4.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
github.com/mattn/go-runewidth v0.0.23 h1:7ykA0T0jkPpzSvMS5i9uoNn2Xy3R383f9HDx3RybWcw=
|
||||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
github.com/mattn/go-runewidth v0.0.23/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=
|
||||||
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
|
|
||||||
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
|
|
||||||
github.com/mattn/go-runewidth v0.0.19 h1:v++JhqYnZuu5jSKrk9RbgF5v4CGUjqRfBm05byFGLdw=
|
|
||||||
github.com/mattn/go-runewidth v0.0.19/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=
|
|
||||||
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=
|
|
||||||
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=
|
|
||||||
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
|
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
|
||||||
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
|
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
|
||||||
github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
|
|
||||||
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
|
|
||||||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||||
github.com/sahilm/fuzzy v0.1.1 h1:ceu5RHF8DGgoi+/dR5PsECjCDH1BE3Fnmpo7aVXOdRA=
|
github.com/sahilm/fuzzy v0.1.1 h1:ceu5RHF8DGgoi+/dR5PsECjCDH1BE3Fnmpo7aVXOdRA=
|
||||||
@@ -52,9 +42,7 @@ github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavM
|
|||||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
|
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
|
||||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
|
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
|
||||||
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
|
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
|
||||||
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
|
||||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||||
golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
|
golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI=
|
||||||
golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||||
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
|
|
||||||
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package guard
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -20,6 +21,7 @@ func ListDevices() ([]Device, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, wrapExecError(err)
|
return nil, wrapExecError(err)
|
||||||
}
|
}
|
||||||
|
rules := listRules()
|
||||||
var devices []Device
|
var devices []Device
|
||||||
for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
|
for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
|
||||||
if line == "" {
|
if line == "" {
|
||||||
@@ -27,12 +29,31 @@ func ListDevices() ([]Device, error) {
|
|||||||
}
|
}
|
||||||
d, err := parseLine(line)
|
d, err := parseLine(line)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
d.Permanent = rules[d.Hash] == d.Status
|
||||||
devices = append(devices, d)
|
devices = append(devices, d)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return devices, nil
|
return devices, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func listRules() map[string]Status {
|
||||||
|
out, err := exec.Command("usbguard", "list-rules").Output()
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
rules := make(map[string]Status)
|
||||||
|
for _, line := range strings.Split(strings.TrimSpace(string(out)), "\n") {
|
||||||
|
if line == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
d, err := parseLine(line)
|
||||||
|
if err == nil && d.Hash != "" {
|
||||||
|
rules[d.Hash] = d.Status
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return rules
|
||||||
|
}
|
||||||
|
|
||||||
func AllowDevice(id int, permanent bool) error { return applyPolicy("allow-device", id, permanent) }
|
func AllowDevice(id int, permanent bool) error { return applyPolicy("allow-device", id, permanent) }
|
||||||
func BlockDevice(id int, permanent bool) error { return applyPolicy("block-device", id, permanent) }
|
func BlockDevice(id int, permanent bool) error { return applyPolicy("block-device", id, permanent) }
|
||||||
func RejectDevice(id int, permanent bool) error { return applyPolicy("reject-device", id, permanent) }
|
func RejectDevice(id int, permanent bool) error { return applyPolicy("reject-device", id, permanent) }
|
||||||
@@ -66,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 {
|
||||||
|
|||||||
@@ -16,10 +16,12 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Device struct {
|
type Device struct {
|
||||||
ID int
|
ID int
|
||||||
Name string
|
Name string
|
||||||
Status Status
|
Status Status
|
||||||
VidPid string
|
VidPid string
|
||||||
|
Hash string
|
||||||
|
Permanent bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d Device) Title() string { return d.Name }
|
func (d Device) Title() string { return d.Name }
|
||||||
@@ -56,6 +58,7 @@ func parseLine(line string) (Device, error) {
|
|||||||
Name: name,
|
Name: name,
|
||||||
Status: status,
|
Status: status,
|
||||||
VidPid: extractUnquoted(rest, "id"),
|
VidPid: extractUnquoted(rest, "id"),
|
||||||
|
Hash: extractField(rest, "hash"),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,6 +76,10 @@ func extractField(rule, field string) string {
|
|||||||
return rest[:end]
|
return rest[:end]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NixOSRule(dev Device, status Status) string {
|
||||||
|
return fmt.Sprintf("%s id %s name \"%s\"", status, dev.VidPid, dev.Name)
|
||||||
|
}
|
||||||
|
|
||||||
func extractUnquoted(rule, field string) string {
|
func extractUnquoted(rule, field string) string {
|
||||||
prefix := field + " "
|
prefix := field + " "
|
||||||
idx := strings.Index(rule, prefix)
|
idx := strings.Index(rule, prefix)
|
||||||
|
|||||||
@@ -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")
|
||||||
)
|
)
|
||||||
|
|||||||
+24
-28
@@ -4,17 +4,16 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/charmbracelet/bubbles/list"
|
"charm.land/bubbles/v2/list"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "charm.land/bubbletea/v2"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"charm.land/lipgloss/v2"
|
||||||
"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 }
|
||||||
func (d deviceDelegate) Spacing() int { return 0 }
|
func (d deviceDelegate) Spacing() int { return 0 }
|
||||||
func (d deviceDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
|
func (d deviceDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
|
||||||
|
|
||||||
func (d deviceDelegate) Render(w io.Writer, m list.Model, index int, item list.Item) {
|
func (d deviceDelegate) Render(w io.Writer, m list.Model, index int, item list.Item) {
|
||||||
@@ -25,19 +24,13 @@ func (d deviceDelegate) Render(w io.Writer, m list.Model, index int, item list.I
|
|||||||
|
|
||||||
selected := index == m.Index()
|
selected := index == m.Index()
|
||||||
|
|
||||||
var color lipgloss.Color
|
colorMap := statusColors
|
||||||
if selected {
|
if selected {
|
||||||
var ok bool
|
colorMap = statusColorsSelected
|
||||||
color, ok = statusColorsSelected[dev.Status]
|
}
|
||||||
if !ok {
|
clr, ok := colorMap[dev.Status]
|
||||||
color = colorMuted
|
if !ok {
|
||||||
}
|
clr = colorMuted
|
||||||
} else {
|
|
||||||
var ok bool
|
|
||||||
color, ok = statusColors[dev.Status]
|
|
||||||
if !ok {
|
|
||||||
color = colorMuted
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var nameStyle, descStyle lipgloss.Style
|
var nameStyle, descStyle lipgloss.Style
|
||||||
@@ -45,7 +38,7 @@ func (d deviceDelegate) Render(w io.Writer, m list.Model, index int, item list.I
|
|||||||
nameStyle = lipgloss.NewStyle().
|
nameStyle = lipgloss.NewStyle().
|
||||||
Border(lipgloss.NormalBorder(), false, false, false, true).
|
Border(lipgloss.NormalBorder(), false, false, false, true).
|
||||||
BorderForeground(colorAccent).
|
BorderForeground(colorAccent).
|
||||||
Foreground(color).
|
Foreground(clr).
|
||||||
Bold(true).
|
Bold(true).
|
||||||
PaddingLeft(1)
|
PaddingLeft(1)
|
||||||
descStyle = lipgloss.NewStyle().
|
descStyle = lipgloss.NewStyle().
|
||||||
@@ -54,33 +47,36 @@ func (d deviceDelegate) Render(w io.Writer, m list.Model, index int, item list.I
|
|||||||
Foreground(colorMuted).
|
Foreground(colorMuted).
|
||||||
PaddingLeft(1)
|
PaddingLeft(1)
|
||||||
} else {
|
} else {
|
||||||
nameStyle = lipgloss.NewStyle().Foreground(color).PaddingLeft(2)
|
nameStyle = lipgloss.NewStyle().Foreground(clr).PaddingLeft(2)
|
||||||
descStyle = lipgloss.NewStyle().Foreground(colorMuted).PaddingLeft(2)
|
descStyle = lipgloss.NewStyle().Foreground(colorMuted).PaddingLeft(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
permIndicator := "○ tmp"
|
||||||
|
if dev.Permanent {
|
||||||
|
permIndicator = "● perm"
|
||||||
|
}
|
||||||
fmt.Fprintf(w, "%s\n%s",
|
fmt.Fprintf(w, "%s\n%s",
|
||||||
nameStyle.Render(dev.Name),
|
nameStyle.Render(dev.Name),
|
||||||
descStyle.Render(fmt.Sprintf("id:%-3d %s %s", dev.ID, dev.VidPid, string(dev.Status))),
|
descStyle.Render(fmt.Sprintf("id:%-3d %s %s %s", dev.ID, dev.VidPid, string(dev.Status), permIndicator)),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
permanent bool
|
permanent bool
|
||||||
status guard.Status
|
status guard.Status
|
||||||
|
nixos bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a actionItem) Title() string { return a.label }
|
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 }
|
||||||
func (d actionDelegate) Spacing() int { return 0 }
|
func (d actionDelegate) Spacing() int { return 0 }
|
||||||
func (d actionDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
|
func (d actionDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
|
||||||
|
|
||||||
func (d actionDelegate) Render(w io.Writer, m list.Model, index int, item list.Item) {
|
func (d actionDelegate) Render(w io.Writer, m list.Model, index int, item list.Item) {
|
||||||
@@ -89,11 +85,11 @@ func (d actionDelegate) Render(w io.Writer, m list.Model, index int, item list.I
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if index == m.Index() {
|
if index == m.Index() {
|
||||||
color, ok := statusColorsSelected[a.status]
|
clr, ok := statusColorsSelected[a.status]
|
||||||
if !ok {
|
if !ok {
|
||||||
color = colorAccent
|
clr = colorAccent
|
||||||
}
|
}
|
||||||
fmt.Fprintf(w, " %s", lipgloss.NewStyle().Bold(true).Foreground(color).Render("> "+a.label))
|
fmt.Fprintf(w, " %s", lipgloss.NewStyle().Bold(true).Foreground(clr).Render("> "+a.label))
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(w, " %s", a.label)
|
fmt.Fprintf(w, " %s", a.label)
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-10
@@ -1,13 +1,13 @@
|
|||||||
package ui
|
package ui
|
||||||
|
|
||||||
import "github.com/charmbracelet/bubbles/key"
|
import "charm.land/bubbles/v2/key"
|
||||||
|
|
||||||
type listKeyMap struct {
|
type listKeyMap struct {
|
||||||
Open key.Binding
|
Open key.Binding
|
||||||
Filter key.Binding
|
Filter key.Binding
|
||||||
Refresh key.Binding
|
Refresh key.Binding
|
||||||
Quit key.Binding
|
Quit key.Binding
|
||||||
Help key.Binding
|
Help key.Binding
|
||||||
// shown only in full help
|
// shown only in full help
|
||||||
Allow key.Binding
|
Allow key.Binding
|
||||||
AllowPerm key.Binding
|
AllowPerm key.Binding
|
||||||
@@ -29,17 +29,17 @@ func (k listKeyMap) FullHelp() [][]key.Binding {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var listKeys = listKeyMap{
|
var listKeys = listKeyMap{
|
||||||
Open: key.NewBinding(key.WithKeys("enter"), key.WithHelp("enter", "select action")),
|
Open: key.NewBinding(key.WithKeys("enter", "tab", "l"), key.WithHelp("enter/l", "select action")),
|
||||||
Filter: key.NewBinding(key.WithKeys("/"), key.WithHelp("/", "filter")),
|
Filter: key.NewBinding(key.WithKeys("/"), key.WithHelp("/", "filter")),
|
||||||
Refresh: key.NewBinding(key.WithKeys("r"), key.WithHelp("r", "refresh")),
|
Refresh: key.NewBinding(key.WithKeys("r"), key.WithHelp("r", "refresh")),
|
||||||
Quit: key.NewBinding(key.WithKeys("q", "ctrl+c"), key.WithHelp("q", "quit")),
|
Quit: key.NewBinding(key.WithKeys("q", "esc", "ctrl+c"), key.WithHelp("q/esc", "quit")),
|
||||||
Help: key.NewBinding(key.WithKeys("?"), key.WithHelp("?", "more")),
|
Help: key.NewBinding(key.WithKeys("?"), key.WithHelp("?", "more")),
|
||||||
Allow: key.NewBinding(key.WithKeys("a"), key.WithHelp("a", "allow")),
|
Allow: key.NewBinding(key.WithKeys("a"), key.WithHelp("a", "allow")),
|
||||||
AllowPerm: key.NewBinding(key.WithKeys("A"), key.WithHelp("A", "allow (perm)")),
|
AllowPerm: key.NewBinding(key.WithKeys("A"), key.WithHelp("A", "allow (perm)")),
|
||||||
Block: key.NewBinding(key.WithKeys("b"), key.WithHelp("b", "block")),
|
Block: key.NewBinding(key.WithKeys("b"), key.WithHelp("b", "block")),
|
||||||
BlockPerm: key.NewBinding(key.WithKeys("B"), key.WithHelp("B", "block (perm)")),
|
BlockPerm: key.NewBinding(key.WithKeys("B"), key.WithHelp("B", "block (perm)")),
|
||||||
Reject: key.NewBinding(key.WithKeys("j"), key.WithHelp("j", "reject")),
|
Reject: key.NewBinding(key.WithKeys("e"), key.WithHelp("e", "reject")),
|
||||||
RejectPerm: key.NewBinding(key.WithKeys("J"), key.WithHelp("J", "reject (perm)")),
|
RejectPerm: key.NewBinding(key.WithKeys("E"), key.WithHelp("E", "reject (perm)")),
|
||||||
}
|
}
|
||||||
|
|
||||||
var cancelKey = key.NewBinding(key.WithKeys("esc", "q", "ctrl+c"), key.WithHelp("esc/q", "cancel"))
|
var cancelKey = key.NewBinding(key.WithKeys("esc", "q", "ctrl+c"), key.WithHelp("esc/q", "cancel"))
|
||||||
|
|||||||
+175
-48
@@ -1,15 +1,17 @@
|
|||||||
package ui
|
package ui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"charm.land/bubbles/v2/help"
|
||||||
|
"charm.land/bubbles/v2/key"
|
||||||
|
"charm.land/bubbles/v2/list"
|
||||||
|
"charm.land/bubbles/v2/textinput"
|
||||||
|
tea "charm.land/bubbletea/v2"
|
||||||
|
"charm.land/lipgloss/v2"
|
||||||
"github.com/anotherhadi/usbguard-tui/internal/guard"
|
"github.com/anotherhadi/usbguard-tui/internal/guard"
|
||||||
"github.com/charmbracelet/bubbles/help"
|
|
||||||
"github.com/charmbracelet/bubbles/key"
|
|
||||||
"github.com/charmbracelet/bubbles/list"
|
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
|
||||||
"github.com/charmbracelet/lipgloss"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type state int
|
type state int
|
||||||
@@ -24,6 +26,7 @@ type (
|
|||||||
devicesMsg []guard.Device
|
devicesMsg []guard.Device
|
||||||
daemonStatusMsg string
|
daemonStatusMsg string
|
||||||
actionMsg struct{ err error }
|
actionMsg struct{ err error }
|
||||||
|
nixRuleMsg struct{ rule string }
|
||||||
)
|
)
|
||||||
|
|
||||||
type Model struct {
|
type Model struct {
|
||||||
@@ -36,8 +39,12 @@ type Model struct {
|
|||||||
height int
|
height int
|
||||||
notice string
|
notice string
|
||||||
selectedDev *guard.Device
|
selectedDev *guard.Device
|
||||||
|
rulesManaged bool
|
||||||
|
pendingRules []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m Model) PendingRules() []string { return m.pendingRules }
|
||||||
|
|
||||||
func New() Model {
|
func New() Model {
|
||||||
l := list.New(nil, deviceDelegate{}, 0, 0)
|
l := list.New(nil, deviceDelegate{}, 0, 0)
|
||||||
l.SetShowHelp(false)
|
l.SetShowHelp(false)
|
||||||
@@ -45,30 +52,59 @@ func New() Model {
|
|||||||
l.SetShowStatusBar(true)
|
l.SetShowStatusBar(true)
|
||||||
l.SetShowTitle(false)
|
l.SetShowTitle(false)
|
||||||
l.DisableQuitKeybindings()
|
l.DisableQuitKeybindings()
|
||||||
// free j/k for our shortcuts
|
|
||||||
l.KeyMap.CursorUp = key.NewBinding(key.WithKeys("up", "k"), key.WithHelp("↑/k", "up"))
|
l.KeyMap.CursorUp = key.NewBinding(key.WithKeys("up", "k"), key.WithHelp("↑/k", "up"))
|
||||||
l.KeyMap.CursorDown = key.NewBinding(key.WithKeys("down"), key.WithHelp("↓", "down"))
|
l.KeyMap.CursorDown = key.NewBinding(key.WithKeys("down", "j"), key.WithHelp("↓/j", "down"))
|
||||||
l.FilterInput.PromptStyle = lipgloss.NewStyle().Foreground(colorAccent)
|
|
||||||
l.FilterInput.Cursor.Style = lipgloss.NewStyle().Foreground(colorAccent)
|
l.Styles = list.DefaultStyles(true)
|
||||||
|
filterStyles := textinput.DefaultStyles(true)
|
||||||
|
filterStyles.Focused.Prompt = filterStyles.Focused.Prompt.Foreground(colorAccent)
|
||||||
|
filterStyles.Blurred.Prompt = filterStyles.Blurred.Prompt.Foreground(colorAccent)
|
||||||
|
l.Styles.Filter = filterStyles
|
||||||
|
|
||||||
|
h := help.New()
|
||||||
|
h.Styles = help.DefaultStyles(true)
|
||||||
|
|
||||||
|
rulesManaged := guard.IsRulesManaged()
|
||||||
|
notice := ""
|
||||||
|
if rulesManaged {
|
||||||
|
notice = "Rules managed by NixOS config: permanent actions will print NixOS rules on exit."
|
||||||
|
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: help.New(),
|
help: h,
|
||||||
|
rulesManaged: rulesManaged,
|
||||||
|
notice: notice,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeActionList() list.Model {
|
func makeActionList(rulesManaged bool) list.Model {
|
||||||
items := []list.Item{
|
var items []list.Item
|
||||||
actionItem{"allow", guard.AllowDevice, false, guard.Allowed},
|
if rulesManaged {
|
||||||
actionItem{"allow (permanent)", guard.AllowDevice, true, guard.Allowed},
|
items = []list.Item{
|
||||||
actionItem{"block", guard.BlockDevice, false, guard.Blocked},
|
actionItem{"allow", guard.AllowDevice, false, guard.Allowed, false},
|
||||||
actionItem{"block (permanent)", guard.BlockDevice, true, guard.Blocked},
|
actionItem{"allow (perm)", nil, true, guard.Allowed, true},
|
||||||
actionItem{"reject", guard.RejectDevice, false, guard.Rejected},
|
actionItem{"block", guard.BlockDevice, false, guard.Blocked, false},
|
||||||
actionItem{"reject (permanent)", guard.RejectDevice, true, guard.Rejected},
|
actionItem{"block (perm)", nil, true, guard.Blocked, true},
|
||||||
|
actionItem{"reject", guard.RejectDevice, false, guard.Rejected, false},
|
||||||
|
actionItem{"reject (perm)", nil, true, guard.Rejected, true},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
items = []list.Item{
|
||||||
|
actionItem{"allow", guard.AllowDevice, false, guard.Allowed, false},
|
||||||
|
actionItem{"allow (permanent)", guard.AllowDevice, true, guard.Allowed, false},
|
||||||
|
actionItem{"block", guard.BlockDevice, false, guard.Blocked, false},
|
||||||
|
actionItem{"block (permanent)", guard.BlockDevice, true, guard.Blocked, false},
|
||||||
|
actionItem{"reject", guard.RejectDevice, false, guard.Rejected, false},
|
||||||
|
actionItem{"reject (permanent)", guard.RejectDevice, true, guard.Rejected, false},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
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)
|
||||||
@@ -87,7 +123,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
case tea.WindowSizeMsg:
|
case tea.WindowSizeMsg:
|
||||||
m.width = msg.Width
|
m.width = msg.Width
|
||||||
m.height = msg.Height
|
m.height = msg.Height
|
||||||
m.help.Width = msg.Width
|
m.help.SetWidth(msg.Width)
|
||||||
m.list.SetSize(msg.Width, m.listHeight())
|
m.list.SetSize(msg.Width, m.listHeight())
|
||||||
m.updateActionListSize()
|
m.updateActionListSize()
|
||||||
return m, nil
|
return m, nil
|
||||||
@@ -107,28 +143,50 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
m.daemonStatus = string(msg)
|
m.daemonStatus = string(msg)
|
||||||
return m, nil
|
return m, nil
|
||||||
|
|
||||||
|
case nixRuleMsg:
|
||||||
|
m.state = stateList
|
||||||
|
m.selectedDev = nil
|
||||||
|
m.pendingRules = append(m.pendingRules, msg.rule)
|
||||||
|
count := len(m.pendingRules)
|
||||||
|
if count == 1 {
|
||||||
|
m.notice = "1 NixOS rule queued (printed on exit)"
|
||||||
|
} else {
|
||||||
|
m.notice = fmt.Sprintf("%d NixOS rules queued (printed on exit)", count)
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
|
||||||
case actionMsg:
|
case actionMsg:
|
||||||
m.state = stateList
|
m.state = stateList
|
||||||
m.selectedDev = nil
|
m.selectedDev = nil
|
||||||
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
|
||||||
|
|
||||||
case tea.KeyMsg:
|
case tea.KeyPressMsg:
|
||||||
if m.state == statePopup {
|
if m.state == statePopup {
|
||||||
return m.updatePopup(msg)
|
return m.updatePopup(msg)
|
||||||
}
|
}
|
||||||
return m.updateList(msg)
|
return m.updateList(msg)
|
||||||
|
|
||||||
|
case tea.MouseClickMsg:
|
||||||
|
if m.state == statePopup {
|
||||||
|
var cmd tea.Cmd
|
||||||
|
m.actionList, cmd = m.actionList.Update(msg)
|
||||||
|
return m, cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
case tea.MouseWheelMsg:
|
||||||
|
return m.updateMouseWheel(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.state == stateList {
|
if m.state == stateList {
|
||||||
@@ -140,7 +198,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Model) updateList(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
func (m Model) updateList(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
|
||||||
if msg.String() == "ctrl+c" {
|
if msg.String() == "ctrl+c" {
|
||||||
return m, tea.Quit
|
return m, tea.Quit
|
||||||
}
|
}
|
||||||
@@ -150,7 +208,7 @@ func (m Model) updateList(msg tea.KeyMsg) (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
|
||||||
@@ -165,18 +223,11 @@ func (m Model) updateList(msg tea.KeyMsg) (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
|
||||||
@@ -184,7 +235,7 @@ func (m Model) updateList(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
|||||||
return m, cmd
|
return m, cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Model) updatePopup(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
func (m Model) updatePopup(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
|
||||||
switch {
|
switch {
|
||||||
case key.Matches(msg, cancelKey):
|
case key.Matches(msg, cancelKey):
|
||||||
m.state = stateList
|
m.state = stateList
|
||||||
@@ -193,6 +244,10 @@ func (m Model) updatePopup(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
|||||||
case key.Matches(msg, listKeys.Open):
|
case key.Matches(msg, listKeys.Open):
|
||||||
if item := m.actionList.SelectedItem(); item != nil {
|
if item := m.actionList.SelectedItem(); item != nil {
|
||||||
a := item.(actionItem)
|
a := item.(actionItem)
|
||||||
|
if a.nixos && m.selectedDev != nil {
|
||||||
|
rule := guard.NixOSRule(*m.selectedDev, a.status)
|
||||||
|
return m, func() tea.Msg { return nixRuleMsg{rule: rule} }
|
||||||
|
}
|
||||||
return m, doAction(m.selectedDev.ID, a.fn, a.permanent)
|
return m, doAction(m.selectedDev.ID, a.fn, a.permanent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -201,7 +256,16 @@ func (m Model) updatePopup(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
|||||||
return m, cmd
|
return m, cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Model) View() string {
|
func (m Model) View() tea.View {
|
||||||
|
return tea.View{
|
||||||
|
Content: m.renderContent(),
|
||||||
|
AltScreen: true,
|
||||||
|
WindowTitle: "USBGuard TUI",
|
||||||
|
MouseMode: tea.MouseModeCellMotion,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m Model) renderContent() string {
|
||||||
header := m.renderHeader()
|
header := m.renderHeader()
|
||||||
notice := m.renderNotice()
|
notice := m.renderNotice()
|
||||||
listView := strings.TrimRight(m.list.View(), "\n")
|
listView := strings.TrimRight(m.list.View(), "\n")
|
||||||
@@ -238,11 +302,16 @@ func (m Model) renderActionSelect() string {
|
|||||||
color := statusColors[dev.Status]
|
color := statusColors[dev.Status]
|
||||||
innerW := m.actionListInnerWidth()
|
innerW := m.actionListInnerWidth()
|
||||||
|
|
||||||
title := popupTitleStyle.Copy().Foreground(color).Width(innerW).Render(dev.Name)
|
title := popupTitleStyle.Foreground(color).Width(innerW).Render(dev.Name)
|
||||||
hint := lipgloss.NewStyle().Foreground(colorMuted).Width(innerW).Render("↑↓ navigate enter confirm esc cancel")
|
hint := lipgloss.NewStyle().Foreground(colorMuted).Width(innerW).Render("↑↓ navigate enter confirm esc cancel")
|
||||||
|
|
||||||
content := strings.Join([]string{title, m.actionList.View(), "", hint}, "\n")
|
parts := []string{title, m.actionList.View(), ""}
|
||||||
return popupStyle.Width(innerW).Render(content)
|
if m.rulesManaged {
|
||||||
|
nixosHint := lipgloss.NewStyle().Foreground(colorMuted).Width(innerW).Render("[NixOS: perm rules printed on exit]")
|
||||||
|
parts = append(parts, nixosHint)
|
||||||
|
}
|
||||||
|
parts = append(parts, hint)
|
||||||
|
return popupStyle.Width(innerW).Render(strings.Join(parts, "\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m Model) popupOuterWidth() int {
|
func (m Model) popupOuterWidth() int {
|
||||||
@@ -260,15 +329,30 @@ 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 will print NixOS rules on exit."
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m Model) actionItemCount() int {
|
||||||
|
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; +1 for NixOS footer
|
||||||
available := m.height - 7 - 2 // 2 lines margin
|
overhead := 7
|
||||||
|
if m.rulesManaged {
|
||||||
|
overhead = 8
|
||||||
|
}
|
||||||
|
available := m.height - overhead - 2 // 2 lines margin
|
||||||
if available >= items {
|
if available >= items {
|
||||||
m.actionList.SetShowPagination(false)
|
m.actionList.SetShowPagination(false)
|
||||||
m.actionList.SetSize(innerW, items)
|
m.actionList.SetSize(innerW, items)
|
||||||
@@ -317,3 +401,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
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package ui
|
|||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/charmbracelet/lipgloss"
|
"charm.land/lipgloss/v2"
|
||||||
"github.com/charmbracelet/x/ansi"
|
"github.com/charmbracelet/x/ansi"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
+14
-14
@@ -1,28 +1,30 @@
|
|||||||
package ui
|
package ui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"image/color"
|
||||||
|
|
||||||
|
"charm.land/lipgloss/v2"
|
||||||
"github.com/anotherhadi/usbguard-tui/internal/guard"
|
"github.com/anotherhadi/usbguard-tui/internal/guard"
|
||||||
"github.com/charmbracelet/lipgloss"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
colorAllowed = lipgloss.Color("28")
|
colorAllowed color.Color = lipgloss.Color("28")
|
||||||
colorAllowedSelected = lipgloss.Color("42")
|
colorAllowedSelected color.Color = lipgloss.Color("42")
|
||||||
colorBlocked = lipgloss.Color("124")
|
colorBlocked color.Color = lipgloss.Color("124")
|
||||||
colorBlockedSelected = lipgloss.Color("196")
|
colorBlockedSelected color.Color = lipgloss.Color("196")
|
||||||
colorRejected = lipgloss.Color("130")
|
colorRejected color.Color = lipgloss.Color("130")
|
||||||
colorRejectedSelected = lipgloss.Color("214")
|
colorRejectedSelected color.Color = lipgloss.Color("214")
|
||||||
colorMuted = lipgloss.Color("240")
|
colorMuted color.Color = lipgloss.Color("240")
|
||||||
colorAccent = lipgloss.Color("99")
|
colorAccent color.Color = lipgloss.Color("99")
|
||||||
)
|
)
|
||||||
|
|
||||||
var statusColors = map[guard.Status]lipgloss.Color{
|
var statusColors = map[guard.Status]color.Color{
|
||||||
guard.Allowed: colorAllowed,
|
guard.Allowed: colorAllowed,
|
||||||
guard.Blocked: colorBlocked,
|
guard.Blocked: colorBlocked,
|
||||||
guard.Rejected: colorRejected,
|
guard.Rejected: colorRejected,
|
||||||
}
|
}
|
||||||
|
|
||||||
var statusColorsSelected = map[guard.Status]lipgloss.Color{
|
var statusColorsSelected = map[guard.Status]color.Color{
|
||||||
guard.Allowed: colorAllowedSelected,
|
guard.Allowed: colorAllowedSelected,
|
||||||
guard.Blocked: colorBlockedSelected,
|
guard.Blocked: colorBlockedSelected,
|
||||||
guard.Rejected: colorRejectedSelected,
|
guard.Rejected: colorRejectedSelected,
|
||||||
@@ -46,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)
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -4,20 +4,38 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "charm.land/bubbletea/v2"
|
||||||
"github.com/anotherhadi/usbguard-tui/internal/guard"
|
"github.com/anotherhadi/usbguard-tui/internal/guard"
|
||||||
"github.com/anotherhadi/usbguard-tui/internal/ui"
|
"github.com/anotherhadi/usbguard-tui/internal/ui"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var version = "dev"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
if len(os.Args) > 1 && (os.Args[1] == "--version" || os.Args[1] == "-version" || os.Args[1] == "-v") {
|
||||||
|
fmt.Println("usbguard-tui", version)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if err := guard.Check(); err != nil {
|
if err := guard.Check(); err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
p := tea.NewProgram(ui.New(), tea.WithAltScreen())
|
p := tea.NewProgram(ui.New())
|
||||||
if _, err := p.Run(); err != nil {
|
m, err := p.Run()
|
||||||
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
if fm, ok := m.(ui.Model); ok {
|
||||||
|
if rules := fm.PendingRules(); len(rules) > 0 {
|
||||||
|
fmt.Println("# Add to your NixOS configuration:")
|
||||||
|
fmt.Println("services.usbguard.rules = lib.mkAfter ''")
|
||||||
|
for _, rule := range rules {
|
||||||
|
fmt.Println(" ", rule)
|
||||||
|
}
|
||||||
|
fmt.Println("'';")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user