Republish mDNS on address change; build the APK natively when the host can
The server compares its LAN and tailnet addresses every 30 seconds and republishes the advert when they change, so a machine that moves networks stops advertising the address it had at startup. ./install --apk builds on this machine with the host Flutter and Android SDK when both exist (--docker forces the container build), signing with the same shared debug keystore so phones keep accepting updates.
This commit is contained in:
@@ -192,9 +192,7 @@ func main() {
|
||||
chatNames = chats.Backends()
|
||||
}
|
||||
if *mdnsOn {
|
||||
if stopMDNS := advertise(*addr, *mdnsPort); stopMDNS != nil {
|
||||
defer stopMDNS()
|
||||
}
|
||||
defer keepAdvertising(*addr, *mdnsPort)()
|
||||
}
|
||||
printBanner(startup{Build: buildinfo.Current(), Addr: *addr, Database: *db, Artifacts: artifactsRoot,
|
||||
Authoring: *allowAuthoring, OpenCode: openCodeStatus, Chat: chatNames})
|
||||
|
||||
@@ -2,9 +2,12 @@ package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/buildinfo"
|
||||
"git.a-wan.net/alan/pipelines/internal/discovery"
|
||||
@@ -14,12 +17,16 @@ import (
|
||||
// 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() {
|
||||
return advertiseWith(addr, proxyPort, discovery.LANAddresses(), discovery.TailscaleAddresses())
|
||||
}
|
||||
|
||||
func advertiseWith(addr string, proxyPort int, lan, tailnet []net.IP) 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(),
|
||||
TailscaleIPs: discovery.TailscaleAddresses(), PublicURL: os.Getenv("PIPELINES_WEB_PUBLIC_URL")})
|
||||
Version: buildinfo.Current().Version, Hostname: hostname, IPs: lan,
|
||||
TailscaleIPs: tailnet, PublicURL: os.Getenv("PIPELINES_WEB_PUBLIC_URL")})
|
||||
if err != nil {
|
||||
log.Printf("mDNS: not advertising: %v", err)
|
||||
return nil
|
||||
@@ -51,3 +58,73 @@ func envPort(value string) int {
|
||||
}
|
||||
return port
|
||||
}
|
||||
|
||||
// addressRecheck is how often the advert is compared with the host's current
|
||||
// addresses. A laptop that changes networks must not keep advertising the IP
|
||||
// it had at startup.
|
||||
const addressRecheck = 30 * time.Second
|
||||
|
||||
// keepAdvertising publishes the server now and republishes whenever the
|
||||
// host's LAN or tailnet addresses change. The returned stop ends both.
|
||||
func keepAdvertising(addr string, proxyPort int) func() {
|
||||
lan, tailnet := discovery.LANAddresses(), discovery.TailscaleAddresses()
|
||||
stop := advertiseWith(addr, proxyPort, lan, tailnet)
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
ticker := time.NewTicker(addressRecheck)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-done:
|
||||
return
|
||||
case <-ticker.C:
|
||||
nextLAN, nextTailnet := discovery.LANAddresses(), discovery.TailscaleAddresses()
|
||||
if sameAddresses(lan, nextLAN) && sameAddresses(tailnet, nextTailnet) {
|
||||
continue
|
||||
}
|
||||
if stop != nil {
|
||||
stop()
|
||||
}
|
||||
lan, tailnet = nextLAN, nextTailnet
|
||||
log.Printf("mDNS: addresses changed to %s; republishing", joinIPs(lan))
|
||||
stop = advertiseWith(addr, proxyPort, lan, tailnet)
|
||||
}
|
||||
}
|
||||
}()
|
||||
return func() {
|
||||
close(done)
|
||||
if stop != nil {
|
||||
stop()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// sameAddresses compares two address sets regardless of order.
|
||||
func sameAddresses(a, b []net.IP) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
as, bs := make([]string, len(a)), make([]string, len(b))
|
||||
for i := range a {
|
||||
as[i], bs[i] = a[i].String(), b[i].String()
|
||||
}
|
||||
sort.Strings(as)
|
||||
sort.Strings(bs)
|
||||
for i := range as {
|
||||
if as[i] != bs[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func joinIPs(ips []net.IP) string {
|
||||
parts := make([]string, 0, len(ips))
|
||||
for _, ip := range ips {
|
||||
parts = append(parts, ip.String())
|
||||
}
|
||||
if len(parts) == 0 {
|
||||
return "no LAN address"
|
||||
}
|
||||
return strings.Join(parts, ", ")
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"net"
|
||||
"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} {
|
||||
@@ -23,3 +26,24 @@ func TestAdvertiseSkipsLoopbackWithoutFailing(t *testing.T) {
|
||||
t.Fatal("loopback-only server was advertised")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSameAddressesIgnoresOrder(t *testing.T) {
|
||||
a := []net.IP{net.ParseIP("10.0.0.75"), net.ParseIP("192.168.104.82")}
|
||||
b := []net.IP{net.ParseIP("192.168.104.82"), net.ParseIP("10.0.0.75")}
|
||||
if !sameAddresses(a, b) {
|
||||
t.Fatal("same addresses in another order compared unequal")
|
||||
}
|
||||
if sameAddresses(a, b[:1]) || sameAddresses(nil, b) || sameAddresses(a, []net.IP{net.ParseIP("10.0.0.75"), net.ParseIP("10.0.0.76")}) {
|
||||
t.Fatal("different address sets compared equal")
|
||||
}
|
||||
if !sameAddresses(nil, nil) {
|
||||
t.Fatal("two empty sets must compare equal")
|
||||
}
|
||||
}
|
||||
|
||||
// keepAdvertising on a loopback-only server publishes nothing, keeps its
|
||||
// watcher running and stops cleanly.
|
||||
func TestKeepAdvertisingStopsCleanly(t *testing.T) {
|
||||
stop := keepAdvertising("127.0.0.1:0", 0)
|
||||
stop()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user