feat: add address and port resolution options
This commit is contained in:
@@ -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
|
||||
|
||||
16
cmd/ls.go
16
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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
14
cmd/top.go
14
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)
|
||||
|
||||
Reference in New Issue
Block a user