2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-05 21:20:51 +00:00
Comrad/komrade/backend/the_operator.py

185 lines
5.0 KiB
Python
Raw Normal View History

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
# print(PATH_OPERATOR_WEB_KEYS_URL)
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.
"""
2020-09-07 17:27:46 +00:00
super().__init__(
name,
passphrase,
path_crypt_keys=PATH_CRYPT_OP_KEYS,
2020-09-09 14:38:37 +00:00
path_crypt_data=PATH_CRYPT_OP_DATA
)
self._keychain = self.operator_keychain
2020-09-07 17:50:58 +00:00
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-08 15:44:17 +00:00
# ends the ring_ring() chain
2020-09-09 15:15:29 +00:00
def answer_phone(self,data_b):
2020-09-08 15:44:17 +00:00
# route incoming call from the switchboard
2020-09-09 15:15:29 +00:00
self.log('Hello, this is the Operator. You said: ',data_b)
2020-09-09 14:47:26 +00:00
# unseal
2020-09-09 15:15:29 +00:00
msg_obj = self.unseal_msg(data_b)
2020-09-09 14:58:43 +00:00
self.log(f'Operator understood message: {msg_obj}')
2020-09-09 19:31:57 +00:00
# decrypt all of it!
msg_obj.decrypt()
self.log('I am now decrypted!',msg_obj)
2020-09-09 20:07:50 +00:00
# self.log('my messages:',msg_obj.messages)
2020-09-09 19:31:57 +00:00
#self.log(f'Operator understood message route: {msg_obj.route}')
2020-09-09 20:07:50 +00:00
self.log('meta msg!',dict_format(msg_obj.meta_msg,tab=6))
2020-09-09 19:53:58 +00:00
2020-09-09 15:14:51 +00:00
# carry out message instructions
route_result = self.route(msg_obj)
2020-09-09 15:24:20 +00:00
self.log('route_result <-',route_result)
2020-09-09 15:14:51 +00:00
# turn msg back around
msg_obj = self.compose_msg_to(route_result,self.phone)
2020-09-09 15:24:20 +00:00
self.log('returning msg:',msg_obj)
2020-09-09 15:14:51 +00:00
# send back down encrypted
msg_sealed = self.seal_msg(msg_obj)
# return back to phone and back down to chain
return msg_sealed
2020-09-08 16:09:51 +00:00
2020-09-09 14:38:37 +00:00
def find_pubkey(self):
return self.operator_keychain['pubkey']
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-06 20:05:03 +00:00
2020-09-09 10:58:00 +00:00
def route(self, msg_obj):
# get route from msg
route = msg_obj.route
# no route?
if not msg_obj.route: raise KomradeException('no route!')
# what we working with?
2020-09-09 15:14:51 +00:00
self.log(f'route() got incoming msg = {msg_obj} and route = {route}')
2020-09-09 10:58:00 +00:00
2020-09-09 15:30:34 +00:00
# pass on data
2020-09-09 19:51:55 +00:00
# data = msg_obj.msg
# if type(data)==dict and ROUTE_KEYNAME in data:
# del data[ROUTE_KEYNAME]
2020-09-09 15:30:34 +00:00
2020-09-09 17:34:19 +00:00
## hard code the acceptable routes
if route == 'forge_new_keys':
2020-09-09 19:51:55 +00:00
return self.forge_new_keys(msg_obj)
2020-09-09 17:46:36 +00:00
elif route == 'does_username_exist':
2020-09-09 19:51:55 +00:00
return self.does_username_exist(msg_obj)
2020-09-09 10:58:00 +00:00
# otherwise, hang up and try again
2020-09-07 21:29:02 +00:00
return OPERATOR_INTERCEPT_MESSAGE
2020-09-06 06:50:23 +00:00
2020-09-09 17:34:19 +00:00
### ROUTES
2020-09-09 19:53:58 +00:00
def forge_new_keys(self,msg_obj):
self.log('about to make some new keys!',msg_obj.msg_d)
2020-09-09 18:31:36 +00:00
# return {'_route':'well_hello_to_you_too'}
2020-09-08 15:44:17 +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-09 15:14:51 +00:00
return forged_keys_plus_id
2020-09-09 17:42:06 +00:00
def does_username_exist(self,data):
2020-09-09 17:34:19 +00:00
assert type(data)==dict and 'name' in data and data['name']
# find pubkey?
2020-09-09 17:45:29 +00:00
name=data.get('name')
2020-09-09 17:48:56 +00:00
pubkey=self.crypt_keys.get(name,prefix='/pubkey/')
2020-09-09 17:45:29 +00:00
self.log(f'looking for {name}, found {pubkey} as pubkey')
return bool(pubkey)
2020-09-09 17:34:19 +00:00
2020-09-07 18:55:34 +00:00
2020-09-06 06:50: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()