From c2e4916814e00b3a259eacf1cb1493b9a91b6040 Mon Sep 17 00:00:00 2001 From: Sabina Aledort Date: Wed, 1 May 2024 13:05:25 +0300 Subject: [PATCH] Fix ss filter causing missing UDP ports --- ss/ss.go | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/ss/ss.go b/ss/ss.go index 19a79114..9f124c97 100644 --- a/ss/ss.go +++ b/ss/ss.go @@ -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) @@ -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) } @@ -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], ":") @@ -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[^"]+)"`) 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 }