portlist: add macOS osImpl, finish migration to new style

Previously:

* 036f70b7b4 for linux
* 35bee36549 for windows

This does macOS.

And removes all the compat code for the old style. (e.g. iOS, js are
no longer mentioned; all platforms without implementations just
default to not doing anything)

One possible regression is that platforms without explicit
implementations previously tried to do the "netstat -na" style to get
open ports (but not process names). Maybe that worked on FreeBSD and
OpenBSD previously, but nobody ever really tested it. And it was kinda
useless without associated process names. So better off removing those
for now until they get a good implementation.

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2022-11-04 06:41:36 -07:00
committed by Brad Fitzpatrick
parent da8def8e13
commit 21ef7e5c35
10 changed files with 239 additions and 290 deletions

View File

@@ -2,13 +2,17 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build !ios && !js
//go:build darwin && !ios
package portlist
import (
"bufio"
"encoding/json"
"strings"
"testing"
"go4.org/mem"
)
func TestParsePort(t *testing.T) {
@@ -27,7 +31,7 @@ func TestParsePort(t *testing.T) {
}
for _, io := range tests {
got := parsePort(io.in)
got := parsePort(mem.S(io.in))
if got != io.expect {
t.Fatalf("input:%#v expect:%v got:%v\n", io.in, io.expect, got)
}
@@ -35,12 +39,6 @@ func TestParsePort(t *testing.T) {
}
const netstatOutput = `
// linux
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
udp 0 0 0.0.0.0:5353 0.0.0.0:*
udp6 0 0 :::5353 :::*
udp6 0 0 :::5354 :::*
// macOS
tcp4 0 0 *.23 *.* LISTEN
tcp6 0 0 *.24 *.* LISTEN
@@ -49,43 +47,26 @@ tcp4 0 0 127.0.0.1.8186 *.* LISTEN
tcp6 0 0 ::1.8187 *.* LISTEN
tcp4 0 0 127.1.2.3.8188 *.* LISTEN
udp6 0 0 *.5453 *.*
udp4 0 0 *.5553 *.*
// Windows 10
Proto Local Address Foreign Address State
TCP 0.0.0.0:32 0.0.0.0:0 LISTENING
[sshd.exe]
UDP 0.0.0.0:5050 *:*
CDPSvc
[svchost.exe]
UDP 0.0.0.0:53 *:*
[chrome.exe]
UDP 10.0.1.43:9353 *:*
[iTunes.exe]
UDP [::]:53 *:*
UDP [::]:53 *:*
[funball.exe]
udp6 0 0 *.106 *.*
udp4 0 0 *.104 *.*
udp46 0 0 *.146 *.*
`
func TestParsePortsNetstat(t *testing.T) {
want := List{
Port{"tcp", 22, ""},
Port{"tcp", 23, ""},
Port{"tcp", 24, ""},
Port{"tcp", 32, "sshd"},
Port{"udp", 53, "chrome"},
Port{"udp", 53, "funball"},
Port{"udp", 5050, "CDPSvc"},
Port{"udp", 5353, ""},
Port{"udp", 5354, ""},
Port{"udp", 5453, ""},
Port{"udp", 5553, ""},
Port{"udp", 104, ""},
Port{"udp", 106, ""},
Port{"udp", 146, ""},
Port{"tcp", 8185, ""}, // but not 8186, 8187, 8188 on localhost
Port{"udp", 9353, "iTunes"},
}
pl := appendParsePortsNetstat(nil, netstatOutput)
pl, err := appendParsePortsNetstat(nil, bufio.NewReader(strings.NewReader(netstatOutput)))
if err != nil {
t.Fatal(err)
}
pl = sortAndDedup(pl)
jgot, _ := json.MarshalIndent(pl, "", "\t")
jwant, _ := json.MarshalIndent(want, "", "\t")
if len(pl) != len(want) {
@@ -93,7 +74,7 @@ func TestParsePortsNetstat(t *testing.T) {
}
for i := range pl {
if pl[i] != want[i] {
t.Errorf("row#%d\n got: %#v\n\nwant: %#v\n",
t.Errorf("row#%d\n got: %+v\n\nwant: %+v\n",
i, pl[i], want[i])
t.Fatalf("Got:\n%s\n\nWant:\n%s\n", jgot, jwant)
}