Merge pull request #687 from croneter/manual_ip

Improve dialog to manually enter PMS IP and port
This commit is contained in:
croneter 2019-02-03 20:25:48 +01:00 committed by GitHub
commit ae3f24f6ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 138 additions and 44 deletions

View file

@ -464,6 +464,11 @@ msgctxt "#30502"
msgid "Sync Plex artwork from the PMS (recommended)" msgid "Sync Plex artwork from the PMS (recommended)"
msgstr "" msgstr ""
# Message shown if SSL HTTPS certificate fails
msgctxt "#30503"
msgid "SSL certificate failed to validate. Please check {0} for solutions."
msgstr ""
# PKC Settings, category name # PKC Settings, category name
msgctxt "#30506" msgctxt "#30506"
msgid "Sync Options" msgid "Sync Options"
@ -1081,6 +1086,16 @@ msgctxt "#39082"
msgid "Direct Paths" msgid "Direct Paths"
msgstr "" msgstr ""
# Dialog for manually entering PMS
msgctxt "#39083"
msgid "Enter PMS IP or URL"
msgstr ""
# Dialog for manually entering PMS
msgctxt "#39084"
msgid "Enter PMS port"
msgstr ""
msgctxt "#39200" msgctxt "#39200"
msgid "Log-out Plex Home User " msgid "Log-out Plex Home User "
msgstr "" msgstr ""
@ -1133,7 +1148,7 @@ msgid "Enter your Plex Media Server's IP or URL, Examples are:"
msgstr "" msgstr ""
msgctxt "#39217" msgctxt "#39217"
msgid "Does your Plex Media Server support SSL connections? (https instead of http)?" msgid "Use HTTPS (SSL) connections? With Kodi 18 or later, HTTPS will likely not work!"
msgstr "" msgstr ""
msgctxt "#39218" msgctxt "#39218"

View file

@ -3,6 +3,7 @@
from __future__ import absolute_import, division, unicode_literals from __future__ import absolute_import, division, unicode_literals
from logging import getLogger from logging import getLogger
import requests import requests
import requests.exceptions as exceptions
from . import utils, clientinfo, app from . import utils, clientinfo, app
@ -116,7 +117,7 @@ class DownloadUtils():
def downloadUrl(self, url, action_type="GET", postBody=None, def downloadUrl(self, url, action_type="GET", postBody=None,
parameters=None, authenticate=True, headerOptions=None, parameters=None, authenticate=True, headerOptions=None,
verifySSL=True, timeout=None, return_response=False, verifySSL=True, timeout=None, return_response=False,
headerOverride=None): headerOverride=None, reraise=False):
""" """
Override SSL check with verifySSL=False Override SSL check with verifySSL=False
@ -174,39 +175,55 @@ class DownloadUtils():
r = self._doDownload(s, action_type, **kwargs) r = self._doDownload(s, action_type, **kwargs)
# THE EXCEPTIONS # THE EXCEPTIONS
except requests.exceptions.SSLError as e: except exceptions.SSLError as e:
LOG.warn("Invalid SSL certificate for: %s", url) LOG.warn("Invalid SSL certificate for: %s", url)
LOG.warn(e) LOG.warn(e)
if reraise:
raise
except requests.exceptions.ConnectionError as e: except exceptions.ConnectionError as e:
# Connection error # Connection error
LOG.warn("Server unreachable at: %s", url) LOG.warn("Server unreachable at: %s", url)
LOG.warn(e) LOG.warn(e)
if reraise:
raise
except requests.exceptions.Timeout as e: except exceptions.Timeout as e:
LOG.warn("Server timeout at: %s", url) LOG.warn("Server timeout at: %s", url)
LOG.warn(e) LOG.warn(e)
if reraise:
raise
except requests.exceptions.HTTPError as e: except exceptions.HTTPError as e:
LOG.warn('HTTP Error at %s', url) LOG.warn('HTTP Error at %s', url)
LOG.warn(e) LOG.warn(e)
if reraise:
raise
except requests.exceptions.TooManyRedirects as e: except exceptions.TooManyRedirects as e:
LOG.warn("Too many redirects connecting to: %s", url) LOG.warn("Too many redirects connecting to: %s", url)
LOG.warn(e) LOG.warn(e)
if reraise:
raise
except requests.exceptions.RequestException as e: except exceptions.RequestException as e:
LOG.warn("Unknown error connecting to: %s", url) LOG.warn("Unknown error connecting to: %s", url)
LOG.warn(e) LOG.warn(e)
if reraise:
raise
except SystemExit: except SystemExit:
LOG.info('SystemExit detected, aborting download') LOG.info('SystemExit detected, aborting download')
self.stopSession() self.stopSession()
if reraise:
raise
except Exception: except Exception:
LOG.warn('Unknown error while downloading. Traceback:') LOG.warn('Unknown error while downloading. Traceback:')
import traceback import traceback
LOG.warn(traceback.format_exc()) LOG.warn(traceback.format_exc())
if reraise:
raise
# THE RESPONSE ##### # THE RESPONSE #####
else: else:

View file

@ -9,7 +9,7 @@ from . import utils
from .utils import etree from .utils import etree
from . import path_ops from . import path_ops
from . import migration from . import migration
from .downloadutils import DownloadUtils as DU from .downloadutils import DownloadUtils as DU, exceptions
from . import plex_functions as PF from . import plex_functions as PF
from . import plex_tv from . import plex_tv
from . import json_rpc as js from . import json_rpc as js
@ -70,46 +70,81 @@ class InitialSetup(object):
utils.window('plex_allows_mediaDeletion', value=value) utils.window('plex_allows_mediaDeletion', value=value)
def enter_new_pms_address(self): def enter_new_pms_address(self):
LOG.info('Start getting manual PMS address and port')
# "Enter your Plex Media Server's IP or URL. Examples are:" # "Enter your Plex Media Server's IP or URL. Examples are:"
utils.messageDialog(utils.lang(29999), utils.messageDialog(utils.lang(29999),
'%s\n%s\n%s' % (utils.lang(39215), '%s\n%s\n%s' % (utils.lang(39215),
'192.168.1.2', '192.168.1.2',
'plex.myServer.org')) 'plex.myServer.org'))
address = utils.dialog('input', "Enter PMS IP or URL") # "Enter PMS IP or URL"
if address == '': address = utils.dialog('input', utils.lang(39083))
if not address:
return False return False
port = utils.dialog('input', "Enter PMS port", '32400', type='{numeric}') port = utils.dialog('input', utils.lang(39084), '32400', type='{numeric}')
if port == '': if not port:
return False return False
url = '%s:%s' % (address, port) url = '%s:%s' % (address, port)
# "Does your Plex Media Server support SSL connections? # "Use HTTPS (SSL) connections? With Kodi 18 or later, HTTPS will likely
# (https instead of http)" # not work!"
https = utils.yesno_dialog(utils.lang(29999), utils.lang(39217)) https = utils.yesno_dialog(utils.lang(29999), utils.lang(39217))
if https: if https:
url = 'https://%s' % url url = 'https://%s' % url
else: else:
url = 'http://%s' % url url = 'http://%s' % url
https = 'true' if https else 'false' https = 'true' if https else 'false'
machine_identifier = PF.GetMachineIdentifier(url) # Try to connect first
if machine_identifier is None: error = False
# "Error contacting url try:
# Abort (Yes) or save address anyway (No)" machine_identifier = PF.GetMachineIdentifier(url)
if utils.yesno_dialog(utils.lang(29999), except exceptions.SSLError:
'%s %s. %s' % (utils.lang(39218), LOG.error('SSL cert error contacting %s', url)
url, # "SSL certificate failed to validate. Please check {0}
utils.lang(39219))): # for solutions."
return False utils.messageDialog(utils.lang(29999),
else: utils.lang(30503).format('github.com/croneter/PlexKodiConnect/issues'))
utils.settings('plex_machineIdentifier', '') return
else: except Exception:
utils.settings('plex_machineIdentifier', machine_identifier) error = True
LOG.info('Set new PMS to https %s, address %s, port %s, machineId %s', if error or machine_identifier is None:
https, address, port, machine_identifier) LOG.error('Could not even get a machineIdentifier for %s', url)
utils.settings('https', value=https) # "Server is unreachable"
utils.settings('ipaddress', value=address) utils.messageDialog(utils.lang(29999), utils.lang(33002))
utils.settings('port', value=port) return
# Chances are this is a local PMS, so disable SSL certificate check # Let's use the main account's token, not managed user token
utils.settings('sslverify', value='false') token = utils.settings('plexToken')
xml = PF.pms_root(url, token)
if xml == 401:
LOG.error('Not yet authorized for %s', url)
# "User is unauthorized for server {0}",
# "Please sign in to plex.tv."
utils.messageDialog(utils.lang(29999),
'%s. %s' % (utils.lang(33010).format(address),
utils.lang(39014)))
return
try:
xml[0].attrib
except (IndexError, TypeError, AttributeError):
LOG.error('Could not get PMS root directory for %s', url)
# "Error contacting PMS"
utils.messageDialog(utils.lang(29999), utils.lang(39218))
return
pms = {
'baseURL': url,
'ip': address,
# Assume PMS is not local so we're not resetting verifyssl
'local': False,
'machineIdentifier': xml.get('machineIdentifier'),
'name': xml.get('friendlyName'),
# Assume that we own this PMS - no easy way to check
'owned': True,
'platform': xml.get('platform'),
'port': port,
# 'relay': True,
'scheme': 'https' if https else 'http',
'token': token,
'version': xml.get('version')
}
return pms
def plex_tv_sign_in(self): def plex_tv_sign_in(self):
""" """

View file

@ -834,7 +834,8 @@ def GetMachineIdentifier(url):
xml = DU().downloadUrl('%s/identity' % url, xml = DU().downloadUrl('%s/identity' % url,
authenticate=False, authenticate=False,
verifySSL=False, verifySSL=False,
timeout=10) timeout=10,
reraise=True)
try: try:
machineIdentifier = xml.attrib['machineIdentifier'] machineIdentifier = xml.attrib['machineIdentifier']
except (AttributeError, KeyError): except (AttributeError, KeyError):
@ -937,6 +938,17 @@ def delete_item_from_pms(plexid):
return False return False
def pms_root(url, token):
"""
Retrieve the PMS' most basic settings by retrieving <url>/
"""
return DU().downloadUrl(
url,
authenticate=False,
verifySSL=True if v.KODIVERSION >= 18 else False,
headerOptions={'X-Plex-Token': token} if token else None)
def get_PMS_settings(url, token): def get_PMS_settings(url, token):
""" """
Retrieve the PMS' settings via <url>/:/prefs Retrieve the PMS' settings via <url>/:/prefs

View file

@ -269,14 +269,29 @@ class Service():
self.auth_running = False self.auth_running = False
def enter_new_pms_address(self): def enter_new_pms_address(self):
if self.setup.enter_new_pms_address(): server = self.setup.enter_new_pms_address()
app.CONN.load() if not server:
app.ACCOUNT.reset_session() return
app.ACCOUNT.set_unauthenticated() if not self.log_out():
self.server_has_been_online = False return False
self.welcome_msg = False # Save changes to to file
# Force a full sync self.setup.save_pms_settings(server['baseURL'], server['token'])
app.SYNC.run_lib_scan = 'full' self.setup.write_pms_to_settings(server)
if not v.KODIVERSION >= 18:
utils.settings('sslverify', value='false')
if not self.log_out():
return False
# Wipe Kodi and Plex database as well as playlists and video nodes
utils.wipe_database()
app.CONN.load()
app.ACCOUNT.reset_session()
app.ACCOUNT.set_unauthenticated()
self.server_has_been_online = False
self.welcome_msg = False
# Force a full sync
app.SYNC.run_lib_scan = 'full'
LOG.info("Choosing new PMS complete")
return True
def _do_auth(self): def _do_auth(self):
LOG.info('Authenticating user') LOG.info('Authenticating user')