navidrome/plugins/host_websocket.go
Deluan Quintão b293b96256
refactor(persistence): stateless repositories with per-call context (#6149)
* refactor(persistence): adopt generic deluan/rest repository API

Pin deluan/rest to the refactor branch. REST-facing repository methods
take a context and return typed values. Drop DataStore.Resource and
ResourceRepository; the native API names typed repositories directly
through a per-request adapter that later commits remove.

* refactor(persistence): base repository helpers take a context

* refactor(persistence): LibraryRepository takes a context per call

* refactor(persistence): PropertyRepository takes a context per call

* refactor(persistence): UserPropsRepository takes a context per call

* refactor(persistence): TranscodingRepository takes a context per call

* refactor(persistence): ShareRepository takes a context per call

* refactor(persistence): PlayerRepository takes a context per call

* refactor(persistence): RadioRepository takes a context per call

* refactor(persistence): PlayQueueRepository takes a context per call

* refactor(persistence): Tag and Genre repositories take a context per call

* refactor(persistence): PluginRepository takes a context per call

* refactor(persistence): Scrobble repositories take a context per call

* refactor(persistence): FolderRepository takes a context per call

* refactor(persistence): Artwork repositories take a context per call

* refactor(persistence): UserRepository takes a context per call

* refactor(persistence): ArtistRepository takes a context per call

ReadAll no longer rewrites the shared sort mappings for the role filter;
it works on a per-call copy.

* test(persistence): assert artist role sort sanitization in ReadAll

* refactor(persistence): AlbumRepository takes a context per call

* test(persistence): pass the test context to album repository helpers

* refactor(persistence): MediaFileRepository takes a context per call

* refactor(persistence): Playlist repositories take a context per call

* refactor(persistence): build all repositories once per store

* refactor(core): REST repository wrappers are built once

* refactor(persistence): repositories are stateless

Remove the context field from the base repository and the per-request
REST adapter. Enable the containedctx linter so no repository can hold a
request context again.

* chore(lint): skip containedctx in test files

* refactor: share simplifications from the stateless repositories sweep

Add deleteOwnedAll on sqlRepository and use it in player/share Delete
to remove the duplicated bulk-delete loop; have Share.Repository()
return model.ShareRepository so subsonic sharing.go drops its repeated
type assertions.

* chore(core): assert REST wrappers implement Persistable

* chore: reformat imports

* perf(persistence): build repositories on first use

Each transaction store used to construct all 21 repositories up front,
paying for filter and sort mapping setup the block never touched. Fields
are now sync.OnceValue thunks, so a store only builds what it uses.

* fix(persistence): clean plugin references per deleted user

A bulk user delete that fails on a later id had already removed the
earlier rows but skipped their plugin cleanup. Cleanup now runs right
after each successful delete.

* fix(core): unload disabled plugins even when a user delete fails

A bulk delete can fail on a later id after earlier users were removed
and their plugins auto-disabled. The wrapper returned before unloading,
leaving those plugins running until the next successful delete or a
restart.

* chore(deps): pin deluan/rest to v1.0.1

Replaces the pseudo-version of the refactor branch with the tagged
release. REST error messages now name the bare type (Artist, not
model.Artist).

* test: use the spec context instead of context.Background()

Replace the context.Background()/context.TODO() calls this branch added
to tests with the spec's ctx, GinkgoT().Context(), or t/b.Context(), so
repository calls are bound to the running spec's lifetime.

* test: declare the spec context once per Describe

Set ctx from GinkgoT().Context() first in each top-level BeforeEach and reuse it, building user contexts on top of it instead of repeating inline calls.
2026-09-25 18:06:10 -04:00

388 lines
11 KiB
Go

package plugins
import (
"context"
"errors"
"fmt"
"maps"
"net"
"net/http"
"net/url"
"strings"
"sync"
"syscall"
"time"
"github.com/gorilla/websocket"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model/id"
"github.com/navidrome/navidrome/plugins/capabilities"
"github.com/navidrome/navidrome/plugins/host"
)
// CapabilityWebSocket indicates the plugin can receive WebSocket callbacks.
// Detected when the plugin exports any of the WebSocket callback functions.
const CapabilityWebSocket Capability = "WebSocket"
// webSocketCallbackTimeout is the maximum duration allowed for a WebSocket callback.
const webSocketCallbackTimeout = 30 * time.Second
// WebSocket callback function names
const (
FuncWebSocketOnTextMessage = "nd_websocket_on_text_message"
FuncWebSocketOnBinaryMessage = "nd_websocket_on_binary_message"
FuncWebSocketOnError = "nd_websocket_on_error"
FuncWebSocketOnClose = "nd_websocket_on_close"
)
func init() {
registerCapability(
CapabilityWebSocket,
FuncWebSocketOnTextMessage,
FuncWebSocketOnBinaryMessage,
FuncWebSocketOnError,
FuncWebSocketOnClose,
)
}
// wsConnection represents an active WebSocket connection.
type wsConnection struct {
conn *websocket.Conn
done chan struct{}
closeMu sync.Mutex
isClosed bool
}
// webSocketServiceImpl implements host.WebSocketService.
// It provides plugins with WebSocket communication capabilities.
type webSocketServiceImpl struct {
baseCtx context.Context //nolint:containedctx // bounds the read loops, which outlive the Connect() call
pluginName string
manager *Manager
requiredHosts []string
mu sync.RWMutex
connections map[string]*wsConnection
}
// newWebSocketService creates a new WebSocketService for a plugin.
func newWebSocketService(ctx context.Context, pluginName string, manager *Manager, permission *WebSocketPermission) *webSocketServiceImpl {
return &webSocketServiceImpl{
baseCtx: ctx,
pluginName: pluginName,
manager: manager,
requiredHosts: permission.RequiredHosts,
connections: make(map[string]*wsConnection),
}
}
func (s *webSocketServiceImpl) Connect(ctx context.Context, urlStr string, headers map[string]string, connectionID string) (string, error) {
// Parse and validate URL
parsedURL, err := url.Parse(urlStr)
if err != nil {
return "", fmt.Errorf("invalid URL: %w", err)
}
// Validate scheme
if parsedURL.Scheme != "ws" && parsedURL.Scheme != "wss" {
return "", fmt.Errorf("invalid URL scheme: must be ws:// or wss://")
}
// Validate host against allowed hosts
if !s.isHostAllowed(parsedURL.Host) {
return "", fmt.Errorf("host %q is not allowed", parsedURL.Host)
}
// Generate connection ID if not provided
if connectionID == "" {
connectionID = id.NewRandom()
}
s.mu.Lock()
if _, exists := s.connections[connectionID]; exists {
s.mu.Unlock()
return "", fmt.Errorf("connection ID %q already exists", connectionID)
}
s.mu.Unlock()
// Create HTTP headers for handshake
httpHeaders := http.Header{}
for k, v := range headers {
httpHeaders.Set(k, v)
}
// Establish WebSocket connection
dialer := websocket.Dialer{
HandshakeTimeout: 30 * time.Second,
NetDialContext: (&net.Dialer{Control: s.dialControl, Resolver: dialResolver}).DialContext,
}
conn, resp, err := dialer.DialContext(ctx, urlStr, httpHeaders)
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
if err != nil {
return "", fmt.Errorf("failed to connect: %w", err)
}
wsConn := &wsConnection{
conn: conn,
done: make(chan struct{}),
}
s.mu.Lock()
s.connections[connectionID] = wsConn
s.mu.Unlock()
// Start read goroutine with the service's base context instead of the
// caller's ctx, because the readLoop must outlive the Connect() call.
// Connections are closed by Close() when the plugin is unloaded, which ends
// the readLoop; the base context is a backstop that also ends it on server
// shutdown (it is never cancelled in one-shot CLI runs).
go s.readLoop(s.baseCtx, connectionID, wsConn)
log.Debug(ctx, "WebSocket connected", "plugin", s.pluginName, "connectionID", connectionID, "url", urlStr)
return connectionID, nil
}
func (s *webSocketServiceImpl) SendText(ctx context.Context, connectionID, message string) error {
wsConn, err := s.getConnection(connectionID)
if err != nil {
return err
}
if err := wsConn.conn.WriteMessage(websocket.TextMessage, []byte(message)); err != nil {
return fmt.Errorf("failed to send text message: %w", err)
}
return nil
}
func (s *webSocketServiceImpl) SendBinary(ctx context.Context, connectionID string, data []byte) error {
wsConn, err := s.getConnection(connectionID)
if err != nil {
return err
}
if err := wsConn.conn.WriteMessage(websocket.BinaryMessage, data); err != nil {
return fmt.Errorf("failed to send binary message: %w", err)
}
return nil
}
func (s *webSocketServiceImpl) CloseConnection(ctx context.Context, connectionID string, code int32, reason string) error {
s.mu.Lock()
wsConn, exists := s.connections[connectionID]
if !exists {
s.mu.Unlock()
return fmt.Errorf("connection ID %q not found", connectionID)
}
delete(s.connections, connectionID)
s.mu.Unlock()
// Mark as closed to prevent callback
wsConn.closeMu.Lock()
wsConn.isClosed = true
wsConn.closeMu.Unlock()
// Send close message
closeMsg := websocket.FormatCloseMessage(int(code), reason)
_ = wsConn.conn.WriteControl(websocket.CloseMessage, closeMsg, time.Now().Add(5*time.Second))
_ = wsConn.conn.Close()
// Signal read goroutine to stop
close(wsConn.done)
// Invoke close callback
s.invokeOnClose(ctx, connectionID, code, reason)
log.Debug(ctx, "WebSocket connection closed", "plugin", s.pluginName, "connectionID", connectionID, "code", code)
return nil
}
// Close closes all connections for this plugin.
// This is called when the plugin is unloaded.
func (s *webSocketServiceImpl) Close() error {
s.mu.Lock()
connections := make(map[string]*wsConnection, len(s.connections))
maps.Copy(connections, s.connections)
s.connections = make(map[string]*wsConnection)
s.mu.Unlock()
ctx := context.Background()
for connID, wsConn := range connections {
wsConn.closeMu.Lock()
wsConn.isClosed = true
wsConn.closeMu.Unlock()
closeMsg := websocket.FormatCloseMessage(websocket.CloseGoingAway, "plugin unloaded")
err := wsConn.conn.WriteControl(websocket.CloseMessage, closeMsg, time.Now().Add(2*time.Second))
if err != nil {
log.Warn("Failed to send WebSocket close message on plugin unload", "plugin", s.pluginName, "connectionID", connID, "error", err)
}
err = wsConn.conn.Close()
if err != nil {
log.Warn("Failed to close WebSocket connection on plugin unload", "plugin", s.pluginName, "connectionID", connID, "error", err)
}
close(wsConn.done)
s.invokeOnClose(ctx, connID, websocket.CloseGoingAway, "plugin unloaded")
log.Debug("WebSocket connection closed on plugin unload", "plugin", s.pluginName, "connectionID", connID)
}
return nil
}
func (s *webSocketServiceImpl) getConnection(connectionID string) (*wsConnection, error) {
s.mu.RLock()
defer s.mu.RUnlock()
wsConn, exists := s.connections[connectionID]
if !exists {
return nil, fmt.Errorf("connection ID %q not found", connectionID)
}
return wsConn, nil
}
func (s *webSocketServiceImpl) isHostAllowed(host string) bool {
return isHostInAllowlist(s.requiredHosts, extractHostname(host))
}
func (s *webSocketServiceImpl) dialControl(_, address string, _ syscall.RawConn) error {
return checkPrivateDial(s.requiredHosts, address)
}
// matchHostPattern matches a host against a pattern.
// Supports "*" (allow all) and wildcards like "*.example.com".
func matchHostPattern(pattern, host string) bool {
if pattern == "*" {
return true
}
if pattern == host {
return true
}
// Handle wildcard patterns like *.example.com
if strings.HasPrefix(pattern, "*.") {
suffix := pattern[1:] // Get .example.com
return strings.HasSuffix(host, suffix)
}
return false
}
func (s *webSocketServiceImpl) readLoop(ctx context.Context, connectionID string, wsConn *wsConnection) {
defer func() {
// Remove connection if still present
s.mu.Lock()
delete(s.connections, connectionID)
s.mu.Unlock()
}()
for {
select {
case <-wsConn.done:
return
default:
}
messageType, data, err := wsConn.conn.ReadMessage()
if err != nil {
wsConn.closeMu.Lock()
isClosed := wsConn.isClosed
wsConn.closeMu.Unlock()
if isClosed {
return
}
// Check if it's a close error
if websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway, websocket.CloseNoStatusReceived) {
closeCode := websocket.CloseNoStatusReceived
closeReason := ""
if ce, ok := errors.AsType[*websocket.CloseError](err); ok {
closeCode = ce.Code
closeReason = ce.Text
}
s.invokeOnClose(ctx, connectionID, int32(closeCode), closeReason)
return
}
// Other read error
s.invokeOnError(ctx, connectionID, err.Error())
return
}
switch messageType {
case websocket.TextMessage:
s.invokeOnTextMessage(ctx, connectionID, string(data))
case websocket.BinaryMessage:
s.invokeOnBinaryMessage(ctx, connectionID, data)
}
}
}
// invokeWebSocketCallback is a generic helper that handles the common callback invocation pattern.
func invokeWebSocketCallback[I any](ctx context.Context, s *webSocketServiceImpl, funcName string, input I, callbackName string, connectionID string) {
instance := s.getPluginInstance()
if instance == nil {
return
}
callbackCtx, cancel := context.WithTimeout(ctx, webSocketCallbackTimeout)
defer cancel()
start := time.Now()
err := callPluginFunctionNoOutput(callbackCtx, instance, funcName, input)
if err != nil {
if !errors.Is(errFunctionNotFound, err) {
log.Error(ctx, "WebSocket "+callbackName+" callback failed", "plugin", s.pluginName, "connectionID", connectionID, "duration", time.Since(start), err)
}
}
}
func (s *webSocketServiceImpl) invokeOnTextMessage(ctx context.Context, connectionID, message string) {
invokeWebSocketCallback(ctx, s, FuncWebSocketOnTextMessage, capabilities.OnTextMessageRequest{
ConnectionID: connectionID,
Message: message,
}, "text message", connectionID)
}
func (s *webSocketServiceImpl) invokeOnBinaryMessage(ctx context.Context, connectionID string, data []byte) {
invokeWebSocketCallback(ctx, s, FuncWebSocketOnBinaryMessage, capabilities.OnBinaryMessageRequest{
ConnectionID: connectionID,
Data: data,
}, "binary message", connectionID)
}
func (s *webSocketServiceImpl) invokeOnError(ctx context.Context, connectionID, errorMsg string) {
invokeWebSocketCallback(ctx, s, FuncWebSocketOnError, capabilities.OnErrorRequest{
ConnectionID: connectionID,
Error: errorMsg,
}, "error", connectionID)
}
func (s *webSocketServiceImpl) invokeOnClose(ctx context.Context, connectionID string, code int32, reason string) {
invokeWebSocketCallback(ctx, s, FuncWebSocketOnClose, capabilities.OnCloseRequest{
ConnectionID: connectionID,
Code: code,
Reason: reason,
}, "close", connectionID)
}
func (s *webSocketServiceImpl) getPluginInstance() *plugin {
s.manager.mu.RLock()
instance, ok := s.manager.plugins[s.pluginName]
s.manager.mu.RUnlock()
if !ok {
log.Warn("Plugin not loaded for WebSocket callback", "plugin", s.pluginName)
return nil
}
return instance
}
// Verify interface implementation
var _ host.WebSocketService = (*webSocketServiceImpl)(nil)