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).
131 lines
3.8 KiB
Go
131 lines
3.8 KiB
Go
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"
|
|
)
|
|
|
|
// 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, lanRoute bool) func() {
|
|
return advertiseWith(addr, proxyPort, lanRoute, discovery.LANAddresses(), discovery.TailscaleAddresses())
|
|
}
|
|
|
|
func advertiseWith(addr string, proxyPort int, lanRoute bool, 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: lan,
|
|
TailscaleIPs: tailnet, PublicURL: os.Getenv("PIPELINES_WEB_PUBLIC_URL"), NoLAN: !lanRoute})
|
|
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
|
|
}
|
|
|
|
// 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, lanRoute bool) func() {
|
|
lan, tailnet := discovery.LANAddresses(), discovery.TailscaleAddresses()
|
|
stop := advertiseWith(addr, proxyPort, lanRoute, 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, lanRoute, 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, ", ")
|
|
}
|