2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-19 15:25:34 +00:00
Comrad/komrade/backend/the_operator.py

158 lines
4.2 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 21:38:32 +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 21:30:14 +00:00
msg_obj = self.unseal_msg(
data_b,
from_whom=self.phone
)
2020-09-09 20:42:57 +00:00
self.log(f'Operator understood message: {msg_obj} {msg_obj.route}')
2020-09-09 19:31:57 +00:00
2020-09-09 23:01:08 +00:00
# decrypt?
msg_obj.decrypt()
2020-09-09 15:14:51 +00:00
# carry out message instructions
2020-09-09 22:23:51 +00:00
resp_msg_obj = self.pronto_pronto(msg_obj) #,route=msg_obj.route)
2020-09-09 21:38:32 +00:00
self.log('route_result <-',resp_msg_obj)
2020-09-09 15:14:51 +00:00
# send back down encrypted
2020-09-09 21:30:14 +00:00
msg_sealed = self.seal_msg(resp_msg_obj.msg_d)
2020-09-09 15:14:51 +00:00
# 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-09 17:34:19 +00:00
### ROUTES
2020-09-10 09:31:32 +00:00
# @deprecated HAPPENS ON CLIENT NOW:
# def forge_new_keys(self,**data):
# self.log('about to make some new keys!',data)
# # return {'_route':'well_hello_to_you_too'}
2020-09-08 15:44:17 +00:00
2020-09-10 09:31:32 +00:00
# # get keys
# forged_keys_plus_id = super().forge_new_keys(
# name=data.get('name'),
# passphrase=data.get('passphrase')
# )
# self.log('<- forged keys',forged_keys_plus_id)
# # return to Telephone/Caller
# return forged_keys_plus_id
2020-09-10 09:00:11 +00:00
def does_username_exist(self,name,**data):
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-10 09:00:11 +00:00
def register_new_user(self,name,pubkey,**data):
self.log('setting pubkey under name')
res = self.crypt_keys.set(name,pubkey,prefix='/pubkey/')
self.log('got result from crypt:',res)
return res
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()