Compare commits

...

2 Commits

Author SHA1 Message Date
Marwan Sulaiman 98a708c099 Remove loopback for tcp 2023-05-02 15:32:15 -04:00
Marwan Sulaiman 7a530173bd temporary pid 2023-04-29 19:10:38 -04:00
4 changed files with 14 additions and 10 deletions

View File

@ -99,10 +99,6 @@ func appendParsePortsNetstat(base []Port, br *bufio.Reader) ([]Port, error) {
// not interested in non-listener sockets // not interested in non-listener sockets
continue continue
} }
if isLoopbackAddr(laddr) {
// not interested in loopback-bound listeners
continue
}
} else if mem.HasPrefixFold(protos, mem.S("udp")) { } else if mem.HasPrefixFold(protos, mem.S("udp")) {
if len(cols) < 3 { if len(cols) < 3 {
continue continue

View File

@ -53,12 +53,12 @@ udp46 0 0 *.146 *.*
func TestParsePortsNetstat(t *testing.T) { func TestParsePortsNetstat(t *testing.T) {
want := List{ want := List{
Port{"tcp", 23, ""}, Port{"tcp", 23, "", 0},
Port{"tcp", 24, ""}, Port{"tcp", 24, "", 0},
Port{"udp", 104, ""}, Port{"udp", 104, "", 0},
Port{"udp", 106, ""}, Port{"udp", 106, "", 0},
Port{"udp", 146, ""}, Port{"udp", 146, "", 0},
Port{"tcp", 8185, ""}, // but not 8186, 8187, 8188 on localhost Port{"tcp", 8185, "", 0}, // but not 8186, 8187, 8188 on localhost
} }
pl, err := appendParsePortsNetstat(nil, bufio.NewReader(strings.NewReader(netstatOutput))) pl, err := appendParsePortsNetstat(nil, bufio.NewReader(strings.NewReader(netstatOutput)))

View File

@ -18,6 +18,7 @@ type Port struct {
Proto string // "tcp" or "udp" Proto string // "tcp" or "udp"
Port uint16 // port number Port uint16 // port number
Process string // optional process name, if found Process string // optional process name, if found
Pid int // process id, if known
} }
// List is a list of Ports. // List is a list of Ports.

View File

@ -11,6 +11,7 @@ import (
"fmt" "fmt"
"log" "log"
"os/exec" "os/exec"
"strconv"
"strings" "strings"
"sync/atomic" "sync/atomic"
"time" "time"
@ -170,6 +171,7 @@ func (im *macOSImpl) addProcesses() error {
im.br.Reset(outPipe) im.br.Reset(outPipe)
var cmd, proto string var cmd, proto string
var pid int
for { for {
line, err := im.br.ReadBytes('\n') line, err := im.br.ReadBytes('\n')
if err != nil { if err != nil {
@ -184,6 +186,10 @@ func (im *macOSImpl) addProcesses() error {
// starting a new process // starting a new process
cmd = "" cmd = ""
proto = "" proto = ""
pid = 0
if p, err := strconv.Atoi(string(val)); err == nil {
pid = p
}
case 'c': case 'c':
cmd = string(val) // TODO(bradfitz): avoid garbage; cache process names between runs? cmd = string(val) // TODO(bradfitz): avoid garbage; cache process names between runs?
case 'P': case 'P':
@ -202,6 +208,7 @@ func (im *macOSImpl) addProcesses() error {
switch { switch {
case m != nil: case m != nil:
m.port.Process = cmd m.port.Process = cmd
m.port.Pid = pid
default: default:
// ignore: processes and ports come and go // ignore: processes and ports come and go
} }