Skip to content

Commit

Permalink
fix(mediaproxy): forward client user-agent to origin to bypass bot pr…
Browse files Browse the repository at this point in the history
…otection
  • Loading branch information
fguillot committed Sep 12, 2024
1 parent 95201fc commit 7d21298
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions internal/ui/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"log/slog"
"net/http"
"net/url"
Expand Down Expand Up @@ -87,13 +88,12 @@ func (h *handler) mediaProxy(w http.ResponseWriter, r *http.Request) {
return
}

// Note: User-Agent HTTP header is omitted to avoid being blocked by bot protection mechanisms.
req.Header.Add("Connection", "close")
req.Header.Set("Connection", "close")

forwardedRequestHeader := []string{"Range", "Accept", "Accept-Encoding"}
forwardedRequestHeader := []string{"Range", "Accept", "Accept-Encoding", "User-Agent"}
for _, requestHeaderName := range forwardedRequestHeader {
if r.Header.Get(requestHeaderName) != "" {
req.Header.Add(requestHeaderName, r.Header.Get(requestHeaderName))
req.Header.Set(requestHeaderName, r.Header.Get(requestHeaderName))
}
}

Expand Down Expand Up @@ -128,7 +128,9 @@ func (h *handler) mediaProxy(w http.ResponseWriter, r *http.Request) {
slog.String("media_url", mediaURL),
slog.Int("status_code", resp.StatusCode),
)
html.NotFound(w, r)

// Forward the status code from the origin.
http.Error(w, fmt.Sprintf("Origin status code is %d", resp.StatusCode), resp.StatusCode)
return
}

Expand Down

0 comments on commit 7d21298

Please sign in to comment.