Skip to content

Commit

Permalink
Revert back to v1.0.15-beta.2
Browse files Browse the repository at this point in the history
This reverts commit ba932ce.
This reverts commit 2fe7690.
This reverts commit f31a40e.
This reverts commit 85e9592.
This reverts commit 364ee0e.
  • Loading branch information
RoyXiang committed Nov 16, 2022
1 parent ba932ce commit 73711ac
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 65 deletions.
2 changes: 0 additions & 2 deletions handler/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
)

const (
domainPlexTv = "plex.tv"

headerPlexPrefix = "X-Plex-"
headerCacheStatus = "X-Plex-Cache-Status"
headerClientIdentity = "X-Plex-Client-Identifier"
Expand Down
26 changes: 6 additions & 20 deletions handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package handler
import (
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"

"github.com/go-chi/chi/v5/middleware"
Expand All @@ -23,7 +21,6 @@ var (
func init() {
plexClient = NewPlexClient(PlexConfig{
BaseUrl: os.Getenv("PLEX_BASEURL"),
SslHost: os.Getenv("PLEX_SSL_HOST"),
Token: os.Getenv("PLEX_TOKEN"),
PlaxtUrl: os.Getenv("PLAXT_URL"),
RedirectWebApp: os.Getenv("REDIRECT_WEB_APP"),
Expand All @@ -49,25 +46,14 @@ func NewRouter() http.Handler {
if !plexClient.NoRequestLogs {
r.Use(middleware.Logger)
}
r.Use(wrapMiddleware, middleware.Recoverer, trafficMiddleware)

plexTvUrl, _ := url.Parse("https://www." + domainPlexTv)
plexTvProxy := httputil.NewSingleHostReverseProxy(plexTvUrl)
plexTvRouter := r.Host(domainPlexTv).Subrouter()
updateRouter := plexTvRouter.Methods(http.MethodPost, http.MethodPut).Subrouter()
updateRouter.Use(plexTvMiddleware)
updateRouter.PathPrefix("/").Handler(plexTvProxy)
plexTvRouter.PathPrefix("/").Handler(plexTvProxy)

pmsRouter := r.MatcherFunc(func(r *http.Request, match *mux.RouteMatch) bool {
return r.Host != domainPlexTv
}).Subrouter()
pmsRouter.Use(wrapMiddleware, middleware.Recoverer, trafficMiddleware)
if redisClient != nil {
// bypass cache
pmsRouter.PathPrefix("/:/").Handler(plexClient)
pmsRouter.PathPrefix("/library/parts/").Handler(plexClient)
r.PathPrefix("/:/").Handler(plexClient)
r.PathPrefix("/library/parts/").Handler(plexClient)

staticRouter := pmsRouter.Methods(http.MethodGet).Subrouter()
staticRouter := r.Methods(http.MethodGet).Subrouter()
staticRouter.Use(staticMiddleware)
staticRouter.Path("/library/media/{key}/chapterImages/{id}").Handler(plexClient)
staticRouter.Path("/library/metadata/{key}/art/{id}").Handler(plexClient)
Expand All @@ -76,11 +62,11 @@ func NewRouter() http.Handler {
staticRouter.PathPrefix("/web/js/").Handler(plexClient)
staticRouter.PathPrefix("/web/static/").Handler(plexClient)

dynamicRouter := pmsRouter.Methods(http.MethodGet).Subrouter()
dynamicRouter := r.Methods(http.MethodGet).Subrouter()
dynamicRouter.Use(dynamicMiddleware)
dynamicRouter.PathPrefix("/").Handler(plexClient)
}
pmsRouter.PathPrefix("/").Handler(plexClient)

r.PathPrefix("/").Handler(plexClient)
return r
}
40 changes: 0 additions & 40 deletions handler/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"net/http/httptest"
"net/url"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
Expand All @@ -22,47 +21,8 @@ var (
cacheInfoCtxKey = &ctxKeyType{"cacheInfo"}
tokenCtxKey = &ctxKeyType{"token"}
userCtxKey = &ctxKeyType{"user"}

reLocalAddresses = regexp.MustCompile(` localAddresses="[^"]*"`)
)

func plexTvMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if plexClient.sslHost == "" {
next.ServeHTTP(w, r)
return
}

var bodyBytes []byte
path := r.URL.EscapedPath()
query := r.URL.Query()
if r.Body != nil {
bodyBytes, _ = io.ReadAll(r.Body)
common.GetLogger().Printf("%s", string(bodyBytes))
}

switch path {
case "/servers.xml":
sslHost := fmt.Sprintf("address=\"%s\" scheme=\"https\"", plexClient.sslHost)
bodyBytes = bytes.ReplaceAll(bodyBytes, []byte("host=\"\""), []byte(sslHost))
bodyBytes = reLocalAddresses.ReplaceAll(bodyBytes, []byte(""))
default:
if strings.HasPrefix(path, "/devices/") {
name := "Connection[][uri]"
if query.Has(name) {
query.Set(name, "https://"+plexClient.sslHost)
}
}
}

nr := cloneRequest(r, r.Header, query)
if bodyBytes != nil {
nr.Body = io.NopCloser(bytes.NewReader(bodyBytes))
}
next.ServeHTTP(w, nr)
})
}

func normalizeMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
headers := http.Header{}
Expand Down
3 changes: 0 additions & 3 deletions handler/plex.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

type PlexConfig struct {
BaseUrl string
SslHost string
Token string
PlaxtUrl string
RedirectWebApp string
Expand All @@ -33,7 +32,6 @@ type PlexClient struct {
proxy *httputil.ReverseProxy
client *plex.Plex

sslHost string
plaxtUrl string
redirectWebApp bool
disableTranscode bool
Expand Down Expand Up @@ -94,7 +92,6 @@ func NewPlexClient(config PlexConfig) *PlexClient {
return &PlexClient{
proxy: proxy,
client: client,
sslHost: config.SslHost,
plaxtUrl: plaxtUrl,
redirectWebApp: redirectWebApp,
disableTranscode: disableTranscode,
Expand Down

0 comments on commit 73711ac

Please sign in to comment.