From dc2967c8da4f57badea3a1a6be49ff88299c767e Mon Sep 17 00:00:00 2001 From: croneter Date: Wed, 23 Dec 2020 17:23:12 +0100 Subject: [PATCH] Add logic to safely enable the Kodi webserver for artwork caching --- .../resource.language.en_gb/strings.po | 4 ++++ resources/lib/app/connection.py | 24 +++++++++++++++---- resources/lib/artwork.py | 5 +--- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/resources/language/resource.language.en_gb/strings.po b/resources/language/resource.language.en_gb/strings.po index 380936d4..73cc22ec 100644 --- a/resources/language/resource.language.en_gb/strings.po +++ b/resources/language/resource.language.en_gb/strings.po @@ -36,6 +36,10 @@ msgctxt "#30003" msgid "Warning: Kodi setting \"Play next video automatically\" is enabled. This could break PKC. Deactivate?" msgstr "" +msgctxt "#30004" +msgid "The Kodi webserver is needed for artwork caching. PKC already set a strong, random password automatically if you haven't done so already. Please confirm the next dialog that you want to enable the webserver now with Yes." +msgstr "" + msgctxt "#30005" msgid "Username: " msgstr "" diff --git a/resources/lib/app/connection.py b/resources/lib/app/connection.py index 402b626e..8e70c80a 100644 --- a/resources/lib/app/connection.py +++ b/resources/lib/app/connection.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- from logging import getLogger +import secrets from .. import utils, json_rpc as js, variables as v @@ -37,14 +38,29 @@ class Connection(object): PKC needs Kodi webserver to work correctly """ LOG.debug('Loading Kodi webserver details') - # Kodi webserver details - if js.get_setting('services.webserver') in (None, False): - # Enable the webserver, it is disabled + if not utils.settings('enableTextureCache') == 'true': + LOG.info('Artwork caching disabled') + return + self.webserver_password = js.get_setting('services.webserverpassword') + if not self.webserver_password: + LOG.warn('No password set for the Kodi web server. Generating a ' + 'new random password') + self.webserver_password = secrets.token_urlsafe(16) + js.set_setting('services.webserverpassword', self.webserver_password) + if not js.get_setting('services.webserver'): + # The Kodi webserver is needed for artwork caching. PKC already set + # a strong, random password automatically if you haven't done so + # already. Please confirm the next dialog that you want to enable + # the webserver now with Yes. + utils.messageDialog(utils.lang(29999), utils.lang(30004)) + # Enable the webserver, it is disabled. Will force a Kodi pop-up js.set_setting('services.webserver', True) + if not js.get_setting('services.webserver'): + LOG.warn('User chose to not enable Kodi webserver') + utils.settings('enableTextureCache', value='false') self.webserver_host = 'localhost' self.webserver_port = js.get_setting('services.webserverport') self.webserver_username = js.get_setting('services.webserverusername') - self.webserver_password = js.get_setting('services.webserverpassword') def load(self): LOG.debug('Loading connection settings') diff --git a/resources/lib/artwork.py b/resources/lib/artwork.py index e91d5f42..f7a74ab3 100644 --- a/resources/lib/artwork.py +++ b/resources/lib/artwork.py @@ -100,10 +100,7 @@ def cache_url(url, should_suspend=None): while True: try: requests.head( - url="http://%s:%s/image/image://%s" - % (app.CONN.webserver_host, - app.CONN.webserver_port, - url), + url=f'http://{app.CONN.webserver_username}:{app.CONN.webserver_password}@{app.CONN.webserver_host}:{app.CONN.webserver_port}/image/image://{url}', auth=(app.CONN.webserver_username, app.CONN.webserver_password), timeout=TIMEOUT)