Skip to content

Commit 3e4c500

Browse files
committed
feat(proxy): allow connecting to PMS through https
ignoring the certificate which reverts previous commit
1 parent 9212db3 commit 3e4c500

File tree

3 files changed

+11
-66
lines changed

3 files changed

+11
-66
lines changed

handler/main.go

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

handler/plex.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package handler
33
import (
44
"bytes"
55
"context"
6+
"crypto/tls"
67
"encoding/json"
78
"fmt"
89
"io"
@@ -20,7 +21,6 @@ import (
2021

2122
type PlexConfig struct {
2223
BaseUrl string
23-
Hostname string
2424
Token string
2525
PlaxtUrl string
2626
RedirectWebApp string
@@ -54,7 +54,16 @@ func NewPlexClient(config PlexConfig) *PlexClient {
5454
return nil
5555
}
5656

57-
proxy := newSingleHostReverseProxy(u, config.Hostname)
57+
transport := http.DefaultTransport.(*http.Transport).Clone()
58+
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
59+
60+
proxy := httputil.NewSingleHostReverseProxy(u)
61+
proxy.Transport = transport
62+
proxy.FlushInterval = -1
63+
proxy.ErrorLog = common.GetLogger()
64+
proxy.ModifyResponse = modifyResponse
65+
proxy.ErrorHandler = proxyErrorHandler
66+
5867
client, _ := plex.New(config.BaseUrl, config.Token)
5968

6069
var plaxtUrl string

handler/utils.go

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

12-
"github.com/RoyXiang/plexproxy/common"
1312
"github.com/go-chi/chi/v5/middleware"
1413
)
1514

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-
7815
func wrapResponseWriter(w http.ResponseWriter, protoMajor int) middleware.WrapResponseWriter {
7916
if nw, ok := w.(middleware.WrapResponseWriter); ok {
8017
return nw

0 commit comments

Comments
 (0)