Skip to content

Commit

Permalink
Fix ss filter causing missing UDP ports
Browse files Browse the repository at this point in the history
  • Loading branch information
sabinaaledort committed May 1, 2024
1 parent 319113c commit c2e4916
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions ss/ss.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,11 @@ const (
duration = time.Second * 5
)

var (
// TcpSSFilterFn is a function variable in Go that filters entries from the 'ss' command output.
// It takes an entry from the 'ss' command output and returns true if the entry represents a TCP port in the listening state.
tcpSSFilterFn = func(s string) bool {
return strings.Contains(s, "127.0.0") || !strings.Contains(s, "LISTEN")
}
// UdpSSFilterFn is a function variable in Go that filters entries from the 'ss' command output.
// It takes an entry from the 'ss' command output and returns true if the entry represents a UDP port in the listening state.
udpSSFilterFn = func(s string) bool {
return strings.Contains(s, "127.0.0") || !strings.Contains(s, "ESTAB")
}
)
// Filters entries from the 'ss' command output.
// Returns true if the 'ss' entry represents a listening port.
var ssFilterFn = func(s string) bool {
return strings.Contains(s, "127.0.0") || strings.Contains(s, "::1") || s == ""
}

func CreateComDetailsFromNode(cs *client.ClientSet, node *corev1.Node, tcpFile, udpFile *os.File) ([]types.ComDetails, error) {
debugPod, err := debug.New(cs, node.Name, consts.DefaultDebugNamespace, consts.DefaultDebugPodImage)
Expand All @@ -50,23 +43,23 @@ func CreateComDetailsFromNode(cs *client.ClientSet, node *corev1.Node, tcpFile,
}
}()

ssOutTCP, err := debugPod.ExecWithRetry("ss -anplt", interval, duration)
ssOutTCP, err := debugPod.ExecWithRetry("ss -anpltH", interval, duration)
if err != nil {
return nil, err
}
ssOutUDP, err := debugPod.ExecWithRetry("ss -anplu", interval, duration)
ssOutUDP, err := debugPod.ExecWithRetry("ss -anpluH", interval, duration)
if err != nil {
return nil, err
}

ssOutFilteredTCP := filterStrings(tcpSSFilterFn, splitByLines(ssOutTCP))
ssOutFilteredUDP := filterStrings(udpSSFilterFn, splitByLines(ssOutUDP))
ssOutFilteredTCP := filterStrings(ssFilterFn, splitByLines(ssOutTCP))
ssOutFilteredUDP := filterStrings(ssFilterFn, splitByLines(ssOutUDP))

_, err = tcpFile.Write([]byte(fmt.Sprintf("node: %s\n%s", node.Name, strings.Join(ssOutFilteredTCP, "\n"))))
_, err = tcpFile.Write([]byte(fmt.Sprintf("node: %s\n%s\n\n", node.Name, strings.Join(ssOutFilteredTCP, "\n"))))
if err != nil {
return nil, fmt.Errorf("failed writing to file: %s", err)
}
_, err = udpFile.Write([]byte(fmt.Sprintf("node: %s\n%s", node.Name, strings.Join(ssOutFilteredUDP, "\n"))))
_, err = udpFile.Write([]byte(fmt.Sprintf("node: %s\n%s\n\n", node.Name, strings.Join(ssOutFilteredUDP, "\n"))))
if err != nil {
return nil, fmt.Errorf("failed writing to file: %s", err)
}
Expand Down Expand Up @@ -205,10 +198,7 @@ func filterStrings(filterOutFn func(string) bool, strs []string) []string {
}

func parseComDetail(ssEntry string) (*types.ComDetails, error) {
serviceName, err := extractServiceName(ssEntry)
if err != nil {
return nil, err
}
serviceName := extractServiceName(ssEntry)

fields := strings.Fields(ssEntry)
portIdx := strings.LastIndex(fields[localAddrPortFieldIdx], ":")
Expand All @@ -226,16 +216,16 @@ func parseComDetail(ssEntry string) (*types.ComDetails, error) {
Optional: false}, nil
}

func extractServiceName(ssEntry string) (string, error) {
func extractServiceName(ssEntry string) string {
re := regexp.MustCompile(`users:\(\("(?P<servicename>[^"]+)"`)

match := re.FindStringSubmatch(ssEntry)

if len(match) < 2 {
return "", fmt.Errorf("service name not found in the input string: %s", ssEntry)
return ""
}

serviceName := match[re.SubexpIndex("servicename")]

return serviceName, nil
return serviceName
}

0 comments on commit c2e4916

Please sign in to comment.