Skip to content

Commit 9212db3

Browse files
committed
feat(proxy): make the hostname of PMS configurable
1 parent f918cee commit 9212db3

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

handler/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var (
2121
func init() {
2222
plexClient = NewPlexClient(PlexConfig{
2323
BaseUrl: os.Getenv("PLEX_BASEURL"),
24+
Hostname: os.Getenv("PLEX_HOSTNAME"),
2425
Token: os.Getenv("PLEX_TOKEN"),
2526
PlaxtUrl: os.Getenv("PLAXT_URL"),
2627
RedirectWebApp: os.Getenv("REDIRECT_WEB_APP"),

handler/plex.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
type PlexConfig struct {
2222
BaseUrl string
23+
Hostname string
2324
Token string
2425
PlaxtUrl string
2526
RedirectWebApp string
@@ -53,12 +54,7 @@ func NewPlexClient(config PlexConfig) *PlexClient {
5354
return nil
5455
}
5556

56-
proxy := httputil.NewSingleHostReverseProxy(u)
57-
proxy.FlushInterval = -1
58-
proxy.ErrorLog = common.GetLogger()
59-
proxy.ModifyResponse = modifyResponse
60-
proxy.ErrorHandler = proxyErrorHandler
61-
57+
proxy := newSingleHostReverseProxy(u, config.Hostname)
6258
client, _ := plex.New(config.BaseUrl, config.Token)
6359

6460
var plaxtUrl string

handler/utils.go

+63
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,72 @@ import (
99
"strings"
1010
"time"
1111

12+
"github.com/RoyXiang/plexproxy/common"
1213
"github.com/go-chi/chi/v5/middleware"
1314
)
1415

16+
func newSingleHostReverseProxy(target *url.URL, hostname string) *httputil.ReverseProxy {
17+
targetQuery := target.RawQuery
18+
director := func(req *http.Request) {
19+
req.URL.Scheme = target.Scheme
20+
if hostname != "" {
21+
req.URL.Host = hostname
22+
} else {
23+
req.URL.Host = target.Host
24+
}
25+
req.URL.Path, req.URL.RawPath = joinURLPath(target, req.URL)
26+
if targetQuery == "" || req.URL.RawQuery == "" {
27+
req.URL.RawQuery = targetQuery + req.URL.RawQuery
28+
} else {
29+
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
30+
}
31+
if _, ok := req.Header["User-Agent"]; !ok {
32+
// explicitly disable User-Agent so it's not set to default value
33+
req.Header.Set("User-Agent", "")
34+
}
35+
}
36+
proxy := &httputil.ReverseProxy{Director: director}
37+
38+
proxy.FlushInterval = -1
39+
proxy.ErrorLog = common.GetLogger()
40+
proxy.ModifyResponse = modifyResponse
41+
proxy.ErrorHandler = proxyErrorHandler
42+
return proxy
43+
}
44+
45+
func joinURLPath(a, b *url.URL) (path, rawpath string) {
46+
if a.RawPath == "" && b.RawPath == "" {
47+
return singleJoiningSlash(a.Path, b.Path), ""
48+
}
49+
// Same as singleJoiningSlash, but uses EscapedPath to determine
50+
// whether a slash should be added
51+
apath := a.EscapedPath()
52+
bpath := b.EscapedPath()
53+
54+
aslash := strings.HasSuffix(apath, "/")
55+
bslash := strings.HasPrefix(bpath, "/")
56+
57+
switch {
58+
case aslash && bslash:
59+
return a.Path + b.Path[1:], apath + bpath[1:]
60+
case !aslash && !bslash:
61+
return a.Path + "/" + b.Path, apath + "/" + bpath
62+
}
63+
return a.Path + b.Path, apath + bpath
64+
}
65+
66+
func singleJoiningSlash(a, b string) string {
67+
aslash := strings.HasSuffix(a, "/")
68+
bslash := strings.HasPrefix(b, "/")
69+
switch {
70+
case aslash && bslash:
71+
return a + b[1:]
72+
case !aslash && !bslash:
73+
return a + "/" + b
74+
}
75+
return a + b
76+
}
77+
1578
func wrapResponseWriter(w http.ResponseWriter, protoMajor int) middleware.WrapResponseWriter {
1679
if nw, ok := w.(middleware.WrapResponseWriter); ok {
1780
return nw

0 commit comments

Comments
 (0)