Vellum now speaks two foreign protocols, so the clients people already own can
use it. Both are translation layers over queries that already exist, and both go
through the same grant chokepoint as the web app — a token is a second way to be
a user, never a second set of permissions.
Shared foundation (coordinator, written before dispatch)
- migrations/0012_api_tokens.sql + internal/apitoken. Both protocols need a
credential that is not the session cookie, and letting two agents each invent
a token table would have produced two incompatible ones.
- The table stores two shapes for a real reason: a Jellyfin token is a bearer
value so only its hash is kept, while a Subsonic key must be recoverable
because verifying `t` means computing md5(key + salt) server-side. token_plain
is therefore the one column in the schema holding a usable credential. It is a
generated random secret, never a user password, and it is revocable.
Wave 9 - Jellyfin
- /System/Info/Public, /Users/AuthenticateByName, /Users/Me, /Users/{id},
/Items, /Users/{id}/Items, /Items/{id}, /Items/{id}/Images/Primary,
/Items/{id}/PlaybackInfo, /Videos/{id}/stream, /Audio/{id}/stream,
/Sessions/Playing{,/Progress,/Stopped}.
- Auth accepts X-Emby-Token, X-MediaBrowser-Token, ?api_key=, and
Authorization: MediaBrowser Token="...". The ?api_key= form is the one that
matters: <video> elements cannot set headers.
- Honest about the one thing Vellum cannot do. PlaybackInfo reports
SupportsDirectPlay/SupportsDirectStream true and SupportsTranscoding FALSE,
with a DirectStreamUrl carrying the api_key. A client that disables both
direct modes is demanding a transcode and is refused with ErrorCode
"NoCompatibleStream" — rather than handed a TranscodingUrl that would never
resolve, which is how a client hangs forever.
Wave 10 - Subsonic
- /rest/{method} dispatch with ping, getLicense, getMusicFolders, getIndexes,
getArtists/getArtist/getAlbum, getMusicDirectory, getSong, search3, stream,
getCoverArt, getAlbumList2, getRandomSongs, scrobble, getUser, getPlaylists,
plus GET /api/v1/subsonic/credentials to hand a user their key.
- Auth via t=md5(key+salt), p=key, constant-time comparison. A bad credential
returns HTTP 200 with the envelope status "failed" and error code 40 — NOT an
HTTP 401. Subsonic clients read the envelope, and getting this wrong is the
single most common integration failure in that protocol.
- stream supports format=raw and never invokes ffmpeg.
Coordinator wired both into main.go (the line agents were told to leave alone)
and verified against real requests.
Verified end to end, 18 packages pass, gofmt/vet clean, svelte-check 0/0:
- Jellyfin: discovery, AuthenticateByName minting a token, /Items reporting 18,
PlaybackInfo showing SupportsTranscoding=false, and a Range request to
/Videos/{id}/stream using ONLY ?api_key= returning 206 with Content-Range.
- Subsonic: ping as JSON and XML, md5(key+salt) auth succeeding, and a bad token
returning the failed envelope with code 40.
- Scoping, both protocols, with a genuinely non-admin user: 0 items, 404 on an
admin-only item, 404 on its stream, 0 music folders; admin still sees 18.
- The SPA is not shadowed by the root-mounted compat paths: /, /healthz and
/api/v1/version all still answer correctly.
Both agents failed with an HTTP 400 at the REPORT stage, not during the work;
their files were complete and compile, so the work was salvaged and verified
here rather than redone.
126 lines
3.9 KiB
Go
126 lines
3.9 KiB
Go
package jellyfin
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/mithral/vellum/internal/auth"
|
|
"github.com/mithral/vellum/internal/httpx"
|
|
"github.com/mithral/vellum/internal/stream"
|
|
)
|
|
|
|
// handlePlaybackInfo answers GET /Items/{itemId}/PlaybackInfo.
|
|
//
|
|
// The response always describes the one thing Vellum can do — direct play — and
|
|
// reports SupportsTranscoding=false. If the client has disabled both direct
|
|
// play and direct stream it is asking for a transcode; that is refused with
|
|
// ErrorCode "NoCompatibleStream" and a warning, rather than by handing back a
|
|
// TranscodingUrl that would not exist.
|
|
func (h *Handlers) handlePlaybackInfo(w http.ResponseWriter, r *http.Request, u auth.User) {
|
|
id, isLib, ok := decodeID(r.PathValue("itemId"))
|
|
if !ok || isLib {
|
|
httpx.WriteError(w, http.StatusNotFound, "item not found")
|
|
return
|
|
}
|
|
it, ok := h.authorizedItem(w, r, u, id)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
q := r.URL.Query()
|
|
directPlay := queryBoolDefault(q.Get("EnableDirectPlay"), true)
|
|
directStream := queryBoolDefault(q.Get("EnableDirectStream"), true)
|
|
|
|
src := buildMediaSource(it, tokenFromRequest(r))
|
|
resp := PlaybackInfoResponse{MediaSources: []MediaSourceInfo{src}}
|
|
if !directPlay && !directStream {
|
|
resp.ErrorCode = "NoCompatibleStream"
|
|
h.log.Warn("jellyfin client requires transcoding; refusing",
|
|
"item", it.ID, "kind", it.Kind)
|
|
}
|
|
httpx.WriteJSON(w, http.StatusOK, resp)
|
|
}
|
|
|
|
// handleVideoStream serves a video item's file.
|
|
func (h *Handlers) handleVideoStream(w http.ResponseWriter, r *http.Request, u auth.User) {
|
|
h.serveStream(w, r, u)
|
|
}
|
|
|
|
// handleAudioStream serves an audio (or other non-video) item's file. Jellyfin
|
|
// clients fetch audiobook and music tracks here.
|
|
func (h *Handlers) handleAudioStream(w http.ResponseWriter, r *http.Request, u auth.User) {
|
|
h.serveStream(w, r, u)
|
|
}
|
|
|
|
// serveStream streams the item's first file through internal/stream, so Range
|
|
// requests (seeking) work exactly as they do for the web app. Auth already came
|
|
// from tokenFromRequest, which includes the ?api_key= form that <video>/<audio>
|
|
// elements require.
|
|
func (h *Handlers) serveStream(w http.ResponseWriter, r *http.Request, u auth.User) {
|
|
// Accept /stream and /stream.<ext> (stream.mp4, stream.mkv, ...). The
|
|
// subtree pattern also catches other suffixes; refuse those.
|
|
last := path.Base(strings.TrimSuffix(r.URL.Path, "/"))
|
|
if last != "stream" && !strings.HasPrefix(last, "stream.") {
|
|
httpx.WriteError(w, http.StatusNotFound, "not found")
|
|
return
|
|
}
|
|
|
|
id, isLib, ok := decodeID(r.PathValue("itemId"))
|
|
if !ok || isLib {
|
|
httpx.WriteError(w, http.StatusNotFound, "item not found")
|
|
return
|
|
}
|
|
if _, ok := h.authorizedItem(w, r, u, id); !ok {
|
|
return
|
|
}
|
|
|
|
var root, rel string
|
|
err := h.db.QueryRowContext(r.Context(), `
|
|
SELECT l.root_path, f.path
|
|
FROM media_items i
|
|
JOIN libraries l ON l.id = i.library_id
|
|
JOIN media_files f ON f.item_id = i.id
|
|
WHERE i.id = ?
|
|
ORDER BY f.id LIMIT 1`, id).Scan(&root, &rel)
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
httpx.WriteError(w, http.StatusNotFound, "item has no file")
|
|
return
|
|
}
|
|
if err != nil {
|
|
h.fail(w, "load file", err)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Disposition", "inline")
|
|
if err := stream.ServeFile(w, r, filepath.Join(root, filepath.FromSlash(rel))); err != nil {
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
httpx.WriteError(w, http.StatusNotFound, "file is not available")
|
|
return
|
|
}
|
|
if errors.Is(err, stream.ErrNotAFile) {
|
|
httpx.WriteError(w, http.StatusBadRequest, "item is not a file")
|
|
return
|
|
}
|
|
h.fail(w, "serve file", err)
|
|
return
|
|
}
|
|
}
|
|
|
|
// queryBoolDefault parses a Jellyfin boolean query parameter, returning def
|
|
// when it is absent or unparseable.
|
|
func queryBoolDefault(v string, def bool) bool {
|
|
switch strings.ToLower(strings.TrimSpace(v)) {
|
|
case "":
|
|
return def
|
|
case "1", "true", "yes":
|
|
return true
|
|
case "0", "false", "no":
|
|
return false
|
|
}
|
|
return def
|
|
}
|