Use different sessions for sending out posts

Posts addressed to onion instances use the onion session, etc
merge-requests/30/head
Bob Mottram 2022-03-11 22:13:22 +00:00
parent 5025924b99
commit 1119f10140
4 changed files with 208 additions and 60 deletions

111
daemon.py
View File

@ -698,13 +698,22 @@ class PubServer(BaseHTTPRequestHandler):
print('AUTH: Secure mode GET request not permitted: ' + key_id)
return False
if self.server.onion_domain:
if '.onion/' in key_id:
curr_session = self.server.session_onion
proxy_type = 'tor'
if self.server.i2p_domain:
if '.i2p/' in key_id:
curr_session = self.server.session_i2p
proxy_type = 'i2p'
curr_session = \
self._establish_session("secure mode",
curr_session, proxy_type)
if not curr_session:
return False
# obtain the public key
# obtain the public key. key_id is the actor
pub_key = \
get_person_pub_key(self.server.base_dir,
curr_session, key_id,
@ -8116,6 +8125,16 @@ class PubServer(BaseHTTPRequestHandler):
self._redirect_headers(actor_path_str, cookie,
calling_domain)
return
if self.server.onion_domain:
if '.onion/' in actor:
curr_session = self.server.session_onion
proxy_type = 'tor'
if self.server.i2p_domain:
if '.onion/' in actor:
curr_session = self.server.session_i2p
proxy_type = 'i2p'
curr_session = \
self._establish_session("announceButton",
curr_session, proxy_type)
@ -8279,6 +8298,16 @@ class PubServer(BaseHTTPRequestHandler):
self._redirect_headers(actor_path_str, cookie,
calling_domain)
return
if self.server.onion_domain:
if '.onion/' in actor:
curr_session = self.server.session_onion
proxy_type = 'tor'
if self.server.i2p_domain:
if '.onion/' in actor:
curr_session = self.server.session_i2p
proxy_type = 'i2p'
curr_session = \
self._establish_session("undoAnnounceButton",
curr_session, proxy_type)
@ -8358,6 +8387,16 @@ class PubServer(BaseHTTPRequestHandler):
handle_nickname + '@' + \
get_full_domain(handle_domain, handle_port)
if '@' in following_handle:
if self.server.onion_domain:
if following_handle.endswith('.onion'):
curr_session = self.server.session_onion
proxy_type = 'tor'
if self.server.i2p_domain:
if following_handle.endswith('.i2p'):
curr_session = self.server.session_i2p
proxy_type = 'i2p'
curr_session = \
self._establish_session("followApproveButton",
curr_session, proxy_type)
@ -8598,6 +8637,16 @@ class PubServer(BaseHTTPRequestHandler):
self._redirect_headers(actor_path_str, cookie,
calling_domain)
return
if self.server.onion_domain:
if '.onion/' in actor:
curr_session = self.server.session_onion
proxy_type = 'tor'
if self.server.i2p_domain:
if '.onion/' in actor:
curr_session = self.server.session_i2p
proxy_type = 'i2p'
curr_session = \
self._establish_session("likeButton",
curr_session, proxy_type)
@ -8774,6 +8823,16 @@ class PubServer(BaseHTTPRequestHandler):
self._redirect_headers(actor_path_str, cookie,
calling_domain)
return
if self.server.onion_domain:
if '.onion/' in actor:
curr_session = self.server.session_onion
proxy_type = 'tor'
if self.server.i2p_domain:
if '.onion/' in actor:
curr_session = self.server.session_i2p
proxy_type = 'i2p'
curr_session = \
self._establish_session("undoLikeButton",
curr_session, proxy_type)
@ -8955,6 +9014,16 @@ class PubServer(BaseHTTPRequestHandler):
self._redirect_headers(actor_path_str, cookie,
calling_domain)
return
if self.server.onion_domain:
if '.onion/' in actor:
curr_session = self.server.session_onion
proxy_type = 'tor'
if self.server.i2p_domain:
if '.onion/' in actor:
curr_session = self.server.session_i2p
proxy_type = 'i2p'
curr_session = \
self._establish_session("reactionButton",
curr_session, proxy_type)
@ -9152,6 +9221,16 @@ class PubServer(BaseHTTPRequestHandler):
calling_domain)
return
emoji_content = urllib.parse.unquote_plus(emoji_content_encoded)
if self.server.onion_domain:
if '.onion/' in actor:
curr_session = self.server.session_onion
proxy_type = 'tor'
if self.server.i2p_domain:
if '.onion/' in actor:
curr_session = self.server.session_i2p
proxy_type = 'i2p'
curr_session = \
self._establish_session("undoReactionButton",
curr_session, proxy_type)
@ -9420,6 +9499,16 @@ class PubServer(BaseHTTPRequestHandler):
self._redirect_headers(actor_path_str, cookie,
calling_domain)
return
if self.server.onion_domain:
if '.onion/' in actor:
curr_session = self.server.session_onion
proxy_type = 'tor'
if self.server.i2p_domain:
if '.onion/' in actor:
curr_session = self.server.session_i2p
proxy_type = 'i2p'
curr_session = \
self._establish_session("bookmarkButton",
curr_session, proxy_type)
@ -9559,6 +9648,16 @@ class PubServer(BaseHTTPRequestHandler):
self._redirect_headers(actor_path_str, cookie,
calling_domain)
return
if self.server.onion_domain:
if '.onion/' in actor:
curr_session = self.server.session_onion
proxy_type = 'tor'
if self.server.i2p_domain:
if '.onion/' in actor:
curr_session = self.server.session_i2p
proxy_type = 'i2p'
curr_session = \
self._establish_session("undo_bookmarkButton",
curr_session, proxy_type)
@ -9713,6 +9812,16 @@ class PubServer(BaseHTTPRequestHandler):
self._redirect_headers(actor + '/' + timeline_str,
cookie, calling_domain)
return
if self.server.onion_domain:
if '.onion/' in actor:
curr_session = self.server.session_onion
proxy_type = 'tor'
if self.server.i2p_domain:
if '.onion/' in actor:
curr_session = self.server.session_i2p
proxy_type = 'i2p'
curr_session = \
self._establish_session("deleteButton",
curr_session, proxy_type)

View File

@ -2647,7 +2647,8 @@ def _group_handle(base_dir: str, handle: str) -> bool:
return actor_json['type'] == 'Group'
def _send_to_group_members(session, base_dir: str, handle: str, port: int,
def _send_to_group_members(session, session_onion, session_i2p,
base_dir: str, handle: str, port: int,
post_json_object: {},
http_prefix: str, federation_list: [],
send_threads: [], post_log: [],
@ -2708,7 +2709,8 @@ def _send_to_group_members(session, base_dir: str, handle: str, port: int,
person_cache, cached_webfingers,
debug, __version__, signing_priv_key_pem)
send_to_followers_thread(session, base_dir, nickname, domain,
send_to_followers_thread(session, session_onion, session_i2p,
base_dir, nickname, domain,
onion_domain, i2p_domain, port,
http_prefix, federation_list,
send_threads, post_log,
@ -3033,8 +3035,8 @@ def _is_valid_dm(base_dir: str, nickname: str, domain: str, port: int,
def _receive_question_vote(base_dir: str, nickname: str, domain: str,
http_prefix: str, handle: str, debug: bool,
post_json_object: {}, recent_posts_cache: {},
session, onion_domain: str,
i2p_domain: str, port: int,
session, session_onion, session_i2p,
onion_domain: str, i2p_domain: str, port: int,
federation_list: [], send_threads: [], post_log: [],
cached_webfingers: {}, person_cache: {},
signing_priv_key_pem: str,
@ -3110,7 +3112,8 @@ def _receive_question_vote(base_dir: str, nickname: str, domain: str,
question_json['type'] = 'Update'
shared_items_federated_domains = []
shared_item_federation_tokens = {}
send_to_followers_thread(session, base_dir, nickname, domain,
send_to_followers_thread(session, session_onion, session_i2p,
base_dir, nickname, domain,
onion_domain, i2p_domain, port,
http_prefix, federation_list,
send_threads, post_log,
@ -3238,7 +3241,8 @@ def _check_for_git_patches(base_dir: str, nickname: str, domain: str,
def _inbox_after_initial(recent_posts_cache: {}, max_recent_posts: int,
session, key_id: str, handle: str, message_json: {},
session, session_onion, session_i2p,
key_id: str, handle: str, message_json: {},
base_dir: str, http_prefix: str, send_threads: [],
post_log: [], cached_webfingers: {}, person_cache: {},
queue: [], domain: str,
@ -3513,7 +3517,8 @@ def _inbox_after_initial(recent_posts_cache: {}, max_recent_posts: int,
_receive_question_vote(base_dir, nickname, domain,
http_prefix, handle, debug,
post_json_object, recent_posts_cache,
session, onion_domain, i2p_domain, port,
session, session_onion, session_i2p,
onion_domain, i2p_domain, port,
federation_list, send_threads, post_log,
cached_webfingers, person_cache,
signing_priv_key_pem,
@ -3679,7 +3684,8 @@ def _inbox_after_initial(recent_posts_cache: {}, max_recent_posts: int,
# send the post out to group members
if is_group:
_send_to_group_members(session, base_dir, handle, port,
_send_to_group_members(session, session_onion, session_i2p,
base_dir, handle, port,
post_json_object,
http_prefix, federation_list,
send_threads,
@ -4167,21 +4173,27 @@ def run_inbox_queue(recent_posts_cache: {}, max_recent_posts: int,
"""
print('Starting new session when starting inbox queue')
curr_session_time = int(time.time())
session_last_update = curr_session_time
session_last_update = 0
session = create_session(proxy_type)
if session:
session_last_update = curr_session_time
# is this is a clearnet instance then optionally start sessions
# for onion and i2p domains
session_onion = None
session_i2p = None
session_last_update_onion = curr_session_time
session_last_update_i2p = curr_session_time
session_last_update_onion = 0
session_last_update_i2p = 0
if proxy_type != 'tor' and onion_domain:
print('Starting onion session when starting inbox queue')
session_onion = create_session('tor')
if session_onion:
session_onion = curr_session_time
if proxy_type != 'i2p' and i2p_domain:
print('Starting i2p session when starting inbox queue')
session_i2p = create_session('i2p')
if session_i2p:
session_i2p = curr_session_time
inbox_handle = 'inbox@' + domain
if debug:
@ -4294,10 +4306,37 @@ def run_inbox_queue(recent_posts_cache: {}, max_recent_posts: int,
account_max_posts_per_day, debug):
continue
# recreate the session periodically
if not session or curr_time - session_last_update > 21600:
print('Regenerating inbox queue session at 6hr interval')
session = create_session(proxy_type)
if session:
session_last_update = curr_time
else:
print('WARN: inbox session not created')
continue
if onion_domain:
if not session_onion or \
curr_time - session_last_update_onion > 21600:
print('Regenerating inbox queue onion session at 6hr interval')
session_onion = create_session('tor')
if session_onion:
session_last_update_onion = curr_time
else:
print('WARN: inbox onion session not created')
continue
if i2p_domain:
if not session_i2p or curr_time - session_last_update_i2p > 21600:
print('Regenerating inbox queue i2p session at 6hr interval')
session_i2p = create_session('i2p')
if session_i2p:
session_last_update_i2p = curr_time
else:
print('WARN: inbox i2p session not created')
continue
curr_session = session
curr_proxy_type = proxy_type
curr_session_last_update = session_last_update
session_type = 'default'
if queue_json.get('actor'):
if isinstance(queue_json['actor'], str):
sender_domain, _ = get_domain_from_actor(queue_json['actor'])
@ -4305,30 +4344,10 @@ def run_inbox_queue(recent_posts_cache: {}, max_recent_posts: int,
session_onion and proxy_type != 'tor':
curr_proxy_type = 'tor'
curr_session = session_onion
session_type = 'onion'
curr_session_last_update = session_last_update_onion
elif (sender_domain.endswith('.i2p') and
session_i2p and proxy_type != 'i2p'):
curr_proxy_type = 'i2p'
curr_session = session_i2p
session_type = 'i2p'
curr_session_last_update = session_last_update_i2p
# recreate the session periodically
if not curr_session or curr_time - curr_session_last_update > 21600:
print('Regenerating inbox queue session at 6hr interval')
curr_session = create_session(curr_proxy_type)
if not curr_session:
continue
if session_type == 'default':
session = curr_session
session_last_update = curr_time
elif session_type == 'onion':
session_onion = curr_session
session_last_update_onion = curr_time
elif session_type == 'i2p':
session_i2p = curr_session
session_last_update_i2p = curr_time
if debug and queue_json.get('actor'):
print('Obtaining public key for actor ' + queue_json['actor'])
@ -4610,7 +4629,8 @@ def run_inbox_queue(recent_posts_cache: {}, max_recent_posts: int,
languages_understood = []
_inbox_after_initial(recent_posts_cache,
max_recent_posts,
curr_session, key_id, handle,
session, session_onion, session_i2p,
key_id, handle,
queue_json['post'],
base_dir, http_prefix,
send_threads, post_log,

View File

@ -510,6 +510,8 @@ def post_message_to_outbox(session, translate: {},
# create a thread to send the post to followers
followers_thread = \
send_to_followers_thread(server.session,
server.session_onion,
server.session_i2p,
base_dir,
post_to_nickname,
domain, onion_domain, i2p_domain,
@ -651,8 +653,10 @@ def post_message_to_outbox(session, translate: {},
print('c2s sender: ' +
post_to_nickname + '@' + domain + ':' + str(port))
named_addresses_thread = \
send_to_named_addresses_thread(server.session, base_dir,
post_to_nickname,
send_to_named_addresses_thread(server.session,
server.session_onion,
server.session_i2p,
base_dir, post_to_nickname,
domain, onion_domain, i2p_domain, port,
http_prefix,
federation_list,

View File

@ -2981,7 +2981,8 @@ def _is_profile_update(post_json_object: {}) -> bool:
return False
def _send_to_named_addresses(session, base_dir: str,
def _send_to_named_addresses(session, session_onion, session_i2p,
base_dir: str,
nickname: str, domain: str,
onion_domain: str, i2p_domain: str, port: int,
http_prefix: str, federation_list: [],
@ -3102,16 +3103,19 @@ def _send_to_named_addresses(session, base_dir: str,
from_domain = domain
from_domain_full = get_full_domain(domain, port)
from_http_prefix = http_prefix
curr_session = session
if onion_domain:
if to_domain.endswith('.onion'):
from_domain = onion_domain
from_domain_full = onion_domain
from_http_prefix = 'http'
elif i2p_domain:
curr_session = session_onion
if i2p_domain:
if to_domain.endswith('.i2p'):
from_domain = i2p_domain
from_domain_full = i2p_domain
from_http_prefix = 'http'
curr_session = session_i2p
cc_list = []
# if the "to" domain is within the shared items
@ -3125,7 +3129,7 @@ def _send_to_named_addresses(session, base_dir: str,
group_account = has_group_type(base_dir, address, person_cache)
send_signed_json(post_json_object, session, base_dir,
send_signed_json(post_json_object, curr_session, base_dir,
nickname, from_domain, port,
to_nickname, to_domain, to_port,
cc_list, from_http_prefix, True, client_to_server,
@ -3136,8 +3140,8 @@ def _send_to_named_addresses(session, base_dir: str,
signing_priv_key_pem, 34436782)
def send_to_named_addresses_thread(session, base_dir: str,
nickname: str, domain: str,
def send_to_named_addresses_thread(session, session_onion, session_i2p,
base_dir: str, nickname: str, domain: str,
onion_domain: str,
i2p_domain: str, port: int,
http_prefix: str, federation_list: [],
@ -3152,8 +3156,8 @@ def send_to_named_addresses_thread(session, base_dir: str,
"""
send_thread = \
thread_with_trace(target=_send_to_named_addresses,
args=(session, base_dir,
nickname, domain,
args=(session, session_onion, session_i2p,
base_dir, nickname, domain,
onion_domain, i2p_domain, port,
http_prefix, federation_list,
send_threads, post_log,
@ -3209,9 +3213,8 @@ def _sending_profile_update(post_json_object: {}) -> bool:
return False
def send_to_followers(session, base_dir: str,
nickname: str,
domain: str,
def send_to_followers(session, session_onion, session_i2p,
base_dir: str, nickname: str, domain: str,
onion_domain: str, i2p_domain: str, port: int,
http_prefix: str, federation_list: [],
send_threads: [], post_log: [],
@ -3224,9 +3227,6 @@ def send_to_followers(session, base_dir: str,
"""sends a post to the followers of the given nickname
"""
print('send_to_followers')
if not session:
print('WARN: No session for send_to_followers')
return
if not _post_is_addressed_to_followers(base_dir, nickname, domain,
port, http_prefix,
post_json_object):
@ -3278,9 +3278,25 @@ def send_to_followers(session, base_dir: str,
print('Sending post to followers domain is active: ' +
follower_domain_url)
# select the appropriate session
curr_session = session
curr_http_prefix = http_prefix
if onion_domain:
if follower_domain.endswith('.onion'):
curr_session = session_onion
curr_http_prefix = 'http'
if i2p_domain:
if follower_domain.endswith('.i2p'):
curr_session = session_i2p
curr_http_prefix = 'http'
if not curr_session:
print('WARN: session not found when sending to follower ' +
follower_domain_url)
continue
with_shared_inbox = \
_has_shared_inbox(session, http_prefix, follower_domain, debug,
signing_priv_key_pem)
_has_shared_inbox(curr_session, curr_http_prefix, follower_domain,
debug, signing_priv_key_pem)
if debug:
if with_shared_inbox:
print(follower_domain + ' has shared inbox')
@ -3305,7 +3321,7 @@ def send_to_followers(session, base_dir: str,
if to_domain.endswith('.onion'):
from_domain = onion_domain
from_http_prefix = 'http'
elif i2p_domain:
if i2p_domain:
if to_domain.endswith('.i2p'):
from_domain = i2p_domain
from_http_prefix = 'http'
@ -3333,7 +3349,7 @@ def send_to_followers(session, base_dir: str,
nickname + '@' + domain +
' to ' + to_nickname + '@' + to_domain)
send_signed_json(post_json_object, session, base_dir,
send_signed_json(post_json_object, curr_session, base_dir,
nickname, from_domain, port,
to_nickname, to_domain, to_port,
cc_list, from_http_prefix, True,
@ -3362,7 +3378,7 @@ def send_to_followers(session, base_dir: str,
nickname + '@' + domain + ' to ' +
to_nickname + '@' + to_domain)
send_signed_json(post_json_object, session, base_dir,
send_signed_json(post_json_object, curr_session, base_dir,
nickname, from_domain, port,
to_nickname, to_domain, to_port,
cc_list, from_http_prefix, True,
@ -3383,9 +3399,8 @@ def send_to_followers(session, base_dir: str,
print('Sending post to followers ends ' + str(sending_mins) + ' mins')
def send_to_followers_thread(session, base_dir: str,
nickname: str,
domain: str,
def send_to_followers_thread(session, session_onion, session_i2p,
base_dir: str, nickname: str, domain: str,
onion_domain: str, i2p_domain: str, port: int,
http_prefix: str, federation_list: [],
send_threads: [], post_log: [],
@ -3399,8 +3414,8 @@ def send_to_followers_thread(session, base_dir: str,
"""
send_thread = \
thread_with_trace(target=send_to_followers,
args=(session, base_dir,
nickname, domain,
args=(session, session_onion, session_i2p,
base_dir, nickname, domain,
onion_domain, i2p_domain, port,
http_prefix, federation_list,
send_threads, post_log,