mirror of
https://github.com/tailscale/tailscale.git
synced 2025-08-20 01:47:33 +00:00
portlist: add Poller.IncludeLocalhost option
This PR parameterizes receiving loopback updates from the portlist package. Callers can now include services bound to localhost if they want. Note that this option is off by default still. Fixes #8171 Signed-off-by: Marwan Sulaiman <marwan@tailscale.com>
This commit is contained in:

committed by
Marwan Sulaiman

parent
3d180a16c3
commit
e32e5c0d0c
@@ -8,6 +8,7 @@ package portlist
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -52,30 +53,40 @@ 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
|
||||
}
|
||||
|
||||
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) {
|
||||
t.Fatalf("Got:\n%s\n\nWant:\n%s\n", jgot, jwant)
|
||||
}
|
||||
for i := range pl {
|
||||
if pl[i] != want[i] {
|
||||
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)
|
||||
}
|
||||
for _, loopBack := range [...]bool{false, true} {
|
||||
t.Run(fmt.Sprintf("loopback_%v", loopBack), func(t *testing.T) {
|
||||
want := List{
|
||||
{"tcp", 23, "", 0},
|
||||
{"tcp", 24, "", 0},
|
||||
{"udp", 104, "", 0},
|
||||
{"udp", 106, "", 0},
|
||||
{"udp", 146, "", 0},
|
||||
{"tcp", 8185, "", 0}, // but not 8186, 8187, 8188 on localhost, when loopback is false
|
||||
}
|
||||
if loopBack {
|
||||
want = append(want,
|
||||
Port{"tcp", 8186, "", 0},
|
||||
Port{"tcp", 8187, "", 0},
|
||||
Port{"tcp", 8188, "", 0},
|
||||
)
|
||||
}
|
||||
pl, err := appendParsePortsNetstat(nil, bufio.NewReader(strings.NewReader(netstatOutput)), loopBack)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pl = sortAndDedup(pl)
|
||||
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 {
|
||||
if pl[i] != want[i] {
|
||||
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)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user