Include user agent in sent posts

merge-requests/30/head
Bob Mottram 2022-02-28 11:55:36 +00:00
parent f852e0e299
commit aaf7f299fa
3 changed files with 39 additions and 22 deletions

View File

@ -2262,7 +2262,8 @@ def thread_send_post(session, post_json_str: str, federation_list: [],
inbox_url: str, base_dir: str, inbox_url: str, base_dir: str,
signature_header_json: {}, signature_header_json: {},
signature_header_json_ld: {}, signature_header_json_ld: {},
post_log: [], debug: bool) -> None: post_log: [], debug: bool,
http_prefix: str, domain_full: str) -> None:
"""Sends a with retries """Sends a with retries
""" """
tries = 0 tries = 0
@ -2276,7 +2277,7 @@ def thread_send_post(session, post_json_str: str, federation_list: [],
post_result, unauthorized, return_code = \ post_result, unauthorized, return_code = \
post_json_string(session, post_json_str, federation_list, post_json_string(session, post_json_str, federation_list,
inbox_url, signature_header_json, inbox_url, signature_header_json,
debug) debug, http_prefix, domain_full)
if return_code >= 500 and return_code < 600: if return_code >= 500 and return_code < 600:
# if an instance is returning a code which indicates that # if an instance is returning a code which indicates that
# it might have a runtime error, like 503, then don't # 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_result, unauthorized, return_code = \
post_json_string(session, post_json_str, federation_list, post_json_string(session, post_json_str, federation_list,
inbox_url, signature_header_json_ld, inbox_url, signature_header_json_ld,
debug) debug, http_prefix, domain_full)
if return_code >= 500 and return_code < 600: if return_code >= 500 and return_code < 600:
# if an instance is returning a code which indicates that # if an instance is returning a code which indicates that
# it might have a runtime error, like 503, then don't # 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 # if the "to" domain is within the shared items
# federation list then send the token for this domain # federation list then send the token for this domain
# so that it can request a catalog # so that it can request a catalog
domain_full = get_full_domain(domain, port)
if to_domain in shared_items_federated_domains: if to_domain in shared_items_federated_domains:
domain_full = get_full_domain(domain, port)
if shared_item_federation_tokens.get(domain_full): if shared_item_federation_tokens.get(domain_full):
signature_header_json['Origin'] = domain_full signature_header_json['Origin'] = domain_full
signature_header_json_ld['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, inbox_url, base_dir,
signature_header_json.copy(), signature_header_json.copy(),
signature_header_json_ld.copy(), signature_header_json_ld.copy(),
post_log, post_log, debug, http_prefix,
debug), daemon=True) domain_full), daemon=True)
send_threads.append(thr) send_threads.append(thr)
thr.start() thr.start()
return 0 return 0
@ -2625,7 +2626,7 @@ def send_post_via_server(signing_priv_key_pem: str, project_version: str,
} }
post_result = \ post_result = \
post_image(session, attach_image_filename, [], post_image(session, attach_image_filename, [],
inbox_url, headers) inbox_url, headers, http_prefix, from_domain_full)
if not post_result: if not post_result:
if debug: if debug:
print('DEBUG: post failed to upload image') 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_dumps = json.dumps(post_json_object)
post_result, unauthorized, return_code = \ post_result, unauthorized, return_code = \
post_json_string(session, post_dumps, [], 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 not post_result:
if debug: if debug:
if unauthorized: if unauthorized:
@ -2877,6 +2880,7 @@ def send_signed_json(post_json_object: {}, session, base_dir: str,
if debug: if debug:
print('DEBUG: starting thread to send post') print('DEBUG: starting thread to send post')
pprint(post_json_object) pprint(post_json_object)
domain_full = get_full_domain(domain, port)
thr = \ thr = \
thread_with_trace(target=thread_send_post, thread_with_trace(target=thread_send_post,
args=(session, args=(session,
@ -2885,8 +2889,8 @@ def send_signed_json(post_json_object: {}, session, base_dir: str,
inbox_url, base_dir, inbox_url, base_dir,
signature_header_json.copy(), signature_header_json.copy(),
signature_header_json_ld.copy(), signature_header_json_ld.copy(),
post_log, post_log, debug,
debug), daemon=True) http_prefix, domain_full), daemon=True)
send_threads.append(thr) send_threads.append(thr)
# thr.start() # thr.start()
return 0 return 0

View File

@ -367,6 +367,15 @@ def download_html(signing_priv_key_pem: str,
None, quiet, debug, False) 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, def post_json(http_prefix: str, domain_full: str,
session, post_json_object: {}, federation_list: [], session, post_json_object: {}, federation_list: [],
inbox_url: str, headers: {}, timeout_sec: int = 60, 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') print('post_json: ' + inbox_url + ' not permitted')
return None return None
session_headers = headers _set_user_agent(session, http_prefix, domain_full)
session_headers['User-Agent'] = 'Epicyon/' + __version__
session_headers['User-Agent'] += \
'; +' + http_prefix + '://' + domain_full + '/'
try: try:
post_result = \ post_result = \
session.post(url=inbox_url, session.post(url=inbox_url,
data=json.dumps(post_json_object), data=json.dumps(post_json_object),
headers=session_headers, timeout=timeout_sec) headers=headers, timeout=timeout_sec)
except requests.Timeout as ex: except requests.Timeout as ex:
if not quiet: if not quiet:
print('EX: post_json timeout ' + inbox_url + ' ' + print('EX: post_json timeout ' + inbox_url + ' ' +
@ -421,6 +427,7 @@ def post_json_string(session, post_jsonStr: str,
inbox_url: str, inbox_url: str,
headers: {}, headers: {},
debug: bool, debug: bool,
http_prefix: str, domain_full: str,
timeout_sec: int = 30, timeout_sec: int = 30,
quiet: bool = False) -> (bool, bool, int): quiet: bool = False) -> (bool, bool, int):
"""Post a json message string to the inbox of another person """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') print('post_json_string: ' + inbox_url + ' not permitted')
return False, True, 0 return False, True, 0
_set_user_agent(session, http_prefix, domain_full)
try: try:
post_result = \ post_result = \
session.post(url=inbox_url, data=post_jsonStr, 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: [], 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 """Post an image to the inbox of another person or outbox via c2s
""" """
# check that we are posting to a permitted domain # 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: with open(attach_image_filename, 'rb') as av_file:
media_binary = av_file.read() media_binary = av_file.read()
_set_user_agent(session, http_prefix, domain_full)
try: try:
post_result = session.post(url=inbox_url, data=media_binary, post_result = session.post(url=inbox_url, data=media_binary,
headers=headers) headers=headers)

View File

@ -653,10 +653,10 @@ def send_share_via_server(base_dir, session,
'host': from_domain, 'host': from_domain,
'Authorization': auth_header 'Authorization': auth_header
} }
inbox_url_str = inbox_url.replace('/' + post_to_box, '/shares')
post_result = \ post_result = \
post_image(session, image_filename, [], post_image(session, image_filename, [], inbox_url_str,
inbox_url.replace('/' + post_to_box, '/shares'), headers, http_prefix, from_domain_full)
headers)
headers = { headers = {
'host': from_domain, 'host': from_domain,
@ -873,10 +873,10 @@ def send_wanted_via_server(base_dir, session,
'host': from_domain, 'host': from_domain,
'Authorization': auth_header 'Authorization': auth_header
} }
inbox_url_str = inbox_url.replace('/' + post_to_box, '/wanted')
post_result = \ post_result = \
post_image(session, image_filename, [], post_image(session, image_filename, [], inbox_url_str,
inbox_url.replace('/' + post_to_box, '/wanted'), headers, http_prefix, from_domain_full)
headers)
headers = { headers = {
'host': from_domain, 'host': from_domain,