2020-09-06 06:50:23 +00:00
|
|
|
"""
|
|
|
|
There is only one operator!
|
|
|
|
Running on node prime.
|
|
|
|
"""
|
|
|
|
# internal imports
|
|
|
|
import os,sys; sys.path.append(os.path.abspath(os.path.join(os.path.abspath(os.path.join(os.path.dirname(__file__),'..')),'..')))
|
|
|
|
from komrade import *
|
2020-09-06 20:17:47 +00:00
|
|
|
from komrade.backend import *
|
2020-09-06 06:50:23 +00:00
|
|
|
|
|
|
|
|
2020-09-07 13:33:47 +00:00
|
|
|
# print(PATH_OPERATOR_WEB_KEYS_URL)
|
2020-09-07 13:04:23 +00:00
|
|
|
|
2020-09-06 06:50:23 +00:00
|
|
|
|
|
|
|
class TheOperator(Operator):
|
|
|
|
"""
|
2020-09-06 20:05:03 +00:00
|
|
|
The remote operator
|
2020-09-06 06:50:23 +00:00
|
|
|
"""
|
2020-09-06 20:17:47 +00:00
|
|
|
@property
|
|
|
|
def phone(self):
|
2020-09-06 20:25:18 +00:00
|
|
|
global TELEPHONE
|
2020-09-06 20:30:36 +00:00
|
|
|
from komrade.backend.the_telephone import TheTelephone
|
2020-09-07 17:27:46 +00:00
|
|
|
if not TELEPHONE: TELEPHONE=TheTelephone()
|
2020-09-06 20:17:47 +00:00
|
|
|
return TELEPHONE
|
2020-09-06 06:50:23 +00:00
|
|
|
|
|
|
|
|
2020-09-07 17:27:46 +00:00
|
|
|
def __init__(self, name = OPERATOR_NAME, passphrase='acc'):
|
2020-09-06 06:50:23 +00:00
|
|
|
"""
|
|
|
|
Boot up the operator. Requires knowing or setting a password of memory.
|
|
|
|
"""
|
|
|
|
# init req paths
|
2020-09-06 09:30:15 +00:00
|
|
|
# if not os.path.exists(PATH_OPERATOR): os.makedirs(PATH_OPERATOR)
|
2020-09-07 17:27:46 +00:00
|
|
|
global OPERATOR_KEYCHAIN,TELEPHONE_KEYCHAIN
|
2020-09-08 15:14:48 +00:00
|
|
|
|
|
|
|
from komrade.backend.phonelines import connect_phonelines
|
2020-09-07 17:27:46 +00:00
|
|
|
if not TELEPHONE_KEYCHAIN or not OPERATOR_KEYCHAIN:
|
|
|
|
OPERATOR_KEYCHAIN,TELEPHONE_KEYCHAIN = connect_phonelines()
|
2020-09-07 17:50:58 +00:00
|
|
|
if not passphrase: self.passphrase=passphrase=getpass.getpass('Hello, this is the Operator speaking. What is the passphrase?\n> ')
|
2020-09-07 17:27:46 +00:00
|
|
|
super().__init__(
|
|
|
|
name,
|
|
|
|
passphrase,
|
|
|
|
path_crypt_keys=PATH_CRYPT_OP_KEYS,
|
|
|
|
path_crypt_data=PATH_CRYPT_OP_DATA)
|
2020-09-07 17:50:58 +00:00
|
|
|
self._keychain = OPERATOR_KEYCHAIN
|
|
|
|
|
2020-09-08 12:11:13 +00:00
|
|
|
def ring(self,
|
2020-09-07 21:29:02 +00:00
|
|
|
from_caller=None,
|
|
|
|
to_caller=None,
|
|
|
|
json_phone2phone={},
|
|
|
|
json_caller2phone={}, # (person) -> operator or operator -> (person)
|
|
|
|
json_caller2caller={}):
|
|
|
|
|
2020-09-08 12:11:13 +00:00
|
|
|
encr_msg_to_send = super().ring(
|
2020-09-07 21:29:02 +00:00
|
|
|
from_phone=self,
|
|
|
|
to_phone=self.phone,
|
|
|
|
from_caller=from_caller,
|
|
|
|
to_caller=to_caller,
|
|
|
|
json_phone2phone=json_phone2phone,
|
|
|
|
json_caller2phone=json_caller2phone, # (person) -> operator
|
|
|
|
json_caller2caller=json_caller2caller)
|
|
|
|
|
|
|
|
return self.send(encr_msg_to_send)
|
|
|
|
|
2020-09-09 09:50:29 +00:00
|
|
|
def can_I_decrypt_this(self,msg_d):
|
|
|
|
|
2020-09-08 15:44:17 +00:00
|
|
|
# ends the ring_ring() chain
|
|
|
|
def answer_phone(self,data_b64_str):
|
|
|
|
# route incoming call from the switchboard
|
|
|
|
self.log('Hello, this is the Operator. You said: ',data_b64_str)
|
|
|
|
|
|
|
|
# decode
|
|
|
|
data_b64 = data_b64_str.encode()
|
|
|
|
data = b64decode(data_b64)
|
2020-09-09 10:41:48 +00:00
|
|
|
msg_d = msg_encr_caller2caller_caller2phone_phone2phone = data
|
2020-09-08 15:44:17 +00:00
|
|
|
self.log('msg_encr_caller2caller_caller2phone_phone2phone incoming',msg_encr_caller2caller_caller2phone_phone2phone)
|
|
|
|
|
2020-09-09 10:41:48 +00:00
|
|
|
# make top-level message object, addressed to me the operator
|
|
|
|
msg = Message(msg_d,caller=self.phone,callee=self)
|
|
|
|
msg.decrypt()
|
2020-09-08 15:44:17 +00:00
|
|
|
|
2020-09-09 10:41:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
# TOTAL_MSG_CHAIN = {}
|
|
|
|
# TOTAL_DECR_MSG
|
|
|
|
|
|
|
|
# # top layer: phone -> me, the op
|
|
|
|
# msg_d = msg_encr_caller2caller_caller2phone = self.unpackage_msg_from(
|
|
|
|
# msg_encr_caller2caller_caller2phone_phone2phone,
|
|
|
|
# self.phone
|
|
|
|
# )
|
|
|
|
# self.log('Operator unrolled the first layer of encryption:',msg_encr_caller2caller_caller2phone)
|
|
|
|
# assert type(msg_encr_caller2caller_caller2phone)==dict
|
2020-09-08 16:09:51 +00:00
|
|
|
|
2020-09-09 10:41:48 +00:00
|
|
|
# # is there another layer, encrypted caller2phone ?
|
|
|
|
# msg_d['_msg'] = self.unpackage_msg_dict(msg_d)
|
|
|
|
|
|
|
|
# # merge unencrypted messages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# route=None
|
|
|
|
# if _msg and type(_msg)==bytes:
|
|
|
|
# alleged_name = msg_d.get('_from_name')
|
|
|
|
# alleged_pubkey = msg_d.get('_from_pub')
|
|
|
|
# if alleged_pubkey and alleged_name:
|
|
|
|
# alleged_caller = Caller(alleged_name)
|
|
|
|
# assert alleged_caller.pubkey == alleged_pubkey
|
|
|
|
|
|
|
|
# msg_d2 = self.unpackage_msg_from(
|
|
|
|
# _msg,
|
|
|
|
# caller
|
|
|
|
# )
|
|
|
|
# assert type(msg_d2)==dict
|
|
|
|
# _msg2 = msg_d2.get('_msg')
|
|
|
|
# route = msg_d2.get('_msg',{}).get('_please')
|
|
|
|
# dict_merge(_msg,_msg2)
|
|
|
|
# msg_d['_msg'] = msg_d2
|
2020-09-08 16:09:51 +00:00
|
|
|
|
2020-09-09 10:41:48 +00:00
|
|
|
# if not route:
|
|
|
|
# route = msg_d.get('_msg',{}).get('_please',None)
|
2020-09-08 16:09:51 +00:00
|
|
|
|
2020-09-09 10:41:48 +00:00
|
|
|
# return self.route(msg_d,_msg,route)
|
2020-09-08 16:09:51 +00:00
|
|
|
|
2020-09-08 15:44:17 +00:00
|
|
|
|
|
|
|
|
2020-09-06 06:50:23 +00:00
|
|
|
|
2020-09-07 18:55:34 +00:00
|
|
|
def send(self,encr_data_b):
|
2020-09-07 22:56:43 +00:00
|
|
|
self.log(type(encr_data_b),encr_data_b,'sending!')
|
2020-09-07 22:53:56 +00:00
|
|
|
return encr_data_b
|
2020-09-07 08:44:40 +00:00
|
|
|
|
2020-09-06 20:05:03 +00:00
|
|
|
|
2020-09-08 16:18:33 +00:00
|
|
|
def route(self, msg_d, _msg, route=None):
|
|
|
|
if not route: route=_msg.get('_please')
|
|
|
|
if not route: raise KomradeException('no route!')
|
|
|
|
if '_please' in _msg: del _msg['_please']
|
2020-09-08 16:16:55 +00:00
|
|
|
self.log(f'route() got incoming msg_d = {msg_d}, _msg = {_msg}, and route = {route}')
|
2020-09-07 08:44:40 +00:00
|
|
|
if route == 'forge_new_keys':
|
2020-09-08 16:16:55 +00:00
|
|
|
return self.forge_new_keys(**_msg)
|
2020-09-07 21:29:02 +00:00
|
|
|
return OPERATOR_INTERCEPT_MESSAGE
|
2020-09-06 06:50:23 +00:00
|
|
|
|
2020-09-07 08:44:40 +00:00
|
|
|
def forge_new_keys(self,**data):
|
2020-09-08 15:44:17 +00:00
|
|
|
self.log('about to make some new keys!',data)
|
|
|
|
|
2020-09-07 08:44:40 +00:00
|
|
|
# get keys
|
2020-09-08 06:58:54 +00:00
|
|
|
forged_keys_plus_id = super().forge_new_keys(**data)
|
2020-09-07 18:55:34 +00:00
|
|
|
|
2020-09-08 06:58:54 +00:00
|
|
|
# return to Telephone/Caller
|
2020-09-08 12:11:13 +00:00
|
|
|
return self.ring(json_phone2phone=forged_keys_plus_id)
|
2020-09-07 21:29:02 +00:00
|
|
|
|
2020-09-07 08:44:40 +00:00
|
|
|
|
2020-09-07 18:55:34 +00:00
|
|
|
|
2020-09-07 08:44:40 +00:00
|
|
|
|
2020-09-06 06:50:23 +00:00
|
|
|
|
2020-09-07 13:04:23 +00:00
|
|
|
|
2020-09-06 06:50:23 +00:00
|
|
|
|
|
|
|
def test_op():
|
2020-09-06 19:39:44 +00:00
|
|
|
from komrade.backend.the_telephone import TheTelephone
|
|
|
|
|
|
|
|
|
|
|
|
op = TheOperator()
|
|
|
|
# op.boot()
|
|
|
|
|
2020-09-06 19:19:44 +00:00
|
|
|
keychain_op = op.keychain(force=True)
|
2020-09-06 19:39:44 +00:00
|
|
|
|
2020-09-06 19:19:44 +00:00
|
|
|
|
2020-09-06 19:39:44 +00:00
|
|
|
phone = TheTelephone()
|
|
|
|
# phone.boot()
|
2020-09-06 19:19:44 +00:00
|
|
|
keychain_ph = phone.keychain(force=True)
|
|
|
|
|
|
|
|
|
2020-09-06 19:18:43 +00:00
|
|
|
from pprint import pprint
|
2020-09-06 19:19:44 +00:00
|
|
|
print('REASSEMBLED OPERATOR KEYCHAIN')
|
|
|
|
pprint(keychain_op)
|
|
|
|
# stop
|
|
|
|
|
2020-09-06 19:18:43 +00:00
|
|
|
print('REASSEMBLED TELEPHONE KEYCHAIN')
|
2020-09-06 19:19:44 +00:00
|
|
|
pprint(keychain_ph)
|
2020-09-06 19:18:43 +00:00
|
|
|
|
2020-09-06 17:44:08 +00:00
|
|
|
# print(op.pubkey(keychain=keychain))
|
2020-09-06 18:52:47 +00:00
|
|
|
# print(op.crypt_keys.get(op.pubkey(), prefix='/privkey_encr/'))
|
2020-09-06 18:51:25 +00:00
|
|
|
# print(op.crypt_keys.get(op.name, prefix='/pubkey_encr/'))
|
2020-09-06 19:18:43 +00:00
|
|
|
# print(op.pubkey_)
|
2020-09-06 18:52:47 +00:00
|
|
|
|
2020-09-06 17:42:57 +00:00
|
|
|
|
2020-09-06 19:18:43 +00:00
|
|
|
# stop
|
2020-09-06 06:50:23 +00:00
|
|
|
|
2020-09-06 19:18:43 +00:00
|
|
|
# pubkey = op.keychain()['pubkey']
|
|
|
|
# pubkey_b64 = b64encode(pubkey)
|
|
|
|
# print(pubkey)
|
2020-09-06 06:50:23 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__': test_op()
|