929e4cc017
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.
60 lines
1.9 KiB
Go
60 lines
1.9 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/discovery"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
var discoverySlots = make(chan struct{}, 4)
|
|
|
|
func discoverServers(ctx context.Context) ([]discovery.FoundServer, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, 4*time.Second)
|
|
defer cancel()
|
|
select {
|
|
case discoverySlots <- struct{}{}:
|
|
defer func() { <-discoverySlots }()
|
|
return discovery.Browse(ctx)
|
|
case <-ctx.Done():
|
|
return nil, ctx.Err()
|
|
}
|
|
}
|
|
|
|
func handleServerDiscovery(ctx context.Context, request protocol.Message, write func(protocol.Message) error) {
|
|
servers, err := discoverServers(ctx)
|
|
if err != nil {
|
|
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "LAN discovery unavailable: " + err.Error()})
|
|
return
|
|
}
|
|
items := make([]protocol.DiscoveredServer, 0, len(servers))
|
|
for _, server := range servers {
|
|
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")
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if err != nil {
|
|
http.Error(w, "LAN discovery unavailable", http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
_ = json.NewEncoder(w).Encode(struct {
|
|
Servers []discovery.FoundServer `json:"discovered_servers"`
|
|
}{servers})
|
|
}
|