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

135 lines
3.6 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-06 06:50:23 +00:00
def recv(self,data):
2020-09-06 20:05:03 +00:00
# decrypt
data_in = self.decrypt_incoming(data)
2020-09-06 20:05:03 +00:00
# route
result = self.route(data_json)
2020-09-06 20:41:45 +00:00
# encrypt
data_out = self.encrypt_outgoing(result)
# send
return self.send(res)
def send(self,res):
if not len(res)==2:
self.log('!! error. argument to send() must be: (json_tophone,json_tosender)')
return
msg_tophone,msg_tocaller = res
caller=None
if msg_tocaller and 'name' in msg_tophone:
caller = Operator(msg_tophone['name'])
self.log('send!',msg_tophone,msg_tocaller,caller)
data = self.encrypt_information(json_phone=msg_tophone,json_caller=caller)
self.log('got back encr:',data)
return data
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':
res = self.forge_new_keys(**data)
else:
res = OPERATOR_INTERCEPT_MESSAGE
return res# 'success!'
2020-09-06 06:50:23 +00:00
def forge_new_keys(self,**data):
# get keys
res = super().forge_new_keys(**data)
pkg={}
pkg['name']=data.get('name')
pkg['_keychain']=res
self.log('returned keys from keymaker.forge_new_keys:','\n'.join(res.keys()))
# return to_phone,to_caller
return (pkg,{})
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()