From 929e4cc017145a00159fed5f7feeb83c1b13eaa5 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sun, 8 Nov 2026 23:59:19 +0000 Subject: [PATCH] Advertise tailnet and public routes in server discovery The mDNS record now carries ts= (tailnet IPv4 addresses) and public= (the installer's HTTPS origin from PIPELINES_WEB_PUBLIC_URL) in TXT, and browse, the discover_servers RPC and /discovery return every route to a machine as addresses with kinds lan, tailscale and public. Endpoint stays the LAN route for older clients. Unsafe ts/public values are dropped. --- cmd/pipelines-server/mdns.go | 5 +- docs/notes/discovery-routes.md | 25 +++++++ internal/discovery/advertise.go | 99 ++++++++++++++++++++++++--- internal/discovery/advertise_test.go | 37 ++++++++++ internal/discovery/browse.go | 36 ++++++++-- internal/discovery/browse_test.go | 28 ++++++++ internal/protocol/server_discovery.go | 16 +++-- internal/server/server_discovery.go | 10 ++- 8 files changed, 237 insertions(+), 19 deletions(-) create mode 100644 docs/notes/discovery-routes.md diff --git a/cmd/pipelines-server/mdns.go b/cmd/pipelines-server/mdns.go index 4637b083..1552ad51 100644 --- a/cmd/pipelines-server/mdns.go +++ b/cmd/pipelines-server/mdns.go @@ -15,8 +15,11 @@ import ( // The returned stop (nil when nothing was published) withdraws the advert. func advertise(addr string, proxyPort int) func() { hostname, _ := os.Hostname() + // The installer exports the TLS front's origin (tailscale serve) for the + // same proxy; it is advertised so clients can choose that route by name. advert, err := discovery.Plan(discovery.Config{ListenAddr: addr, ProxyPort: proxyPort, - Version: buildinfo.Current().Version, Hostname: hostname, IPs: discovery.LANAddresses()}) + Version: buildinfo.Current().Version, Hostname: hostname, IPs: discovery.LANAddresses(), + TailscaleIPs: discovery.TailscaleAddresses(), PublicURL: os.Getenv("PIPELINES_WEB_PUBLIC_URL")}) if err != nil { log.Printf("mDNS: not advertising: %v", err) return nil diff --git a/docs/notes/discovery-routes.md b/docs/notes/discovery-routes.md new file mode 100644 index 00000000..1cb3320a --- /dev/null +++ b/docs/notes/discovery-routes.md @@ -0,0 +1,25 @@ +# Discovery advertises every route to a machine + +`internal/discovery.Plan` adds two optional TXT keys to the `_pipelines._tcp` +record: `ts=[,]` lists the host's tailnet addresses +(100.64.0.0/10, found on any up interface, including the tunnel interfaces +`LANAddresses` skips) and `public=` is the TLS front's +origin for the same web proxy, taken from `PIPELINES_WEB_PUBLIC_URL`, which the +installer already exports to the server process. `PublicOrigin` accepts only a +bare origin: no credentials, path, query or fragment. + +`Browse` returns `FoundServer.Addresses`, every route to the same machine with +`Kind` `lan` (the mDNS A record and advertised port), `tailscale` (same scheme +and port on each tailnet IP) or `public` (`wss://` for an https origin, path +`/connect`). `Endpoint` stays the LAN route for older clients. The +`discover_servers` RPC and `/discovery` (and the proxy's `/api/discovery`, +which forwards the body verbatim) carry the same `addresses` array. + +A Tailscale HTTPS certificate covers only the MagicDNS name, so there is no +`wss://` route; the tailnet IP route is `ws://` on the plain web +port, encrypted by WireGuard. + +Tests: `TestPlanAdvertisesTailnetAndPublicRoutes`, +`TestBrowseListsEveryRouteToTheSameMachine` (unsafe `ts`/`public` values are +dropped). `go test ./internal/discovery ./internal/server ./internal/protocol` +passes. diff --git a/internal/discovery/advertise.go b/internal/discovery/advertise.go index a544c42a..35a3885b 100644 --- a/internal/discovery/advertise.go +++ b/internal/discovery/advertise.go @@ -13,6 +13,7 @@ import ( "fmt" "log" "net" + "net/url" "strconv" "strings" @@ -35,6 +36,12 @@ type Config struct { Hostname string // IPs are the LAN addresses clients may dial. IPs []net.IP + // TailscaleIPs are this host's tailnet addresses (100.64.0.0/10). They are + // published in TXT because mDNS A records only carry the LAN. + TailscaleIPs []net.IP + // PublicURL is the HTTPS origin a TLS front (tailscale serve) exposes for + // the same web proxy, for example https://box.tail1.ts.net:8443. + PublicURL string } // Advert is the record set the server publishes. @@ -78,23 +85,97 @@ func Plan(cfg Config) (Advert, error) { if version == "" { version = "dev" } + // TXT keys follow RFC 6763 (short, lower case). The scheme and path are + // what the client puts in its ws(s):// URL. Extra routes to this same + // machine follow: `ts` lists tailnet IPv4 addresses that dial the same + // port, `public` an HTTPS origin fronting the same proxy. + txt := []string{ + "v=" + version, + "name=" + label, + "path=/connect", + "tls=0", + "auth=" + auth, + } + if ts := joinIPs(cfg.TailscaleIPs); ts != "" { + txt = append(txt, "ts="+ts) + } + if public, ok := PublicOrigin(cfg.PublicURL); ok { + txt = append(txt, "public="+public) + } return Advert{ Instance: "Pipelines-" + label, Host: label + ".local.", Port: port, IPs: cfg.IPs, - // TXT keys follow RFC 6763 (short, lower case). The scheme and path are - // what the client puts in its ws(s):// URL. - TXT: []string{ - "v=" + version, - "name=" + label, - "path=/connect", - "tls=0", - "auth=" + auth, - }, + TXT: txt, }, nil } +// PublicOrigin validates an advertised HTTPS/HTTP origin: scheme and host +// (with optional port) only, no credentials, path, query or fragment, and +// short enough for one TXT string. +func PublicOrigin(raw string) (string, bool) { + raw = strings.TrimSpace(raw) + if raw == "" || len(raw) > 200 { + return "", false + } + u, err := url.Parse(raw) + if err != nil || (u.Scheme != "https" && u.Scheme != "http") || u.Hostname() == "" || u.User != nil || + (u.Path != "" && u.Path != "/") || u.RawQuery != "" || u.Fragment != "" || u.Opaque != "" { + return "", false + } + if port := u.Port(); port != "" { + if n, err := strconv.Atoi(port); err != nil || n < 1 || n > 65535 { + return "", false + } + } + return u.Scheme + "://" + u.Host, true +} + +func joinIPs(ips []net.IP) string { + parts := make([]string, 0, len(ips)) + for _, ip := range ips { + if ip4 := ip.To4(); ip4 != nil && len(parts) < 4 { + parts = append(parts, ip4.String()) + } + } + return strings.Join(parts, ",") +} + +// tailnet is the CGNAT range Tailscale assigns its IPv4 addresses from. +var tailnet = net.IPNet{IP: net.IPv4(100, 64, 0, 0), Mask: net.CIDRMask(10, 32)} + +// IsTailscaleIP reports whether ip is a tailnet IPv4 address. +func IsTailscaleIP(ip net.IP) bool { + ip4 := ip.To4() + return ip4 != nil && tailnet.Contains(ip4) +} + +// TailscaleAddresses lists this host's tailnet IPv4 addresses on any up +// interface, including the tunnel interfaces LANAddresses skips. +func TailscaleAddresses() []net.IP { + interfaces, err := net.Interfaces() + if err != nil { + return nil + } + var ips []net.IP + for _, iface := range interfaces { + if iface.Flags&net.FlagUp == 0 { + continue + } + addrs, err := iface.Addrs() + if err != nil { + continue + } + for _, addr := range addrs { + if network, ok := addr.(*net.IPNet); ok && IsTailscaleIP(network.IP) { + ips = append(ips, network.IP.To4()) + } + } + } + return ips +} + // hostLabel reduces a host name to one DNS label: the first dotted part, with // anything other than letters, digits and hyphens replaced, so instance and // host names never need escaping on the wire. diff --git a/internal/discovery/advertise_test.go b/internal/discovery/advertise_test.go index 8b454189..76e1e99a 100644 --- a/internal/discovery/advertise_test.go +++ b/internal/discovery/advertise_test.go @@ -4,6 +4,7 @@ import ( "errors" "net" "slices" + "strings" "testing" "github.com/miekg/dns" @@ -120,3 +121,39 @@ func TestVirtualInterfacesAreSkipped(t *testing.T) { } } } + +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") + } +} diff --git a/internal/discovery/browse.go b/internal/discovery/browse.go index 0325b7e1..bc400232 100644 --- a/internal/discovery/browse.go +++ b/internal/discovery/browse.go @@ -15,11 +15,22 @@ import ( ) // FoundServer is a validated service advertised on the serving host's LAN. +// Endpoint is its LAN route; Addresses lists every route to this same +// machine (LAN first), so a client can offer the choice. type FoundServer struct { - Name string `json:"name"` + Name string `json:"name"` + Endpoint string `json:"endpoint"` + Auth string `json:"auth"` + Version string `json:"version"` + Addresses []FoundAddress `json:"addresses"` +} + +// FoundAddress is one way to dial a discovered machine. Kind is "lan", +// "tailscale" (a tailnet IP, same port as the LAN) or "public" (an HTTPS +// origin fronting the same proxy, usually the machine's tailnet DNS name). +type FoundAddress struct { + Kind string `json:"kind"` Endpoint string `json:"endpoint"` - Auth string `json:"auth"` - Version string `json:"version"` } // Browse sends one bounded DNS-SD query, never a network port scan. @@ -109,5 +120,22 @@ func discovered(entry *mdns.ServiceEntry) (FoundServer, bool) { return FoundServer{}, false } endpoint := (&url.URL{Scheme: scheme, Host: net.JoinHostPort(ip.String(), strconv.Itoa(entry.Port)), Path: path}).String() - return FoundServer{Name: name, Endpoint: endpoint, Auth: auth, Version: fields["v"]}, true + addresses := []FoundAddress{{Kind: "lan", Endpoint: endpoint}} + for _, value := range strings.Split(fields["ts"], ",") { + ts := net.ParseIP(strings.TrimSpace(value)) + if ts == nil || !IsTailscaleIP(ts) || len(addresses) > 5 { + continue + } + addresses = append(addresses, FoundAddress{Kind: "tailscale", + Endpoint: (&url.URL{Scheme: scheme, Host: net.JoinHostPort(ts.String(), strconv.Itoa(entry.Port)), Path: path}).String()}) + } + if public, ok := PublicOrigin(fields["public"]); ok { + u, _ := url.Parse(public) + wsScheme := "ws" + if u.Scheme == "https" { + wsScheme = "wss" + } + addresses = append(addresses, FoundAddress{Kind: "public", Endpoint: (&url.URL{Scheme: wsScheme, Host: u.Host, Path: path}).String()}) + } + return FoundServer{Name: name, Endpoint: endpoint, Auth: auth, Version: fields["v"], Addresses: addresses}, true } diff --git a/internal/discovery/browse_test.go b/internal/discovery/browse_test.go index b0db8fef..58631851 100644 --- a/internal/discovery/browse_test.go +++ b/internal/discovery/browse_test.go @@ -3,6 +3,7 @@ package discovery import ( "context" "net" + "slices" "testing" "github.com/hashicorp/mdns" @@ -59,3 +60,30 @@ func TestDiscoveryRejectsMalformedAndUnsafeAdvertisements(t *testing.T) { t.Fatal("accepted invalid port") } } + +func TestBrowseListsEveryRouteToTheSameMachine(t *testing.T) { + item := service("192.168.1.2") + item.InfoFields = append(item.InfoFields, "ts=100.85.25.33, 10.0.0.9,100.70.1.1", "public=https://box.tail1.ts.net:8443") + found, ok := discovered(item) + if !ok { + t.Fatal("rejected a valid advertisement with extra routes") + } + want := []FoundAddress{ + {Kind: "lan", Endpoint: "ws://192.168.1.2:8083/connect"}, + {Kind: "tailscale", Endpoint: "ws://100.85.25.33:8083/connect"}, + {Kind: "tailscale", Endpoint: "ws://100.70.1.1:8083/connect"}, + {Kind: "public", Endpoint: "wss://box.tail1.ts.net:8443/connect"}, + } + if !slices.Equal(found.Addresses, want) || found.Endpoint != want[0].Endpoint { + t.Fatalf("addresses = %+v", found.Addresses) + } + plain, _ := discovered(service("192.168.1.2")) + if !slices.Equal(plain.Addresses, want[:1]) { + t.Fatalf("a LAN-only advert must list its one route: %+v", plain.Addresses) + } + bad := service("192.168.1.2") + bad.InfoFields = append(bad.InfoFields, "ts=evil.example", "public=javascript:alert(1)") + if found, _ := discovered(bad); len(found.Addresses) != 1 { + t.Fatalf("unsafe routes accepted: %+v", found.Addresses) + } +} diff --git a/internal/protocol/server_discovery.go b/internal/protocol/server_discovery.go index c00d5264..659da8ea 100644 --- a/internal/protocol/server_discovery.go +++ b/internal/protocol/server_discovery.go @@ -6,9 +6,17 @@ const ( ) // DiscoveredServer is one Pipelines DNS-SD advertisement on the serving LAN. +// Endpoint is the LAN route; Addresses lists every route to the same machine. type DiscoveredServer struct { - Name string `json:"name"` - Endpoint string `json:"endpoint"` - Auth string `json:"auth"` - Version string `json:"version"` + Name string `json:"name"` + Endpoint string `json:"endpoint"` + Auth string `json:"auth"` + Version string `json:"version"` + Addresses []DiscoveredAddress `json:"addresses"` +} + +// DiscoveredAddress is one route: kind "lan", "tailscale" or "public". +type DiscoveredAddress struct { + Kind string `json:"kind"` + Endpoint string `json:"endpoint"` } diff --git a/internal/server/server_discovery.go b/internal/server/server_discovery.go index 0a5448e9..40c2073f 100644 --- a/internal/server/server_discovery.go +++ b/internal/server/server_discovery.go @@ -32,11 +32,19 @@ func handleServerDiscovery(ctx context.Context, request protocol.Message, write } items := make([]protocol.DiscoveredServer, 0, len(servers)) for _, server := range servers { - items = append(items, protocol.DiscoveredServer(server)) + items = append(items, discoveredServer(server)) } _ = write(protocol.Message{Type: protocol.DiscoverServersResult, ID: request.ID, DiscoveredServers: items}) } +func discoveredServer(server discovery.FoundServer) protocol.DiscoveredServer { + addresses := make([]protocol.DiscoveredAddress, 0, len(server.Addresses)) + for _, address := range server.Addresses { + addresses = append(addresses, protocol.DiscoveredAddress(address)) + } + return protocol.DiscoveredServer{Name: server.Name, Endpoint: server.Endpoint, Auth: server.Auth, Version: server.Version, Addresses: addresses} +} + func serveServerDiscovery(w http.ResponseWriter, r *http.Request) { servers, err := discoverServers(r.Context()) w.Header().Set("Cache-Control", "no-store")