Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ build/

# script artifacts
/*.csv
/*.tsv
*.tsv
*.crt
*.key
go/
Expand Down
51 changes: 39 additions & 12 deletions triage/network_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net"
"os"
"sort"

"github.com/cakturk/go-netstat/netstat"
"github.com/charmbracelet/log"
Expand Down Expand Up @@ -110,17 +111,43 @@ func printAddrs(list []string, msg string) string {
}

func printSockets(title string, sockets []netstat.SockTabEntry) string {
var result = ""
if len(sockets) > 0 {
fmt.Print(title)
for _, e := range sockets {
if e.State.String() == "LISTEN" && !e.LocalAddr.IP.IsLoopback() {
type entry struct {
port uint16
process string
}

var result string
seen := make(map[uint16]bool)
var entries []entry

for _, e := range sockets {
if e.State.String() == "LISTEN" && !e.LocalAddr.IP.IsLoopback() {
port := e.LocalAddr.Port

if !seen[port] {
seen[port] = true
entries = append(entries, entry{
port: port,
process: e.Process.String(),
})

fmt.Printf("%s %s %d %s\n", e.LocalAddr.String(), e.State.String(), e.UID, e.Process)
result += fmt.Sprintf("%d\t%s\n", e.LocalAddr.Port, e.Process)
}
}
}

if len(entries) > 0 {
fmt.Print(title)

sort.Slice(entries, func(i, j int) bool {
return entries[i].port < entries[j].port
})

for _, e := range entries {
result += fmt.Sprintf("%d\t%s\n", e.port, e.process)
}
}

if len(result) == 0 {
result = "NONE"
}
Expand All @@ -136,31 +163,31 @@ func printNetstat() string {
fmt.Print(err)
result += "err"
} else {
result += "## TCP ##\n" + printSockets("\nTCP IPv4 Sockets:", tcpSocks)
result += "## TCPv4 ##\n" + printSockets("\nTCP IPv4 Sockets:", tcpSocks)
}
// Get UDP IPv4 sockets
udpSocks, err := netstat.UDPSocks(netstat.NoopFilter)
if err != nil {
fmt.Print(err)
result += "err"
} else {
result += "\n## UDP ##\n" + printSockets("\nUDP IPv4 Sockets:", udpSocks)
result += "\n\n## UDPv4 ##\n" + printSockets("\nUDP IPv4 Sockets:", udpSocks)
}
// Get TCP IPv6 sockets
tcp6Socks, err := netstat.TCP6Socks(netstat.NoopFilter)
if err != nil {
fmt.Print(err)
//result += "err"
result += "err"
} else {
printSockets("\nTCP IPv6 Sockets:", tcp6Socks)
result += "\n\n## TCPv6 ##\n" + printSockets("\nTCP IPv6 Sockets:", tcp6Socks)
}
Comment thread
thankgod4rob marked this conversation as resolved.
// Get UDP IPv6 sockets
udp6Socks, err := netstat.UDP6Socks(netstat.NoopFilter)
if err != nil {
fmt.Print(err)
//result += "err"
result += "err"
} else {
printSockets("\nUDP IPv6 Sockets:", udp6Socks)
result += "\n\n## UDPv6 ##\n" + printSockets("\nUDP IPv6 Sockets:", udp6Socks)
}
Comment thread
thankgod4rob marked this conversation as resolved.
return "\"" + result + "\"\t"
}
Loading