Skip to content

Commit

Permalink
Merge pull request #30 from luigi311/dev
Browse files Browse the repository at this point in the history
Add ssl_bypass to skip hostname validation.
  • Loading branch information
luigi311 authored Nov 22, 2022
2 parents 644dc8e + 47bc4e9 commit 9ffbc49
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ PLEX_TOKEN = "SuperSecretToken"
#PLEX_USERNAME = ""
#PLEX_PASSWORD = ""
#PLEX_SERVERNAME = "Plex Server"

## Skip hostname validation for ssl certificates.
SSL_BYPASS = "False"

## Jellyfin server URL, use hostname or IP address if the hostname is not resolving correctly
## Comma seperated list for multiple servers
Expand Down
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ ENV WHITELIST_USERS ''

WORKDIR /app

RUN apt-get update && \
apt-get install -y --no-install-recommends \
build-essential && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

COPY ./requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

Expand Down
27 changes: 19 additions & 8 deletions src/plex.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re, requests
from urllib3.poolmanager import PoolManager

from plexapi.server import PlexServer
from plexapi.myplex import MyPlexAccount
Expand All @@ -11,6 +12,14 @@
future_thread_executor,
)

# Bypass hostname validation for ssl. Taken from https://github.com/pkkid/python-plexapi/issues/143#issuecomment-775485186
class HostNameIgnoringAdapter(requests.adapters.HTTPAdapter):
def init_poolmanager(self, connections, maxsize, block=..., **pool_kwargs):
self.poolmanager = PoolManager(num_pools=connections,
maxsize=maxsize,
block=block,
assert_hostname=False,
**pool_kwargs)

def get_user_watched(user, user_plex, library):
try:
Expand Down Expand Up @@ -252,20 +261,22 @@ def __init__(
self.username = username
self.password = password
self.servername = servername
self.plex = self.login(ssl_bypass)
self.ssl_bypass = ssl_bypass
self.plex = self.login(self.baseurl, self.token, ssl_bypass)
self.admin_user = self.plex.myPlexAccount()
self.users = self.get_users()

def login(self, ssl_bypass=False):
def login(self, baseurl, token, ssl_bypass=False):
try:
if self.baseurl and self.token:
if baseurl and token:
# Login via token
if ssl_bypass:
session = requests.Session()
session.verify = False
plex = PlexServer(self.baseurl, self.token, session=session)
# By pass ssl hostname check https://github.com/pkkid/python-plexapi/issues/143#issuecomment-775485186
session.mount("https://", HostNameIgnoringAdapter())
plex = PlexServer(baseurl, token, session=session)
else:
plex = PlexServer(self.baseurl, self.token)
plex = PlexServer(baseurl, token)
elif self.username and self.password and self.servername:
# Login via plex account
account = MyPlexAccount(self.username, self.password)
Expand Down Expand Up @@ -312,8 +323,8 @@ def get_watched(
if self.admin_user == user:
user_plex = self.plex
else:
user_plex = PlexServer(
self.plex._baseurl, user.get_token(self.plex.machineIdentifier)
user_plex = self.login(
self.plex._baseurl, user.get_token(self.plex.machineIdentifier), self.ssl_bypass
)

libraries = user_plex.library.sections()
Expand Down

0 comments on commit 9ffbc49

Please sign in to comment.