Define the UA once

This commit is contained in:
Dennis C. Oosterhof 2025-04-24 14:20:23 +02:00
parent 4742770a63
commit acb690123f
3 changed files with 13 additions and 7 deletions

View file

@ -14,6 +14,12 @@ import (
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
) )
// Define common constants
const (
// UserAgent is the user agent string used for all HTTP requests
UserAgent = "FediResolve/1.0 (https://melroy.org)"
)
// fetchActivityPubObjectWithSignature is a helper function that always signs HTTP requests // fetchActivityPubObjectWithSignature is a helper function that always signs HTTP requests
// This is the preferred way to fetch ActivityPub content as many instances require signatures // This is the preferred way to fetch ActivityPub content as many instances require signatures
func (r *Resolver) fetchActivityPubObjectWithSignature(objectURL string) (string, error) { func (r *Resolver) fetchActivityPubObjectWithSignature(objectURL string) (string, error) {
@ -59,7 +65,7 @@ func (r *Resolver) fetchActivityPubObjectWithSignature(objectURL string) (string
// Set headers // Set headers
req.Header.Set("Accept", "application/activity+json, application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\", application/json") req.Header.Set("Accept", "application/activity+json, application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\", application/json")
req.Header.Set("User-Agent", "FediResolve/1.0 (https://github.com/dennis/fediresolve)") req.Header.Set("User-Agent", UserAgent)
req.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat)) req.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
// Sign the request // Sign the request
@ -135,7 +141,7 @@ func (r *Resolver) fetchActivityPubObjectDirect(objectURL string) (string, error
// Set Accept headers to request ActivityPub data // Set Accept headers to request ActivityPub data
req.Header.Set("Accept", "application/activity+json, application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\", application/json") req.Header.Set("Accept", "application/activity+json, application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\", application/json")
req.Header.Set("User-Agent", "FediResolve/1.0 (https://github.com/dennis/fediresolve)") req.Header.Set("User-Agent", UserAgent)
// Perform the request // Perform the request
fmt.Printf("Sending direct request with headers: %v\n", req.Header) fmt.Printf("Sending direct request with headers: %v\n", req.Header)
@ -225,7 +231,7 @@ func (r *Resolver) fetchWithSignature(objectURL string) (string, error) {
// Set headers // Set headers
req.Header.Set("Accept", "application/activity+json, application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\", application/json") req.Header.Set("Accept", "application/activity+json, application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\", application/json")
req.Header.Set("User-Agent", "FediResolve/1.0 (https://github.com/dennis/fediresolve)") req.Header.Set("User-Agent", UserAgent)
req.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat)) req.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
// Sign the request // Sign the request
@ -352,7 +358,7 @@ func (r *Resolver) fetchActorData(actorURL string) (map[string]interface{}, erro
// Set headers // Set headers
req.Header.Set("Accept", "application/activity+json, application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\", application/json") req.Header.Set("Accept", "application/activity+json, application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\", application/json")
req.Header.Set("User-Agent", "FediResolve/1.0 (https://github.com/dennis/fediresolve)") req.Header.Set("User-Agent", UserAgent)
// Send the request // Send the request
resp, err := r.client.Do(req) resp, err := r.client.Do(req)
@ -457,7 +463,7 @@ func (r *Resolver) resolveActorViaWebFinger(username, domain string) (string, er
// Set headers // Set headers
req.Header.Set("Accept", "application/jrd+json, application/json") req.Header.Set("Accept", "application/jrd+json, application/json")
req.Header.Set("User-Agent", "FediResolve/1.0 (https://github.com/dennis/fediresolve)") req.Header.Set("User-Agent", UserAgent)
// Send the request // Send the request
resp, err := r.client.Do(req) resp, err := r.client.Do(req)

View file

@ -26,7 +26,7 @@ func (r *Resolver) checkForRedirect(inputURL string) (string, error) {
} }
// Set standard browser-like headers // Set standard browser-like headers
req.Header.Set("User-Agent", "FediResolve/1.0 (https://github.com/dennis/fediresolve)") req.Header.Set("User-Agent", UserAgent)
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml") req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml")
// Perform the request // Perform the request

View file

@ -104,7 +104,7 @@ func (r *Resolver) resolveHandle(handle string) (string, error) {
// Set appropriate headers for WebFinger // Set appropriate headers for WebFinger
req.Header.Set("Accept", "application/jrd+json, application/json") req.Header.Set("Accept", "application/jrd+json, application/json")
req.Header.Set("User-Agent", "FediResolve/1.0 (https://github.com/dennis/fediresolve)") req.Header.Set("User-Agent", UserAgent)
// Fetch WebFinger data // Fetch WebFinger data
resp, err := r.client.Do(req) resp, err := r.client.Do(req)