You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Comrad/komrade/backend/operators.py

274 lines
8.2 KiB
Python

4 years ago
# internal imports
4 years ago
import os,sys; sys.path.append(os.path.abspath(os.path.join(os.path.abspath(os.path.join(os.path.dirname(__file__),'..')),'..')))
4 years ago
from komrade import *
4 years ago
# from komrade.backend.crypt import *
# from komrade.backend.keymaker import *
# from komrade.backend.mazes import *
# from komrade.backend.switchboard import *
from komrade.backend import *
4 years ago
4 years ago
4 years ago
def locate_an_operator(name):
4 years ago
global OPERATOR,TELEPHONE
from komrade.backend.the_operator import TheOperator
from komrade.backend.the_telephone import TheTelephone
from komrade.backend.callers import Caller
4 years ago
if name == OPERATOR_NAME:
4 years ago
return OPERATOR if OPERATOR else TheOperator()
4 years ago
if name == TELEPHONE_NAME:
4 years ago
return TELEPHONE if TELEPHONE else TheTelephone()
4 years ago
return Caller(name)
4 years ago
class Operator(Keymaker):
4 years ago
ROUTES = ['forge_new_keys','does_username_exist','hello_world']
4 years ago
4 years ago
def __init__(self, name, passphrase=DEBUG_DEFAULT_PASSPHRASE, keychain = {}, path_crypt_keys=PATH_CRYPT_CA_KEYS, path_crypt_data=PATH_CRYPT_CA_DATA):
4 years ago
super().__init__(name=name,passphrase=passphrase, keychain=keychain,
path_crypt_keys=path_crypt_keys, path_crypt_data=path_crypt_data)
4 years ago
# self.boot(create=False)
4 years ago
4 years ago
# connect phonelines?
from komrade.backend.phonelines import connect_phonelines
self.operator_keychain,self.telephone_keychain,self.omega_key = connect_phonelines()
4 years ago
# def boot(self,create=False):
# # Do I have my keys?
# have_keys = self.exists()
4 years ago
# # If not, forge them -- only once!
# if not have_keys and create:
# self.get_new_keys()
4 years ago
4 years ago
@property
def phone(self):
4 years ago
from komrade.backend.the_telephone import TheTelephone
if type(self)==TheTelephone: return self
4 years ago
if hasattr(self,'_phone'): return self._phone
4 years ago
4 years ago
global TELEPHONE,TELEPHONE_KEYCHAIN
if TELEPHONE: return TELEPHONE
4 years ago
4 years ago
self._phone=TELEPHONE=TheTelephone()
4 years ago
4 years ago
return TELEPHONE
@property
def op(self):
4 years ago
from komrade.backend.the_operator import TheOperator
if type(self)==TheOperator: return self
if hasattr(self,'_op'): return self._op
4 years ago
global OPERATOR,OPERATOR_KEYCHAIN
if OPERATOR: return OPERATOR
4 years ago
self._op=OPERATOR=TheOperator()
4 years ago
return OPERATOR
4 years ago
def compose_msg_to(self,msg,another):
4 years ago
if not self.privkey or not self.pubkey:
4 years ago
raise KomradeException('why do I have no pub/privkey pair!?',self,self.name,self.pubkey,self.privkey,self.keychain())
4 years ago
if not another.name or not another.pubkey:
4 years ago
raise KomradeException('why do I not know whom I\'m writing to?')
4 years ago
4 years ago
# otherwise create msg
4 years ago
msg_d = {
4 years ago
'_from_pub':self.pubkey,
'_from_name':self.name,
'_to_pub':another.pubkey,
4 years ago
'_to_name':another.name,
4 years ago
'_msg':msg,
4 years ago
}
4 years ago
# self.log(f'I am {self} packaging a message to {another}: {msg_d}')
4 years ago
from komrade.backend.messages import Message
4 years ago
4 years ago
msg_obj = Message(msg_d,from_whom=self,to_whom=another)
4 years ago
4 years ago
# encrypt!
4 years ago
# msg_obj.encrypt()
4 years ago
return msg_obj
4 years ago
# def compose_reply(self,msg,another):
4 years ago
def seal_msg(self,msg_d):
4 years ago
# make sure encrypted
4 years ago
self.log('sealing msg!:',dict_format(msg_d))
# msg_obj.encrypt(recursive=True)
4 years ago
# return pure binary version of self's entire msg_d
4 years ago
msg_b = pickle.dumps(msg_d)
4 years ago
self.log('pickled!',msg_b)
4 years ago
# encrypt by omega key
4 years ago
msg_b_encr = self.omega_key.encrypt(msg_b)
4 years ago
self.log('final seal:',msg_b_encr)
4 years ago
return msg_b_encr
4 years ago
def unseal_msg(self,msg_b_encr,from_whom=None,to_whom=None):
# default to assumption that I am the recipient
if not to_whom: to_whom=self
4 years ago
# decrypt by omega
msg_b = self.omega_key.decrypt(msg_b_encr)
# unpackage from transmission
4 years ago
msg_d = pickle.loads(msg_b)
4 years ago
# get message obj
4 years ago
print('unsealed msg:',msg_d)
4 years ago
from komrade.backend.messages import Message
4 years ago
msg_obj = Message(msg_d,from_whom=from_whom,to_whom=to_whom)
4 years ago
# decrypt msg
return msg_obj
4 years ago
4 years ago
def __repr__(self):
clsname=(type(self)).__name__
4 years ago
name = clsname+' '+self.name if self.name!=clsname else clsname
4 years ago
keystr='+'.join(self.top_keys)
4 years ago
return f'{name} [{self.uri}] ({keystr})'
4 years ago
4 years ago
def locate_an_operator(self,name):
if name == OPERATOR_NAME:
return TheOperator()
if name == TELEPHONE_NAME:
return TheTelephone()
4 years ago
return Caller(name)
4 years ago
4 years ago
4 years ago
# return resp_msg_obj
4 years ago
4 years ago
def route(self,msg_obj):
data,route = msg_obj.data, msg_obj.route
if not hasattr(self,route) or route not in self.ROUTES:
raise KomradeException('route not valid!')
# route it!
func = getattr(self,route)
new_data = func(**data)
self.log('got back from route func <-',new_data)
# return the other way
self.log('message was sent this way:',msg_obj)
msg_obj.mark_return_to_sender(new_msg=new_data)
self.log('now it\'s going the other way:',msg_obj)
4 years ago
# if not decrypted
if not msg_obj.is_encrypted:
msg_obj.encrypt()
4 years ago
return msg_obj
4 years ago
4 years ago
def route_msg(self,msg_obj):
4 years ago
# decrypt
4 years ago
self.log('got msg_obj!',msg_obj)
4 years ago
if msg_obj.is_encrypted:
msg_obj.decrypt()
4 years ago
# are there instructions for us?
if msg_obj.route:
# get result from routing
self.log(f'routing msg to self.{msg_obj.route}(**{msg_obj.data})')
4 years ago
return self.route(msg_obj)
4 years ago
# can we pass the buck on?
elif msg_obj.has_embedded_msg:
4 years ago
embedded_msg = msg_obj.msg
4 years ago
embedded_recipient = embedded_msg.to_whom
4 years ago
4 years ago
# whew, then we can make someone else take the phone
4 years ago
self.log(f'passing msg onto {embedded_recipient} ...')
4 years ago
return embedded_recipient.route_msg(embedded_msg)
4 years ago
# ???
4 years ago
self.log('what do I do? giving this back to you...')
return msg_obj
4 years ago
4 years ago
def ring_ring(self,msg,to_whom,get_resp_from=None):
# ring ring
self.log(f'''
ring ring!
I am {self}.
I have been told to pass onto {to_whom},
by way of function {get_resp_from},
the following msg:
{dict_format(msg,tab=5)}
''')
# get encr msg obj
msg_obj = self.compose_msg_to(
msg,
to_whom
)
self.log(f'ring ring! here is the message object I made, to send to {to_whom}: {msg_obj}')
# encrypting
msg_obj.encrypt()
# 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_obj = get_resp_from(msg_obj.msg_d)
self.log('resp_msg_obj <-',resp_msg_obj)
4 years ago
if resp_msg_obj.is_encrypted:
resp_msg_obj.decrypt()
4 years ago
4 years ago
4 years ago
# route back?
4 years ago
route_result = self.route_msg(resp_msg_obj)
self.log('route_result 2?',route_result)
return route_result
4 years ago
4 years ago
# decrypt?
4 years ago
# from komrade.backend.messages import Message
# if type(route_result)==Message:
# if route_result.is_encrypted:
# route_result.decrypt()
4 years ago
4 years ago
# return route_result
4 years ago
4 years ago
def pronto_pronto(self, msg_obj):
self.log(f'''
pronto pronto!
>> {msg_obj}
''')
route_response = self.route_msg(msg_obj)
4 years ago
self.log('route_response',route_response)
4 years ago
# set this to be the new msg
4 years ago
#msg_obj.msg = msg_obj.msg_d['_msg'] = response
#self.log('what msg_obj looks like now:',msg_obj)
# send new content back
resp_msg_obj = msg_obj.to_whom.compose_msg_to(
4 years ago
route_response,
4 years ago
msg_obj.from_whom
)
self.log('resp_msg_obj',resp_msg_obj)
4 years ago
# re-encrypt
4 years ago
resp_msg_obj.encrypt()
self.log(f're-encrypted: {resp_msg_obj}')
4 years ago
4 years ago
# pass msg back the chain
4 years ago
return resp_msg_obj
4 years ago