Skip to content

Commit 2fe7690

Browse files
committed
feat(ssl): alter requests to https://plex.tv/devices/<device_id>
1 parent f31a40e commit 2fe7690

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

handler/main.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http/httputil"
77
"net/url"
88
"os"
9+
"strings"
910

1011
"github.com/go-chi/chi/v5/middleware"
1112
"github.com/go-redis/redis/v8"
@@ -53,7 +54,16 @@ func NewRouter() http.Handler {
5354
plexTvUrl, _ := url.Parse("https://www." + domainPlexTv)
5455
plexTvProxy := httputil.NewSingleHostReverseProxy(plexTvUrl)
5556
plexTvRouter := r.Host(domainPlexTv).Subrouter()
56-
sslRouter := plexTvRouter.Methods(http.MethodPost).Path("/servers.xml").Subrouter()
57+
sslRouter := plexTvRouter.MatcherFunc(func(r *http.Request, _ *mux.RouteMatch) bool {
58+
path := r.URL.EscapedPath()
59+
if r.Method == http.MethodPost && path == "/servers.xml" {
60+
return true
61+
}
62+
if r.Method == http.MethodPut && strings.HasPrefix(path, "/devices/") {
63+
return true
64+
}
65+
return false
66+
}).Subrouter()
5767
sslRouter.Use(sslMiddleware)
5868
sslRouter.PathPrefix("/").Handler(plexTvProxy)
5969
plexTvRouter.PathPrefix("/").Handler(plexTvProxy)

handler/middleware.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@ var (
2929
func sslMiddleware(next http.Handler) http.Handler {
3030
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
3131
if plexClient.sslHost != "" && r.Body != nil {
32-
nr := cloneRequest(r, r.Header, r.URL.Query())
32+
name := "Connection[][uri]"
33+
query := r.URL.Query()
34+
if query.Has(name) {
35+
query.Set(name, "https://"+plexClient.sslHost)
36+
}
37+
nr := cloneRequest(r, r.Header, query)
38+
3339
bodyBytes, _ := io.ReadAll(r.Body)
3440
sslHost := fmt.Sprintf("address=\"%s\" scheme=\"https\"", plexClient.sslHost)
3541
modifiedBody := bytes.ReplaceAll(bodyBytes, []byte("host=\"\""), []byte(sslHost))
3642
modifiedBody = reLocalAddresses.ReplaceAll(modifiedBody, []byte(""))
3743
nr.Body = io.NopCloser(bytes.NewReader(modifiedBody))
44+
3845
next.ServeHTTP(w, nr)
3946
} else {
4047
next.ServeHTTP(w, r)

0 commit comments

Comments
 (0)