Skip to content

Commit

Permalink
ActionUsedPorts: support windows
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Oct 19, 2023
1 parent 7ab8868 commit 743502a
Showing 1 changed file with 54 additions and 18 deletions.
72 changes: 54 additions & 18 deletions pkg/actions/net/port.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net

import (
"os/exec"
"runtime"
"strings"

"github.com/rsteube/carapace"
Expand Down Expand Up @@ -52,26 +52,62 @@ func ActionKnownPorts() carapace.Action {
// 8000 (used by: python)
func ActionUsedPorts() carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
// TODO support other tools
if _, err := exec.LookPath("lsof"); err == nil {
return carapace.ActionExecCommand("lsof", "-i", "-P", "-n")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
switch runtime.GOOS {
case "darwin", "linux":
return actionUsedPortsLsof()
case "windows":
return actionUsedPortsNetstat()
default:
return carapace.ActionValues()
}
}).Tag("used ports")
}

func actionUsedPortsLsof() carapace.Action {
return carapace.ActionExecCommand("lsof", "-i", "-P", "-n")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")

vals := make([]string, 0)
for _, line := range lines {
if fields := strings.Fields(line); len(fields) > 9 {
if fields[len(fields)-1] != "(LISTEN)" {
continue
}

vals := make([]string, 0)
for _, line := range lines {
if fields := strings.Fields(line); len(fields) > 9 {
if fields[len(fields)-1] != "(LISTEN)" {
continue
}
if splitted := strings.Split(fields[len(fields)-2], ":"); len(splitted) > 1 {
vals = append(vals, splitted[len(splitted)-1], "used by: "+fields[0])
}
}
}
return carapace.ActionValuesDescribed(vals...).Style(style.Carapace.KeywordNegative)
})
}

if splitted := strings.Split(fields[len(fields)-2], ":"); len(splitted) > 1 {
vals = append(vals, splitted[len(splitted)-1], "used by: "+fields[0])
}
func actionUsedPortsNetstat() carapace.Action {
return carapace.ActionExecCommand("netstat", "-abon")(func(output []byte) carapace.Action {
lines := strings.Split(string(output), "\n")
vals := make([]string, 0)
for index, line := range lines {
if fields := strings.Fields(line); len(fields) > 3 {
switch {
case fields[3] != "LISTENING":
continue
case fields[0] != "TCP" && fields[0] != "UDP":
continue
}

if splitted := strings.Split(fields[1], ":"); len(splitted) > 1 {
executable := lines[index-1]
switch {
case strings.HasPrefix(executable, "["):
executable = executable[1 : len(executable)-1]
default:
executable = "unknown"
}
vals = append(vals, splitted[1], "used by: "+executable)
}
return carapace.ActionValuesDescribed(vals...).Style(style.Carapace.KeywordNegative)
})
}
}
return carapace.ActionValues()
}).Tag("used ports")
return carapace.ActionValuesDescribed(vals...).Style(style.Carapace.KeywordNegative)
})
}

0 comments on commit 743502a

Please sign in to comment.