diff --git a/daemon.py b/daemon.py index 38e315ca0..27179492a 100644 --- a/daemon.py +++ b/daemon.py @@ -366,7 +366,7 @@ from fitnessFunctions import fitness_performance from fitnessFunctions import fitness_thread from fitnessFunctions import sorted_watch_points from fitnessFunctions import html_watch_points_graph -from siteactive import site_is_active +from siteactive import referer_is_active import os @@ -1106,19 +1106,11 @@ class PubServer(BaseHTTPRequestHandler): self._400() self.server.masto_api_is_active = False return True - - referer_url = http_prefix + '://' + referer_domain - if referer_domain + '/' in ua_str: - referer_url = referer_url + ua_str.split(referer_domain)[1] - if ' ' in referer_url: - referer_url = referer_url.split(' ')[0] - if ';' in referer_url: - referer_url = referer_url.split(';')[0] - if ')' in referer_url: - referer_url = referer_url.split(')')[0] - if not site_is_active(referer_url, calling_site_timeout): + if not referer_is_active(http_prefix, + referer_domain, ua_str, + calling_site_timeout): print('mastodon api referer url is not active ' + - referer_url) + referer_domain) self._400() self.server.masto_api_is_active = False return True @@ -1236,18 +1228,11 @@ class PubServer(BaseHTTPRequestHandler): self.server.nodeinfo_is_active = False return True - referer_url = http_prefix + '://' + referer_domain - if referer_domain + '/' in ua_str: - referer_url = referer_url + ua_str.split(referer_domain)[1] - if ' ' in referer_url: - referer_url = referer_url.split(' ')[0] - if ';' in referer_url: - referer_url = referer_url.split(';')[0] - if ')' in referer_url: - referer_url = referer_url.split(')')[0] - if not site_is_active(referer_url, calling_site_timeout): + if not referer_is_active(http_prefix, + referer_domain, ua_str, + calling_site_timeout): print('nodeinfo referer url is not active ' + - referer_url) + referer_domain) self._400() self.server.nodeinfo_is_active = False return True diff --git a/siteactive.py b/siteactive.py index ebc4ab444..05e6e94f2 100644 --- a/siteactive.py +++ b/siteactive.py @@ -120,3 +120,18 @@ def site_is_active(url: str, timeout: int) -> bool: except BaseException: print('EX: site_is_active ' + str(loc)) return False + + +def referer_is_active(http_prefix: str, + referer_domain: str, ua_str: str, + calling_site_timeout: int) -> bool: + """Returns true if the given referer is an active website + """ + referer_url = http_prefix + '://' + referer_domain + if referer_domain + '/' in ua_str: + referer_url = referer_url + ua_str.split(referer_domain)[1] + ending_chars = (' ', ';', ')') + for end_ch in ending_chars: + if end_ch in referer_url: + referer_url = referer_url.split(end_ch)[0] + return site_is_active(referer_url, calling_site_timeout)