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-09 21:30:14 +00:00
|
|
|
ROUTES = ['forge_new_keys','does_username_exist','hello_world']
|
2020-09-04 15:50:08 +00:00
|
|
|
|
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-09 17:34:19 +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-09 17:34:19 +00:00
|
|
|
# def boot(self,create=False):
|
|
|
|
# # Do I have my keys?
|
|
|
|
# have_keys = self.exists()
|
2020-09-05 14:09:31 +00:00
|
|
|
|
2020-09-09 17:34:19 +00:00
|
|
|
# # 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 15:41:33 +00:00
|
|
|
raise KomradeException('why do I have no pub/privkey pair!?',self,self.name,self.pubkey,self.privkey,self.keychain())
|
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 18:31:36 +00:00
|
|
|
# self.log(f'I am {self} packaging a message to {another}: {msg_d}')
|
2020-09-09 14:38:37 +00:00
|
|
|
|
|
|
|
from komrade.backend.messages import Message
|
|
|
|
msg_obj = Message(msg_d,caller=self,callee=another)
|
2020-09-09 18:31:36 +00:00
|
|
|
# self.log('created msg obj:',msg_obj)
|
|
|
|
|
2020-09-09 19:15:35 +00:00
|
|
|
# encrypt!
|
2020-09-09 19:41:32 +00:00
|
|
|
msg_obj.encrypt()
|
2020-09-09 14:38:37 +00:00
|
|
|
|
|
|
|
return msg_obj
|
|
|
|
|
2020-09-09 19:15:35 +00:00
|
|
|
def seal_msg(self,msg_d):
|
2020-09-09 14:38:37 +00:00
|
|
|
# make sure encrypted
|
2020-09-09 19:15:35 +00:00
|
|
|
self.log('sealing msg!:',dict_format(msg_d))
|
|
|
|
# msg_obj.encrypt(recursive=True)
|
2020-09-09 14:38:37 +00:00
|
|
|
# return pure binary version of self's entire msg_d
|
2020-09-09 19:22:13 +00:00
|
|
|
msg_b = pickle.dumps(msg_d)
|
2020-09-09 19:15:35 +00:00
|
|
|
self.log('pickled!',msg_b)
|
|
|
|
|
2020-09-09 18:31:36 +00:00
|
|
|
# encrypt by omega key
|
2020-09-09 14:38:37 +00:00
|
|
|
msg_b_encr = self.omega_key.encrypt(msg_b)
|
2020-09-09 19:15:35 +00:00
|
|
|
self.log('final seal:',msg_b_encr)
|
|
|
|
|
2020-09-09 14:38:37 +00:00
|
|
|
return msg_b_encr
|
|
|
|
|
2020-09-09 21:30:14 +00:00
|
|
|
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
|
2020-09-09 14:38:37 +00:00
|
|
|
# decrypt by omega
|
|
|
|
msg_b = self.omega_key.decrypt(msg_b_encr)
|
|
|
|
# unpackage from transmission
|
2020-09-09 19:22:13 +00:00
|
|
|
msg_d = pickle.loads(msg_b)
|
2020-09-09 14:38:37 +00:00
|
|
|
# get message obj
|
2020-09-09 15:49:30 +00:00
|
|
|
print('unsealed msg:',msg_d)
|
2020-09-09 14:50:10 +00:00
|
|
|
from komrade.backend.messages import Message
|
2020-09-09 21:30:14 +00:00
|
|
|
msg_obj = Message(msg_d,caller=from_whom,callee=to_whom)
|
2020-09-09 14:38:37 +00:00
|
|
|
# decrypt msg
|
2020-09-09 20:41:50 +00:00
|
|
|
msg_obj.decrypt()
|
2020-09-09 14:38:37 +00:00
|
|
|
return msg_obj
|
2020-09-07 20:00:21 +00:00
|
|
|
|
2020-09-09 18:31:36 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
clsname=(type(self)).__name__
|
|
|
|
keystr='+'.join(self.top_keys)
|
|
|
|
return f'[{clsname}] {self.name} ({keystr})'
|
|
|
|
|
2020-09-09 21:30:14 +00:00
|
|
|
def locate_an_operator(self,name):
|
|
|
|
if name == OPERATOR_NAME:
|
|
|
|
return TheOperator()
|
|
|
|
if name == TELEPHONE_NAME:
|
|
|
|
return TheTelephone()
|
|
|
|
return Caller(name)
|
|
|
|
|
|
|
|
|
2020-09-09 14:38:37 +00:00
|
|
|
def ring_ring(self,msg,to_whom,get_resp_from=None):
|
2020-09-09 18:31:36 +00:00
|
|
|
# ring ring
|
2020-09-09 19:22:13 +00:00
|
|
|
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:
|
2020-09-09 19:48:23 +00:00
|
|
|
{dict_format(msg,tab=5)}
|
2020-09-09 19:22:13 +00:00
|
|
|
''')
|
2020-09-09 18:31:36 +00:00
|
|
|
|
2020-09-09 14:38:37 +00:00
|
|
|
# get encr msg obj
|
|
|
|
msg_obj = self.compose_msg_to(msg, to_whom)
|
2020-09-09 19:22:13 +00:00
|
|
|
self.log(f'ring ring! here is the message object I made, to send to {to_whom}: {msg_obj}')
|
2020-09-09 18:31:36 +00:00
|
|
|
|
2020-09-09 19:15:35 +00:00
|
|
|
# msg_encr = msg_obj.msg
|
2020-09-09 18:31:36 +00:00
|
|
|
# encrypting
|
2020-09-09 18:43:53 +00:00
|
|
|
# msg_obj.encrypt()
|
2020-09-09 18:31:36 +00:00
|
|
|
# self.log(f'now I look like: {msg_obj}')
|
2020-09-09 14:38:37 +00:00
|
|
|
# get pure encrypted binary, sealed
|
2020-09-09 18:31:36 +00:00
|
|
|
#msg_sealed = self.seal_msg(msg_obj)
|
2020-09-09 14:38:37 +00:00
|
|
|
|
|
|
|
# pass onto next person...
|
|
|
|
if not get_resp_from: get_resp_from=to_whom.ring_ring
|
2020-09-09 19:15:35 +00:00
|
|
|
resp_msg_obj = get_resp_from(msg_obj.msg_d)
|
2020-09-09 18:31:36 +00:00
|
|
|
self.log('resp_msg_obj <-',resp_msg_obj)
|
|
|
|
|
|
|
|
# decrypt?
|
2020-09-09 18:43:53 +00:00
|
|
|
# resp_msg_obj.decrypt()
|
2020-09-09 14:38:37 +00:00
|
|
|
|
|
|
|
# unseal msg
|
2020-09-09 18:31:36 +00:00
|
|
|
# resp_msg_obj = self.unseal_msg(resp_msg_b)
|
2020-09-09 14:38:37 +00:00
|
|
|
|
|
|
|
return resp_msg_obj
|
2020-09-09 21:30:14 +00:00
|
|
|
|
|
|
|
def route(self,data,route):
|
|
|
|
if hasattr(self,route) and route in self.ROUTES:
|
|
|
|
func = getattr(self,route)
|
|
|
|
return func(**data)
|
|
|
|
|
|
|
|
|
2020-09-09 21:38:32 +00:00
|
|
|
def route_msg(self, msg_obj):
|
2020-09-09 21:30:14 +00:00
|
|
|
self.log(f'''
|
|
|
|
>> {msg_obj}
|
|
|
|
''')
|
|
|
|
|
|
|
|
# decrypt
|
|
|
|
if msg_obj.is_encrypted:
|
|
|
|
msg_obj.decrypt()
|
|
|
|
# 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})')
|
|
|
|
response = self.route(msg_obj.data, route=msg_obj.route)
|
|
|
|
self.log('route response:',response)
|
|
|
|
# can we pass the buck on?
|
|
|
|
elif msg_obj.has_embedded_msg:
|
2020-09-09 21:35:01 +00:00
|
|
|
embedded_msg = msg_obj.msg
|
|
|
|
embedded_recipient = embedded_msg.callee
|
2020-09-09 21:30:14 +00:00
|
|
|
# whew, then we can make someone else take the phone
|
2020-09-09 21:35:01 +00:00
|
|
|
self.log(f'passing msg onto {embedded_recipient} ...')
|
|
|
|
|
2020-09-09 21:38:32 +00:00
|
|
|
response = embedded_recipient.route_msg(embedded_msg)
|
2020-09-09 21:35:01 +00:00
|
|
|
self.log(f'passed msg onto {embedded_recipient}, got this response: {response} ...')
|
2020-09-09 21:30:14 +00:00
|
|
|
# otherwise what are we doing?
|
|
|
|
else:
|
|
|
|
raise KomradeException('No route, no embedded msg. What to do?')
|
|
|
|
|
|
|
|
# set this to be the new msg
|
2020-09-09 21:35:01 +00:00
|
|
|
msg_obj.msg = msg_obj.msg_d['_msg'] = response
|
2020-09-09 21:30:14 +00:00
|
|
|
|
|
|
|
# re-encrypt
|
|
|
|
msg_obj.encrypt()
|
2020-09-09 21:35:01 +00:00
|
|
|
self.log(f're-encrypted: {msg_obj}')
|
2020-09-09 21:30:14 +00:00
|
|
|
|
2020-09-09 21:35:01 +00:00
|
|
|
# passing msg back the chain
|
2020-09-09 21:30:14 +00:00
|
|
|
return msg_obj
|
2020-09-09 14:38:37 +00:00
|
|
|
|