2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-17 21:25:37 +00:00
Comrad/komrade/backend/operators.py

142 lines
4.0 KiB
Python
Raw Normal View History

2020-09-04 15:50:08 +00:00
"""
There is only one operator!
Running on node prime.
"""
2020-09-05 16:26:37 +00:00
# internal imports
2020-09-04 15:50:08 +00:00
import os,sys; sys.path.append(os.path.abspath(os.path.join(os.path.abspath(os.path.join(os.path.dirname(__file__),'..')),'..')))
2020-09-05 16:26:37 +00:00
from komrade import *
2020-09-05 21:11:42 +00:00
from komrade.backend.crypt import *
from komrade.backend.keymaker import *
from komrade.backend.mazes import *
2020-09-04 15:50:08 +00:00
2020-09-05 17:31:49 +00:00
OPERATOR_NAME = 'TheOperator'
TELEPHONE_NAME = 'TheTelephone'
2020-09-04 15:50:08 +00:00
class Operator(Keymaker):
2020-09-05 14:09:31 +00:00
def __init__(self, name, passphrase=None):
super().__init__(name=name,passphrase=passphrase)
2020-09-04 15:50:08 +00:00
2020-09-05 14:09:31 +00:00
def boot(self,create=False):
# Do I have my keys?
have_keys = self.exists()
# If not, forge them -- only once!
if not have_keys and create:
self.get_new_keys()
2020-09-04 15:50:08 +00:00
2020-09-05 14:09:31 +00:00
# load keychain into memory
self._keychain = self.keychain(force = True)
2020-09-04 15:50:08 +00:00
2020-09-05 16:26:37 +00:00
2020-09-05 15:01:53 +00:00
class Caller(Operator):
2020-09-05 16:26:37 +00:00
"""
Variant of an Operator which handles local keys and keymaking.
"""
@property
2020-09-05 17:31:49 +00:00
def phone(self):
2020-09-05 16:26:37 +00:00
"""
Operator on the line.
"""
2020-09-05 17:31:49 +00:00
if not hasattr(self,'_phone'):
self._phone = TheTelephone(caller = self)
return self._phone
2020-09-05 16:26:37 +00:00
2020-09-05 15:01:53 +00:00
def get_new_keys(self,pubkey_pass = None, privkey_pass = None, adminkey_pass = None):
2020-09-05 16:26:37 +00:00
"""
This is the local caller's version.
He never touches the encrypted keys. Only the Operator does!
"""
2020-09-05 15:01:53 +00:00
# Get decryptor keys back from The Operator (one half of the Keymaker)
2020-09-05 17:31:49 +00:00
keychain = self.forge_new_keys(self.name)
2020-09-05 15:01:53 +00:00
self.log('create_keys() res from Operator? <-',keychain)
# Now lock the decryptor keys away, sealing it with a password of memory!
self.lock_new_keys(keychain)
2020-09-04 15:50:08 +00:00
class TheOperator(Operator):
"""
2020-09-05 14:09:31 +00:00
The remote operator! Only one!
2020-09-04 15:50:08 +00:00
"""
2020-09-05 16:26:37 +00:00
2020-09-04 15:50:08 +00:00
2020-09-05 17:31:49 +00:00
def __init__(self, name = OPERATOR_NAME, passphrase='acc'):
2020-09-04 15:50:08 +00:00
"""
Boot up the operator. Requires knowing or setting a password of memory.
"""
2020-09-05 16:26:37 +00:00
# init req paths
if not os.path.exists(PATH_OPERATOR): os.makedirs(PATH_OPERATOR)
2020-09-05 14:09:31 +00:00
if not passphrase:
passphrase=getpass.getpass('Hello, this is the Operator speaking. What is the passphrase?\n> ')
super().__init__(name,passphrase)
2020-09-04 15:50:08 +00:00
2020-09-05 17:55:12 +00:00
def route(self, data):
# step 1 split:
data_unencr,data_encr = data.split(BSEP)
if data_encr and 'name' in data_unencr:
name=data_unencr['name']
keychain=data_unencr.get('keychain',{})
# decrypt using this user's pubkey on record
caller = Operator(name)
from_pubkey = user.pubkey(keychain=keychain)
data_unencr2 = SMessage(OPERATOR.privkey_, from_pubkey).unwrap(data_encr)
if type(data_unencr)==dict and type(data_unencr2)==dict:
data = data_unencr
dict_merge(data_unencr2, data)
else:
data=(data_unencr,data_unencr2)
else:
data = data_unencr
print(data)
2020-09-05 16:26:37 +00:00
2020-09-05 17:31:49 +00:00
def init_operators():
op = Operator(name=OPERATOR_NAME)
phone = Operator(name=TELEPHONE_NAME)
2020-09-05 16:26:37 +00:00
2020-09-05 17:31:49 +00:00
op.get_new_keys()
phone.get_new_keys()
op_pub = op.pubkey_
phone_pub = phone.pubkey_
phone_priv = phone.privkey_
print('OPERATOR_PUBKEY',op_pub)
print('TELEPHONE_PUBKEY =',phone_pub)
print('TELEPHONE_PRIVKEY =',phone_priv)
2020-09-05 16:26:37 +00:00
def test_op():
op = TheOperator()
#op.boot()
#pubkey = op.keychain()['pubkey']
#pubkey_b64 = b64encode(pubkey)
#print(pubkey_b64)
keychain = op.keychain(force=True)
from pprint import pprint
pprint(keychain)
pubkey = op.keychain()['pubkey']
pubkey_b64 = b64encode(pubkey)
2020-09-05 17:31:49 +00:00
print(pubkey)
2020-09-05 16:26:37 +00:00
2020-09-05 17:31:49 +00:00
def test_call():
caller = Operator('marx3') #Caller('marx')
# caller.boot(create=True)
# print(caller.keychain())
phone = TheTelephone(caller=caller)
res = phone.req({'name':'marx', 'pubkey_is_public':True})
print(res)
2020-09-04 15:50:08 +00:00
2020-09-05 15:01:53 +00:00
if __name__ == '__main__':
2020-09-05 16:26:37 +00:00
#run_forever()
2020-09-05 17:31:49 +00:00
# test_op()
# init_operators()
test_call()