19 Commits

Author SHA1 Message Date
Karol Broda
6d6d057675 Merge pull request #6 from karol-broda/feat/reverse-dns-lookup 2025-12-23 16:31:03 +01:00
Karol Broda
c58f2a233d feat: add address and port resolution options 2025-12-23 16:24:29 +01:00
Karol Broda
fd4c5500ea Merge pull request #4 from karol-broda/fix/darwin-support 2025-12-23 11:21:52 +01:00
Karol Broda
df6fd318fc ci: pin determinate systems action versions 2025-12-23 11:15:43 +01:00
Karol Broda
dc7e5d435f fix: use nixos-25.11 with apple-sdk_15 for darwin support (fixes #1)
- Switch to nixos-25.11 for modern apple-sdk packages
- Use apple-sdk_15 which includes SecTrustCopyCertificateChain (macOS 12+)
- Required for Go 1.25 crypto/x509 compatibility on darwin
2025-12-23 11:09:44 +01:00
Karol Broda
c95a5ebd23 fix: add darwin/macOS platform support for nix flake (fixes #1)
- Enable CGO on darwin (required for libproc)
- Set MACOSX_DEPLOYMENT_TARGET=12.0 for Go 1.25 crypto/x509 compatibility
- Add nix build CI on macOS (macos-14 = Apple Silicon)
2025-12-23 11:01:15 +01:00
Karol Broda
755605de26 refactor: remove explicit macOS SDK framework buildInputs in flake.nix 2025-12-23 10:51:23 +01:00
Karol Broda
5b6e098e68 fix: add darwin SDK frameworks for macOS 12+ compatibility 2025-12-23 10:45:23 +01:00
Karol Broda
f20fc96c96 Merge pull request #3 from karol-broda/fix/go-and-nix-build-not-working 2025-12-23 10:02:55 +01:00
Karol Broda
7fdb1ed477 fix(build): go and nix builds not working properly 2025-12-23 10:01:01 +01:00
Karol Broda
b2be0df2f9 feat: enhance versioning and upgrade feedback for nix installations 2025-12-21 22:04:51 +01:00
Karol Broda
0252087bd0 feat: enhance install script and upgrade command 2025-12-21 21:19:07 +01:00
Karol Broda
eadd1b3452 feat: add upgrade command 2025-12-21 17:02:50 +01:00
Karol Broda
2615fe5871 fix: json command didnt filter 2025-12-21 12:22:01 +01:00
Karol Broda
29891c0bb8 feat: improve empty state 2025-12-21 12:13:50 +01:00
Karol Broda
a93e682aa2 refactor: simplify flake.nix structure and improve go binary derivation 2025-12-21 01:57:32 +01:00
Karol Broda
04aa42a9c9 feat: add install script for automated binary installation 2025-12-20 20:46:41 +01:00
Karol Broda
6e4f6b3d61 build: add aur target 2025-12-20 19:55:14 +01:00
Karol Broda
e99e6c8df7 chore: update readme 2025-12-20 19:41:49 +01:00
30 changed files with 1377 additions and 223 deletions

View File

@@ -36,3 +36,21 @@ jobs:
with: with:
version: latest version: latest
nix-build:
strategy:
matrix:
os: [ubuntu-latest, macos-14]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: DeterminateSystems/nix-installer-action@v17
- uses: DeterminateSystems/magic-nix-cache-action@v9
- name: nix flake check
run: nix flake check
- name: nix build
run: nix build

View File

@@ -27,6 +27,7 @@ jobs:
args: release --clean args: release --clean
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
AUR_KEY: ${{ secrets.AUR_KEY }}
release-darwin: release-darwin:
needs: release-linux needs: release-linux

View File

@@ -25,6 +25,9 @@ archives:
{{- .Version }}_ {{- .Version }}_
{{- .Os }}_ {{- .Os }}_
{{- .Arch }} {{- .Arch }}
files:
- LICENSE
- README.md
release: release:
github: github:

View File

@@ -33,6 +33,9 @@ archives:
{{- .Os }}_ {{- .Os }}_
{{- .Arch }} {{- .Arch }}
{{- if .Arm }}v{{ .Arm }}{{ end }} {{- if .Arm }}v{{ .Arm }}{{ end }}
files:
- LICENSE
- README.md
checksum: checksum:
name_template: "checksums.txt" name_template: "checksums.txt"
@@ -61,6 +64,27 @@ nfpms:
- rpm - rpm
- apk - apk
aurs:
- name: snitch-bin
homepage: https://github.com/karol-broda/snitch
description: a friendlier ss/netstat for humans
maintainers:
- "Karol Broda <me@karolbroda.com>"
license: MIT
private_key: "{{ .Env.AUR_KEY }}"
git_url: "ssh://aur@aur.archlinux.org/snitch-bin.git"
depends:
- glibc
provides:
- snitch
conflicts:
- snitch
package: |-
install -Dm755 "./snitch" "${pkgdir}/usr/bin/snitch"
install -Dm644 "./LICENSE" "${pkgdir}/usr/share/licenses/snitch/LICENSE"
commit_msg_template: "Update to {{ .Tag }}"
skip_upload: auto
release: release:
github: github:
owner: karol-broda owner: karol-broda

View File

@@ -28,18 +28,48 @@ nix profile install github:karol-broda/snitch
# then use: inputs.snitch.packages.${system}.default # then use: inputs.snitch.packages.${system}.default
``` ```
### arch linux (aur)
```bash
# with yay
yay -S snitch-bin
# with paru
paru -S snitch-bin
```
### shell script
```bash
curl -sSL https://raw.githubusercontent.com/karol-broda/snitch/master/install.sh | sh
```
installs to `~/.local/bin` if available, otherwise `/usr/local/bin`. override with:
```bash
curl -sSL https://raw.githubusercontent.com/karol-broda/snitch/master/install.sh | INSTALL_DIR=~/bin sh
```
> **macos:** the install script automatically removes the quarantine attribute (`com.apple.quarantine`) from the binary to allow it to run without gatekeeper warnings. to disable this, set `KEEP_QUARANTINE=1`.
### binary ### binary
download from [releases](https://github.com/karol-broda/snitch/releases): download from [releases](https://github.com/karol-broda/snitch/releases):
```bash - **linux:** `snitch_<version>_linux_<arch>.tar.gz` or `.deb`/`.rpm`/`.apk`
# amd64 - **macos:** `snitch_<version>_darwin_<arch>.tar.gz`
curl -L https://github.com/karol-broda/snitch/releases/latest/download/snitch_linux_amd64.tar.gz | tar xz
sudo mv snitch /usr/local/bin/
# or install .deb/.rpm/.apk from releases ```bash
tar xzf snitch_*.tar.gz
sudo mv snitch /usr/local/bin/
``` ```
> **macos:** if blocked with "cannot be opened because the developer cannot be verified", run:
>
> ```bash
> xattr -d com.apple.quarantine /usr/local/bin/snitch
> ```
## quick start ## quick start
```bash ```bash
@@ -116,6 +146,16 @@ snitch watch -i 1s | jq '.count'
snitch watch -l -i 500ms snitch watch -l -i 500ms
``` ```
### `snitch upgrade`
check for updates and upgrade in-place.
```bash
snitch upgrade # check for updates
snitch upgrade --yes # upgrade automatically
snitch upgrade -v 0.1.7 # install specific version
```
## filters ## filters
shortcut flags work on all commands: shortcut flags work on all commands:
@@ -174,5 +214,6 @@ theme = "auto"
## requirements ## requirements
- linux (reads from `/proc/net/*`) - linux or macos
- root or `CAP_NET_ADMIN` for full process info - linux: reads from `/proc/net/*`, root or `CAP_NET_ADMIN` for full process info
- macos: uses system APIs, may require sudo for full process info

View File

@@ -6,7 +6,7 @@ import (
"strings" "strings"
"testing" "testing"
"snitch/internal/testutil" "github.com/karol-broda/snitch/internal/testutil"
) )
// TestCLIContract tests the CLI interface contracts as specified in the README // TestCLIContract tests the CLI interface contracts as specified in the README
@@ -71,7 +71,7 @@ func TestCLIContract(t *testing.T) {
name: "version", name: "version",
args: []string{"version"}, args: []string{"version"},
expectExitCode: 0, expectExitCode: 0,
expectStdout: []string{"snitch", "commit:", "built:"}, expectStdout: []string{"snitch", "commit", "built"},
expectStderr: nil, expectStderr: nil,
description: "version command should show version information", description: "version command should show version information",
}, },
@@ -364,7 +364,8 @@ func resetGlobalFlags() {
filterIPv4 = false filterIPv4 = false
filterIPv6 = false filterIPv6 = false
colorMode = "auto" colorMode = "auto"
numeric = false resolveAddrs = true
resolvePorts = false
} }
// TestEnvironmentVariables tests that environment variables are properly handled // TestEnvironmentVariables tests that environment variables are properly handled

View File

@@ -9,8 +9,8 @@ import (
"strings" "strings"
"testing" "testing"
"snitch/internal/collector" "github.com/karol-broda/snitch/internal/collector"
"snitch/internal/testutil" "github.com/karol-broda/snitch/internal/testutil"
) )
var updateGolden = flag.Bool("update-golden", false, "Update golden files") var updateGolden = flag.Bool("update-golden", false, "Update golden files")

View File

@@ -15,4 +15,5 @@ var jsonCmd = &cobra.Command{
func init() { func init() {
rootCmd.AddCommand(jsonCmd) rootCmd.AddCommand(jsonCmd)
addFilterFlags(jsonCmd)
} }

View File

@@ -8,10 +8,10 @@ import (
"log" "log"
"os" "os"
"os/exec" "os/exec"
"snitch/internal/collector" "github.com/karol-broda/snitch/internal/collector"
"snitch/internal/color" "github.com/karol-broda/snitch/internal/color"
"snitch/internal/config" "github.com/karol-broda/snitch/internal/config"
"snitch/internal/resolver" "github.com/karol-broda/snitch/internal/resolver"
"strconv" "strconv"
"strings" "strings"
"text/tabwriter" "text/tabwriter"
@@ -30,7 +30,8 @@ var (
sortBy string sortBy string
fields string fields string
colorMode string colorMode string
numeric bool resolveAddrs bool
resolvePorts bool
plainOutput bool plainOutput bool
) )
@@ -51,7 +52,7 @@ Available filters:
} }
func runListCommand(outputFormat string, args []string) { func runListCommand(outputFormat string, args []string) {
rt, err := NewRuntime(args, colorMode, numeric) rt, err := NewRuntime(args, colorMode)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@@ -98,14 +99,18 @@ func getFieldMap(c collector.Connection) map[string]string {
lport := strconv.Itoa(c.Lport) lport := strconv.Itoa(c.Lport)
rport := strconv.Itoa(c.Rport) rport := strconv.Itoa(c.Rport)
// Apply name resolution if not in numeric mode // apply address resolution
if !numeric { if resolveAddrs {
if resolvedLaddr := resolver.ResolveAddr(c.Laddr); resolvedLaddr != c.Laddr { if resolvedLaddr := resolver.ResolveAddr(c.Laddr); resolvedLaddr != c.Laddr {
laddr = resolvedLaddr laddr = resolvedLaddr
} }
if resolvedRaddr := resolver.ResolveAddr(c.Raddr); resolvedRaddr != c.Raddr && c.Raddr != "*" && c.Raddr != "" { if resolvedRaddr := resolver.ResolveAddr(c.Raddr); resolvedRaddr != c.Raddr && c.Raddr != "*" && c.Raddr != "" {
raddr = resolvedRaddr raddr = resolvedRaddr
} }
}
// apply port resolution
if resolvePorts {
if resolvedLport := resolver.ResolvePort(c.Lport, c.Proto); resolvedLport != strconv.Itoa(c.Lport) { if resolvedLport := resolver.ResolvePort(c.Lport, c.Proto); resolvedLport != strconv.Itoa(c.Lport) {
lport = resolvedLport 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(&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().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().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)") lsCmd.Flags().BoolVarP(&plainOutput, "plain", "p", false, "Plain output (parsable, no styling)")
// shared filter flags // shared filter flags

View File

@@ -4,8 +4,8 @@ import (
"strings" "strings"
"testing" "testing"
"snitch/internal/collector" "github.com/karol-broda/snitch/internal/collector"
"snitch/internal/testutil" "github.com/karol-broda/snitch/internal/testutil"
) )
func TestLsCommand_EmptyResults(t *testing.T) { func TestLsCommand_EmptyResults(t *testing.T) {

View File

@@ -3,7 +3,7 @@ package cmd
import ( import (
"fmt" "fmt"
"os" "os"
"snitch/internal/config" "github.com/karol-broda/snitch/internal/config"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@@ -44,6 +44,8 @@ func init() {
cfg := config.Get() cfg := config.Get()
rootCmd.Flags().StringVar(&topTheme, "theme", cfg.Defaults.Theme, "Theme for TUI (dark, light, mono, auto)") 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().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 // shared filter flags for root command
addFilterFlags(rootCmd) addFilterFlags(rootCmd)

View File

@@ -2,8 +2,8 @@ package cmd
import ( import (
"fmt" "fmt"
"snitch/internal/collector" "github.com/karol-broda/snitch/internal/collector"
"snitch/internal/color" "github.com/karol-broda/snitch/internal/color"
"strconv" "strconv"
"strings" "strings"
@@ -20,8 +20,9 @@ type Runtime struct {
Connections []collector.Connection Connections []collector.Connection
// common settings // common settings
ColorMode string ColorMode string
Numeric bool ResolveAddrs bool
ResolvePorts bool
} }
// shared filter flags - used by all commands // 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. // 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) color.Init(colorMode)
filters, err := BuildFilters(args) filters, err := BuildFilters(args)
@@ -87,10 +88,11 @@ func NewRuntime(args []string, colorMode string, numeric bool) (*Runtime, error)
} }
return &Runtime{ return &Runtime{
Filters: filters, Filters: filters,
Connections: connections, Connections: connections,
ColorMode: colorMode, ColorMode: colorMode,
Numeric: numeric, ResolveAddrs: resolveAddrs,
ResolvePorts: resolvePorts,
}, nil }, nil
} }

View File

@@ -8,7 +8,7 @@ import (
"log" "log"
"os" "os"
"os/signal" "os/signal"
"snitch/internal/collector" "github.com/karol-broda/snitch/internal/collector"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"

View File

@@ -2,8 +2,8 @@ package cmd
import ( import (
"log" "log"
"snitch/internal/config" "github.com/karol-broda/snitch/internal/config"
"snitch/internal/tui" "github.com/karol-broda/snitch/internal/tui"
"time" "time"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
@@ -12,8 +12,10 @@ import (
// top-specific flags // top-specific flags
var ( var (
topTheme string topTheme string
topInterval time.Duration topInterval time.Duration
topResolveAddrs bool
topResolvePorts bool
) )
var topCmd = &cobra.Command{ var topCmd = &cobra.Command{
@@ -28,8 +30,10 @@ var topCmd = &cobra.Command{
} }
opts := tui.Options{ opts := tui.Options{
Theme: theme, Theme: theme,
Interval: topInterval, Interval: topInterval,
ResolveAddrs: topResolveAddrs,
ResolvePorts: topResolvePorts,
} }
// if any filter flag is set, use exclusive mode // if any filter flag is set, use exclusive mode
@@ -58,6 +62,8 @@ func init() {
// top-specific flags // top-specific flags
topCmd.Flags().StringVar(&topTheme, "theme", cfg.Defaults.Theme, "Theme for TUI (dark, light, mono, auto)") 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().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 // shared filter flags
addFilterFlags(topCmd) addFilterFlags(topCmd)

View File

@@ -7,8 +7,8 @@ import (
"log" "log"
"os" "os"
"os/signal" "os/signal"
"snitch/internal/collector" "github.com/karol-broda/snitch/internal/collector"
"snitch/internal/resolver" "github.com/karol-broda/snitch/internal/resolver"
"strings" "strings"
"syscall" "syscall"
"time" "time"

688
cmd/upgrade.go Normal file
View File

@@ -0,0 +1,688 @@
package cmd
import (
"archive/tar"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/karol-broda/snitch/internal/tui"
)
const (
repoOwner = "karol-broda"
repoName = "snitch"
githubAPI = "https://api.github.com"
firstUpgradeVersion = "0.1.8"
)
var (
upgradeYes bool
upgradeVersion string
)
var upgradeCmd = &cobra.Command{
Use: "upgrade",
Short: "Check for updates and optionally upgrade snitch",
Long: `Check for available updates and show upgrade instructions.
Use --yes to perform an in-place upgrade automatically.
Use --version to install a specific version.`,
RunE: runUpgrade,
}
func init() {
upgradeCmd.Flags().BoolVarP(&upgradeYes, "yes", "y", false, "Perform the upgrade automatically")
upgradeCmd.Flags().StringVarP(&upgradeVersion, "version", "v", "", "Install a specific version (e.g., v0.1.7)")
rootCmd.AddCommand(upgradeCmd)
}
type githubRelease struct {
TagName string `json:"tag_name"`
HTMLURL string `json:"html_url"`
}
type githubCommit struct {
SHA string `json:"sha"`
}
type githubCompare struct {
Status string `json:"status"`
AheadBy int `json:"ahead_by"`
BehindBy int `json:"behind_by"`
TotalCommits int `json:"total_commits"`
}
func runUpgrade(cmd *cobra.Command, args []string) error {
current := Version
nixInstall := isNixInstall()
nixVersion := isNixVersion(current)
if upgradeVersion != "" {
if nixInstall || nixVersion {
return handleNixSpecificVersion(current, upgradeVersion)
}
return handleSpecificVersion(current, upgradeVersion)
}
latest, err := fetchLatestVersion()
if err != nil {
return fmt.Errorf("failed to check for updates: %w", err)
}
if nixInstall || nixVersion {
return handleNixUpgrade(current, latest)
}
currentClean := strings.TrimPrefix(current, "v")
latestClean := strings.TrimPrefix(latest, "v")
printVersionComparison(current, latest)
if currentClean == latestClean {
green := color.New(color.FgGreen)
green.Println(tui.SymbolSuccess + " you are running the latest version")
return nil
}
if current == "dev" {
yellow := color.New(color.FgYellow)
yellow.Println(tui.SymbolWarning + " you are running a development build")
fmt.Println()
fmt.Println("use one of the methods below to install a release version:")
fmt.Println()
printUpgradeInstructions()
return nil
}
green := color.New(color.FgGreen, color.Bold)
green.Printf(tui.SymbolSuccess+" update available: %s "+tui.SymbolArrowRight+" %s\n", current, latest)
fmt.Println()
if !upgradeYes {
printUpgradeInstructions()
fmt.Println()
faint := color.New(color.Faint)
cmdStyle := color.New(color.FgCyan)
faint.Print(" in-place ")
cmdStyle.Println("snitch upgrade --yes")
return nil
}
return performUpgrade(latest)
}
func handleSpecificVersion(current, target string) error {
if !strings.HasPrefix(target, "v") {
target = "v" + target
}
targetClean := strings.TrimPrefix(target, "v")
printVersionComparisonTarget(current, target)
if isVersionLower(targetClean, firstUpgradeVersion) {
yellow := color.New(color.FgYellow)
yellow.Printf(tui.SymbolWarning+" warning: the upgrade command was introduced in v%s\n", firstUpgradeVersion)
faint := color.New(color.Faint)
faint.Printf(" version %s does not include this command\n", target)
faint.Println(" you will need to use other methods to upgrade from that version")
fmt.Println()
}
currentClean := strings.TrimPrefix(current, "v")
if currentClean == targetClean {
green := color.New(color.FgGreen)
green.Println(tui.SymbolSuccess + " you are already running this version")
return nil
}
if !upgradeYes {
faint := color.New(color.Faint)
cmdStyle := color.New(color.FgCyan)
if isVersionLower(targetClean, currentClean) {
yellow := color.New(color.FgYellow)
yellow.Printf(tui.SymbolArrowDown+" this will downgrade from %s to %s\n", current, target)
} else {
green := color.New(color.FgGreen)
green.Printf(tui.SymbolArrowUp+" this will upgrade from %s to %s\n", current, target)
}
fmt.Println()
faint.Print("run ")
cmdStyle.Printf("snitch upgrade --version %s --yes", target)
faint.Println(" to proceed")
return nil
}
return performUpgrade(target)
}
func handleNixUpgrade(current, latest string) error {
faint := color.New(color.Faint)
version := color.New(color.FgCyan)
currentCommit := extractCommitFromVersion(current)
dirty := isNixDirty(current)
faint.Print("current ")
version.Print(current)
if currentCommit != "" {
faint.Printf(" (commit %s)", currentCommit)
}
fmt.Println()
faint.Print("latest ")
version.Println(latest)
fmt.Println()
if dirty {
yellow := color.New(color.FgYellow)
yellow.Println(tui.SymbolWarning + " you are running a dirty nix build (uncommitted changes)")
fmt.Println()
printNixUpgradeInstructions()
return nil
}
if currentCommit == "" {
yellow := color.New(color.FgYellow)
yellow.Println(tui.SymbolWarning + " this is a nix installation")
faint.Println(" nix store is immutable; use nix commands to upgrade")
fmt.Println()
printNixUpgradeInstructions()
return nil
}
releaseCommit, err := fetchCommitForTag(latest)
if err != nil {
faint.Printf(" (could not fetch release commit: %v)\n", err)
fmt.Println()
yellow := color.New(color.FgYellow)
yellow.Println(tui.SymbolWarning + " this is a nix installation")
faint.Println(" nix store is immutable; use nix commands to upgrade")
fmt.Println()
printNixUpgradeInstructions()
return nil
}
releaseShort := releaseCommit
if len(releaseShort) > 7 {
releaseShort = releaseShort[:7]
}
if strings.HasPrefix(releaseCommit, currentCommit) || strings.HasPrefix(currentCommit, releaseShort) {
green := color.New(color.FgGreen)
green.Printf(tui.SymbolSuccess+" you are running %s (commit %s)\n", latest, releaseShort)
return nil
}
comparison, err := compareCommits(latest, currentCommit)
if err != nil {
green := color.New(color.FgGreen, color.Bold)
green.Printf(tui.SymbolSuccess+" update available: %s "+tui.SymbolArrowRight+" %s\n", currentCommit, latest)
faint.Printf(" your commit: %s\n", currentCommit)
faint.Printf(" release: %s (%s)\n", releaseShort, latest)
fmt.Println()
yellow := color.New(color.FgYellow)
yellow.Println(tui.SymbolWarning + " this is a nix installation")
faint.Println(" nix store is immutable; use nix commands to upgrade")
fmt.Println()
printNixUpgradeInstructions()
return nil
}
if comparison.AheadBy > 0 {
cyan := color.New(color.FgCyan)
cyan.Printf(tui.SymbolArrowUp+" you are %d commit(s) ahead of %s\n", comparison.AheadBy, latest)
faint.Printf(" your commit: %s\n", currentCommit)
faint.Printf(" release: %s (%s)\n", releaseShort, latest)
fmt.Println()
faint.Println("you are running a newer build than the latest release")
return nil
}
if comparison.BehindBy > 0 {
green := color.New(color.FgGreen, color.Bold)
green.Printf(tui.SymbolSuccess+" update available: %d commit(s) behind %s\n", comparison.BehindBy, latest)
faint.Printf(" your commit: %s\n", currentCommit)
faint.Printf(" release: %s (%s)\n", releaseShort, latest)
fmt.Println()
yellow := color.New(color.FgYellow)
yellow.Println(tui.SymbolWarning + " this is a nix installation")
faint.Println(" nix store is immutable; use nix commands to upgrade")
fmt.Println()
printNixUpgradeInstructions()
return nil
}
green := color.New(color.FgGreen)
green.Printf(tui.SymbolSuccess+" you are running %s (commit %s)\n", latest, releaseShort)
return nil
}
func handleNixSpecificVersion(current, target string) error {
if !strings.HasPrefix(target, "v") {
target = "v" + target
}
printVersionComparisonTarget(current, target)
yellow := color.New(color.FgYellow)
yellow.Println(tui.SymbolWarning + " this is a nix installation")
faint := color.New(color.Faint)
faint.Println(" nix store is immutable; in-place upgrades are not supported")
fmt.Println()
bold := color.New(color.Bold)
cmd := color.New(color.FgCyan)
bold.Println("to install a specific version with nix:")
fmt.Println()
faint.Print(" specific ref ")
cmd.Printf("nix profile install github:%s/%s/%s\n", repoOwner, repoName, target)
faint.Print(" latest ")
cmd.Printf("nix profile install github:%s/%s\n", repoOwner, repoName)
return nil
}
func isVersionLower(v1, v2 string) bool {
parts1 := parseVersion(v1)
parts2 := parseVersion(v2)
for i := 0; i < 3; i++ {
if parts1[i] < parts2[i] {
return true
}
if parts1[i] > parts2[i] {
return false
}
}
return false
}
func parseVersion(v string) [3]int {
var parts [3]int
segments := strings.Split(v, ".")
for i := 0; i < len(segments) && i < 3; i++ {
n, err := strconv.Atoi(segments[i])
if err == nil {
parts[i] = n
}
}
return parts
}
func fetchLatestVersion() (string, error) {
url := fmt.Sprintf("%s/repos/%s/%s/releases/latest", githubAPI, repoOwner, repoName)
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("github api returned status %d", resp.StatusCode)
}
var release githubRelease
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
return "", err
}
if release.TagName == "" {
return "", fmt.Errorf("no releases found")
}
return release.TagName, nil
}
func printVersionComparison(current, latest string) {
faint := color.New(color.Faint)
version := color.New(color.FgCyan)
faint.Print("current ")
version.Println(current)
faint.Print("latest ")
version.Println(latest)
fmt.Println()
}
func printVersionComparisonTarget(current, target string) {
faint := color.New(color.Faint)
version := color.New(color.FgCyan)
faint.Print("current ")
version.Println(current)
faint.Print("target ")
version.Println(target)
fmt.Println()
}
func printUpgradeInstructions() {
bold := color.New(color.Bold)
faint := color.New(color.Faint)
cmd := color.New(color.FgCyan)
bold.Println("upgrade options:")
fmt.Println()
faint.Print(" go install ")
cmd.Printf("go install github.com/%s/%s@latest\n", repoOwner, repoName)
faint.Print(" shell script ")
cmd.Printf("curl -sSL https://raw.githubusercontent.com/%s/%s/master/install.sh | sh\n", repoOwner, repoName)
faint.Print(" arch (aur) ")
cmd.Println("yay -S snitch-bin")
faint.Print(" nix ")
cmd.Printf("nix profile upgrade --inputs-from github:%s/%s\n", repoOwner, repoName)
}
func performUpgrade(version string) error {
execPath, err := os.Executable()
if err != nil {
return fmt.Errorf("failed to get executable path: %w", err)
}
execPath, err = filepath.EvalSymlinks(execPath)
if err != nil {
return fmt.Errorf("failed to resolve executable path: %w", err)
}
if strings.HasPrefix(execPath, "/nix/store/") {
yellow := color.New(color.FgYellow)
yellow.Println(tui.SymbolWarning + " cannot perform in-place upgrade for nix installation")
fmt.Println()
printNixUpgradeInstructions()
return nil
}
goos := runtime.GOOS
goarch := runtime.GOARCH
versionClean := strings.TrimPrefix(version, "v")
archiveName := fmt.Sprintf("%s_%s_%s_%s.tar.gz", repoName, versionClean, goos, goarch)
downloadURL := fmt.Sprintf("https://github.com/%s/%s/releases/download/%s/%s",
repoOwner, repoName, version, archiveName)
faint := color.New(color.Faint)
cyan := color.New(color.FgCyan)
faint.Print(tui.SymbolDownload + " downloading ")
cyan.Printf("%s", archiveName)
faint.Println("...")
resp, err := http.Get(downloadURL)
if err != nil {
return fmt.Errorf("failed to download: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("download failed with status %d", resp.StatusCode)
}
tmpDir, err := os.MkdirTemp("", "snitch-upgrade-*")
if err != nil {
return fmt.Errorf("failed to create temp directory: %w", err)
}
defer os.RemoveAll(tmpDir)
binaryPath, err := extractBinaryFromTarGz(resp.Body, tmpDir)
if err != nil {
return fmt.Errorf("failed to extract binary: %w", err)
}
if goos == "darwin" {
removeQuarantine(binaryPath)
}
// check if we can write to the target location
targetDir := filepath.Dir(execPath)
if !isWritable(targetDir) {
yellow := color.New(color.FgYellow)
cmdStyle := color.New(color.FgCyan)
yellow.Printf(tui.SymbolWarning+" elevated permissions required to install to %s\n", targetDir)
fmt.Println()
faint.Println("run with sudo or install to a user-writable location:")
fmt.Println()
faint.Print(" sudo ")
cmdStyle.Println("sudo snitch upgrade --yes")
faint.Print(" custom dir ")
cmdStyle.Printf("curl -sSL https://raw.githubusercontent.com/%s/%s/master/install.sh | INSTALL_DIR=~/.local/bin sh\n",
repoOwner, repoName)
return nil
}
// replace the binary
backupPath := execPath + ".bak"
if err := os.Rename(execPath, backupPath); err != nil {
return fmt.Errorf("failed to backup current binary: %w", err)
}
if err := copyFile(binaryPath, execPath); err != nil {
// try to restore backup
if restoreErr := os.Rename(backupPath, execPath); restoreErr != nil {
return fmt.Errorf("failed to install new binary and restore backup: %w (restore error: %v)", err, restoreErr)
}
return fmt.Errorf("failed to install new binary: %w", err)
}
if err := os.Chmod(execPath, 0755); err != nil {
return fmt.Errorf("failed to set executable permissions: %w", err)
}
if err := os.Remove(backupPath); err != nil {
// non-fatal, just warn
yellow := color.New(color.FgYellow)
yellow.Fprintf(os.Stderr, tui.SymbolWarning + " warning: failed to remove backup file %s: %v\n", backupPath, err)
}
green := color.New(color.FgGreen, color.Bold)
green.Printf(tui.SymbolSuccess + " successfully upgraded to %s\n", version)
return nil
}
func extractBinaryFromTarGz(r io.Reader, destDir string) (string, error) {
gzr, err := gzip.NewReader(r)
if err != nil {
return "", err
}
defer gzr.Close()
tr := tar.NewReader(gzr)
for {
header, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return "", err
}
if header.Typeflag != tar.TypeReg {
continue
}
// look for the snitch binary
name := filepath.Base(header.Name)
if name != repoName {
continue
}
destPath := filepath.Join(destDir, name)
outFile, err := os.Create(destPath)
if err != nil {
return "", err
}
if _, err := io.Copy(outFile, tr); err != nil {
outFile.Close()
return "", err
}
outFile.Close()
return destPath, nil
}
return "", fmt.Errorf("binary not found in archive")
}
func isWritable(path string) bool {
testFile := filepath.Join(path, ".snitch-write-test")
f, err := os.Create(testFile)
if err != nil {
return false
}
f.Close()
os.Remove(testFile)
return true
}
func copyFile(src, dst string) error {
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
dstFile, err := os.Create(dst)
if err != nil {
return err
}
defer dstFile.Close()
if _, err := io.Copy(dstFile, srcFile); err != nil {
return err
}
return dstFile.Sync()
}
func removeQuarantine(path string) {
cmd := exec.Command("xattr", "-d", "com.apple.quarantine", path)
if err := cmd.Run(); err == nil {
faint := color.New(color.Faint)
faint.Println(" removed macOS quarantine attribute")
}
}
func isNixInstall() bool {
execPath, err := os.Executable()
if err != nil {
return false
}
resolved, err := filepath.EvalSymlinks(execPath)
if err != nil {
return false
}
return strings.HasPrefix(resolved, "/nix/store/")
}
var nixVersionPattern = regexp.MustCompile(`^nix-([a-f0-9]+)(-dirty)?$`)
var commitHashPattern = regexp.MustCompile(`^[a-f0-9]{7,40}$`)
func isNixVersion(version string) bool {
if nixVersionPattern.MatchString(version) {
return true
}
if commitHashPattern.MatchString(version) {
return true
}
return false
}
func extractCommitFromVersion(version string) string {
matches := nixVersionPattern.FindStringSubmatch(version)
if len(matches) >= 2 {
return matches[1]
}
if commitHashPattern.MatchString(version) {
return version
}
return ""
}
func isNixDirty(version string) bool {
return strings.HasSuffix(version, "-dirty")
}
func fetchCommitForTag(tag string) (string, error) {
url := fmt.Sprintf("%s/repos/%s/%s/commits/%s", githubAPI, repoOwner, repoName, tag)
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("github api returned status %d", resp.StatusCode)
}
var commit githubCommit
if err := json.NewDecoder(resp.Body).Decode(&commit); err != nil {
return "", err
}
return commit.SHA, nil
}
func compareCommits(base, head string) (*githubCompare, error) {
url := fmt.Sprintf("%s/repos/%s/%s/compare/%s...%s", githubAPI, repoOwner, repoName, base, head)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("github api returned status %d", resp.StatusCode)
}
var compare githubCompare
if err := json.NewDecoder(resp.Body).Decode(&compare); err != nil {
return nil, err
}
return &compare, nil
}
func printNixUpgradeInstructions() {
bold := color.New(color.Bold)
faint := color.New(color.Faint)
cmd := color.New(color.FgCyan)
bold.Println("nix upgrade options:")
fmt.Println()
faint.Print(" flake profile ")
cmd.Printf("nix profile install github:%s/%s\n", repoOwner, repoName)
faint.Print(" flake update ")
cmd.Println("nix flake update snitch (in your system/home-manager config)")
faint.Print(" rebuild ")
cmd.Println("nixos-rebuild switch or home-manager switch")
}

View File

@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"runtime" "runtime"
"github.com/fatih/color"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@@ -17,11 +18,25 @@ var versionCmd = &cobra.Command{
Use: "version", Use: "version",
Short: "Show version/build info", Short: "Show version/build info",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("snitch %s\n", Version) bold := color.New(color.Bold)
fmt.Printf(" commit: %s\n", Commit) cyan := color.New(color.FgCyan)
fmt.Printf(" built: %s\n", Date) faint := color.New(color.Faint)
fmt.Printf(" go: %s\n", runtime.Version())
fmt.Printf(" os: %s/%s\n", runtime.GOOS, runtime.GOARCH) bold.Print("snitch ")
cyan.Println(Version)
fmt.Println()
faint.Print(" commit ")
fmt.Println(Commit)
faint.Print(" built ")
fmt.Println(Date)
faint.Print(" go ")
fmt.Println(runtime.Version())
faint.Print(" os ")
fmt.Printf("%s/%s\n", runtime.GOOS, runtime.GOARCH)
}, },
} }

26
flake.lock generated
View File

@@ -2,39 +2,23 @@
"nodes": { "nodes": {
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1765687488, "lastModified": 1766201043,
"narHash": "sha256-7YAJ6xgBAQ/Nr+7MI13Tui1ULflgAdKh63m1tfYV7+M=", "narHash": "sha256-eplAP+rorKKd0gNjV3rA6+0WMzb1X1i16F5m5pASnjA=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "d02bcc33948ca19b0aaa0213fe987ceec1f4ebe1", "rev": "b3aad468604d3e488d627c0b43984eb60e75e782",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "NixOS", "owner": "NixOS",
"ref": "nixos-25.05", "ref": "nixos-25.11",
"repo": "nixpkgs", "repo": "nixpkgs",
"type": "github" "type": "github"
} }
}, },
"root": { "root": {
"inputs": { "inputs": {
"nixpkgs": "nixpkgs", "nixpkgs": "nixpkgs"
"systems": "systems"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
} }
} }
}, },

159
flake.nix
View File

@@ -1,107 +1,110 @@
{ {
description = "snitch - a friendlier ss/netstat for humans"; description = "snitch - a friendlier ss/netstat for humans";
inputs = { inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
systems.url = "github:nix-systems/default";
};
outputs = { self, nixpkgs, systems }: outputs = { self, nixpkgs }:
let let
supportedSystems = import systems; systems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
forAllSystems = f: nixpkgs.lib.genAttrs supportedSystems (system: f system); eachSystem = nixpkgs.lib.genAttrs systems;
# go 1.25 overlay (required until nixpkgs has it) # go 1.25 binary derivation (required until nixpkgs ships it)
goOverlay = final: prev: mkGo125 = pkgs:
let let
version = "1.25.0"; version = "1.25.0";
platformInfo = { platform = {
"x86_64-linux" = { suffix = "linux-amd64"; sri = "sha256-KFKvDLIKExObNEiZLmm4aOUO0Pih5ZQO4d6eGaEjthM="; }; "x86_64-linux" = { suffix = "linux-amd64"; hash = "sha256-KFKvDLIKExObNEiZLmm4aOUO0Pih5ZQO4d6eGaEjthM="; GOOS = "linux"; GOARCH = "amd64"; };
"aarch64-linux" = { suffix = "linux-arm64"; sri = "sha256-Bd511plKJ4NpmBXuVTvVqTJ9i3mZHeNuOLZoYngvVK4="; }; "aarch64-linux" = { suffix = "linux-arm64"; hash = "sha256-Bd511plKJ4NpmBXuVTvVqTJ9i3mZHeNuOLZoYngvVK4="; GOOS = "linux"; GOARCH = "arm64"; };
"x86_64-darwin" = { suffix = "darwin-amd64"; sri = "sha256-W9YOgjA3BiwjB8cegRGAmGURZxTW9rQQWXz1B139gO8="; }; "x86_64-darwin" = { suffix = "darwin-amd64"; hash = "sha256-W9YOgjA3BiwjB8cegRGAmGURZxTW9rQQWXz1B139gO8="; GOOS = "darwin"; GOARCH = "amd64"; };
"aarch64-darwin" = { suffix = "darwin-arm64"; sri = "sha256-VEkyhEFW2Bcveij3fyrJwVojBGaYtiQ/YzsKCwDAdJw="; }; "aarch64-darwin" = { suffix = "darwin-arm64"; hash = "sha256-VEkyhEFW2Bcveij3fyrJwVojBGaYtiQ/YzsKCwDAdJw="; GOOS = "darwin"; GOARCH = "arm64"; };
}; }.${pkgs.stdenv.hostPlatform.system} or (throw "unsupported system: ${pkgs.stdenv.hostPlatform.system}");
hostSystem = prev.stdenv.hostPlatform.system;
chosen = platformInfo.${hostSystem} or (throw "unsupported system: ${hostSystem}");
in in
{ pkgs.stdenv.mkDerivation {
go_1_25 = prev.stdenvNoCC.mkDerivation { pname = "go";
pname = "go"; inherit version;
inherit version; src = pkgs.fetchurl {
src = prev.fetchurl { url = "https://go.dev/dl/go${version}.${platform.suffix}.tar.gz";
url = "https://go.dev/dl/go${version}.${chosen.suffix}.tar.gz"; inherit (platform) hash;
hash = chosen.sri; };
}; dontBuild = true;
dontBuild = true; dontPatchELF = true;
installPhase = '' dontStrip = true;
runHook preInstall installPhase = ''
mkdir -p "$out"/{bin,share} runHook preInstall
tar -C "$TMPDIR" -xzf "$src" mkdir -p $out/{bin,share/go}
cp -a "$TMPDIR/go" "$out/share/go" tar -xzf $src --strip-components=1 -C $out/share/go
ln -s "$out/share/go/bin/go" "$out/bin/go" ln -s $out/share/go/bin/go $out/bin/go
ln -s "$out/share/go/bin/gofmt" "$out/bin/gofmt" ln -s $out/share/go/bin/gofmt $out/bin/gofmt
runHook postInstall runHook postInstall
''; '';
dontPatchELF = true; passthru = {
dontStrip = true; inherit (platform) GOOS GOARCH;
};
};
pkgsFor = system: import nixpkgs { inherit system; };
mkSnitch = pkgs:
let
rev = self.shortRev or self.dirtyShortRev or "unknown";
version = "nix-${rev}";
isDarwin = pkgs.stdenv.isDarwin;
go = mkGo125 pkgs;
buildGoModule = pkgs.buildGoModule.override { inherit go; };
in
buildGoModule {
pname = "snitch";
inherit version;
src = self;
vendorHash = "sha256-fX3wOqeOgjH7AuWGxPQxJ+wbhp240CW8tiF4rVUUDzk=";
# darwin requires cgo for libproc, linux uses pure go with /proc
env.CGO_ENABLED = if isDarwin then "1" else "0";
env.GOTOOLCHAIN = "local";
# darwin: use macOS 15 SDK for SecTrustCopyCertificateChain (Go 1.25 crypto/x509)
buildInputs = pkgs.lib.optionals isDarwin [ pkgs.apple-sdk_15 ];
ldflags = [
"-s"
"-w"
"-X snitch/cmd.Version=${version}"
"-X snitch/cmd.Commit=${rev}"
"-X snitch/cmd.Date=${self.lastModifiedDate or "unknown"}"
];
meta = {
description = "a friendlier ss/netstat for humans";
homepage = "https://github.com/karol-broda/snitch";
license = pkgs.lib.licenses.mit;
platforms = pkgs.lib.platforms.linux ++ pkgs.lib.platforms.darwin;
mainProgram = "snitch";
}; };
}; };
in in
{ {
overlays.default = final: prev: { packages = eachSystem (system:
snitch = final.callPackage ./nix/package.nix { }; let pkgs = pkgsFor system; in
};
packages = forAllSystems (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ goOverlay ];
};
in
let
version = self.shortRev or self.dirtyShortRev or "dev";
in
{ {
default = pkgs.buildGoModule { default = mkSnitch pkgs;
pname = "snitch"; snitch = mkSnitch pkgs;
inherit version;
src = self;
vendorHash = "sha256-fX3wOqeOgjH7AuWGxPQxJ+wbhp240CW8tiF4rVUUDzk=";
env.CGO_ENABLED = 0;
ldflags = [
"-s" "-w"
"-X snitch/cmd.Version=${version}"
"-X snitch/cmd.Commit=${version}"
"-X snitch/cmd.Date=${self.lastModifiedDate or "unknown"}"
];
meta = with pkgs.lib; {
description = "a friendlier ss/netstat for humans";
homepage = "https://github.com/karol-broda/snitch";
license = licenses.mit;
platforms = platforms.linux;
mainProgram = "snitch";
};
};
} }
); );
devShells = forAllSystems (system: devShells = eachSystem (system:
let let
pkgs = import nixpkgs { pkgs = pkgsFor system;
inherit system; go = mkGo125 pkgs;
overlays = [ goOverlay ];
};
in in
{ {
default = pkgs.mkShell { default = pkgs.mkShell {
packages = [ pkgs.go_1_25 pkgs.git pkgs.vhs ]; packages = [ go pkgs.git pkgs.vhs ];
GOTOOLCHAIN = "local"; env.GOTOOLCHAIN = "local";
shellHook = '' shellHook = ''
echo "go toolchain: $(go version)" echo "go toolchain: $(go version)"
''; '';
}; };
} }
); );
overlays.default = final: _prev: {
snitch = mkSnitch final;
};
}; };
} }

2
go.mod
View File

@@ -1,4 +1,4 @@
module snitch module github.com/karol-broda/snitch
go 1.24.0 go 1.24.0

113
install.sh Executable file
View File

@@ -0,0 +1,113 @@
#!/bin/sh
set -e
REPO="karol-broda/snitch"
BINARY_NAME="snitch"
# allow override via environment
INSTALL_DIR="${INSTALL_DIR:-}"
KEEP_QUARANTINE="${KEEP_QUARANTINE:-}"
detect_install_dir() {
if [ -n "$INSTALL_DIR" ]; then
echo "$INSTALL_DIR"
return
fi
# prefer user-local directory if it exists and is in PATH
if [ -d "$HOME/.local/bin" ] && echo "$PATH" | grep -q "$HOME/.local/bin"; then
echo "$HOME/.local/bin"
return
fi
# fallback to /usr/local/bin
echo "/usr/local/bin"
}
detect_os() {
os=$(uname -s | tr '[:upper:]' '[:lower:]')
case "$os" in
darwin) echo "darwin" ;;
linux) echo "linux" ;;
*)
echo "error: unsupported operating system: $os" >&2
exit 1
;;
esac
}
detect_arch() {
arch=$(uname -m)
case "$arch" in
x86_64|amd64) echo "amd64" ;;
aarch64|arm64) echo "arm64" ;;
armv7l) echo "armv7" ;;
*)
echo "error: unsupported architecture: $arch" >&2
exit 1
;;
esac
}
fetch_latest_version() {
version=$(curl -sL "https://api.github.com/repos/${REPO}/releases/latest" | grep '"tag_name"' | head -1 | cut -d'"' -f4)
if [ -z "$version" ]; then
echo "error: failed to fetch latest version" >&2
exit 1
fi
echo "$version"
}
main() {
os=$(detect_os)
arch=$(detect_arch)
install_dir=$(detect_install_dir)
version=$(fetch_latest_version)
version_no_v="${version#v}"
archive_name="${BINARY_NAME}_${version_no_v}_${os}_${arch}.tar.gz"
download_url="https://github.com/${REPO}/releases/download/${version}/${archive_name}"
echo "installing ${BINARY_NAME} ${version} for ${os}/${arch}..."
tmp_dir=$(mktemp -d)
trap 'rm -rf "$tmp_dir"' EXIT
echo "downloading ${download_url}..."
if ! curl -sL --fail "$download_url" -o "${tmp_dir}/${archive_name}"; then
echo "error: failed to download ${download_url}" >&2
exit 1
fi
echo "extracting..."
tar -xzf "${tmp_dir}/${archive_name}" -C "$tmp_dir"
if [ ! -f "${tmp_dir}/${BINARY_NAME}" ]; then
echo "error: binary not found in archive" >&2
exit 1
fi
# remove macos quarantine attribute unless disabled
if [ "$os" = "darwin" ] && [ -z "$KEEP_QUARANTINE" ]; then
if xattr -d com.apple.quarantine "${tmp_dir}/${BINARY_NAME}" 2>/dev/null; then
echo "warning: removed macOS quarantine attribute from binary"
fi
fi
# install binary
if [ -w "$install_dir" ]; then
mv "${tmp_dir}/${BINARY_NAME}" "${install_dir}/${BINARY_NAME}"
else
echo "elevated permissions required to install to ${install_dir}"
sudo mv "${tmp_dir}/${BINARY_NAME}" "${install_dir}/${BINARY_NAME}"
fi
chmod +x "${install_dir}/${BINARY_NAME}"
echo "installed ${BINARY_NAME} to ${install_dir}/${BINARY_NAME}"
echo ""
echo "run '${BINARY_NAME} --help' to get started"
}
main

View File

@@ -5,7 +5,7 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"snitch/internal/collector" "github.com/karol-broda/snitch/internal/collector"
) )
// TestCollector wraps MockCollector for use in tests // TestCollector wraps MockCollector for use in tests

View File

@@ -1,9 +1,8 @@
package tui package tui
import ( import (
"fmt"
"regexp" "regexp"
"snitch/internal/collector" "github.com/karol-broda/snitch/internal/collector"
"strings" "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)
}

View File

@@ -2,7 +2,7 @@ package tui
import ( import (
"fmt" "fmt"
"snitch/internal/collector" "github.com/karol-broda/snitch/internal/collector"
"time" "time"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
@@ -210,6 +210,28 @@ func (m model) handleNormalKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
m.showKillConfirm = true 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 return m, nil

View File

@@ -2,7 +2,7 @@ package tui
import ( import (
"fmt" "fmt"
"snitch/internal/collector" "github.com/karol-broda/snitch/internal/collector"
"syscall" "syscall"
"time" "time"

View File

@@ -2,8 +2,8 @@ package tui
import ( import (
"fmt" "fmt"
"snitch/internal/collector" "github.com/karol-broda/snitch/internal/collector"
"snitch/internal/theme" "github.com/karol-broda/snitch/internal/theme"
"time" "time"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
@@ -28,6 +28,10 @@ type model struct {
sortField collector.SortField sortField collector.SortField
sortReverse bool 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 // ui state
theme *theme.Theme theme *theme.Theme
showHelp bool showHelp bool
@@ -50,14 +54,16 @@ type model struct {
} }
type Options struct { type Options struct {
Theme string Theme string
Interval time.Duration Interval time.Duration
TCP bool TCP bool
UDP bool UDP bool
Listening bool Listening bool
Established bool Established bool
Other bool Other bool
FilterSet bool // true if user specified any filter flags 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 { func New(opts Options) model {
@@ -102,6 +108,8 @@ func New(opts Options) model {
showEstablished: showEstablished, showEstablished: showEstablished,
showOther: showOther, showOther: showOther,
sortField: collector.SortByLport, sortField: collector.SortByLport,
resolveAddrs: opts.ResolveAddrs,
resolvePorts: opts.ResolvePorts,
theme: theme.GetTheme(opts.Theme), theme: theme.GetTheme(opts.Theme),
interval: interval, interval: interval,
lastRefresh: time.Now(), lastRefresh: time.Now(),

View File

@@ -1,7 +1,7 @@
package tui package tui
import ( import (
"snitch/internal/collector" "github.com/karol-broda/snitch/internal/collector"
"testing" "testing"
"time" "time"
@@ -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)
}
}

View File

@@ -15,6 +15,7 @@ const (
SymbolArrowDown = string('\u2193') // downwards arrow SymbolArrowDown = string('\u2193') // downwards arrow
SymbolRefresh = string('\u21BB') // clockwise open circle arrow SymbolRefresh = string('\u21BB') // clockwise open circle arrow
SymbolEllipsis = string('\u2026') // horizontal ellipsis SymbolEllipsis = string('\u2026') // horizontal ellipsis
SymbolDownload = string('\u21E9') // downwards white arrow
// box drawing rounded // box drawing rounded
BoxTopLeft = string('\u256D') // light arc down and right BoxTopLeft = string('\u256D') // light arc down and right

View File

@@ -2,7 +2,9 @@ package tui
import ( import (
"fmt" "fmt"
"snitch/internal/collector" "github.com/karol-broda/snitch/internal/collector"
"github.com/karol-broda/snitch/internal/resolver"
"strconv"
"strings" "strings"
"time" "time"
@@ -47,37 +49,14 @@ func (m model) renderTitle() string {
func (m model) renderFilters() string { func (m model) renderFilters() string {
var parts []string var parts []string
if m.showTCP { parts = append(parts, m.renderFilterLabel("t", "cp", m.showTCP))
parts = append(parts, m.theme.Styles.Success.Render("tcp")) parts = append(parts, m.renderFilterLabel("u", "dp", m.showUDP))
} 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(BoxVertical)) parts = append(parts, m.theme.Styles.Border.Render(BoxVertical))
if m.showListening { parts = append(parts, m.renderFilterLabel("l", "isten", m.showListening))
parts = append(parts, m.theme.Styles.Success.Render("listen")) parts = append(parts, m.renderFilterLabel("e", "stab", m.showEstablished))
} else { parts = append(parts, m.renderFilterLabel("o", "ther", m.showOther))
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, " ") left := " " + strings.Join(parts, " ")
@@ -119,6 +98,18 @@ func (m model) renderTableHeader() string {
return m.theme.Styles.Header.Render(header) + "\n" return m.theme.Styles.Header.Render(header) + "\n"
} }
func (m model) renderFilterLabel(firstChar, rest string, active bool) string {
baseStyle := m.theme.Styles.Normal
if active {
baseStyle = m.theme.Styles.Success
}
underlinedFirst := baseStyle.Underline(true).Render(firstChar)
restPart := baseStyle.Render(rest)
return underlinedFirst + restPart
}
func (m model) renderSeparator() string { func (m model) renderSeparator() string {
w := m.width - 4 w := m.width - 4
if w < 1 { if w < 1 {
@@ -134,8 +125,11 @@ func (m model) renderConnections() string {
pageSize := m.pageSize() pageSize := m.pageSize()
if len(visible) == 0 { if len(visible) == 0 {
empty := "\n " + m.theme.Styles.Normal.Render("no connections match filters") + "\n" b.WriteString(" " + m.theme.Styles.Normal.Render("no connections match filters") + "\n")
return empty for i := 1; i < pageSize; i++ {
b.WriteString("\n")
}
return b.String()
} }
start := m.scrollOffset(pageSize, len(visible)) start := m.scrollOffset(pageSize, len(visible))
@@ -169,19 +163,19 @@ func (m model) renderRow(c collector.Connection, selected bool) string {
process = SymbolDash process = SymbolDash
} }
port := fmt.Sprintf("%d", c.Lport) port := truncate(m.resolvePort(c.Lport, c.Proto), cols.port)
proto := c.Proto proto := c.Proto
state := c.State state := c.State
if state == "" { if state == "" {
state = SymbolDash state = SymbolDash
} }
local := c.Laddr local := truncate(m.resolveAddr(c.Laddr), cols.local)
if local == "*" || local == "" { if local == "*" || local == "" {
local = "*" local = "*"
} }
remote := formatRemote(c.Raddr, c.Rport) remote := truncate(m.formatRemote(c.Raddr, c.Rport, c.Proto), cols.remote)
// apply styling // apply styling
protoStyled := m.theme.Styles.GetProtoStyle(proto).Render(fmt.Sprintf("%-*s", cols.proto, proto)) protoStyled := m.theme.Styles.GetProtoStyle(proto).Render(fmt.Sprintf("%-*s", cols.proto, proto))
@@ -193,8 +187,8 @@ func (m model) renderRow(c collector.Connection, selected bool) string {
cols.port, port, cols.port, port,
protoStyled, protoStyled,
stateStyled, stateStyled,
cols.local, truncate(local, cols.local), cols.local, local,
truncate(remote, cols.remote)) remote)
if selected { if selected {
return m.theme.Styles.Selected.Render(row) + "\n" return m.theme.Styles.Selected.Render(row) + "\n"
@@ -209,7 +203,7 @@ func (m model) renderStatusLine() string {
return " " + m.theme.Styles.Warning.Render(m.statusMessage) 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 // show watched count if any
if m.watchedCount() > 0 { if m.watchedCount() > 0 {
@@ -217,6 +211,21 @@ func (m model) renderStatusLine() string {
left += m.theme.Styles.Watched.Render(watchedInfo) 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 return left
} }
@@ -248,6 +257,11 @@ func (m model) renderHelp() string {
s cycle sort field s cycle sort field
S reverse sort order S reverse sort order
display
───────
n toggle address resolution (dns)
N toggle port resolution (service names)
process management process management
────────────────── ──────────────────
w watch/unwatch process (highlight & track) w watch/unwatch process (highlight & track)
@@ -277,6 +291,11 @@ func (m model) renderDetail() string {
b.WriteString(" " + m.theme.Styles.Header.Render("connection details") + "\n") b.WriteString(" " + m.theme.Styles.Header.Render("connection details") + "\n")
b.WriteString(" " + m.theme.Styles.Border.Render(strings.Repeat(BoxHorizontal, 40)) + "\n\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 { fields := []struct {
label string label string
value string value string
@@ -286,8 +305,8 @@ func (m model) renderDetail() string {
{"user", c.User}, {"user", c.User},
{"protocol", c.Proto}, {"protocol", c.Proto},
{"state", c.State}, {"state", c.State},
{"local", fmt.Sprintf("%s:%d", c.Laddr, c.Lport)}, {"local", fmt.Sprintf("%s:%s", localAddr, localPort)},
{"remote", fmt.Sprintf("%s:%d", c.Raddr, c.Rport)}, {"remote", fmt.Sprintf("%s:%s", remoteAddr, remotePort)},
{"interface", c.Interface}, {"interface", c.Interface},
{"inode", fmt.Sprintf("%d", c.Inode)}, {"inode", fmt.Sprintf("%d", c.Inode)},
} }
@@ -506,23 +525,72 @@ type columns struct {
} }
func (m model) columnWidths() columns { func (m model) columnWidths() columns {
available := m.safeWidth() - 16 // minimum widths (header lengths + padding)
c := columns{ c := columns{
process: 16, process: 7, // "PROCESS"
port: 6, port: 4, // "PORT"
proto: 5, proto: 5, // "PROTO"
state: 11, state: 5, // "STATE"
local: 15, local: 5, // "LOCAL"
remote: 20, remote: 6, // "REMOTE"
} }
used := c.process + c.port + c.proto + c.state + c.local + c.remote // scan visible connections to find max content width for each column
extra := available - used visible := m.visibleConnections()
for _, conn := range visible {
if len(conn.Process) > c.process {
c.process = len(conn.Process)
}
if extra > 0 { port := m.resolvePort(conn.Lport, conn.Proto)
c.process += extra / 3 if len(port) > c.port {
c.remote += extra - extra/3 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 return c
@@ -544,3 +612,29 @@ func formatDuration(d time.Duration) string {
} }
return fmt.Sprintf("%.0fm", d.Minutes()) 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)
}

View File

@@ -1,7 +1,7 @@
package main package main
import ( import (
"snitch/cmd" "github.com/karol-broda/snitch/cmd"
) )
func main() { func main() {