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).
279 lines
8.5 KiB
Go
279 lines
8.5 KiB
Go
// Package discovery advertises the Pipelines server on the local network with
|
|
// mDNS/DNS-SD (Bonjour), so a phone on the same LAN can find it without typing
|
|
// an address.
|
|
//
|
|
// The advertised port is the one a LAN client dials. The installed server binds
|
|
// loopback behind the authenticated web proxy, so the installer passes the
|
|
// proxy's port (-mdns-port); a server bound to a LAN address with no proxy
|
|
// advertises its own listen port and no authentication.
|
|
package discovery
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/mdns"
|
|
)
|
|
|
|
// ServiceType is the DNS-SD service type clients browse for.
|
|
const ServiceType = "_pipelines._tcp"
|
|
|
|
// Config is what the server knows about how LAN clients reach it.
|
|
type Config struct {
|
|
// ListenAddr is the server's own -addr.
|
|
ListenAddr string
|
|
// ProxyPort, when non-zero, is the port of the authenticated web proxy on
|
|
// this host that fronts the server for LAN clients.
|
|
ProxyPort int
|
|
// Version is the server build version.
|
|
Version string
|
|
// Hostname is this machine's host name (os.Hostname).
|
|
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
|
|
// NoLAN means the proxy refuses LAN connections (the installer's loopback
|
|
// + tailnet default). The advert then carries no LAN route: its address
|
|
// records are the tailnet IPv4 addresses, TXT says lan=0, and tailnet
|
|
// clients on the same LAN still find the machine through ts= and public=.
|
|
NoLAN bool
|
|
}
|
|
|
|
// Advert is the record set the server publishes.
|
|
type Advert struct {
|
|
Instance string
|
|
Host string // fully qualified, e.g. "box.local."
|
|
Port int
|
|
IPs []net.IP
|
|
TXT []string
|
|
}
|
|
|
|
// ErrNotReachable means no LAN client could dial what the server would
|
|
// advertise (loopback-only listener without a proxy, or no LAN address).
|
|
var ErrNotReachable = errors.New("not reachable from the LAN")
|
|
|
|
// Plan decides what to advertise. It is pure, so tests check record content
|
|
// without touching the network.
|
|
func Plan(cfg Config) (Advert, error) {
|
|
port, auth := cfg.ProxyPort, "password"
|
|
if port == 0 {
|
|
host, value, err := net.SplitHostPort(cfg.ListenAddr)
|
|
if err != nil {
|
|
return Advert{}, fmt.Errorf("listen address %q: %w", cfg.ListenAddr, err)
|
|
}
|
|
if ip := net.ParseIP(host); host == "localhost" || (ip != nil && ip.IsLoopback()) {
|
|
return Advert{}, fmt.Errorf("server listens on loopback %s and no -mdns-port names its LAN proxy: %w", cfg.ListenAddr, ErrNotReachable)
|
|
}
|
|
if port, err = strconv.Atoi(value); err != nil || port <= 0 {
|
|
return Advert{}, fmt.Errorf("listen address %q has no usable port: %w", cfg.ListenAddr, ErrNotReachable)
|
|
}
|
|
auth = "none"
|
|
}
|
|
if port <= 0 || port > 65535 {
|
|
return Advert{}, fmt.Errorf("advertised port %d is out of range", port)
|
|
}
|
|
ips := cfg.IPs
|
|
if cfg.NoLAN {
|
|
ips = nil
|
|
for _, ip := range cfg.TailscaleIPs {
|
|
if IsTailscaleIP(ip) {
|
|
ips = append(ips, ip.To4())
|
|
}
|
|
}
|
|
if len(ips) == 0 {
|
|
return Advert{}, fmt.Errorf("LAN connections are refused and there is no tailnet address: %w", ErrNotReachable)
|
|
}
|
|
} else if len(ips) == 0 {
|
|
return Advert{}, fmt.Errorf("no LAN IPv4 address: %w", ErrNotReachable)
|
|
}
|
|
label := hostLabel(cfg.Hostname)
|
|
version := cfg.Version
|
|
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 cfg.NoLAN {
|
|
txt = append(txt, "lan=0")
|
|
}
|
|
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: ips,
|
|
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.
|
|
func hostLabel(hostname string) string {
|
|
first, _, _ := strings.Cut(strings.TrimSpace(hostname), ".")
|
|
var b strings.Builder
|
|
for _, r := range first {
|
|
switch {
|
|
case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9', r == '-':
|
|
b.WriteRune(r)
|
|
default:
|
|
b.WriteRune('-')
|
|
}
|
|
}
|
|
label := strings.Trim(b.String(), "-")
|
|
if label == "" {
|
|
return "pipelines"
|
|
}
|
|
if len(label) > 50 {
|
|
label = label[:50]
|
|
}
|
|
return label
|
|
}
|
|
|
|
// Zone builds the DNS-SD zone that answers queries for the advert.
|
|
func (a Advert) Zone() (*mdns.MDNSService, error) {
|
|
return mdns.NewMDNSService(a.Instance, ServiceType, "local.", a.Host, a.Port, a.IPs, a.TXT)
|
|
}
|
|
|
|
// Start publishes the advert until the returned stop is called. Failure is an
|
|
// error for the caller to log; it never blocks startup.
|
|
func Start(a Advert, logger *log.Logger) (stop func(), err error) {
|
|
zone, err := a.Zone()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
server, err := mdns.NewServer(&mdns.Config{Zone: zone, Logger: logger})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return func() { _ = server.Shutdown() }, nil
|
|
}
|
|
|
|
// LANAddresses lists the up, multicast-capable, physical interfaces' IPv4
|
|
// addresses. VPN, tunnel, container and loopback interfaces are skipped: a LAN
|
|
// client cannot dial them (the tailnet address is reached by name instead).
|
|
func LANAddresses() []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 || iface.Flags&net.FlagMulticast == 0 || iface.Flags&net.FlagLoopback != 0 || virtual(iface.Name) {
|
|
continue
|
|
}
|
|
addrs, err := iface.Addrs()
|
|
if err != nil {
|
|
continue
|
|
}
|
|
for _, addr := range addrs {
|
|
network, ok := addr.(*net.IPNet)
|
|
if !ok {
|
|
continue
|
|
}
|
|
if ip4 := network.IP.To4(); ip4 != nil && !ip4.IsLinkLocalUnicast() && !ip4.IsLoopback() {
|
|
ips = append(ips, ip4)
|
|
}
|
|
}
|
|
}
|
|
return ips
|
|
}
|
|
|
|
func virtual(name string) bool {
|
|
for _, prefix := range []string{"lo", "docker", "veth", "br-", "virbr", "tun", "tap", "utun", "wg", "tailscale", "zt", "vpn", "bridge", "awdl", "llw", "anpi", "ap"} {
|
|
if strings.HasPrefix(name, prefix) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|