diff --git a/go.mod b/go.mod index c85bbce..92fc5b3 100644 --- a/go.mod +++ b/go.mod @@ -5,14 +5,12 @@ go 1.17 require ( github.com/bluele/gcache v0.0.2 github.com/go-chi/chi/v5 v5.0.11 - github.com/gorilla/handlers v1.5.2 github.com/gorilla/mux v1.8.1 github.com/jrudio/go-plex-client v0.0.0-20220106065909-9e1d590b99aa github.com/xanderstrike/plexhooks v0.0.0-20200926011736-c63bcd35fe3e ) require ( - github.com/felixge/httpsnoop v1.0.3 // indirect github.com/google/uuid v1.3.0 // indirect github.com/gorilla/websocket v1.5.0 // indirect github.com/onsi/ginkgo v1.16.5 // indirect diff --git a/go.sum b/go.sum index 887077a..bdb1351 100644 --- a/go.sum +++ b/go.sum @@ -22,8 +22,6 @@ github.com/dgraph-io/badger/v3 v3.2103.2/go.mod h1:RHo4/GmYcKKh5Lxu63wLEMHJ70Pac github.com/dgraph-io/ristretto v0.1.0/go.mod h1:fux0lOrBhrVCJd3lcTHsIJhq1T2rokOu6v9Vcb3Q9ug= github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= -github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk= -github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= @@ -54,8 +52,6 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/gorilla/handlers v1.5.2 h1:cLTUSsNkgcwhgRqvCNmdbRWG0A3N4F+M2nWKdScwyEE= -github.com/gorilla/handlers v1.5.2/go.mod h1:dX+xVpaxdSw+q0Qek8SSsl3dfMk3jNddUkMzo0GtH0w= github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= diff --git a/handler/const.go b/handler/const.go index 0494caa..e4fde35 100644 --- a/handler/const.go +++ b/handler/const.go @@ -9,6 +9,7 @@ const ( headerPageStart = "X-Plex-Container-Start" headerToken = "X-Plex-Token" headerUserId = "X-Plex-User-Id" + headerAccept = "Accept" headerAcceptLanguage = "Accept-Language" headerCacheControl = "Cache-Control" @@ -16,6 +17,11 @@ const ( headerRange = "Range" headerVary = "Vary" + headerForwardedFor = "X-Forwarded-For" + headerRealIP = "X-Real-IP" + headerForwardedProto = "X-Forwarded-Proto" + headerForwardedScheme = "X-Forwarded-Scheme" + cachePrefixDynamic = "dynamic" cachePrefixStatic = "static" cachePrefixPlex = "plex" diff --git a/handler/main.go b/handler/main.go index 3d7e3e5..fec84cf 100644 --- a/handler/main.go +++ b/handler/main.go @@ -6,7 +6,6 @@ import ( "os" "github.com/go-chi/chi/v5/middleware" - "github.com/gorilla/handlers" "github.com/gorilla/mux" ) @@ -33,7 +32,7 @@ func init() { func NewRouter() http.Handler { r := mux.NewRouter() - r.Use(handlers.ProxyHeaders, normalizeMiddleware) + r.Use(normalizeMiddleware) if !plexClient.NoRequestLogs { r.Use(middleware.Logger) } diff --git a/handler/utils.go b/handler/utils.go index 6590b93..084b005 100644 --- a/handler/utils.go +++ b/handler/utils.go @@ -65,9 +65,37 @@ func cloneRequest(r *http.Request, headers http.Header, query url.Values) *http. nr.URL.RawQuery = query.Encode() nr.RequestURI = nr.URL.RequestURI() } + if fwd := getIP(headers); fwd != "" { + nr.RemoteAddr = fwd + } + if scheme := getScheme(headers); scheme != "" { + nr.URL.Scheme = scheme + } return nr } +func getIP(headers http.Header) (addr string) { + if fwd := headers.Get(headerForwardedFor); fwd != "" { + s := strings.Index(fwd, ", ") + if s == -1 { + s = len(fwd) + } + addr = fwd[:s] + } else if fwd = headers.Get(headerRealIP); fwd != "" { + addr = fwd + } + return +} + +func getScheme(headers http.Header) (scheme string) { + if proto := headers.Get(headerForwardedProto); proto != "" { + scheme = strings.ToLower(proto) + } else if proto = headers.Get(headerForwardedScheme); proto != "" { + scheme = strings.ToLower(proto) + } + return +} + func getAcceptContentType(r *http.Request) string { accept := r.Header.Get(headerAccept) if accept == "" {