52 lines
2.0 KiB
Python
Executable File
52 lines
2.0 KiB
Python
Executable File
#!/usr/bin/python3
|
|
import sys, pathlib, subprocess, os
|
|
import ressenger_common, ressenger_exceptions, ressenger_cryptography
|
|
|
|
# TODO: Add interruption handling to this user daemon
|
|
|
|
def run_in_background(pyfile, args=None, stdout=None, stderr=None):
|
|
args = args or []
|
|
cmd = [sys.executable, pyfile] + args
|
|
|
|
p = subprocess.Popen(
|
|
cmd,
|
|
stdout=stdout or subprocess.DEVNULL,
|
|
stderr=stderr or subprocess.DEVNULL,
|
|
start_new_session=True,
|
|
close_fds=True
|
|
)
|
|
return p
|
|
|
|
def user_daemon(username, password):
|
|
user=ressenger_common.load_user(password, username)
|
|
run_in_background('ressenger_server.py', args=[user['port']])
|
|
cache1_path = pathlib.Path(f"~/.ressenger/cache_{user['port']}").expanduser()
|
|
cache2_path = pathlib.Path(f"~/.ressenger/cache_{username}").expanduser()
|
|
with open(cache2_path, 'wb') as file:
|
|
file.write(pickle.dumps({}, protocol=pickle.HIGHEST_PROTOCOL))
|
|
cache1_processed=[]
|
|
cache2_processed=[]
|
|
while True:
|
|
with open(cache1_path, 'rb') as file:
|
|
cache1=pickle.loads(file.read())
|
|
with open(cache2_path, 'rb') as file:
|
|
cache2=pickle.loads(file.read())
|
|
for i1 in cache1.keys():
|
|
if not (i1 in cache1_processed):
|
|
packet=pickle.loads(cache1[i1])
|
|
decrypted_data_bytes, validity = ressenger_cryptography.decrypt(packet['data'], user['enc_pri'], packet['sig_pub'])
|
|
if validity:
|
|
data=pickle.loads(decrypted_data_bytes)
|
|
event={'type':'recv', 'data':data, 'contact':contact_hash(packet['enc_pub'], packet['sig_pub'])}
|
|
user['events'][time.time_ns()]=event
|
|
cache1_processed.append(i1)
|
|
for i1 in cache2.keys():
|
|
if not (i1 in cache2_processed):
|
|
event=cache2[i1]
|
|
user['events'][time.time_ns()]=event
|
|
cache2_processed.append(i1)
|
|
ressenger_common.dump_user(password, user, username)
|
|
|
|
if __name__=='__main__':
|
|
user_daemon(sys.argv[1], sys.argv[2])
|