mirror of
https://github.com/navidrome/navidrome.git
synced 2026-09-23 19:55:43 -04:00
* fix(plugins): build public URLs on the caller's address instead of localhost The artwork host service had no `*http.Request`, so it passed `nil` to `publicurl.ImageURL`. With neither `ShareURL` nor `BaseURL` configured, that produced `http://localhost/share/img/...`, which is useless to anything outside the server. The Discord Rich Presence plugin explicitly drops localhost URLs, so it fell back to the Navidrome logo instead of the real cover art. `serverAddressMiddleware` already works out the client-facing scheme and host from the `X-Forwarded-*` headers. It now also records them in the request context, and `publicurl` takes a `context.Context` instead of an `*http.Request` so any caller can reach them. Extism passes the caller's context through to host functions, so plugins invoked during a request now get a reachable URL with no configuration. Switching the parameter also removes the need for a second, parallel entry point: the package previously wanted only a scheme, a host, and a context, and took a whole request to get them. `AbsoluteURL` no longer dereferences a possibly-nil request on its parse-error path. Plugin calls that start from `context.Background()` (scheduler and websocket callbacks, the buffered scrobble drain) still fall back to localhost, since they have no request to learn from. A debug log now points at `ShareURL` when that happens. * fix(publicurl): include the configured port in the localhost fallback The last-resort fallback built `http://localhost/...`, which points at port 80 and so is unreachable for a server listening anywhere else — the default 4533 included. Use `conf.Server.Port` so a consumer on the same machine can actually fetch the URL. * fix(publicurl): use https in the localhost fallback when TLS is configured The fallback hardcoded the http scheme, so a TLS-only server with no BaseURL advertised a URL it does not answer on. Mirror the server's own switch, which requires both a certificate and a key. * refactor(publicurl): tidy the localhost fallback and its tests Use gg.If for the fallback scheme so it reads as an expression, like the BaseScheme branch above it, instead of assigning http and overwriting it. Drop two tests the ctx refactor left redundant: one asserted PublicURL "works without a request" but became a byte-identical copy of the ShareURL spec once the *http.Request parameter went away, and the two port specs differed only in the integer, where the non-default port is the stronger assertion. * refactor(conf): add TLSEnabled and use it instead of repeating the predicate Whether the server speaks HTTPS was decided inline in three unconnected places. This PR added the third, in a URL-building package that has no business inferring the transport config. Move the rule to conf, next to the fields it derives from, and call it from publicurl and the insights collector. server.Run keeps its own expression: it takes the certificate and key as parameters, and its test passes values that do not come from the config.
180 lines
4.6 KiB
Go
180 lines
4.6 KiB
Go
package request
|
|
|
|
import (
|
|
"context"
|
|
"sync/atomic"
|
|
|
|
"github.com/navidrome/navidrome/model"
|
|
)
|
|
|
|
type contextKey string
|
|
|
|
const (
|
|
User = contextKey("user")
|
|
Username = contextKey("username")
|
|
Client = contextKey("client")
|
|
Version = contextKey("version")
|
|
Player = contextKey("player")
|
|
Transcoding = contextKey("transcoding")
|
|
ClientUniqueId = contextKey("clientUniqueId")
|
|
ReverseProxyIp = contextKey("reverseProxyIp")
|
|
InternalAuth = contextKey("internalAuth") // Used for internal API calls, e.g., from the plugins
|
|
TokenEpochHolder = contextKey("tokenEpochHolder")
|
|
ServerAddress = contextKey("serverAddress")
|
|
)
|
|
|
|
var allKeys = []contextKey{
|
|
User,
|
|
Username,
|
|
Client,
|
|
Version,
|
|
Player,
|
|
Transcoding,
|
|
ClientUniqueId,
|
|
ReverseProxyIp,
|
|
InternalAuth,
|
|
ServerAddress,
|
|
}
|
|
|
|
func WithUser(ctx context.Context, u model.User) context.Context {
|
|
return context.WithValue(ctx, User, u)
|
|
}
|
|
|
|
func WithUsername(ctx context.Context, username string) context.Context {
|
|
return context.WithValue(ctx, Username, username)
|
|
}
|
|
|
|
func WithClient(ctx context.Context, client string) context.Context {
|
|
return context.WithValue(ctx, Client, client)
|
|
}
|
|
|
|
func WithVersion(ctx context.Context, version string) context.Context {
|
|
return context.WithValue(ctx, Version, version)
|
|
}
|
|
|
|
func WithPlayer(ctx context.Context, player model.Player) context.Context {
|
|
return context.WithValue(ctx, Player, player)
|
|
}
|
|
|
|
func WithTranscoding(ctx context.Context, t model.Transcoding) context.Context {
|
|
return context.WithValue(ctx, Transcoding, t)
|
|
}
|
|
|
|
func WithClientUniqueId(ctx context.Context, clientUniqueId string) context.Context {
|
|
return context.WithValue(ctx, ClientUniqueId, clientUniqueId)
|
|
}
|
|
|
|
func WithReverseProxyIp(ctx context.Context, reverseProxyIp string) context.Context {
|
|
return context.WithValue(ctx, ReverseProxyIp, reverseProxyIp)
|
|
}
|
|
|
|
func WithInternalAuth(ctx context.Context, username string) context.Context {
|
|
return context.WithValue(ctx, InternalAuth, username)
|
|
}
|
|
|
|
// serverAddress is the public scheme and host the client used to reach this server,
|
|
// so code running without an http.Request can still build absolute URLs.
|
|
type serverAddress struct {
|
|
scheme string
|
|
host string
|
|
}
|
|
|
|
func WithServerAddress(ctx context.Context, scheme, host string) context.Context {
|
|
return context.WithValue(ctx, ServerAddress, serverAddress{scheme: scheme, host: host})
|
|
}
|
|
|
|
func ServerAddressFrom(ctx context.Context) (scheme, host string, ok bool) {
|
|
a, ok := ctx.Value(ServerAddress).(serverAddress)
|
|
if !ok || a.host == "" {
|
|
return "", "", false
|
|
}
|
|
return a.scheme, a.host, true
|
|
}
|
|
|
|
func UserFrom(ctx context.Context) (model.User, bool) {
|
|
v, ok := ctx.Value(User).(model.User)
|
|
return v, ok
|
|
}
|
|
|
|
func UsernameFrom(ctx context.Context) (string, bool) {
|
|
v, ok := ctx.Value(Username).(string)
|
|
return v, ok
|
|
}
|
|
|
|
func ClientFrom(ctx context.Context) (string, bool) {
|
|
v, ok := ctx.Value(Client).(string)
|
|
return v, ok
|
|
}
|
|
|
|
func VersionFrom(ctx context.Context) (string, bool) {
|
|
v, ok := ctx.Value(Version).(string)
|
|
return v, ok
|
|
}
|
|
|
|
func PlayerFrom(ctx context.Context) (model.Player, bool) {
|
|
v, ok := ctx.Value(Player).(model.Player)
|
|
return v, ok
|
|
}
|
|
|
|
func TranscodingFrom(ctx context.Context) (model.Transcoding, bool) {
|
|
v, ok := ctx.Value(Transcoding).(model.Transcoding)
|
|
return v, ok
|
|
}
|
|
|
|
func ClientUniqueIdFrom(ctx context.Context) (string, bool) {
|
|
v, ok := ctx.Value(ClientUniqueId).(string)
|
|
return v, ok
|
|
}
|
|
|
|
func ReverseProxyIpFrom(ctx context.Context) (string, bool) {
|
|
v, ok := ctx.Value(ReverseProxyIp).(string)
|
|
return v, ok
|
|
}
|
|
|
|
func InternalAuthFrom(ctx context.Context) (string, bool) {
|
|
if v := ctx.Value(InternalAuth); v != nil {
|
|
if username, ok := v.(string); ok {
|
|
return username, true
|
|
}
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
func AddValues(ctx, requestCtx context.Context) context.Context {
|
|
for _, key := range allKeys {
|
|
if v := requestCtx.Value(key); v != nil {
|
|
ctx = context.WithValue(ctx, key, v)
|
|
}
|
|
}
|
|
return ctx
|
|
}
|
|
|
|
type tokenEpochHolder struct {
|
|
value atomic.Int64
|
|
}
|
|
|
|
// WithTokenEpochHolder installs a slot a handler can use to report a bumped token epoch
|
|
// back to middleware that has already returned from the handler's perspective.
|
|
func WithTokenEpochHolder(ctx context.Context) context.Context {
|
|
h := &tokenEpochHolder{}
|
|
h.value.Store(-1)
|
|
return context.WithValue(ctx, TokenEpochHolder, h)
|
|
}
|
|
|
|
func SetTokenEpoch(ctx context.Context, epoch int) {
|
|
if h, ok := ctx.Value(TokenEpochHolder).(*tokenEpochHolder); ok {
|
|
h.value.Store(int64(epoch))
|
|
}
|
|
}
|
|
|
|
func TokenEpochFrom(ctx context.Context) (int, bool) {
|
|
h, ok := ctx.Value(TokenEpochHolder).(*tokenEpochHolder)
|
|
if !ok {
|
|
return 0, false
|
|
}
|
|
if v := h.value.Load(); v >= 0 {
|
|
return int(v), true
|
|
}
|
|
return 0, false
|
|
}
|