feat: WebSocket IPC endpoint for web UI

Add --ws-port flag to waste-daemon; when set, starts a WebSocket IPC
server alongside the existing TCP one. The web UI connects to this from
the browser. Uses nhooyr.io/websocket with localhost-only origin policy.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Fredrik Johansson
2026-06-22 21:57:10 +02:00
parent c1dea1d19d
commit 77830a3b3f
2 changed files with 32 additions and 0 deletions

View File

@@ -14,14 +14,37 @@ import (
"fmt"
"log"
"net"
"net/http"
"time"
"nhooyr.io/websocket"
"github.com/waste-go/internal/crypto"
"github.com/waste-go/internal/invite"
"github.com/waste-go/internal/netmgr"
"github.com/waste-go/internal/proto"
)
// RunWS starts a WebSocket IPC server on 127.0.0.1:wsPort.
// Each WebSocket connection gets the same handleClient treatment as TCP.
// The OriginPatterns option allows connections from local dev servers.
func RunWS(mgr *netmgr.Manager, wsPort int) error {
addr := fmt.Sprintf("127.0.0.1:%d", wsPort)
log.Printf("ipc: WS listening on %s", addr)
return http.ListenAndServe(addr, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
conn, err := websocket.Accept(w, r, &websocket.AcceptOptions{
OriginPatterns: []string{"localhost:*", "127.0.0.1:*"},
})
if err != nil {
log.Printf("ipc: ws accept: %v", err)
return
}
log.Printf("ipc: WS client connected")
nc := websocket.NetConn(r.Context(), conn, websocket.MessageText)
handleClient(nc, mgr)
}))
}
// Run starts the IPC listener. Blocks until the listener fails.
func Run(mgr *netmgr.Manager, port int) error {
addr := fmt.Sprintf("127.0.0.1:%d", port)