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
continue
}
if isLoopbackAddr(laddr) {
// not interested in loopback-bound listeners
continue
}
} else if mem.HasPrefixFold(protos, mem.S("udp")) {
if len(cols) < 3 {
continue

View File

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

View File

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

View File

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