Skip to content

Commit

Permalink
Windows: Adding tcp inspector
Browse files Browse the repository at this point in the history
  • Loading branch information
deven96 committed Mar 25, 2022
1 parent 9e06cd7 commit c0e4eaa
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
65 changes: 65 additions & 0 deletions inspector/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ type TcpLinux struct {
Values TcpMetrics
}

type TcpWin struct {
Command string
Driver *driver.Driver
Values TcpMetrics
}

/* Parse : parsing the following kind of output
Active Internet connections (including servers)
Proto Recv-Q Send-Q Local Address Foreign Address (state)
Expand Down Expand Up @@ -135,6 +141,61 @@ func (i *TcpLinux) Execute() {
}
}

/* Parse for output
Active Connections
Proto Local Address Foreign Address State
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
TCP 0.0.0.0:5040 0.0.0.0:0 LISTENING
TCP 0.0.0.0:5700 0.0.0.0:0 LISTENING
TCP 0.0.0.0:6646 0.0.0.0:0 LISTENING
TCP 0.0.0.0:49664 0.0.0.0:0 LISTENING
*/
func (i *TcpWin) Parse(output string) {
ports := make(map[int]string)
lines := strings.Split(output, "\n")
for index, line := range lines {
// skip title lines
if index == 0 || index == 1 || index == 3 {
continue
}
columns := strings.Fields(line)
if len(columns) > 3 {
status := columns[3]
address := strings.Split(columns[1], ":")
portString := address[len(address)-1]
port, err := strconv.Atoi(portString)
if err != nil {
log.Fatal("Could not parse port number in TcpWin")
}
ports[port] = status

}
}
i.Values.Ports = ports
}

func (i *TcpWin) SetDriver(driver *driver.Driver) {
details := (*driver).GetDetails()
if !details.IsWindows {
panic("Cannot use TcpWin on drivers outside (windows)")
}
i.Driver = driver
}

func (i TcpWin) driverExec() driver.Command {
return (*i.Driver).RunCommand
}

func (i *TcpWin) Execute() {
output, err := i.driverExec()(i.Command)
if err == nil {
i.Parse(output)
}
}

// NewTcp: Initialize a new Tcp instance
func NewTcp(driver *driver.Driver, _ ...string) (Inspector, error) {
var tcp Inspector
Expand All @@ -150,6 +211,10 @@ func NewTcp(driver *driver.Driver, _ ...string) (Inspector, error) {
tcp = &TcpLinux{
Command: `ss -tan`,
}
} else if details.IsWindows {
tcp = &TcpWin{
Command: `netstat -anp tcp`,
}
}
tcp.SetDriver(driver)
return tcp, nil
Expand Down
13 changes: 13 additions & 0 deletions integration/integration_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,16 @@ func TestDFonLocal(t *testing.T) {
}
}
}

func TestTcponLocal(t *testing.T) {
d := NewLocalForTest()
i, _ := inspector.Init(`tcp`, &d)
i.Execute()
iConcreteWindows, ok := i.(*inspector.TcpWin)
if ok {
if len(iConcreteWindows.Values.Ports) == 0 {
t.Errorf("%#v", iConcreteWindows.Values.Ports)
}
fmt.Printf("%#v", iConcreteWindows.Values.Ports)
}
}

0 comments on commit c0e4eaa

Please sign in to comment.