portlist: exclude services bound to IPv6 loopback address

Fixes #1683

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2021-04-12 09:06:57 -07:00
parent 87eb8384f5
commit f325aa7e38
2 changed files with 17 additions and 2 deletions

View File

@ -42,7 +42,10 @@ func parsePort(s string) int {
} }
func isLoopbackAddr(s string) bool { func isLoopbackAddr(s string) bool {
return strings.HasPrefix(s, "127.0.0.1:") || strings.HasPrefix(s, "127.0.0.1.") return strings.HasPrefix(s, "127.0.0.1:") ||
strings.HasPrefix(s, "127.0.0.1.") ||
strings.HasPrefix(s, "[::1]:") ||
strings.HasPrefix(s, "::1.")
} }
type nothing struct{} type nothing struct{}

View File

@ -5,6 +5,7 @@
package portlist package portlist
import ( import (
"encoding/json"
"testing" "testing"
) )
@ -40,7 +41,11 @@ type InOut struct {
// macOS // macOS
tcp4 0 0 *.23 *.* LISTEN tcp4 0 0 *.23 *.* LISTEN
tcp6 0 0 *.24 *.* LISTEN tcp6 0 0 *.24 *.* LISTEN
tcp4 0 0 *.8185 *.* LISTEN
tcp4 0 0 127.0.0.1.8186 *.* LISTEN
tcp6 0 0 ::1.8187 *.* LISTEN
udp6 0 0 *.5453 *.* udp6 0 0 *.5453 *.*
udp4 0 0 *.5553 *.* udp4 0 0 *.5553 *.*
@ -73,14 +78,21 @@ func TestParsePortsNetstat(t *testing.T) {
Port{"udp", 5354, "", ""}, Port{"udp", 5354, "", ""},
Port{"udp", 5453, "", ""}, Port{"udp", 5453, "", ""},
Port{"udp", 5553, "", ""}, Port{"udp", 5553, "", ""},
Port{"tcp", 8185, "", ""}, // but not 8186 or 8187 on localhost
Port{"udp", 9353, "iTunes", ""}, Port{"udp", 9353, "iTunes", ""},
} }
pl := parsePortsNetstat(netstatOutput) pl := parsePortsNetstat(netstatOutput)
jgot, _ := json.MarshalIndent(pl, "", "\t")
jwant, _ := json.MarshalIndent(want, "", "\t")
if len(pl) != len(want) {
t.Fatalf("Got:\n%s\n\nWant:\n%s\n", jgot, jwant)
}
for i := range pl { for i := range pl {
if pl[i] != want[i] { 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]) i, pl[i], want[i])
t.Fatalf("Got:\n%s\n\nWant:\n%s\n", jgot, jwant)
} }
} }
} }