nothing-unencr
quadrismegistus 4 years ago
parent 3dc2ca1e34
commit 79fd7617cf

@ -31,13 +31,13 @@ class Message(Logger):
self._caller=caller
self._callee=callee
self.messenger=None
self.is_encrypted=False
self._is_encrypted=None
# get operators straight away?
if not self._caller or not self._callee:
self.get_callers()
@property
def meta_msg(self):
def data(self):
md={}
msg_d=self.msg_d
while msg_d:
@ -115,7 +115,7 @@ class Message(Logger):
return False
return True
def decrypt(self,recursive=True):
def decrypt(self,recursive=False):
# get callers
self.log(f'attempting to decrypt msg',self.msg) # {self.msg} from {caller} to {callee}')
@ -137,20 +137,29 @@ class Message(Logger):
self.log('got decr msg back:',decr_msg)
# now, is the decrypted message itself a message?
if recursive and is_valid_msg_d(decr_msg):
if is_valid_msg_d(decr_msg):
self.log('this is a valid msg in its own right!',decr_msg)
# then ... make that, a message object and decrypt it too!
self.msg = Message(decr_msg)
# but do not decrypt it yet!
# self.msg.decrypt()
# for now this should be rolled out individually ,like an onion
# ring_ring on client -> pronto_pronto on server
# so we don't need or want to decrypt all at once
if recursive:
self.msg.decrypt()
self.log(f'done decrypting! {self}')
return decr_msg
@property
def is_encrypted(self):
if self.msg._is_encrypted is not None:
return self.msg._is_encrypted
return type(self.msg) == bytes
def encrypt(self): # each child message should already be encrypted before coming to its parent message ,recursive=False):
if self.is_encrypted: return
if self._is_encrypted: return
# self.log(f'attempting to encrypt msg {self.msg} from {self.caller} to {self.callee}')
self.log(f'About to encrypt self.msg! I now look like v1: {self}')
@ -168,7 +177,7 @@ class Message(Logger):
self.msg = msg_encr
self.msg_d['_msg'] = msg_encr
self.log(f'Encrypted! I now look like v2: {self}')
self.is_encrypted = True
self._is_encrypted = True

@ -10,6 +10,7 @@ from komrade.backend import *
class Operator(Keymaker):
ROUTES = ['forge_new_keys','does_username_exist','hello_world']
def __init__(self, name, passphrase=DEBUG_DEFAULT_PASSPHRASE, keychain = {}, path_crypt_keys=PATH_CRYPT_CA_KEYS, path_crypt_data=PATH_CRYPT_CA_DATA):
super().__init__(name=name,passphrase=passphrase, keychain=keychain,
@ -97,7 +98,9 @@ class Operator(Keymaker):
return msg_b_encr
def unseal_msg(self,msg_b_encr):
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
# decrypt by omega
msg_b = self.omega_key.decrypt(msg_b_encr)
# unpackage from transmission
@ -105,7 +108,7 @@ class Operator(Keymaker):
# get message obj
print('unsealed msg:',msg_d)
from komrade.backend.messages import Message
msg_obj = Message(msg_d)
msg_obj = Message(msg_d,caller=from_whom,callee=to_whom)
# decrypt msg
msg_obj.decrypt()
return msg_obj
@ -116,6 +119,14 @@ class Operator(Keymaker):
keystr='+'.join(self.top_keys)
return f'[{clsname}] {self.name} ({keystr})'
def locate_an_operator(self,name):
if name == OPERATOR_NAME:
return TheOperator()
if name == TELEPHONE_NAME:
return TheTelephone()
return Caller(name)
def ring_ring(self,msg,to_whom,get_resp_from=None):
# ring ring
self.log(f'''
@ -150,4 +161,45 @@ class Operator(Keymaker):
# resp_msg_obj = self.unseal_msg(resp_msg_b)
return resp_msg_obj
def route(self,data,route):
if hasattr(self,route) and route in self.ROUTES:
func = getattr(self,route)
return func(**data)
def pronto_pronto(self, msg_obj):
self.log(f'''
<< "Pronto?"
>> {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:
# whew, then we can make someone else take the phone
self.log(f'passing msg onto {msg_obj.callee} ...')
response = msg_obj.callee.pronto_pronto(msg_obj)
self.log(f'passed msg onto {msg_obj.callee}, got this response: {response} ...')
# otherwise what are we doing?
else:
raise KomradeException('No route, no embedded msg. What to do?')
# set this to be the new msg
msg_obj.msg = self.msg_d['_msg'] = response
# re-encrypt
msg_obj.encrypt()
self.log(f're-encrypted: {self}')
# passing... my self back?
return msg_obj

@ -54,31 +54,29 @@ class TheOperator(Operator):
return self.send(encr_msg_to_send)
# ends the ring_ring() chain
def answer_phone(self,data_b):
def pronto_pronto(self,data_b):
# route incoming call from the switchboard
self.log('Hello, this is the Operator. You said: ',data_b)
# unseal
msg_obj = self.unseal_msg(data_b)
msg_obj = self.unseal_msg(
data_b,
from_whom=self.phone
)
self.log(f'Operator understood message: {msg_obj} {msg_obj.route}')
pause()
# self.log('my messages:',msg_obj.messages)
#self.log(f'Operator understood message route: {msg_obj.route}')
data = msg_obj.meta_msg
self.log('meta msg = all data?',dict_format(data,tab=6))
# carry out message instructions
route_result = self.route(data,route=msg_obj.route)
resp_msg_obj = super().pronto_pronto(msg_obj) #,route=msg_obj.route)
self.log('route_result <-',route_result)
# turn msg back around
msg_obj = self.compose_msg_to(route_result,self.phone)
self.log('returning msg:',msg_obj)
# should be encrypted already
assert resp_msg_obj.is_encrypted
# it should be pointing back from me to the telephone
assert resp_msg_obj.callee.pubkey==self.phone.pubkey
assert resp_msg_obj.caller.pubkey==self.pubkey
# send back down encrypted
msg_sealed = self.seal_msg(msg_obj.msg_d)
msg_sealed = self.seal_msg(resp_msg_obj.msg_d)
# return back to phone and back down to chain
return msg_sealed
@ -93,20 +91,31 @@ class TheOperator(Operator):
return encr_data_b
def route(self, data, route):
if not route: raise KomradeException('no route!')
# def route(self, msg_obj):
# if not msg_obj.route:
# # nothign explicitly requested yet?
# # can we pass the msg on?
# if msg_obj.has_embedded_msg:
# self.route(msg_obj, route=msg_obj)
# # self.log('my messages:',msg_obj.messages)
# if not route: raise KomradeException('no route!')
# what we working with?
self.log(f'route() got incoming msg data = {data} and route = {route}')
# # what we working with?
# self.log(f'route() got incoming msg data = {data} and route = {route}')
## hard code the acceptable routes
if route == 'forge_new_keys':
return self.forge_new_keys(data)
elif route == 'does_username_exist':
return self.does_username_exist(data)
# ## hard code the acceptable routes
# if route == 'forge_new_keys':
# return self.forge_new_keys(data)
# elif route == 'does_username_exist':
# return self.does_username_exist(data)
# otherwise, hang up and try again
return OPERATOR_INTERCEPT_MESSAGE
# # otherwise, hang up and try again
# return OPERATOR_INTERCEPT_MESSAGE

Loading…
Cancel
Save