Discover LAN servers and relay authenticated browser peer connections

This commit is contained in:
2026-11-08 21:57:05 +00:00
parent 89814d9fbe
commit 349428f11d
13 changed files with 1265 additions and 8 deletions
+51
View File
@@ -0,0 +1,51 @@
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, protocol.DiscoveredServer(server))
}
_ = write(protocol.Message{Type: protocol.DiscoverServersResult, ID: request.ID, DiscoveredServers: items})
}
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})
}
+5
View File
@@ -153,6 +153,7 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
}
runs.serveArtifact(w, r)
})
mux.HandleFunc("GET /discovery", serveServerDiscovery)
mux.HandleFunc("GET /files", serveHomeFile)
mux.HandleFunc("POST /chat-uploads", serveChatUpload)
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) {
@@ -276,6 +277,10 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
if handleDirectoryListing(request, write) {
continue
}
if request.Type == protocol.DiscoverServersRequest {
go handleServerDiscovery(connCtx, request, write)
continue
}
if request.Type == protocol.ServerStatsRequest {
go handleServerStats(connCtx, request, runs, options.ServerStatsSampler, started, build, write)
continue