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
|
|
|
|
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
|
|
|
|
2020-09-07 08:44:40 +00:00
|
|
|
|
|
|
|
def recv(self,data):
|
2020-09-06 20:05:03 +00:00
|
|
|
# decrypt
|
2020-09-07 09:25:38 +00:00
|
|
|
data_in = self.decrypt_incoming(data)
|
2020-09-06 20:05:03 +00:00
|
|
|
|
2020-09-07 09:25:38 +00:00
|
|
|
# route
|
|
|
|
result = self.route(data_json)
|
2020-09-06 20:41:45 +00:00
|
|
|
|
2020-09-07 09:25:38 +00:00
|
|
|
# encrypt
|
|
|
|
data_out = self.encrypt_outgoing(result)
|
2020-09-07 08:44:40 +00:00
|
|
|
|
2020-09-07 09:25:38 +00:00
|
|
|
# send
|
2020-09-07 08:44:40 +00:00
|
|
|
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
|
2020-09-07 08:50:06 +00:00
|
|
|
caller=None
|
2020-09-07 08:44:40 +00:00
|
|
|
if msg_tocaller and 'name' in msg_tophone:
|
|
|
|
caller = Operator(msg_tophone['name'])
|
|
|
|
self.log('send!',msg_tophone,msg_tocaller,caller)
|
2020-09-07 11:23:10 +00:00
|
|
|
data = self.encrypt_information(json_phone=msg_tophone,json_caller=caller)
|
2020-09-07 08:44:40 +00:00
|
|
|
self.log('got back encr:',data)
|
|
|
|
return data
|
|
|
|
|
2020-09-06 20:05:03 +00:00
|
|
|
|
|
|
|
def route(self, data):
|
2020-09-07 07:32:13 +00:00
|
|
|
res=None
|
2020-09-07 08:44:40 +00:00
|
|
|
route = data.get('_route')
|
|
|
|
if not route: return OPERATOR_INTERCEPT_MESSAGE
|
|
|
|
del data['_route']
|
|
|
|
|
|
|
|
if route == 'forge_new_keys':
|
2020-09-07 08:50:06 +00:00
|
|
|
res = self.forge_new_keys(**data)
|
2020-09-07 07:32:13 +00:00
|
|
|
else:
|
|
|
|
res = OPERATOR_INTERCEPT_MESSAGE
|
2020-09-07 07:29:50 +00:00
|
|
|
return res# 'success!'
|
2020-09-06 06:50:23 +00:00
|
|
|
|
2020-09-07 08:44:40 +00:00
|
|
|
def forge_new_keys(self,**data):
|
|
|
|
# get keys
|
|
|
|
res = super().forge_new_keys(**data)
|
2020-09-07 08:50:06 +00:00
|
|
|
pkg={}
|
|
|
|
pkg['name']=data.get('name')
|
|
|
|
pkg['_keychain']=res
|
2020-09-07 08:44:40 +00:00
|
|
|
self.log('returned keys from keymaker.forge_new_keys:','\n'.join(res.keys()))
|
|
|
|
|
|
|
|
# return to_phone,to_caller
|
2020-09-07 08:50:06 +00:00
|
|
|
return (pkg,{})
|
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()
|