initial commit

This commit is contained in:
Karol Broda
2025-12-16 22:42:49 +01:00
commit 371f4d13a6
61 changed files with 6872 additions and 0 deletions

53
internal/tui/helpers.go Normal file
View File

@@ -0,0 +1,53 @@
package tui
import (
"fmt"
"regexp"
"snitch/internal/collector"
"strings"
)
func truncate(s string, max int) string {
if len(s) <= max {
return s
}
if max <= 2 {
return s[:max]
}
return s[:max-1] + "…"
}
var ansiRegex = regexp.MustCompile(`\x1b\[[0-9;]*m`)
func stripAnsi(s string) string {
return ansiRegex.ReplaceAllString(s, "")
}
func containsIgnoreCase(s, substr string) bool {
return strings.Contains(strings.ToLower(s), strings.ToLower(substr))
}
func sortFieldLabel(f collector.SortField) string {
switch f {
case collector.SortByLport:
return "port"
case collector.SortByProcess:
return "proc"
case collector.SortByPID:
return "pid"
case collector.SortByState:
return "state"
case collector.SortByProto:
return "proto"
default:
return "port"
}
}
func formatRemote(addr string, port int) string {
if addr == "" || addr == "*" || port == 0 {
return "-"
}
return fmt.Sprintf("%s:%d", addr, port)
}

185
internal/tui/keys.go Normal file
View File

@@ -0,0 +1,185 @@
package tui
import (
"snitch/internal/collector"
tea "github.com/charmbracelet/bubbletea"
)
func (m model) handleKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
// search mode captures all input
if m.searchActive {
return m.handleSearchKey(msg)
}
// detail view only allows closing
if m.showDetail {
return m.handleDetailKey(msg)
}
// help view only allows closing
if m.showHelp {
return m.handleHelpKey(msg)
}
return m.handleNormalKey(msg)
}
func (m model) handleSearchKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
switch msg.String() {
case "esc":
m.searchActive = false
m.searchQuery = ""
case "enter":
m.searchActive = false
m.cursor = 0
case "backspace":
if len(m.searchQuery) > 0 {
m.searchQuery = m.searchQuery[:len(m.searchQuery)-1]
}
default:
if len(msg.String()) == 1 {
m.searchQuery += msg.String()
}
}
return m, nil
}
func (m model) handleDetailKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
switch msg.String() {
case "esc", "enter", "q":
m.showDetail = false
m.selected = nil
}
return m, nil
}
func (m model) handleHelpKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
switch msg.String() {
case "esc", "enter", "q", "?":
m.showHelp = false
}
return m, nil
}
func (m model) handleNormalKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
switch msg.String() {
case "q", "ctrl+c":
return m, tea.Sequence(tea.ShowCursor, tea.Quit)
// navigation
case "j", "down":
m.moveCursor(1)
case "k", "up":
m.moveCursor(-1)
case "g":
m.cursor = 0
case "G":
visible := m.visibleConnections()
if len(visible) > 0 {
m.cursor = len(visible) - 1
}
case "ctrl+d":
m.moveCursor(m.pageSize() / 2)
case "ctrl+u":
m.moveCursor(-m.pageSize() / 2)
case "ctrl+f", "pgdown":
m.moveCursor(m.pageSize())
case "ctrl+b", "pgup":
m.moveCursor(-m.pageSize())
// filter toggles
case "t":
m.showTCP = !m.showTCP
m.clampCursor()
case "u":
m.showUDP = !m.showUDP
m.clampCursor()
case "l":
m.showListening = !m.showListening
m.clampCursor()
case "e":
m.showEstablished = !m.showEstablished
m.clampCursor()
case "o":
m.showOther = !m.showOther
m.clampCursor()
case "a":
m.showTCP = true
m.showUDP = true
m.showListening = true
m.showEstablished = true
m.showOther = true
// sorting
case "s":
m.cycleSort()
case "S":
m.sortReverse = !m.sortReverse
m.applySorting()
// search
case "/":
m.searchActive = true
m.searchQuery = ""
// actions
case "enter", " ":
visible := m.visibleConnections()
if m.cursor < len(visible) {
conn := visible[m.cursor]
m.selected = &conn
m.showDetail = true
}
case "r":
return m, m.fetchData()
case "?":
m.showHelp = true
}
return m, nil
}
func (m *model) moveCursor(delta int) {
visible := m.visibleConnections()
m.cursor += delta
if m.cursor < 0 {
m.cursor = 0
}
if m.cursor >= len(visible) {
m.cursor = len(visible) - 1
}
if m.cursor < 0 {
m.cursor = 0
}
}
func (m model) pageSize() int {
size := m.height - 6
if size < 1 {
return 10
}
return size
}
func (m *model) cycleSort() {
fields := []collector.SortField{
collector.SortByLport,
collector.SortByProcess,
collector.SortByPID,
collector.SortByState,
collector.SortByProto,
}
for i, f := range fields {
if f == m.sortField {
m.sortField = fields[(i+1)%len(fields)]
m.applySorting()
return
}
}
m.sortField = collector.SortByLport
m.applySorting()
}

35
internal/tui/messages.go Normal file
View File

@@ -0,0 +1,35 @@
package tui
import (
"snitch/internal/collector"
"time"
tea "github.com/charmbracelet/bubbletea"
)
type tickMsg time.Time
type dataMsg struct {
connections []collector.Connection
}
type errMsg struct {
err error
}
func (m model) tick() tea.Cmd {
return tea.Tick(m.interval, func(t time.Time) tea.Msg {
return tickMsg(t)
})
}
func (m model) fetchData() tea.Cmd {
return func() tea.Msg {
conns, err := collector.GetConnections()
if err != nil {
return errMsg{err}
}
return dataMsg{connections: conns}
}
}

220
internal/tui/model.go Normal file
View File

@@ -0,0 +1,220 @@
package tui
import (
"snitch/internal/collector"
"snitch/internal/theme"
"time"
tea "github.com/charmbracelet/bubbletea"
)
type model struct {
connections []collector.Connection
cursor int
width int
height int
// filtering
showTCP bool
showUDP bool
showListening bool
showEstablished bool
showOther bool
searchQuery string
searchActive bool
// sorting
sortField collector.SortField
sortReverse bool
// ui state
theme *theme.Theme
showHelp bool
showDetail bool
selected *collector.Connection
interval time.Duration
lastRefresh time.Time
err error
}
type Options struct {
Theme string
Interval time.Duration
TCP bool
UDP bool
Listening bool
Established bool
Other bool
FilterSet bool // true if user specified any filter flags
}
func New(opts Options) model {
interval := opts.Interval
if interval == 0 {
interval = time.Second
}
// default: show everything
showTCP := true
showUDP := true
showListening := true
showEstablished := true
showOther := true
// if user specified filters, use those instead
if opts.FilterSet {
showTCP = opts.TCP
showUDP = opts.UDP
showListening = opts.Listening
showEstablished = opts.Established
showOther = opts.Other
// if only proto filters set, show all states
if !opts.Listening && !opts.Established && !opts.Other {
showListening = true
showEstablished = true
showOther = true
}
// if only state filters set, show all protos
if !opts.TCP && !opts.UDP {
showTCP = true
showUDP = true
}
}
return model{
connections: []collector.Connection{},
showTCP: showTCP,
showUDP: showUDP,
showListening: showListening,
showEstablished: showEstablished,
showOther: showOther,
sortField: collector.SortByLport,
theme: theme.GetTheme(opts.Theme),
interval: interval,
lastRefresh: time.Now(),
}
}
func (m model) Init() tea.Cmd {
return tea.Batch(
tea.HideCursor,
m.fetchData(),
m.tick(),
)
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
return m, nil
case tea.KeyMsg:
return m.handleKey(msg)
case tickMsg:
return m, tea.Batch(m.fetchData(), m.tick())
case dataMsg:
m.connections = msg.connections
m.lastRefresh = time.Now()
m.applySorting()
m.clampCursor()
return m, nil
case errMsg:
m.err = msg.err
return m, nil
}
return m, nil
}
func (m model) View() string {
if m.err != nil {
return m.renderError()
}
if m.showHelp {
return m.renderHelp()
}
if m.showDetail && m.selected != nil {
return m.renderDetail()
}
return m.renderMain()
}
func (m *model) applySorting() {
direction := collector.SortAsc
if m.sortReverse {
direction = collector.SortDesc
}
collector.SortConnections(m.connections, collector.SortOptions{
Field: m.sortField,
Direction: direction,
})
}
func (m *model) clampCursor() {
visible := m.visibleConnections()
if m.cursor >= len(visible) {
m.cursor = len(visible) - 1
}
if m.cursor < 0 {
m.cursor = 0
}
}
func (m model) visibleConnections() []collector.Connection {
var result []collector.Connection
for _, c := range m.connections {
if !m.matchesFilters(c) {
continue
}
if m.searchQuery != "" && !m.matchesSearch(c) {
continue
}
result = append(result, c)
}
return result
}
func (m model) matchesFilters(c collector.Connection) bool {
isTCP := c.Proto == "tcp" || c.Proto == "tcp6"
isUDP := c.Proto == "udp" || c.Proto == "udp6"
if isTCP && !m.showTCP {
return false
}
if isUDP && !m.showUDP {
return false
}
isListening := c.State == "LISTEN"
isEstablished := c.State == "ESTABLISHED"
isOther := !isListening && !isEstablished
if isListening && !m.showListening {
return false
}
if isEstablished && !m.showEstablished {
return false
}
if isOther && !m.showOther {
return false
}
return true
}
func (m model) matchesSearch(c collector.Connection) bool {
return containsIgnoreCase(c.Process, m.searchQuery) ||
containsIgnoreCase(c.Laddr, m.searchQuery) ||
containsIgnoreCase(c.Raddr, m.searchQuery) ||
containsIgnoreCase(c.User, m.searchQuery) ||
containsIgnoreCase(c.Proto, m.searchQuery) ||
containsIgnoreCase(c.State, m.searchQuery)
}

352
internal/tui/view.go Normal file
View File

@@ -0,0 +1,352 @@
package tui
import (
"fmt"
"snitch/internal/collector"
"strings"
"time"
)
func (m model) renderMain() string {
var b strings.Builder
b.WriteString("\n")
b.WriteString(m.renderTitle())
b.WriteString("\n")
b.WriteString(m.renderFilters())
b.WriteString("\n\n")
b.WriteString(m.renderTableHeader())
b.WriteString(m.renderSeparator())
b.WriteString(m.renderConnections())
b.WriteString("\n")
b.WriteString(m.renderStatusLine())
return b.String()
}
func (m model) renderTitle() string {
visible := m.visibleConnections()
total := len(m.connections)
left := m.theme.Styles.Header.Render("snitch")
ago := time.Since(m.lastRefresh).Round(time.Millisecond * 100)
right := m.theme.Styles.Normal.Render(fmt.Sprintf("%d/%d connections ↻ %s", len(visible), total, formatDuration(ago)))
w := m.safeWidth()
gap := w - len(stripAnsi(left)) - len(stripAnsi(right)) - 2
if gap < 0 {
gap = 0
}
return " " + left + strings.Repeat(" ", gap) + right
}
func (m model) renderFilters() string {
var parts []string
if m.showTCP {
parts = append(parts, m.theme.Styles.Success.Render("tcp"))
} else {
parts = append(parts, m.theme.Styles.Normal.Render("tcp"))
}
if m.showUDP {
parts = append(parts, m.theme.Styles.Success.Render("udp"))
} else {
parts = append(parts, m.theme.Styles.Normal.Render("udp"))
}
parts = append(parts, m.theme.Styles.Border.Render("│"))
if m.showListening {
parts = append(parts, m.theme.Styles.Success.Render("listen"))
} else {
parts = append(parts, m.theme.Styles.Normal.Render("listen"))
}
if m.showEstablished {
parts = append(parts, m.theme.Styles.Success.Render("estab"))
} else {
parts = append(parts, m.theme.Styles.Normal.Render("estab"))
}
if m.showOther {
parts = append(parts, m.theme.Styles.Success.Render("other"))
} else {
parts = append(parts, m.theme.Styles.Normal.Render("other"))
}
left := " " + strings.Join(parts, " ")
sortLabel := sortFieldLabel(m.sortField)
sortDir := "↑"
if m.sortReverse {
sortDir = "↓"
}
var right string
if m.searchActive {
right = m.theme.Styles.Warning.Render(fmt.Sprintf("/%s▌", m.searchQuery))
} else if m.searchQuery != "" {
right = m.theme.Styles.Normal.Render(fmt.Sprintf("filter: %s", m.searchQuery))
} else {
right = m.theme.Styles.Normal.Render(fmt.Sprintf("sort: %s %s", sortLabel, sortDir))
}
w := m.safeWidth()
gap := w - len(stripAnsi(left)) - len(stripAnsi(right)) - 2
if gap < 0 {
gap = 0
}
return left + strings.Repeat(" ", gap) + right + " "
}
func (m model) renderTableHeader() string {
cols := m.columnWidths()
header := fmt.Sprintf(" %-*s %-*s %-*s %-*s %-*s %s",
cols.process, "PROCESS",
cols.port, "PORT",
cols.proto, "PROTO",
cols.state, "STATE",
cols.local, "LOCAL",
"REMOTE")
return m.theme.Styles.Header.Render(header) + "\n"
}
func (m model) renderSeparator() string {
w := m.width - 4
if w < 1 {
w = 76
}
line := " " + strings.Repeat("─", w)
return m.theme.Styles.Border.Render(line) + "\n"
}
func (m model) renderConnections() string {
var b strings.Builder
visible := m.visibleConnections()
pageSize := m.pageSize()
if len(visible) == 0 {
empty := "\n " + m.theme.Styles.Normal.Render("no connections match filters") + "\n"
return empty
}
start := m.scrollOffset(pageSize, len(visible))
for i := 0; i < pageSize; i++ {
idx := start + i
if idx >= len(visible) {
b.WriteString("\n")
continue
}
isSelected := idx == m.cursor
b.WriteString(m.renderRow(visible[idx], isSelected))
}
return b.String()
}
func (m model) renderRow(c collector.Connection, selected bool) string {
cols := m.columnWidths()
indicator := " "
if selected {
indicator = m.theme.Styles.Success.Render("▸ ")
}
process := truncate(c.Process, cols.process)
if process == "" {
process = ""
}
port := fmt.Sprintf("%d", c.Lport)
proto := c.Proto
state := c.State
if state == "" {
state = ""
}
local := c.Laddr
if local == "*" || local == "" {
local = "*"
}
remote := formatRemote(c.Raddr, c.Rport)
// apply styling
protoStyled := m.theme.Styles.GetProtoStyle(proto).Render(fmt.Sprintf("%-*s", cols.proto, proto))
stateStyled := m.theme.Styles.GetStateStyle(state).Render(fmt.Sprintf("%-*s", cols.state, truncate(state, cols.state)))
row := fmt.Sprintf("%s%-*s %-*s %s %s %-*s %s",
indicator,
cols.process, process,
cols.port, port,
protoStyled,
stateStyled,
cols.local, truncate(local, cols.local),
truncate(remote, cols.remote))
if selected {
return m.theme.Styles.Selected.Render(row) + "\n"
}
return m.theme.Styles.Normal.Render(row) + "\n"
}
func (m model) renderStatusLine() string {
left := " " + m.theme.Styles.Normal.Render("t/u proto l/e/o state s sort / search ? help q quit")
return left
}
func (m model) renderError() string {
return fmt.Sprintf("\n %s\n\n press q to quit\n",
m.theme.Styles.Error.Render(fmt.Sprintf("error: %v", m.err)))
}
func (m model) renderHelp() string {
help := `
navigation
──────────
j/k ↑/↓ move cursor
g/G jump to top/bottom
ctrl+d/u half page down/up
enter show connection details
filters
───────
t toggle tcp
u toggle udp
l toggle listening
e toggle established
o toggle other states
a reset all filters
sorting
───────
s cycle sort field
S reverse sort order
other
─────
/ search
r refresh now
q quit
press ? or esc to close
`
return m.theme.Styles.Normal.Render(help)
}
func (m model) renderDetail() string {
if m.selected == nil {
return ""
}
c := m.selected
var b strings.Builder
b.WriteString("\n")
b.WriteString(" " + m.theme.Styles.Header.Render("connection details") + "\n")
b.WriteString(" " + m.theme.Styles.Border.Render(strings.Repeat("─", 40)) + "\n\n")
fields := []struct {
label string
value string
}{
{"process", c.Process},
{"pid", fmt.Sprintf("%d", c.PID)},
{"user", c.User},
{"protocol", c.Proto},
{"state", c.State},
{"local", fmt.Sprintf("%s:%d", c.Laddr, c.Lport)},
{"remote", fmt.Sprintf("%s:%d", c.Raddr, c.Rport)},
{"interface", c.Interface},
{"inode", fmt.Sprintf("%d", c.Inode)},
}
for _, f := range fields {
val := f.value
if val == "" || val == "0" || val == ":0" {
val = ""
}
line := fmt.Sprintf(" %-12s %s\n", m.theme.Styles.Header.Render(f.label), val)
b.WriteString(line)
}
b.WriteString("\n")
b.WriteString(" " + m.theme.Styles.Normal.Render("press esc to close") + "\n")
return b.String()
}
func (m model) scrollOffset(pageSize, total int) int {
if total <= pageSize {
return 0
}
// keep cursor roughly centered
offset := m.cursor - pageSize/2
if offset < 0 {
offset = 0
}
if offset > total-pageSize {
offset = total - pageSize
}
return offset
}
type columns struct {
process int
port int
proto int
state int
local int
remote int
}
func (m model) columnWidths() columns {
available := m.safeWidth() - 16
c := columns{
process: 16,
port: 6,
proto: 5,
state: 11,
local: 15,
remote: 20,
}
used := c.process + c.port + c.proto + c.state + c.local + c.remote
extra := available - used
if extra > 0 {
c.process += extra / 3
c.remote += extra - extra/3
}
return c
}
func (m model) safeWidth() int {
if m.width < 80 {
return 80
}
return m.width
}
func formatDuration(d time.Duration) string {
if d < time.Second {
return fmt.Sprintf("%dms", d.Milliseconds())
}
if d < time.Minute {
return fmt.Sprintf("%.1fs", d.Seconds())
}
return fmt.Sprintf("%.0fm", d.Minutes())
}