Skip to content

Commit ba932ce

Browse files
committed
feat(plex.tv): log all POST and PUT requests
1 parent 2fe7690 commit ba932ce

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

handler/main.go

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

1110
"github.com/go-chi/chi/v5/middleware"
1211
"github.com/go-redis/redis/v8"
@@ -54,18 +53,9 @@ func NewRouter() http.Handler {
5453
plexTvUrl, _ := url.Parse("https://www." + domainPlexTv)
5554
plexTvProxy := httputil.NewSingleHostReverseProxy(plexTvUrl)
5655
plexTvRouter := r.Host(domainPlexTv).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()
67-
sslRouter.Use(sslMiddleware)
68-
sslRouter.PathPrefix("/").Handler(plexTvProxy)
56+
updateRouter := plexTvRouter.Methods(http.MethodPost, http.MethodPut).Subrouter()
57+
updateRouter.Use(plexTvMiddleware)
58+
updateRouter.PathPrefix("/").Handler(plexTvProxy)
6959
plexTvRouter.PathPrefix("/").Handler(plexTvProxy)
7060

7161
pmsRouter := r.MatcherFunc(func(r *http.Request, match *mux.RouteMatch) bool {

handler/middleware.go

+29-15
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,40 @@ var (
2626
reLocalAddresses = regexp.MustCompile(` localAddresses="[^"]*"`)
2727
)
2828

29-
func sslMiddleware(next http.Handler) http.Handler {
29+
func plexTvMiddleware(next http.Handler) http.Handler {
3030
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
31-
if plexClient.sslHost != "" && r.Body != nil {
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)
31+
if plexClient.sslHost == "" {
32+
next.ServeHTTP(w, r)
33+
return
34+
}
35+
36+
var bodyBytes []byte
37+
path := r.URL.EscapedPath()
38+
query := r.URL.Query()
39+
if r.Body != nil {
40+
bodyBytes, _ = io.ReadAll(r.Body)
41+
common.GetLogger().Printf("%s", string(bodyBytes))
42+
}
3843

39-
bodyBytes, _ := io.ReadAll(r.Body)
44+
switch path {
45+
case "/servers.xml":
4046
sslHost := fmt.Sprintf("address=\"%s\" scheme=\"https\"", plexClient.sslHost)
41-
modifiedBody := bytes.ReplaceAll(bodyBytes, []byte("host=\"\""), []byte(sslHost))
42-
modifiedBody = reLocalAddresses.ReplaceAll(modifiedBody, []byte(""))
43-
nr.Body = io.NopCloser(bytes.NewReader(modifiedBody))
47+
bodyBytes = bytes.ReplaceAll(bodyBytes, []byte("host=\"\""), []byte(sslHost))
48+
bodyBytes = reLocalAddresses.ReplaceAll(bodyBytes, []byte(""))
49+
default:
50+
if strings.HasPrefix(path, "/devices/") {
51+
name := "Connection[][uri]"
52+
if query.Has(name) {
53+
query.Set(name, "https://"+plexClient.sslHost)
54+
}
55+
}
56+
}
4457

45-
next.ServeHTTP(w, nr)
46-
} else {
47-
next.ServeHTTP(w, r)
58+
nr := cloneRequest(r, r.Header, query)
59+
if bodyBytes != nil {
60+
nr.Body = io.NopCloser(bytes.NewReader(bodyBytes))
4861
}
62+
next.ServeHTTP(w, nr)
4963
})
5064
}
5165

0 commit comments

Comments
 (0)