From 3c44ef788a1c199dc03f6f2e7994c3732e50c350 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=B6ller?= Date: Fri, 7 Sep 2018 18:20:24 +0200 Subject: [PATCH] cache: add plex_insecure option to skip certificate validation Fixes #2215 --- backend/cache/cache.go | 9 +++++++-- backend/cache/plex.go | 28 ++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/backend/cache/cache.go b/backend/cache/cache.go index f9e4d8899..afdd6cd89 100644 --- a/backend/cache/cache.go +++ b/backend/cache/cache.go @@ -81,6 +81,10 @@ func init() { Help: "The plex token for authentication - auto set normally", Hide: fs.OptionHideBoth, Advanced: true, + }, { + Name: "plex_insecure", + Help: "Skip all certificate verifications when connecting to the Plex server", + Advanced: true, }, { Name: "chunk_size", Help: "The size of a chunk. Lower value good for slow connections but can affect seamless reading.", @@ -195,6 +199,7 @@ type Options struct { PlexUsername string `config:"plex_username"` PlexPassword string `config:"plex_password"` PlexToken string `config:"plex_token"` + PlexInsecure bool `config:"plex_insecure"` ChunkSize fs.SizeSuffix `config:"chunk_size"` InfoAge fs.Duration `config:"info_age"` ChunkTotalSize fs.SizeSuffix `config:"chunk_total_size"` @@ -297,7 +302,7 @@ func NewFs(name, rootPath string, m configmap.Mapper) (fs.Fs, error) { f.plexConnector = &plexConnector{} if opt.PlexURL != "" { if opt.PlexToken != "" { - f.plexConnector, err = newPlexConnectorWithToken(f, opt.PlexURL, opt.PlexToken) + f.plexConnector, err = newPlexConnectorWithToken(f, opt.PlexURL, opt.PlexToken, opt.PlexInsecure) if err != nil { return nil, errors.Wrapf(err, "failed to connect to the Plex API %v", opt.PlexURL) } @@ -307,7 +312,7 @@ func NewFs(name, rootPath string, m configmap.Mapper) (fs.Fs, error) { if err != nil { decPass = opt.PlexPassword } - f.plexConnector, err = newPlexConnector(f, opt.PlexURL, opt.PlexUsername, decPass, func(token string) { + f.plexConnector, err = newPlexConnector(f, opt.PlexURL, opt.PlexUsername, decPass, opt.PlexInsecure, func(token string) { m.Set("plex_token", token) }) if err != nil { diff --git a/backend/cache/plex.go b/backend/cache/plex.go index bac387db5..eed1843ea 100644 --- a/backend/cache/plex.go +++ b/backend/cache/plex.go @@ -3,6 +3,7 @@ package cache import ( + "crypto/tls" "encoding/json" "fmt" "net/http" @@ -54,6 +55,7 @@ type plexConnector struct { username string password string token string + insecure bool f *Fs mu sync.Mutex running bool @@ -63,7 +65,7 @@ type plexConnector struct { } // newPlexConnector connects to a Plex server and generates a token -func newPlexConnector(f *Fs, plexURL, username, password string, saveToken func(string)) (*plexConnector, error) { +func newPlexConnector(f *Fs, plexURL, username, password string, insecure bool, saveToken func(string)) (*plexConnector, error) { u, err := url.ParseRequestURI(strings.TrimRight(plexURL, "/")) if err != nil { return nil, err @@ -75,6 +77,7 @@ func newPlexConnector(f *Fs, plexURL, username, password string, saveToken func( username: username, password: password, token: "", + insecure: insecure, stateCache: cache.New(time.Hour, time.Minute), saveToken: saveToken, } @@ -83,7 +86,7 @@ func newPlexConnector(f *Fs, plexURL, username, password string, saveToken func( } // newPlexConnector connects to a Plex server and generates a token -func newPlexConnectorWithToken(f *Fs, plexURL, token string) (*plexConnector, error) { +func newPlexConnectorWithToken(f *Fs, plexURL, token string, insecure bool) (*plexConnector, error) { u, err := url.ParseRequestURI(strings.TrimRight(plexURL, "/")) if err != nil { return nil, err @@ -93,6 +96,7 @@ func newPlexConnectorWithToken(f *Fs, plexURL, token string) (*plexConnector, er f: f, url: u, token: token, + insecure: insecure, stateCache: cache.New(time.Hour, time.Minute), } pc.listenWebsocket() @@ -107,14 +111,26 @@ func (p *plexConnector) closeWebsocket() { p.running = false } +func (p *plexConnector) websocketDial() (*websocket.Conn, error) { + u := strings.TrimRight(strings.Replace(strings.Replace( + p.url.String(), "http://", "ws://", 1), "https://", "wss://", 1), "/") + url := fmt.Sprintf(defPlexNotificationURL, u, p.token) + + config, err := websocket.NewConfig(url, "http://localhost") + if err != nil { + return nil, err + } + if p.insecure { + config.TlsConfig = &tls.Config{InsecureSkipVerify: true} + } + return websocket.DialConfig(config) +} + func (p *plexConnector) listenWebsocket() { p.runningMu.Lock() defer p.runningMu.Unlock() - u := strings.Replace(p.url.String(), "http://", "ws://", 1) - u = strings.Replace(u, "https://", "wss://", 1) - conn, err := websocket.Dial(fmt.Sprintf(defPlexNotificationURL, strings.TrimRight(u, "/"), p.token), - "", "http://localhost") + conn, err := p.websocketDial() if err != nil { fs.Errorf("plex", "%v", err) return