From c58f2a233d0c6205982dde2b5c402dffeb51b3de Mon Sep 17 00:00:00 2001 From: Karol Broda Date: Tue, 23 Dec 2025 16:24:29 +0100 Subject: [PATCH] feat: add address and port resolution options --- cmd/cli_test.go | 3 +- cmd/ls.go | 16 +++-- cmd/root.go | 2 + cmd/runtime.go | 16 +++-- cmd/top.go | 14 ++-- internal/tui/helpers.go | 8 --- internal/tui/keys.go | 22 ++++++ internal/tui/model.go | 24 ++++--- internal/tui/model_test.go | 129 +++++++++++++++++++++++++++++++++ internal/tui/view.go | 144 +++++++++++++++++++++++++++++++------ 10 files changed, 324 insertions(+), 54 deletions(-) diff --git a/cmd/cli_test.go b/cmd/cli_test.go index 574daaf..392d736 100644 --- a/cmd/cli_test.go +++ b/cmd/cli_test.go @@ -364,7 +364,8 @@ func resetGlobalFlags() { filterIPv4 = false filterIPv6 = false colorMode = "auto" - numeric = false + resolveAddrs = true + resolvePorts = false } // TestEnvironmentVariables tests that environment variables are properly handled diff --git a/cmd/ls.go b/cmd/ls.go index 013b748..93cd7ad 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -30,7 +30,8 @@ var ( sortBy string fields string colorMode string - numeric bool + resolveAddrs bool + resolvePorts bool plainOutput bool ) @@ -51,7 +52,7 @@ Available filters: } func runListCommand(outputFormat string, args []string) { - rt, err := NewRuntime(args, colorMode, numeric) + rt, err := NewRuntime(args, colorMode) if err != nil { log.Fatal(err) } @@ -98,14 +99,18 @@ func getFieldMap(c collector.Connection) map[string]string { lport := strconv.Itoa(c.Lport) rport := strconv.Itoa(c.Rport) - // Apply name resolution if not in numeric mode - if !numeric { + // apply address resolution + if resolveAddrs { if resolvedLaddr := resolver.ResolveAddr(c.Laddr); resolvedLaddr != c.Laddr { laddr = resolvedLaddr } if resolvedRaddr := resolver.ResolveAddr(c.Raddr); resolvedRaddr != c.Raddr && c.Raddr != "*" && c.Raddr != "" { raddr = resolvedRaddr } + } + + // apply port resolution + if resolvePorts { if resolvedLport := resolver.ResolvePort(c.Lport, c.Proto); resolvedLport != strconv.Itoa(c.Lport) { lport = resolvedLport } @@ -395,7 +400,8 @@ func init() { lsCmd.Flags().StringVarP(&sortBy, "sort", "s", cfg.Defaults.SortBy, "Sort by column (e.g., pid:desc)") lsCmd.Flags().StringVarP(&fields, "fields", "f", strings.Join(cfg.Defaults.Fields, ","), "Comma-separated list of fields to show") lsCmd.Flags().StringVar(&colorMode, "color", cfg.Defaults.Color, "Color mode (auto, always, never)") - lsCmd.Flags().BoolVarP(&numeric, "numeric", "n", cfg.Defaults.Numeric, "Don't resolve hostnames") + lsCmd.Flags().BoolVar(&resolveAddrs, "resolve-addrs", !cfg.Defaults.Numeric, "Resolve IP addresses to hostnames") + lsCmd.Flags().BoolVar(&resolvePorts, "resolve-ports", false, "Resolve port numbers to service names") lsCmd.Flags().BoolVarP(&plainOutput, "plain", "p", false, "Plain output (parsable, no styling)") // shared filter flags diff --git a/cmd/root.go b/cmd/root.go index 4f7bbb5..c16be75 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -44,6 +44,8 @@ func init() { cfg := config.Get() rootCmd.Flags().StringVar(&topTheme, "theme", cfg.Defaults.Theme, "Theme for TUI (dark, light, mono, auto)") rootCmd.Flags().DurationVarP(&topInterval, "interval", "i", 0, "Refresh interval (default 1s)") + rootCmd.Flags().BoolVar(&topResolveAddrs, "resolve-addrs", !cfg.Defaults.Numeric, "Resolve IP addresses to hostnames") + rootCmd.Flags().BoolVar(&topResolvePorts, "resolve-ports", false, "Resolve port numbers to service names") // shared filter flags for root command addFilterFlags(rootCmd) diff --git a/cmd/runtime.go b/cmd/runtime.go index 785adf8..cf757e5 100644 --- a/cmd/runtime.go +++ b/cmd/runtime.go @@ -20,8 +20,9 @@ type Runtime struct { Connections []collector.Connection // common settings - ColorMode string - Numeric bool + ColorMode string + ResolveAddrs bool + ResolvePorts bool } // shared filter flags - used by all commands @@ -73,7 +74,7 @@ func FetchConnections(filters collector.FilterOptions) ([]collector.Connection, } // NewRuntime creates a runtime with fetched and filtered connections. -func NewRuntime(args []string, colorMode string, numeric bool) (*Runtime, error) { +func NewRuntime(args []string, colorMode string) (*Runtime, error) { color.Init(colorMode) filters, err := BuildFilters(args) @@ -87,10 +88,11 @@ func NewRuntime(args []string, colorMode string, numeric bool) (*Runtime, error) } return &Runtime{ - Filters: filters, - Connections: connections, - ColorMode: colorMode, - Numeric: numeric, + Filters: filters, + Connections: connections, + ColorMode: colorMode, + ResolveAddrs: resolveAddrs, + ResolvePorts: resolvePorts, }, nil } diff --git a/cmd/top.go b/cmd/top.go index cbc873b..4546585 100644 --- a/cmd/top.go +++ b/cmd/top.go @@ -12,8 +12,10 @@ import ( // top-specific flags var ( - topTheme string - topInterval time.Duration + topTheme string + topInterval time.Duration + topResolveAddrs bool + topResolvePorts bool ) var topCmd = &cobra.Command{ @@ -28,8 +30,10 @@ var topCmd = &cobra.Command{ } opts := tui.Options{ - Theme: theme, - Interval: topInterval, + Theme: theme, + Interval: topInterval, + ResolveAddrs: topResolveAddrs, + ResolvePorts: topResolvePorts, } // if any filter flag is set, use exclusive mode @@ -58,6 +62,8 @@ func init() { // top-specific flags topCmd.Flags().StringVar(&topTheme, "theme", cfg.Defaults.Theme, "Theme for TUI (dark, light, mono, auto)") topCmd.Flags().DurationVarP(&topInterval, "interval", "i", time.Second, "Refresh interval") + topCmd.Flags().BoolVar(&topResolveAddrs, "resolve-addrs", !cfg.Defaults.Numeric, "Resolve IP addresses to hostnames") + topCmd.Flags().BoolVar(&topResolvePorts, "resolve-ports", false, "Resolve port numbers to service names") // shared filter flags addFilterFlags(topCmd) diff --git a/internal/tui/helpers.go b/internal/tui/helpers.go index b4f9049..c49daa6 100644 --- a/internal/tui/helpers.go +++ b/internal/tui/helpers.go @@ -1,7 +1,6 @@ package tui import ( - "fmt" "regexp" "github.com/karol-broda/snitch/internal/collector" "strings" @@ -44,10 +43,3 @@ func sortFieldLabel(f collector.SortField) string { } } -func formatRemote(addr string, port int) string { - if addr == "" || addr == "*" || port == 0 { - return "-" - } - return fmt.Sprintf("%s:%d", addr, port) -} - diff --git a/internal/tui/keys.go b/internal/tui/keys.go index 043571d..0921afc 100644 --- a/internal/tui/keys.go +++ b/internal/tui/keys.go @@ -210,6 +210,28 @@ func (m model) handleNormalKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) { m.showKillConfirm = true } } + + // toggle address resolution + case "n": + m.resolveAddrs = !m.resolveAddrs + if m.resolveAddrs { + m.statusMessage = "address resolution: on" + } else { + m.statusMessage = "address resolution: off" + } + m.statusExpiry = time.Now().Add(2 * time.Second) + return m, clearStatusAfter(2 * time.Second) + + // toggle port resolution + case "N": + m.resolvePorts = !m.resolvePorts + if m.resolvePorts { + m.statusMessage = "port resolution: on" + } else { + m.statusMessage = "port resolution: off" + } + m.statusExpiry = time.Now().Add(2 * time.Second) + return m, clearStatusAfter(2 * time.Second) } return m, nil diff --git a/internal/tui/model.go b/internal/tui/model.go index 77db8b5..9698df1 100644 --- a/internal/tui/model.go +++ b/internal/tui/model.go @@ -28,6 +28,10 @@ type model struct { sortField collector.SortField sortReverse bool + // display options + resolveAddrs bool // when true, resolve IP addresses to hostnames + resolvePorts bool // when true, resolve port numbers to service names + // ui state theme *theme.Theme showHelp bool @@ -50,14 +54,16 @@ type model struct { } 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 + Theme string + Interval time.Duration + TCP bool + UDP bool + Listening bool + Established bool + Other bool + FilterSet bool // true if user specified any filter flags + ResolveAddrs bool // when true, resolve IP addresses to hostnames + ResolvePorts bool // when true, resolve port numbers to service names } func New(opts Options) model { @@ -102,6 +108,8 @@ func New(opts Options) model { showEstablished: showEstablished, showOther: showOther, sortField: collector.SortByLport, + resolveAddrs: opts.ResolveAddrs, + resolvePorts: opts.ResolvePorts, theme: theme.GetTheme(opts.Theme), interval: interval, lastRefresh: time.Now(), diff --git a/internal/tui/model_test.go b/internal/tui/model_test.go index 2bc17e1..0aef3a8 100644 --- a/internal/tui/model_test.go +++ b/internal/tui/model_test.go @@ -301,3 +301,132 @@ func TestTUI_ViewRenders(t *testing.T) { } } +func TestTUI_ResolutionOptions(t *testing.T) { + // test default resolution settings + m := New(Options{Theme: "dark", Interval: time.Hour}) + + if m.resolveAddrs != false { + t.Error("expected resolveAddrs to be false by default (must be explicitly set)") + } + if m.resolvePorts != false { + t.Error("expected resolvePorts to be false by default") + } + + // test with explicit options + m2 := New(Options{ + Theme: "dark", + Interval: time.Hour, + ResolveAddrs: true, + ResolvePorts: true, + }) + + if m2.resolveAddrs != true { + t.Error("expected resolveAddrs to be true when set") + } + if m2.resolvePorts != true { + t.Error("expected resolvePorts to be true when set") + } +} + +func TestTUI_ToggleResolution(t *testing.T) { + m := New(Options{Theme: "dark", Interval: time.Hour, ResolveAddrs: true}) + + if m.resolveAddrs != true { + t.Fatal("expected resolveAddrs to be true initially") + } + + // toggle address resolution with 'n' + newModel, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'n'}}) + m = newModel.(model) + + if m.resolveAddrs != false { + t.Error("expected resolveAddrs to be false after toggle") + } + + // toggle back + newModel, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'n'}}) + m = newModel.(model) + + if m.resolveAddrs != true { + t.Error("expected resolveAddrs to be true after second toggle") + } + + // toggle port resolution with 'N' + if m.resolvePorts != false { + t.Fatal("expected resolvePorts to be false initially") + } + + newModel, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'N'}}) + m = newModel.(model) + + if m.resolvePorts != true { + t.Error("expected resolvePorts to be true after toggle") + } + + // toggle back + newModel, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'N'}}) + m = newModel.(model) + + if m.resolvePorts != false { + t.Error("expected resolvePorts to be false after second toggle") + } +} + +func TestTUI_ResolveAddrHelper(t *testing.T) { + m := New(Options{Theme: "dark", Interval: time.Hour}) + m.resolveAddrs = false + + // when resolution is off, should return original address + addr := m.resolveAddr("192.168.1.1") + if addr != "192.168.1.1" { + t.Errorf("expected original address when resolution off, got %s", addr) + } + + // empty and wildcard addresses should pass through unchanged + if m.resolveAddr("") != "" { + t.Error("expected empty string to pass through") + } + if m.resolveAddr("*") != "*" { + t.Error("expected wildcard to pass through") + } +} + +func TestTUI_ResolvePortHelper(t *testing.T) { + m := New(Options{Theme: "dark", Interval: time.Hour}) + m.resolvePorts = false + + // when resolution is off, should return port number as string + port := m.resolvePort(80, "tcp") + if port != "80" { + t.Errorf("expected '80' when resolution off, got %s", port) + } + + port = m.resolvePort(443, "tcp") + if port != "443" { + t.Errorf("expected '443' when resolution off, got %s", port) + } +} + +func TestTUI_FormatRemoteHelper(t *testing.T) { + m := New(Options{Theme: "dark", Interval: time.Hour}) + m.resolveAddrs = false + m.resolvePorts = false + + // empty/wildcard addresses should return dash + if m.formatRemote("", 80, "tcp") != "-" { + t.Error("expected dash for empty address") + } + if m.formatRemote("*", 80, "tcp") != "-" { + t.Error("expected dash for wildcard address") + } + if m.formatRemote("192.168.1.1", 0, "tcp") != "-" { + t.Error("expected dash for zero port") + } + + // valid address:port should format correctly + result := m.formatRemote("192.168.1.1", 443, "tcp") + if result != "192.168.1.1:443" { + t.Errorf("expected '192.168.1.1:443', got %s", result) + } +} + diff --git a/internal/tui/view.go b/internal/tui/view.go index 6a3327d..041c0e7 100644 --- a/internal/tui/view.go +++ b/internal/tui/view.go @@ -3,6 +3,8 @@ package tui import ( "fmt" "github.com/karol-broda/snitch/internal/collector" + "github.com/karol-broda/snitch/internal/resolver" + "strconv" "strings" "time" @@ -161,19 +163,19 @@ func (m model) renderRow(c collector.Connection, selected bool) string { process = SymbolDash } - port := fmt.Sprintf("%d", c.Lport) + port := truncate(m.resolvePort(c.Lport, c.Proto), cols.port) proto := c.Proto state := c.State if state == "" { state = SymbolDash } - local := c.Laddr + local := truncate(m.resolveAddr(c.Laddr), cols.local) if local == "*" || local == "" { local = "*" } - remote := formatRemote(c.Raddr, c.Rport) + remote := truncate(m.formatRemote(c.Raddr, c.Rport, c.Proto), cols.remote) // apply styling protoStyled := m.theme.Styles.GetProtoStyle(proto).Render(fmt.Sprintf("%-*s", cols.proto, proto)) @@ -185,8 +187,8 @@ func (m model) renderRow(c collector.Connection, selected bool) string { cols.port, port, protoStyled, stateStyled, - cols.local, truncate(local, cols.local), - truncate(remote, cols.remote)) + cols.local, local, + remote) if selected { return m.theme.Styles.Selected.Render(row) + "\n" @@ -201,7 +203,7 @@ func (m model) renderStatusLine() string { return " " + m.theme.Styles.Warning.Render(m.statusMessage) } - left := " " + m.theme.Styles.Normal.Render("t/u proto l/e/o state w watch K kill s sort / search ? help q quit") + left := " " + m.theme.Styles.Normal.Render("t/u proto l/e/o state n/N dns w watch K kill s sort / search ? help q quit") // show watched count if any if m.watchedCount() > 0 { @@ -209,6 +211,21 @@ func (m model) renderStatusLine() string { left += m.theme.Styles.Watched.Render(watchedInfo) } + // show dns resolution status + var resolveStatus string + if m.resolveAddrs && m.resolvePorts { + resolveStatus = "all" + } else if m.resolveAddrs { + resolveStatus = "addrs" + } else if m.resolvePorts { + resolveStatus = "ports" + } else { + resolveStatus = "off" + } + if resolveStatus != "addrs" { // addrs is the default, don't show + left += m.theme.Styles.Normal.Render(fmt.Sprintf(" dns: %s", resolveStatus)) + } + return left } @@ -240,6 +257,11 @@ func (m model) renderHelp() string { s cycle sort field S reverse sort order + display + ─────── + n toggle address resolution (dns) + N toggle port resolution (service names) + process management ────────────────── w watch/unwatch process (highlight & track) @@ -269,6 +291,11 @@ func (m model) renderDetail() string { b.WriteString(" " + m.theme.Styles.Header.Render("connection details") + "\n") b.WriteString(" " + m.theme.Styles.Border.Render(strings.Repeat(BoxHorizontal, 40)) + "\n\n") + localAddr := m.resolveAddr(c.Laddr) + localPort := m.resolvePort(c.Lport, c.Proto) + remoteAddr := m.resolveAddr(c.Raddr) + remotePort := m.resolvePort(c.Rport, c.Proto) + fields := []struct { label string value string @@ -278,8 +305,8 @@ func (m model) renderDetail() string { {"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)}, + {"local", fmt.Sprintf("%s:%s", localAddr, localPort)}, + {"remote", fmt.Sprintf("%s:%s", remoteAddr, remotePort)}, {"interface", c.Interface}, {"inode", fmt.Sprintf("%d", c.Inode)}, } @@ -498,23 +525,72 @@ type columns struct { } func (m model) columnWidths() columns { - available := m.safeWidth() - 16 - + // minimum widths (header lengths + padding) c := columns{ - process: 16, - port: 6, - proto: 5, - state: 11, - local: 15, - remote: 20, + process: 7, // "PROCESS" + port: 4, // "PORT" + proto: 5, // "PROTO" + state: 5, // "STATE" + local: 5, // "LOCAL" + remote: 6, // "REMOTE" } - used := c.process + c.port + c.proto + c.state + c.local + c.remote - extra := available - used + // scan visible connections to find max content width for each column + visible := m.visibleConnections() + for _, conn := range visible { + if len(conn.Process) > c.process { + c.process = len(conn.Process) + } - if extra > 0 { - c.process += extra / 3 - c.remote += extra - extra/3 + port := m.resolvePort(conn.Lport, conn.Proto) + if len(port) > c.port { + c.port = len(port) + } + + if len(conn.Proto) > c.proto { + c.proto = len(conn.Proto) + } + + if len(conn.State) > c.state { + c.state = len(conn.State) + } + + local := m.resolveAddr(conn.Laddr) + if len(local) > c.local { + c.local = len(local) + } + + remote := m.formatRemote(conn.Raddr, conn.Rport, conn.Proto) + if len(remote) > c.remote { + c.remote = len(remote) + } + } + + // calculate total and available width + spacing := 12 // 2 spaces between each of 6 columns + indicator := 2 + margin := 2 + available := m.safeWidth() - spacing - indicator - margin + + total := c.process + c.port + c.proto + c.state + c.local + c.remote + + // if content fits, we're done + if total <= available { + return c + } + + // content exceeds available space - need to shrink columns proportionally + // fixed columns that shouldn't shrink much: port, proto, state + fixedWidth := c.port + c.proto + c.state + flexibleAvailable := available - fixedWidth + + // distribute flexible space between process, local, remote + flexibleTotal := c.process + c.local + c.remote + if flexibleTotal > 0 && flexibleAvailable > 0 { + ratio := float64(flexibleAvailable) / float64(flexibleTotal) + c.process = max(7, int(float64(c.process)*ratio)) + c.local = max(5, int(float64(c.local)*ratio)) + c.remote = max(6, int(float64(c.remote)*ratio)) } return c @@ -536,3 +612,29 @@ func formatDuration(d time.Duration) string { } return fmt.Sprintf("%.0fm", d.Minutes()) } + +func (m model) resolveAddr(addr string) string { + if !m.resolveAddrs { + return addr + } + if addr == "" || addr == "*" { + return addr + } + return resolver.ResolveAddr(addr) +} + +func (m model) resolvePort(port int, proto string) string { + if !m.resolvePorts { + return strconv.Itoa(port) + } + return resolver.ResolvePort(port, proto) +} + +func (m model) formatRemote(addr string, port int, proto string) string { + if addr == "" || addr == "*" || port == 0 { + return "-" + } + resolvedAddr := m.resolveAddr(addr) + resolvedPort := m.resolvePort(port, proto) + return fmt.Sprintf("%s:%s", resolvedAddr, resolvedPort) +}