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

141 lines
3.8 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.
"""
# 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
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-07 21:29:02 +00:00
def ring_ring(self,
from_caller=None,
to_caller=None,
json_phone2phone={},
json_caller2phone={}, # (person) -> operator or operator -> (person)
json_caller2caller={}):
encr_msg_to_send = super().ring_ring(
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-06 06:50:23 +00:00
def recv(self,data):
2020-09-06 20:05:03 +00:00
# decrypt
2020-09-07 18:44:09 +00:00
self.log('recv 1: got',data)
2020-09-07 20:00:21 +00:00
# answer the phone!
2020-09-07 20:58:09 +00:00
data_in = self.answer_phone(data, from_phone=self.phone, to_phone=self)
2020-09-07 20:00:21 +00:00
self.log('recv 2: answer_phone gave me',data_in)
2020-09-06 20:05:03 +00:00
# route
2020-09-07 21:29:02 +00:00
return self.route(data_in)
# self.log('recv 3: route gave me',encr_result)
2020-09-07 21:29:02 +00:00
# # send
# return self.send(encr_result)
2020-09-07 18:55:34 +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
def route(self, data):
res=None
route = data.get('_route')
if not route: return OPERATOR_INTERCEPT_MESSAGE
del data['_route']
if route == 'forge_new_keys':
2020-09-07 21:29:02 +00:00
return self.forge_new_keys(**data)
return OPERATOR_INTERCEPT_MESSAGE
2020-09-06 06:50:23 +00:00
def forge_new_keys(self,**data):
# 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
return self.ring_ring(json_phone2phone=forged_keys_plus_id)
2020-09-07 21:29:02 +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()