ad6cdab418
The web proxy binds the wildcard and drops connections that are not loopback or Tailscale at both ends. A LAN listener is opt-in (web.lan) and refuses sign-in and signed-in pages over plain HTTP unless web.lan_plain_login is set. Existing installs keep their bind address with a warning. Without the LAN the mDNS advert carries only the tailnet and public routes (lan=0).
196 lines
7.3 KiB
Go
196 lines
7.3 KiB
Go
package discovery
|
|
|
|
import (
|
|
"errors"
|
|
"net"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/miekg/dns"
|
|
)
|
|
|
|
var lan = []net.IP{net.ParseIP("192.168.1.20").To4()}
|
|
|
|
func TestPlanAdvertisesTheWebProxyWithPasswordAuth(t *testing.T) {
|
|
advert, err := Plan(Config{ListenAddr: "127.0.0.1:8080", ProxyPort: 8083, Version: "v1.4.0", Hostname: "Alans-Mac.local", IPs: lan})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if advert.Port != 8083 || advert.Instance != "Pipelines-Alans-Mac" || advert.Host != "Alans-Mac.local." {
|
|
t.Fatalf("advert = %+v", advert)
|
|
}
|
|
want := []string{"v=v1.4.0", "name=Alans-Mac", "path=/connect", "tls=0", "auth=password"}
|
|
if !slices.Equal(advert.TXT, want) {
|
|
t.Fatalf("TXT = %q, want %q", advert.TXT, want)
|
|
}
|
|
}
|
|
|
|
func TestPlanAdvertisesADirectLANListenerWithoutAuth(t *testing.T) {
|
|
advert, err := Plan(Config{ListenAddr: "0.0.0.0:8080", Hostname: "box", IPs: lan})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if advert.Port != 8080 || !slices.Contains(advert.TXT, "auth=none") || !slices.Contains(advert.TXT, "v=dev") {
|
|
t.Fatalf("advert = %+v", advert)
|
|
}
|
|
}
|
|
|
|
func TestPlanRefusesWhatALANClientCannotDial(t *testing.T) {
|
|
for name, cfg := range map[string]Config{
|
|
"loopback only": {ListenAddr: "127.0.0.1:8080", Hostname: "box", IPs: lan},
|
|
"localhost only": {ListenAddr: "localhost:8080", Hostname: "box", IPs: lan},
|
|
"no LAN address": {ListenAddr: "127.0.0.1:8080", ProxyPort: 8083, Hostname: "box"},
|
|
} {
|
|
if _, err := Plan(cfg); !errors.Is(err, ErrNotReachable) {
|
|
t.Errorf("%s: err = %v, want ErrNotReachable", name, err)
|
|
}
|
|
}
|
|
if _, err := Plan(Config{ListenAddr: "127.0.0.1:8080", ProxyPort: 70000, IPs: lan}); err == nil {
|
|
t.Error("out-of-range port accepted")
|
|
}
|
|
}
|
|
|
|
func TestHostLabelIsOneSafeDNSLabel(t *testing.T) {
|
|
for in, want := range map[string]string{
|
|
"Alans-MacBook-Pro.local": "Alans-MacBook-Pro",
|
|
"my box_2": "my-box-2",
|
|
"": "pipelines",
|
|
"...": "pipelines",
|
|
} {
|
|
if got := hostLabel(in); got != want {
|
|
t.Errorf("hostLabel(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The zone answers a DNS-SD browse (PTR), resolve (SRV + TXT) and address
|
|
// lookup (A) with the advertised content.
|
|
func TestZoneAnswersBrowseResolveAndAddressQueries(t *testing.T) {
|
|
advert, err := Plan(Config{ListenAddr: "127.0.0.1:8080", ProxyPort: 8083, Version: "v2", Hostname: "box", IPs: lan})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
zone, err := advert.Zone()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
instance := "Pipelines-box._pipelines._tcp.local."
|
|
|
|
ptr := zone.Records(dns.Question{Name: "_pipelines._tcp.local.", Qtype: dns.TypePTR, Qclass: dns.ClassINET})
|
|
if len(ptr) == 0 || ptr[0].(*dns.PTR).Ptr != instance {
|
|
t.Fatalf("PTR = %v", ptr)
|
|
}
|
|
srv := zone.Records(dns.Question{Name: instance, Qtype: dns.TypeSRV, Qclass: dns.ClassINET})
|
|
if len(srv) == 0 || srv[0].(*dns.SRV).Port != 8083 || srv[0].(*dns.SRV).Target != "box.local." {
|
|
t.Fatalf("SRV = %v", srv)
|
|
}
|
|
txt := zone.Records(dns.Question{Name: instance, Qtype: dns.TypeTXT, Qclass: dns.ClassINET})
|
|
if len(txt) != 1 || !slices.Equal(txt[0].(*dns.TXT).Txt, advert.TXT) {
|
|
t.Fatalf("TXT = %v", txt)
|
|
}
|
|
a := zone.Records(dns.Question{Name: "box.local.", Qtype: dns.TypeA, Qclass: dns.ClassINET})
|
|
if len(a) != 1 || !a[0].(*dns.A).A.Equal(lan[0]) {
|
|
t.Fatalf("A = %v", a)
|
|
}
|
|
// The records survive a wire round trip unescaped.
|
|
msg := new(dns.Msg)
|
|
msg.Answer = append(append(ptr, srv...), txt...)
|
|
packed, err := msg.Pack()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var back dns.Msg
|
|
if err := back.Unpack(packed); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if back.Answer[0].(*dns.PTR).Ptr != instance {
|
|
t.Fatalf("round trip PTR = %v", back.Answer[0])
|
|
}
|
|
}
|
|
|
|
func TestVirtualInterfacesAreSkipped(t *testing.T) {
|
|
for _, name := range []string{"utun3", "tailscale0", "docker0", "lo0", "awdl0", "bridge100"} {
|
|
if !virtual(name) {
|
|
t.Errorf("%s treated as LAN", name)
|
|
}
|
|
}
|
|
for _, name := range []string{"en0", "eth0", "wlan0", "enp3s0"} {
|
|
if virtual(name) {
|
|
t.Errorf("%s treated as virtual", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPlanAdvertisesTailnetAndPublicRoutes(t *testing.T) {
|
|
advert, err := Plan(Config{ListenAddr: "127.0.0.1:8080", ProxyPort: 8083, Version: "v1", Hostname: "box", IPs: lan,
|
|
TailscaleIPs: []net.IP{net.ParseIP("100.85.25.33"), net.ParseIP("fd7a:115c:a1e0::d501:199e")},
|
|
PublicURL: "https://box.tail1.ts.net:8443"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !slices.Contains(advert.TXT, "ts=100.85.25.33") || !slices.Contains(advert.TXT, "public=https://box.tail1.ts.net:8443") {
|
|
t.Fatalf("TXT = %q", advert.TXT)
|
|
}
|
|
plain, _ := Plan(Config{ListenAddr: "127.0.0.1:8080", ProxyPort: 8083, Hostname: "box", IPs: lan, PublicURL: "https://user@box.ts.net/app?x=1"})
|
|
for _, item := range plain.TXT {
|
|
if strings.HasPrefix(item, "ts=") || strings.HasPrefix(item, "public=") {
|
|
t.Fatalf("unsafe or empty route advertised: %q", plain.TXT)
|
|
}
|
|
}
|
|
for raw, want := range map[string]string{
|
|
"https://box.tail1.ts.net:8443": "https://box.tail1.ts.net:8443",
|
|
"https://box.tail1.ts.net/": "https://box.tail1.ts.net",
|
|
"http://10.0.0.5:8083": "http://10.0.0.5:8083",
|
|
"wss://box.ts.net:8443": "",
|
|
"https://box.ts.net:99999": "",
|
|
"https://box.ts.net/path": "",
|
|
"https://user:pw@box.ts.net": "",
|
|
"https://box.ts.net?next=/login": "",
|
|
} {
|
|
got, ok := PublicOrigin(raw)
|
|
if got != want || ok != (want != "") {
|
|
t.Errorf("PublicOrigin(%q) = %q, %v", raw, got, ok)
|
|
}
|
|
}
|
|
if !IsTailscaleIP(net.ParseIP("100.127.255.254")) || IsTailscaleIP(net.ParseIP("100.128.0.1")) || IsTailscaleIP(net.ParseIP("10.0.0.1")) {
|
|
t.Fatal("tailnet range check")
|
|
}
|
|
}
|
|
|
|
// With LAN connections refused the advert names no LAN route: the address
|
|
// records are the tailnet IPs and TXT says lan=0, so tailnet clients on the
|
|
// same LAN still find the machine on the proxy port that answers them.
|
|
func TestPlanWithoutLANAdvertisesOnlyTailnetAndPublicRoutes(t *testing.T) {
|
|
tailnetIPs := []net.IP{net.ParseIP("100.85.25.33"), net.ParseIP("fd7a:115c:a1e0::d501:199e")}
|
|
advert, err := Plan(Config{ListenAddr: "127.0.0.1:8080", ProxyPort: 8083, Version: "v1", Hostname: "box", IPs: lan,
|
|
TailscaleIPs: tailnetIPs, PublicURL: "https://box.tail1.ts.net:8443", NoLAN: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if advert.Port != 8083 || len(advert.IPs) != 1 || !advert.IPs[0].Equal(net.ParseIP("100.85.25.33")) {
|
|
t.Fatalf("advert = %+v", advert)
|
|
}
|
|
for _, want := range []string{"lan=0", "ts=100.85.25.33", "public=https://box.tail1.ts.net:8443"} {
|
|
if !slices.Contains(advert.TXT, want) {
|
|
t.Fatalf("TXT %q lacks %s", advert.TXT, want)
|
|
}
|
|
}
|
|
zone, err := advert.Zone()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
records := zone.Records(dns.Question{Name: advert.Host, Qtype: dns.TypeA, Qclass: dns.ClassINET})
|
|
if len(records) != 1 || !records[0].(*dns.A).A.Equal(net.ParseIP("100.85.25.33")) {
|
|
t.Fatalf("A records = %v", records)
|
|
}
|
|
if _, err := Plan(Config{ListenAddr: "127.0.0.1:8080", ProxyPort: 8083, Hostname: "box", IPs: lan, NoLAN: true,
|
|
PublicURL: "https://box.tail1.ts.net:8443"}); !errors.Is(err, ErrNotReachable) {
|
|
t.Fatalf("no tailnet address must not publish: %v", err)
|
|
}
|
|
withLAN, _ := Plan(Config{ListenAddr: "127.0.0.1:8080", ProxyPort: 8083, Hostname: "box", IPs: lan, TailscaleIPs: tailnetIPs})
|
|
if slices.Contains(withLAN.TXT, "lan=0") || !withLAN.IPs[0].Equal(lan[0]) {
|
|
t.Fatalf("LAN advert changed: %+v", withLAN)
|
|
}
|
|
}
|