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-09 22:01:41 +00:00
|
|
|
|
2020-09-06 06:50:23 +00:00
|
|
|
|
2020-09-12 20:19:53 +00:00
|
|
|
def locate_an_operator(name=None,pubkey=None):
|
2020-09-09 22:10:58 +00:00
|
|
|
global OPERATOR,TELEPHONE
|
|
|
|
|
|
|
|
from komrade.backend.the_operator import TheOperator
|
|
|
|
from komrade.backend.the_telephone import TheTelephone
|
|
|
|
from komrade.backend.callers import Caller
|
|
|
|
|
2020-09-12 20:19:53 +00:00
|
|
|
if not OPERATOR: OPERATOR = TheOperator()
|
|
|
|
if not TELEPHONE: TELEPHONE = TheTelephone()
|
|
|
|
|
|
|
|
if pubkey:
|
|
|
|
assert type(pubkey)==bytes
|
|
|
|
if not isBase64(pubkey): pubkey=b64encode(pubkey)
|
2020-09-12 20:20:27 +00:00
|
|
|
|
2020-09-09 22:09:08 +00:00
|
|
|
if name == OPERATOR_NAME:
|
2020-09-12 20:19:53 +00:00
|
|
|
return OPERATOR
|
|
|
|
if pubkey and pubkey == OPERATOR.pubkey.data_b64:
|
|
|
|
return OPERATOR
|
2020-09-12 20:20:27 +00:00
|
|
|
|
2020-09-12 20:19:53 +00:00
|
|
|
if name==TELEPHONE_NAME:
|
|
|
|
return TELEPHONE
|
2020-09-12 20:20:27 +00:00
|
|
|
if pubkey and pubkey == TELEPHONE.pubkey.data_b64:
|
2020-09-12 20:19:53 +00:00
|
|
|
return TELEPHONE
|
|
|
|
|
|
|
|
return Caller(name=name,pubkey=pubkey)
|
2020-09-09 22:09:08 +00:00
|
|
|
|
2020-09-04 15:50:08 +00:00
|
|
|
|
2020-09-10 11:11:46 +00:00
|
|
|
from komrade.constants import OPERATOR_ROUTES
|
2020-09-04 15:50:08 +00:00
|
|
|
class Operator(Keymaker):
|
2020-09-10 11:11:02 +00:00
|
|
|
ROUTES = OPERATOR_ROUTES
|
2020-09-04 15:50:08 +00:00
|
|
|
|
2020-09-12 20:19:53 +00:00
|
|
|
def __init__(self, name=None, pubkey=None, passphrase=DEBUG_DEFAULT_PASSPHRASE, keychain = {}, path_crypt_keys=PATH_CRYPT_CA_KEYS, path_crypt_data=PATH_CRYPT_CA_DATA):
|
|
|
|
if pubkey:
|
|
|
|
assert type(pubkey)==bytes
|
|
|
|
if isBase64(pubkey): pubkey = b64decode(pubkey)
|
|
|
|
if keychain.get('pubkey'):
|
2020-09-12 20:20:39 +00:00
|
|
|
assert keychain.get('pubkey').data == pubkey
|
2020-09-12 20:19:53 +00:00
|
|
|
else:
|
|
|
|
keychain['pubkey']=KomradeAsymmetricPublicKey(pubkey)
|
|
|
|
|
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-12 16:33:54 +00:00
|
|
|
|
2020-09-12 20:19:53 +00:00
|
|
|
|
2020-09-12 20:20:57 +00:00
|
|
|
# def boot(self):
|
|
|
|
# ## get both name and pubkey somehow
|
|
|
|
# if not self.pubkey and self.name:
|
|
|
|
# self._keychain['pubkey'] = self.find_pubkey()
|
|
|
|
# elif self.pubkey and not self.name:
|
2020-09-12 20:20:27 +00:00
|
|
|
|
2020-09-09 14:38:37 +00:00
|
|
|
|
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
|
|
|
|
2020-09-12 18:18:37 +00:00
|
|
|
def compose_msg_to(self,msg,another,incl_from_name=False,incl_to_name=False):
|
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-12 20:51:27 +00:00
|
|
|
frompub = self.pubkey.data if hasattr(self.pubkey,'data') else self.pubkey
|
|
|
|
topub = another.pubkey.data if hasattr(another.pubkey,'data') else another.pubkey
|
|
|
|
|
2020-09-09 11:01:27 +00:00
|
|
|
msg_d = {
|
2020-09-12 20:51:27 +00:00
|
|
|
'from':frompub,
|
2020-09-12 18:18:37 +00:00
|
|
|
# 'from_name':self.name,
|
2020-09-12 20:51:27 +00:00
|
|
|
'to':topub,
|
2020-09-12 18:18:37 +00:00
|
|
|
# 'to_name':another.name,
|
|
|
|
'msg':msg
|
2020-09-08 11:23:41 +00:00
|
|
|
}
|
2020-09-12 18:18:37 +00:00
|
|
|
if incl_from_name: msg_d['from_name']=self.name
|
|
|
|
if incl_to_name: msg_d['to_name']=self.name
|
2020-09-09 18:31:36 +00:00
|
|
|
# self.log(f'I am {self} packaging a message to {another}: {msg_d}')
|
2020-09-09 22:10:58 +00:00
|
|
|
from komrade.backend.messages import Message
|
2020-09-09 14:38:37 +00:00
|
|
|
|
2020-09-09 22:01:41 +00:00
|
|
|
msg_obj = Message(msg_d,from_whom=self,to_whom=another)
|
2020-09-09 18:31:36 +00:00
|
|
|
|
2020-09-09 19:15:35 +00:00
|
|
|
# encrypt!
|
2020-09-09 22:26:48 +00:00
|
|
|
# msg_obj.encrypt()
|
2020-09-09 14:38:37 +00:00
|
|
|
|
|
|
|
return msg_obj
|
|
|
|
|
2020-09-09 22:23:51 +00:00
|
|
|
# def compose_reply(self,msg,another):
|
|
|
|
|
|
|
|
|
2020-09-12 19:00:33 +00:00
|
|
|
# def seal_msg(self,msg_d):
|
|
|
|
# msg_b = pickle.dumps(msg_d)
|
|
|
|
# self.log('Message has being sealed in a final binary package:',b64encode(msg_b))
|
|
|
|
# return msg_b
|
2020-09-09 14:38:37 +00:00
|
|
|
|
2020-09-12 19:07:27 +00:00
|
|
|
def unseal_msg(self,msg_b,from_whom=None,to_whom=None):
|
2020-09-09 21:30:14 +00:00
|
|
|
# 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
|
2020-09-12 18:18:37 +00:00
|
|
|
# msg_b = self.omega_key.decrypt(msg_b_encr)
|
2020-09-12 19:07:27 +00:00
|
|
|
# msg_b = msg_b_encr
|
2020-09-09 14:38:37 +00:00
|
|
|
# unpackage from transmission
|
2020-09-12 19:07:27 +00:00
|
|
|
# msg_d = pickle.loads(msg_b)
|
2020-09-09 14:38:37 +00:00
|
|
|
# get message obj
|
2020-09-10 09:31:32 +00:00
|
|
|
# print('unsealed msg:',msg_d)
|
2020-09-12 19:07:27 +00:00
|
|
|
msg_d = {
|
|
|
|
'from':from_whom.pubkey,
|
|
|
|
'to':to_whom,
|
|
|
|
'msg':msg_b
|
|
|
|
}
|
|
|
|
|
2020-09-09 14:50:10 +00:00
|
|
|
from komrade.backend.messages import Message
|
2020-09-09 22:01:41 +00:00
|
|
|
msg_obj = Message(msg_d,from_whom=from_whom,to_whom=to_whom)
|
2020-09-09 14:38:37 +00:00
|
|
|
# decrypt msg
|
|
|
|
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__
|
2020-09-12 18:18:37 +00:00
|
|
|
#name = clsname+' '+
|
2020-09-12 18:43:50 +00:00
|
|
|
name = '@'+self.name # if self.name!=clsname else clsname
|
2020-09-12 18:18:37 +00:00
|
|
|
# try:
|
|
|
|
# keystr= 'on device: ' + ('+'.join(self.top_keys) if self.pubkey else '')
|
|
|
|
# except TypeError:
|
|
|
|
# keystr=''
|
|
|
|
# # if self.pubkey:
|
|
|
|
keystr=''
|
2020-09-10 08:23:44 +00:00
|
|
|
if False:
|
2020-09-10 08:22:21 +00:00
|
|
|
pubk=self.pubkey_b64.decode()
|
2020-09-10 08:23:05 +00:00
|
|
|
pubk=pubk[-5:]
|
2020-09-10 08:22:21 +00:00
|
|
|
pubk = f' ({pubk})'# if pubk else ''
|
|
|
|
else:
|
|
|
|
pubk = ''
|
2020-09-12 18:18:37 +00:00
|
|
|
return f'{name}' #' ({keystr})'
|
2020-09-09 18:31:36 +00:00
|
|
|
|
2020-09-12 20:19:53 +00:00
|
|
|
|
2020-09-09 21:30:14 +00:00
|
|
|
|
|
|
|
|
2020-09-10 12:04:12 +00:00
|
|
|
def route_msg(self,msg_obj,reencrypt=True):
|
2020-09-09 21:30:14 +00:00
|
|
|
# decrypt
|
2020-09-10 07:44:03 +00:00
|
|
|
self.log('got msg_obj!',msg_obj)
|
2020-09-10 07:53:40 +00:00
|
|
|
if msg_obj.is_encrypted:
|
|
|
|
msg_obj.decrypt()
|
2020-09-10 11:57:53 +00:00
|
|
|
|
|
|
|
# try route
|
2020-09-10 11:58:17 +00:00
|
|
|
if msg_obj.route:
|
2020-09-10 11:57:53 +00:00
|
|
|
data,route = msg_obj.data, msg_obj.route
|
2020-09-10 11:59:06 +00:00
|
|
|
if not hasattr(self,route) or route not in self.ROUTES:
|
2020-09-10 11:57:53 +00:00
|
|
|
raise KomradeException(f'Not a valid route!: {route}')
|
|
|
|
|
|
|
|
# route it!
|
|
|
|
func = getattr(self,route)
|
|
|
|
new_data = func(**data)
|
2020-09-12 18:18:37 +00:00
|
|
|
msg_obj.msg = msg_obj.msg_d['msg'] = new_data
|
2020-09-10 11:57:53 +00:00
|
|
|
|
|
|
|
# try passing it on?
|
|
|
|
if msg_obj.has_embedded_msg:
|
|
|
|
new_data = self.route_msg(msg_obj.msg)
|
2020-09-12 18:18:37 +00:00
|
|
|
msg_obj.msg = msg_obj.msg_d['msg'] = new_data
|
2020-09-10 11:57:53 +00:00
|
|
|
|
|
|
|
# time to turn around and encrypt
|
|
|
|
msg_obj.mark_return_to_sender()
|
|
|
|
self.log('returning to sender as:',msg_obj)
|
|
|
|
|
|
|
|
# encrypt
|
2020-09-10 12:04:12 +00:00
|
|
|
if reencrypt:
|
|
|
|
msg_obj.encrypt()
|
2020-09-10 11:57:53 +00:00
|
|
|
|
2020-09-10 07:53:40 +00:00
|
|
|
return msg_obj
|
2020-09-10 11:57:53 +00:00
|
|
|
|
|
|
|
|
2020-09-12 18:18:37 +00:00
|
|
|
def ring_ring(self,msg,to_whom,get_resp_from=None,route=None,caller=None):
|
2020-09-09 23:23:27 +00:00
|
|
|
# ring ring
|
2020-09-12 13:11:39 +00:00
|
|
|
from komrade.cli.artcode import ART_PHONE_SM1
|
2020-09-12 14:32:03 +00:00
|
|
|
import textwrap as tw
|
2020-09-12 18:18:37 +00:00
|
|
|
nxt=get_class_that_defined_method(get_resp_from).__name__
|
|
|
|
nxtfunc=get_resp_from.__name__
|
|
|
|
# if from_whom != self:
|
|
|
|
# self.status(f'''ring ring!
|
|
|
|
# @{self}: *picks up phone*
|
|
|
|
# @{from_whom}: I have a message I need you to send for me.
|
|
|
|
# @{self}: To whom?
|
|
|
|
# @{from_whom}: To @{to_whom}. But not directly.
|
|
|
|
# @{self}: Who should it I pass it through?
|
|
|
|
# @{from_whom}: Pass it to {nxt}. Tell them to use "{nxtfunc}".
|
|
|
|
# @{self}: Got it... So what's the message?
|
|
|
|
# @{from_whom}: The message is:
|
|
|
|
# {dict_format(msg,tab=4)}
|
|
|
|
# ''')
|
|
|
|
if caller!=self:
|
2020-09-12 18:43:50 +00:00
|
|
|
from komrade.cli.artcode import ART_PHONE_SM1
|
|
|
|
self.log(f'ring ring! I the {self} have received a message from {caller},\n which I will now encrypt and send along to {to_whom}.\n {ART_PHONE_SM1} ')
|
2020-09-12 18:18:37 +00:00
|
|
|
else:
|
2020-09-12 18:43:50 +00:00
|
|
|
pass
|
|
|
|
# self.log(f'I ({self}) will now compose and send an encrypted message to {to_whom}.')
|
2020-09-12 14:32:03 +00:00
|
|
|
|
2020-09-12 18:18:37 +00:00
|
|
|
if route and type(msg)==dict and not ROUTE_KEYNAME in msg:
|
|
|
|
msg[ROUTE_KEYNAME] = route
|
2020-09-12 14:32:03 +00:00
|
|
|
|
2020-09-09 23:23:27 +00:00
|
|
|
# get encr msg obj
|
2020-09-12 18:18:37 +00:00
|
|
|
|
2020-09-09 23:23:27 +00:00
|
|
|
msg_obj = self.compose_msg_to(
|
|
|
|
msg,
|
2020-09-12 18:18:37 +00:00
|
|
|
to_whom
|
2020-09-09 23:23:27 +00:00
|
|
|
)
|
2020-09-12 19:00:33 +00:00
|
|
|
self.log(f'Here is the message I will now encrypt and to send to {to_whom}:\n\n {dict_format(msg_obj.msg,tab = 2)}')
|
2020-09-09 23:23:27 +00:00
|
|
|
|
|
|
|
# encrypting
|
|
|
|
msg_obj.encrypt()
|
2020-09-10 11:57:53 +00:00
|
|
|
|
|
|
|
# pass through the telephone wire by the get_resp_from function
|
2020-09-09 23:23:27 +00:00
|
|
|
if not get_resp_from: get_resp_from=to_whom.ring_ring
|
2020-09-12 18:18:37 +00:00
|
|
|
resp_msg_obj = get_resp_from(msg_obj.msg_d,caller=caller)
|
2020-09-09 23:23:27 +00:00
|
|
|
self.log('resp_msg_obj <-',resp_msg_obj)
|
|
|
|
|
2020-09-10 11:57:53 +00:00
|
|
|
# decrypt
|
2020-09-10 08:08:50 +00:00
|
|
|
if resp_msg_obj.is_encrypted:
|
|
|
|
resp_msg_obj.decrypt()
|
|
|
|
|
2020-09-09 23:23:27 +00:00
|
|
|
# route back?
|
2020-09-10 12:05:03 +00:00
|
|
|
return self.route_msg(resp_msg_obj,reencrypt=False).msg
|
2020-09-09 23:23:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-09-09 23:18:22 +00:00
|
|
|
def pronto_pronto(self, msg_obj):
|
|
|
|
self.log(f'''
|
|
|
|
pronto pronto!
|
|
|
|
>> {msg_obj}
|
|
|
|
''')
|
|
|
|
|
2020-09-10 12:04:12 +00:00
|
|
|
return self.route_msg(msg_obj,reencrypt=True)
|
2020-09-10 11:57:53 +00:00
|
|
|
|
|
|
|
# route_response = self.route_msg(msg_obj)
|
|
|
|
# self.log('route_response',route_response)
|
|
|
|
# # set this to be the new msg
|
2020-09-12 18:18:37 +00:00
|
|
|
# #msg_obj.msg = msg_obj.msg_d['msg'] = response
|
2020-09-10 11:57:53 +00:00
|
|
|
# #self.log('what msg_obj looks like now:',msg_obj)
|
|
|
|
|
|
|
|
# # send new content back
|
|
|
|
# # from komrade.backend.messages import Message
|
|
|
|
# # if type(route_response)==Message:
|
|
|
|
# # resp_msg_obj = route_response
|
|
|
|
# # else:
|
|
|
|
# resp_msg_obj = msg_obj.to_whom.compose_msg_to(
|
|
|
|
# route_response,
|
|
|
|
# msg_obj.from_whom
|
|
|
|
# )
|
|
|
|
# self.log('resp_msg_obj',resp_msg_obj)
|
2020-09-09 22:23:51 +00:00
|
|
|
|
2020-09-10 11:57:53 +00:00
|
|
|
# # re-encrypt
|
|
|
|
# if not resp_msg_obj.is_encrypted:
|
|
|
|
# resp_msg_obj.encrypt()
|
|
|
|
# self.log(f're-encrypted: {resp_msg_obj}')
|
2020-09-09 21:30:14 +00:00
|
|
|
|
2020-09-10 11:57:53 +00:00
|
|
|
# # pass msg back the chain
|
|
|
|
# return resp_msg_obj
|
2020-09-09 14:38:37 +00:00
|
|
|
|