diff --git a/cmd/minstrel/main.go b/cmd/minstrel/main.go index 51a06877..2afe23df 100644 --- a/cmd/minstrel/main.go +++ b/cmd/minstrel/main.go @@ -210,15 +210,11 @@ func run() error { acoustIDLookup := library.NewAcoustIDLookupWorker(pool, logger.With("component", "acoustid_lookup"), acoustIDSettings, acoustIDClient) - contact := cfg.Library.ContactEmail - if contact == "" { - contact = "https://git.fabledsword.com/bvandeusen/minstrel" - } // Swap in the production User-Agent + MinPeriod on the registered // MBCAA provider. init() registered it with default config; this // updates the running instance. coverart.NewMBCAAProviderFromConfig(coverart.FetcherConfig{ - UserAgent: fmt.Sprintf("Minstrel/dev (%s)", contact), + UserAgent: buildinfo.UserAgent(cfg.Library.ContactEmail), MinPeriod: time.Second, }) diff --git a/internal/acoustid/client.go b/internal/acoustid/client.go index 4cbfe5f3..4fec60b4 100644 --- a/internal/acoustid/client.go +++ b/internal/acoustid/client.go @@ -30,6 +30,8 @@ import ( "strconv" "sync" "time" + + "git.fabledsword.com/bvandeusen/minstrel/internal/buildinfo" ) // DefaultBaseURL is the lookup endpoint. @@ -45,7 +47,6 @@ const ( // bodyLimit caps the response read. A lookup with meta=recordings for a // popular song runs to tens of kilobytes. bodyLimit = 2 << 20 - userAgent = "Minstrel ( https://git.fabledsword.com/bvandeusen/minstrel )" ) // The server's error codes that the worker treats differently. @@ -127,7 +128,7 @@ func (c *Client) Lookup(ctx context.Context, apiKey, fingerprint string, duratio req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Content-Encoding", "gzip") req.Header.Set("Accept", "application/json") - req.Header.Set("User-Agent", userAgent) + req.Header.Set("User-Agent", buildinfo.UserAgent("")) resp, err := c.http.Do(req) if err != nil { diff --git a/internal/buildinfo/buildinfo.go b/internal/buildinfo/buildinfo.go index 67bc4d8b..8af7b4a9 100644 --- a/internal/buildinfo/buildinfo.go +++ b/internal/buildinfo/buildinfo.go @@ -29,3 +29,19 @@ var Channel = "local" // Started is when this process started, for the admin Overview's uptime. var Started = time.Now() + +// ProjectURL is where Minstrel's source lives. Outside services that ask a +// client to say how to reach its maker are pointed here. +const ProjectURL = "https://git.fabledsword.com/bvandeusen/minstrel" + +// UserAgent is how Minstrel names itself to outside services (MusicBrainz, +// the Cover Art Archive, AcoustID), which ask for the app's name, its +// version and a contact, so a release can be told from a dev build in +// their logs. contact is the operator's address when one is configured; +// without one the project's URL stands in. +func UserAgent(contact string) string { + if contact == "" { + contact = ProjectURL + } + return "Minstrel/" + Version + " ( " + contact + " )" +} diff --git a/internal/buildinfo/useragent_test.go b/internal/buildinfo/useragent_test.go new file mode 100644 index 00000000..8fd8d1fb --- /dev/null +++ b/internal/buildinfo/useragent_test.go @@ -0,0 +1,19 @@ +package buildinfo + +import "testing" + +// The User-Agent carries the build's own version, so MusicBrainz and the +// others can tell a release from a dev build, and a contact: the operator's +// when set, the project's otherwise. +func TestUserAgent_NamesTheVersionAndAContact(t *testing.T) { + was := Version + t.Cleanup(func() { Version = was }) + Version = "2026.11.28.1700" + + if got, want := UserAgent("admin@example.com"), "Minstrel/2026.11.28.1700 ( admin@example.com )"; got != want { + t.Errorf("with a contact: %q, want %q", got, want) + } + if got, want := UserAgent(""), "Minstrel/2026.11.28.1700 ( "+ProjectURL+" )"; got != want { + t.Errorf("without one: %q, want %q", got, want) + } +} diff --git a/internal/musicbrainz/client.go b/internal/musicbrainz/client.go index b0a25e93..f1ace3b7 100644 --- a/internal/musicbrainz/client.go +++ b/internal/musicbrainz/client.go @@ -10,15 +10,13 @@ import ( "net/url" "strings" "time" + + "git.fabledsword.com/bvandeusen/minstrel/internal/buildinfo" ) // DefaultBaseURL is the public MusicBrainz web service. const DefaultBaseURL = "https://musicbrainz.org/ws/2" -// UserAgent identifies Minstrel. MusicBrainz refuses requests without a -// descriptive one. -const UserAgent = "Minstrel/1.0 ( https://git.fabledsword.com/bvandeusen/minstrel )" - // DefaultTimeout bounds one request. A browse page with track // lists is the slowest thing asked for. const DefaultTimeout = 20 * time.Second @@ -130,7 +128,8 @@ func (c *Client) once(ctx context.Context, full string, out any) (retry bool, er return false, fmt.Errorf("musicbrainz: build request: %w", err) } req.Header.Set("Accept", "application/json") - req.Header.Set("User-Agent", UserAgent) + // MusicBrainz refuses requests without a descriptive User-Agent. + req.Header.Set("User-Agent", buildinfo.UserAgent("")) resp, err := c.httpClient().Do(req) if err != nil { diff --git a/internal/tags/provider_musicbrainz.go b/internal/tags/provider_musicbrainz.go index dad0f410..5661799e 100644 --- a/internal/tags/provider_musicbrainz.go +++ b/internal/tags/provider_musicbrainz.go @@ -8,6 +8,7 @@ import ( "sync/atomic" "time" + "git.fabledsword.com/bvandeusen/minstrel/internal/buildinfo" "git.fabledsword.com/bvandeusen/minstrel/internal/musicbrainz" "git.fabledsword.com/bvandeusen/minstrel/internal/providers" ) @@ -16,8 +17,6 @@ import ( var mbBaseURL = "https://musicbrainz.org/ws/2" const ( - // mbUserAgent identifies the app — MusicBrainz rejects requests without one. - mbUserAgent = musicbrainz.UserAgent // artistTagWeightFactor down-weights artist-level fallback tags relative // to recording-specific tags — the artist's overall character is a coarser // signal than a tag on the exact recording. @@ -38,8 +37,9 @@ type musicbrainzProvider struct { func init() { Register(&musicbrainzProvider{ client: providers.NewHTTPClient(providers.HTTPOptions{ - Name: "musicbrainz", - UserAgent: mbUserAgent, + Name: "musicbrainz", + // MusicBrainz rejects requests without one. + UserAgent: buildinfo.UserAgent(""), HTTPClient: &http.Client{Timeout: 30 * time.Second}, // MusicBrainz's 1 request/second is per address: share it with // the release catalogue.