diff --git a/posts.py b/posts.py index 5027c74e4..50f8a0532 100644 --- a/posts.py +++ b/posts.py @@ -2262,7 +2262,8 @@ def thread_send_post(session, post_json_str: str, federation_list: [], inbox_url: str, base_dir: str, signature_header_json: {}, signature_header_json_ld: {}, - post_log: [], debug: bool) -> None: + post_log: [], debug: bool, + http_prefix: str, domain_full: str) -> None: """Sends a with retries """ tries = 0 @@ -2276,7 +2277,7 @@ def thread_send_post(session, post_json_str: str, federation_list: [], post_result, unauthorized, return_code = \ post_json_string(session, post_json_str, federation_list, inbox_url, signature_header_json, - debug) + debug, http_prefix, domain_full) if return_code >= 500 and return_code < 600: # if an instance is returning a code which indicates that # it might have a runtime error, like 503, then don't @@ -2298,7 +2299,7 @@ def thread_send_post(session, post_json_str: str, federation_list: [], post_result, unauthorized, return_code = \ post_json_string(session, post_json_str, federation_list, inbox_url, signature_header_json_ld, - debug) + debug, http_prefix, domain_full) if return_code >= 500 and return_code < 600: # if an instance is returning a code which indicates that # it might have a runtime error, like 503, then don't @@ -2467,8 +2468,8 @@ def send_post(signing_priv_key_pem: str, project_version: str, # if the "to" domain is within the shared items # federation list then send the token for this domain # so that it can request a catalog + domain_full = get_full_domain(domain, port) if to_domain in shared_items_federated_domains: - domain_full = get_full_domain(domain, port) if shared_item_federation_tokens.get(domain_full): signature_header_json['Origin'] = domain_full signature_header_json_ld['Origin'] = domain_full @@ -2501,8 +2502,8 @@ def send_post(signing_priv_key_pem: str, project_version: str, inbox_url, base_dir, signature_header_json.copy(), signature_header_json_ld.copy(), - post_log, - debug), daemon=True) + post_log, debug, http_prefix, + domain_full), daemon=True) send_threads.append(thr) thr.start() return 0 @@ -2625,7 +2626,7 @@ def send_post_via_server(signing_priv_key_pem: str, project_version: str, } post_result = \ post_image(session, attach_image_filename, [], - inbox_url, headers) + inbox_url, headers, http_prefix, from_domain_full) if not post_result: if debug: print('DEBUG: post failed to upload image') @@ -2639,7 +2640,9 @@ def send_post_via_server(signing_priv_key_pem: str, project_version: str, post_dumps = json.dumps(post_json_object) post_result, unauthorized, return_code = \ post_json_string(session, post_dumps, [], - inbox_url, headers, debug, 5, True) + inbox_url, headers, debug, + http_prefix, from_domain_full, + 5, True) if not post_result: if debug: if unauthorized: @@ -2877,6 +2880,7 @@ def send_signed_json(post_json_object: {}, session, base_dir: str, if debug: print('DEBUG: starting thread to send post') pprint(post_json_object) + domain_full = get_full_domain(domain, port) thr = \ thread_with_trace(target=thread_send_post, args=(session, @@ -2885,8 +2889,8 @@ def send_signed_json(post_json_object: {}, session, base_dir: str, inbox_url, base_dir, signature_header_json.copy(), signature_header_json_ld.copy(), - post_log, - debug), daemon=True) + post_log, debug, + http_prefix, domain_full), daemon=True) send_threads.append(thr) # thr.start() return 0 diff --git a/session.py b/session.py index af7fdf79b..dbb471bb6 100644 --- a/session.py +++ b/session.py @@ -367,6 +367,15 @@ def download_html(signing_priv_key_pem: str, None, quiet, debug, False) +def _set_user_agent(session, http_prefix: str, domain_full: str) -> None: + """Sets the user agent + """ + ua_str = \ + 'Epicyon/' + __version__ + '; +' + \ + http_prefix + '://' + domain_full + '/' + session.headers.update({'User-Agent': ua_str}) + + def post_json(http_prefix: str, domain_full: str, session, post_json_object: {}, federation_list: [], inbox_url: str, headers: {}, timeout_sec: int = 60, @@ -379,16 +388,13 @@ def post_json(http_prefix: str, domain_full: str, print('post_json: ' + inbox_url + ' not permitted') return None - session_headers = headers - session_headers['User-Agent'] = 'Epicyon/' + __version__ - session_headers['User-Agent'] += \ - '; +' + http_prefix + '://' + domain_full + '/' + _set_user_agent(session, http_prefix, domain_full) try: post_result = \ session.post(url=inbox_url, data=json.dumps(post_json_object), - headers=session_headers, timeout=timeout_sec) + headers=headers, timeout=timeout_sec) except requests.Timeout as ex: if not quiet: print('EX: post_json timeout ' + inbox_url + ' ' + @@ -421,6 +427,7 @@ def post_json_string(session, post_jsonStr: str, inbox_url: str, headers: {}, debug: bool, + http_prefix: str, domain_full: str, timeout_sec: int = 30, quiet: bool = False) -> (bool, bool, int): """Post a json message string to the inbox of another person @@ -435,6 +442,8 @@ def post_json_string(session, post_jsonStr: str, print('post_json_string: ' + inbox_url + ' not permitted') return False, True, 0 + _set_user_agent(session, http_prefix, domain_full) + try: post_result = \ session.post(url=inbox_url, data=post_jsonStr, @@ -473,7 +482,8 @@ def post_json_string(session, post_jsonStr: str, def post_image(session, attach_image_filename: str, federation_list: [], - inbox_url: str, headers: {}) -> str: + inbox_url: str, headers: {}, + http_prefix: str, domain_full: str) -> str: """Post an image to the inbox of another person or outbox via c2s """ # check that we are posting to a permitted domain @@ -504,6 +514,9 @@ def post_image(session, attach_image_filename: str, federation_list: [], with open(attach_image_filename, 'rb') as av_file: media_binary = av_file.read() + + _set_user_agent(session, http_prefix, domain_full) + try: post_result = session.post(url=inbox_url, data=media_binary, headers=headers) diff --git a/shares.py b/shares.py index 2393712e4..bbca66365 100644 --- a/shares.py +++ b/shares.py @@ -653,10 +653,10 @@ def send_share_via_server(base_dir, session, 'host': from_domain, 'Authorization': auth_header } + inbox_url_str = inbox_url.replace('/' + post_to_box, '/shares') post_result = \ - post_image(session, image_filename, [], - inbox_url.replace('/' + post_to_box, '/shares'), - headers) + post_image(session, image_filename, [], inbox_url_str, + headers, http_prefix, from_domain_full) headers = { 'host': from_domain, @@ -873,10 +873,10 @@ def send_wanted_via_server(base_dir, session, 'host': from_domain, 'Authorization': auth_header } + inbox_url_str = inbox_url.replace('/' + post_to_box, '/wanted') post_result = \ - post_image(session, image_filename, [], - inbox_url.replace('/' + post_to_box, '/wanted'), - headers) + post_image(session, image_filename, [], inbox_url_str, + headers, http_prefix, from_domain_full) headers = { 'host': from_domain,