From ecb66f39c98e8d7dd01967f28859bfe3f67fdca9 Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Sun, 24 Oct 2021 21:27:27 -0700 Subject: [PATCH] Add dynamic anonymous redirect service setting --- data/interfaces/default/settings.html | 24 ++++++++++++++++++------ plexpy/config.py | 3 ++- plexpy/helpers.py | 26 +++++++++++++++++++++++++- plexpy/webserve.py | 4 +++- 4 files changed, 48 insertions(+), 9 deletions(-) diff --git a/data/interfaces/default/settings.html b/data/interfaces/default/settings.html index 2e780b9e..c505258b 100644 --- a/data/interfaces/default/settings.html +++ b/data/interfaces/default/settings.html @@ -640,14 +640,25 @@ -
- -
-
- +
+ +

+ Allow Tautulli to use the dynamic anonymous redirect service listed here. + Disable to specify your own service. +

+
+
+
+ +
+
+ +
+

Backlink protection via anonymizer service, must end in "?". Leave blank to disable.

-

Backlink protection via anonymizer service, must end in "?".

@@ -2363,6 +2374,7 @@ $(document).ready(function() { initConfigCheckbox('#api_enabled'); initConfigCheckbox('#enable_https'); + initConfigCheckbox('#anon_redirect_dynamic', null, true); initConfigCheckbox('#https_create_cert'); initConfigCheckbox('#check_github'); initConfigCheckbox('#monitor_pms_updates'); diff --git a/plexpy/config.py b/plexpy/config.py index 4d0a032a..5806c9e9 100644 --- a/plexpy/config.py +++ b/plexpy/config.py @@ -76,7 +76,8 @@ _CONFIG_DEFINITIONS = { 'PMS_UPDATE_CHECK_INTERVAL': (int, 'Advanced', 24), 'PMS_WEB_URL': (str, 'PMS', 'https://app.plex.tv/desktop'), 'TIME_FORMAT': (str, 'General', 'HH:mm'), - 'ANON_REDIRECT': (str, 'General', 'https://www.nullrefer.com/?'), + 'ANON_REDIRECT': (str, 'General', ''), + 'ANON_REDIRECT_DYNAMIC': (int, 'General', 1), 'API_ENABLED': (int, 'General', 1), 'API_KEY': (str, 'General', ''), 'API_SQL': (int, 'General', 0), diff --git a/plexpy/helpers.py b/plexpy/helpers.py index f2e9211f..cd4bf921 100644 --- a/plexpy/helpers.py +++ b/plexpy/helpers.py @@ -775,7 +775,31 @@ def anon_url(*url): """ Return a URL string consisting of the Anonymous redirect URL and an arbitrary number of values appended. """ - return '' if None in url else '%s%s' % (plexpy.CONFIG.ANON_REDIRECT, ''.join(str(s) for s in url)) + if plexpy.CONFIG.ANON_REDIRECT_DYNAMIC: + cache_time = timestamp() + cache_filepath = os.path.join(plexpy.CONFIG.CACHE_DIR, 'anonymizer.json') + try: + with open(cache_filepath, 'r', encoding='utf-8') as cache_file: + cache_data = json.load(cache_file) + if cache_time - cache_data['_cache_time'] < 86400: # 24 hours + anon_redirect = cache_data['anon_redirect'] + else: + raise + except: + try: + anon_redirect = request.request_content('https://tautulli.com/anonymizer.txt').decode('utf-8') + cache_data = { + 'anon_redirect': anon_redirect, + '_cache_time': cache_time + } + with open(cache_filepath, 'w', encoding='utf-8') as cache_file: + json.dump(cache_data, cache_file) + except: + anon_redirect = plexpy.CONFIG.ANON_REDIRECT + else: + anon_redirect = plexpy.CONFIG.ANON_REDIRECT + + return '' if None in url else '%s%s' % (anon_redirect, ''.join(str(s) for s in url)) def get_img_service(include_self=False): diff --git a/plexpy/webserve.py b/plexpy/webserve.py index 5df2c43a..afeb055f 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -3143,6 +3143,7 @@ class WebInterface(object): "https_ip": plexpy.CONFIG.HTTPS_IP, "http_base_url": plexpy.CONFIG.HTTP_BASE_URL, "anon_redirect": plexpy.CONFIG.ANON_REDIRECT, + "anon_redirect_dynamic": checked(plexpy.CONFIG.ANON_REDIRECT_DYNAMIC), "api_enabled": checked(plexpy.CONFIG.API_ENABLED), "api_key": plexpy.CONFIG.API_KEY, "update_db_interval": plexpy.CONFIG.UPDATE_DB_INTERVAL, @@ -3265,7 +3266,8 @@ class WebInterface(object): "allow_guest_access", "cache_images", "http_proxy", "notify_concurrent_by_ip", "history_table_activity", "plexpy_auto_update", "themoviedb_lookup", "tvmaze_lookup", "musicbrainz_lookup", "http_plex_admin", - "newsletter_self_hosted", "newsletter_inline_styles", "sys_tray_icon" + "newsletter_self_hosted", "newsletter_inline_styles", "sys_tray_icon", + "anon_redirect_dynamic" ] for checked_config in checked_configs: if checked_config not in kwargs: