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.
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/buildinfo"
|
||||
"git.a-wan.net/alan/pipelines/internal/discovery"
|
||||
"git.a-wan.net/alan/pipelines/internal/opencode"
|
||||
"git.a-wan.net/alan/pipelines/internal/paths"
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
@@ -34,6 +35,8 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
db := flag.String("db", defaultDB, "SQLite database path (honors XDG_DATA_HOME)")
|
||||
mdnsOn := flag.Bool("mdns", envEnabled(os.Getenv("PIPELINES_MDNS")), "Advertise the server on the LAN with mDNS/DNS-SD ("+discovery.ServiceType+"); PIPELINES_MDNS=off disables")
|
||||
mdnsPort := flag.Int("mdns-port", envPort(os.Getenv("PIPELINES_MDNS_PORT")), "Port LAN clients dial: the authenticated web proxy fronting this server (default: the -addr port when it is not loopback)")
|
||||
flag.Parse()
|
||||
if err := os.MkdirAll(filepath.Dir(*db), 0700); err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -147,6 +150,11 @@ func main() {
|
||||
if len(chats.Backends()) > 0 {
|
||||
chatNames = chats.Backends()
|
||||
}
|
||||
if *mdnsOn {
|
||||
if stopMDNS := advertise(*addr, *mdnsPort); stopMDNS != nil {
|
||||
defer stopMDNS()
|
||||
}
|
||||
}
|
||||
printBanner(startup{Build: buildinfo.Current(), Addr: *addr, Database: *db, Artifacts: artifactsRoot,
|
||||
Authoring: *allowAuthoring, OpenCode: openCodeStatus, Chat: chatNames})
|
||||
log.Printf("listening on %s (WebSocket endpoint: /connect)", *addr)
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/buildinfo"
|
||||
"git.a-wan.net/alan/pipelines/internal/discovery"
|
||||
)
|
||||
|
||||
// advertise publishes the server on the LAN. Any failure is logged and the
|
||||
// server carries on: discovery is a convenience, never a startup requirement.
|
||||
// The returned stop (nil when nothing was published) withdraws the advert.
|
||||
func advertise(addr string, proxyPort int) func() {
|
||||
hostname, _ := os.Hostname()
|
||||
advert, err := discovery.Plan(discovery.Config{ListenAddr: addr, ProxyPort: proxyPort,
|
||||
Version: buildinfo.Current().Version, Hostname: hostname, IPs: discovery.LANAddresses()})
|
||||
if err != nil {
|
||||
log.Printf("mDNS: not advertising: %v", err)
|
||||
return nil
|
||||
}
|
||||
stop, err := discovery.Start(advert, log.Default())
|
||||
if err != nil {
|
||||
log.Printf("mDNS: advertising failed: %v", err)
|
||||
return nil
|
||||
}
|
||||
log.Printf("mDNS: advertising %s.%s on port %d (%s)", advert.Instance, discovery.ServiceType, advert.Port, strings.Join(advert.TXT, " "))
|
||||
return stop
|
||||
}
|
||||
|
||||
// envEnabled reads PIPELINES_MDNS: unset or anything but an explicit off value
|
||||
// leaves advertising on.
|
||||
func envEnabled(value string) bool {
|
||||
switch strings.ToLower(strings.TrimSpace(value)) {
|
||||
case "0", "false", "off", "no", "disabled":
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// envPort reads PIPELINES_MDNS_PORT; anything unusable means "not set".
|
||||
func envPort(value string) int {
|
||||
port, err := strconv.Atoi(strings.TrimSpace(value))
|
||||
if err != nil || port < 0 || port > 65535 {
|
||||
return 0
|
||||
}
|
||||
return port
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestMDNSEnvironmentSwitches(t *testing.T) {
|
||||
for value, want := range map[string]bool{"": true, "1": true, "on": true, "off": false, "0": false, "FALSE": false, " disabled ": false} {
|
||||
if got := envEnabled(value); got != want {
|
||||
t.Errorf("envEnabled(%q) = %v, want %v", value, got, want)
|
||||
}
|
||||
}
|
||||
for value, want := range map[string]int{"": 0, "8083": 8083, "x": 0, "70000": 0} {
|
||||
if got := envPort(value); got != want {
|
||||
t.Errorf("envPort(%q) = %d, want %d", value, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A loopback-only server with no proxy port publishes nothing and does not
|
||||
// fail: the caller only defers a non-nil stop.
|
||||
func TestAdvertiseSkipsLoopbackWithoutFailing(t *testing.T) {
|
||||
if stop := advertise("127.0.0.1:0", 0); stop != nil {
|
||||
stop()
|
||||
t.Fatal("loopback-only server was advertised")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user