Files
pipelines/internal/discovery/advertise_test.go
T
alan 7ecc12b747 LAN discovery: server advertises _pipelines._tcp over mDNS; app finds servers on the connect screen
Server: internal/discovery publishes a DNS-SD record (hashicorp/mdns) with
the port LAN clients dial (the web proxy via -mdns-port, which the
supervisor passes, or a non-loopback -addr) and TXT v, name, path, tls, auth.
On by default; -mdns=false or PIPELINES_MDNS=off disables it. Failures are
logged and never block startup; the advert is withdrawn on shutdown.

App: the Connect to Pipelines screen gets 'Find servers on this network'
behind a ServerDiscovery interface (Bonsoir: Android NSD with multicast lock,
iOS Bonjour with NSLocalNetworkUsageDescription and NSBonjourServices).
Tapping a server fills the address and connects; manual entry stays. Web has
no scan.
2026-11-03 14:54:54 +00:00

123 lines
3.9 KiB
Go

package discovery
import (
"errors"
"net"
"slices"
"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)
}
}
}