2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-17 21:25:37 +00:00
Comrad/komrade/backend/operators.py

124 lines
3.9 KiB
Python
Raw Normal View History

2020-09-05 16:26:37 +00:00
# internal imports
2020-09-04 15:50:08 +00:00
import os,sys; sys.path.append(os.path.abspath(os.path.join(os.path.abspath(os.path.join(os.path.dirname(__file__),'..')),'..')))
2020-09-05 16:26:37 +00:00
from komrade import *
2020-09-09 14:38:37 +00:00
# from komrade.backend.crypt import *
# from komrade.backend.keymaker import *
# from komrade.backend.mazes import *
# from komrade.backend.switchboard import *
from komrade.backend import *
2020-09-04 15:50:08 +00:00
2020-09-06 06:50:23 +00:00
2020-09-04 15:50:08 +00:00
class Operator(Keymaker):
2020-09-08 07:20:42 +00:00
def __init__(self, name, passphrase=DEBUG_DEFAULT_PASSPHRASE, keychain = {}, path_crypt_keys=PATH_CRYPT_CA_KEYS, path_crypt_data=PATH_CRYPT_CA_DATA):
2020-09-07 17:11:52 +00:00
super().__init__(name=name,passphrase=passphrase, keychain=keychain,
path_crypt_keys=path_crypt_keys, path_crypt_data=path_crypt_data)
2020-09-06 19:39:44 +00:00
self.boot(create=False)
2020-09-04 15:50:08 +00:00
2020-09-09 14:38:37 +00:00
# connect phonelines?
from komrade.backend.phonelines import connect_phonelines
self.operator_keychain,self.telephone_keychain,self.omega_key = connect_phonelines()
2020-09-05 14:09:31 +00:00
def boot(self,create=False):
# Do I have my keys?
have_keys = self.exists()
# If not, forge them -- only once!
if not have_keys and create:
self.get_new_keys()
2020-09-08 07:01:35 +00:00
2020-09-07 17:11:52 +00:00
@property
def phone(self):
2020-09-07 20:00:21 +00:00
from komrade.backend.the_telephone import TheTelephone
if type(self)==TheTelephone: return self
2020-09-07 17:50:58 +00:00
if hasattr(self,'_phone'): return self._phone
2020-09-07 20:00:21 +00:00
2020-09-07 17:11:52 +00:00
global TELEPHONE,TELEPHONE_KEYCHAIN
if TELEPHONE: return TELEPHONE
2020-09-07 20:00:21 +00:00
2020-09-07 17:50:58 +00:00
self._phone=TELEPHONE=TheTelephone()
2020-09-07 20:00:21 +00:00
2020-09-07 17:11:52 +00:00
return TELEPHONE
@property
def op(self):
2020-09-07 20:00:21 +00:00
from komrade.backend.the_operator import TheOperator
if type(self)==TheOperator: return self
if hasattr(self,'_op'): return self._op
2020-09-07 17:11:52 +00:00
global OPERATOR,OPERATOR_KEYCHAIN
if OPERATOR: return OPERATOR
2020-09-07 20:00:21 +00:00
self._op=OPERATOR=TheOperator()
2020-09-07 17:11:52 +00:00
return OPERATOR
2020-09-09 14:38:37 +00:00
def compose_msg_to(self,msg,another):
2020-09-08 15:14:48 +00:00
if not self.privkey or not self.pubkey:
2020-09-09 14:38:37 +00:00
raise KomradeException('why do I have no pub/privkey pair!?',self,self.name)
2020-09-08 15:14:48 +00:00
if not another.name or not another.pubkey:
2020-09-09 14:38:37 +00:00
raise KomradeException('why do I not know whom I\'m writing to?')
2020-09-08 15:14:48 +00:00
2020-09-09 14:38:37 +00:00
# otherwise create msg
2020-09-09 11:01:27 +00:00
msg_d = {
2020-09-08 11:23:41 +00:00
'_from_pub':self.pubkey,
'_from_name':self.name,
'_to_pub':another.pubkey,
2020-09-08 15:14:48 +00:00
'_to_name':another.name,
2020-09-09 14:38:37 +00:00
'_msg':msg,
2020-09-08 11:23:41 +00:00
}
2020-09-09 14:38:37 +00:00
self.log(f'I am a {type(self)} packaging a message to {another}: {msg_d}')
from komrade.backend.messages import Message
msg_obj = Message(msg_d,caller=self,callee=another)
self.log('created msg obj:',msg_obj)
return msg_obj
def seal_msg(self,msg_obj):
# make sure encrypted
msg_obj.encrypt()
# return pure binary version of self's entire msg_d
msg_b = package_for_transmission(msg_obj.msg_d)
# encrypte by omega key
msg_b_encr = self.omega_key.encrypt(msg_b)
return msg_b_encr
def unseal_msg(self,msg_b_encr):
# decrypt by omega
msg_b = self.omega_key.decrypt(msg_b_encr)
# unpackage from transmission
msg_d = unpackage_from_transmission(msg_b)
# get message obj
2020-09-09 14:50:10 +00:00
from komrade.backend.messages import Message
2020-09-09 14:38:37 +00:00
msg_obj = Message(msg_d)
# decrypt msg
msg_obj.decrypt()
return msg_obj
2020-09-07 20:00:21 +00:00
2020-09-07 17:11:52 +00:00
2020-09-09 14:38:37 +00:00
# self = caller
# towhom = phone
# bywayof = op
def ring_ring(self,msg,to_whom,get_resp_from=None):
# get encr msg obj
msg_obj = self.compose_msg_to(msg, to_whom)
# pass onto next person
# get pure encrypted binary, sealed
msg_sealed = self.seal_msg(msg_obj)
# pass onto next person...
if not get_resp_from: get_resp_from=to_whom.ring_ring
resp_msg_b = get_resp_from(msg_sealed)
# unseal msg
resp_msg_obj = self.unseal_msg(resp_msg_b)
return resp_msg_obj