2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-01 21:40:32 +00:00
Comrad/komrade/operators/operators.py

235 lines
7.1 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 *
from komrade.operators.crypt import *
from komrade.operators.keymaker import *
from komrade.operators.mazes import *
2020-09-04 15:50:08 +00:00
2020-09-05 16:26:37 +00:00
# external imports
from flask import Flask, request, jsonify
from flask_classful import FlaskView
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
### ACTUAL PHONE CONNECTIONS
2020-09-05 17:31:49 +00:00
class TheTelephone(Logger):
2020-09-05 16:26:37 +00:00
"""
API client class for Caller to interact with The Operator.
"""
def __init__(self, caller):
self.caller = caller
2020-09-05 17:31:49 +00:00
2020-09-05 16:26:37 +00:00
@property
def sess(self):
"""
Get connection to Tor
"""
if not hasattr(self,'_sess'):
self._sess = get_tor_proxy_session()
return self._sess
2020-09-05 17:31:49 +00:00
def req(self,json_coming_from_phone={},json_coming_from_caller={}):
# Two parts of every request:
2020-09-05 16:26:37 +00:00
2020-09-05 17:31:49 +00:00
# 1) only overall encryption layer E2EE Telephone -> Operator:
req_data = []
if json_coming_from_phone:
json_coming_from_phone_s = json.dumps(json_coming_from_phone)
json_coming_from_phone_b = json_coming_from_phone_s.encode()
#json_coming_from_phone_b_encr = SMessage(TELEPHONE_PRIVKEY,OPERATOR_PUBKEY).wrap(json_coming_from_phone_b)
else:
json_coming_from_phone_b=b''
# 2) (optional) extra E2EE encrypted layer Caller -> Operator
if json_coming_from_caller:
json_coming_from_caller_s = json.dumps(json_coming_from_caller)
json_coming_from_caller_b = json_coming_from_caller_s.encode()
json_coming_from_caller_b_encr = SMessage(self.caller.privkey_,OPERATOR_PUBKEY).wrap(json_coming_from_caller_b)
else:
json_coming_from_caller_b_encr = b''
# encrypt whole package E2EE, Telephone to Operator
req_data = json_coming_from_phone_b + BSEP + json_coming_from_caller_b_encr
req_data_encr = SMessage(TELEPHONE_PRIVKEY, OPERATOR_PUBKEY).wrap(req_data)
req_data_encr_b64 = b64encode(req_data_encr)
self.log('req_data_encr_b64 <--',req_data_encr_b64)
# send!
res = self.sess.post(OPERATOR_API_URL, data=req_data_encr_b64)
self.log('result from operator?',res)
return res
2020-09-05 16:26:37 +00:00
def forge_new_keys(self, name, pubkey_is_public=False):
req_json = {'name':name, 'pubkey_is_public':pubkey_is_public}
req_json_s = jsonify(req_json)
req_json_s_encr = SMessage()
return self.sess.post(json=req_json)
2020-09-04 15:50:08 +00:00
2020-09-05 15:01:53 +00:00
OPERATOR = None
2020-09-05 17:31:49 +00:00
class TheSwitchboard(FlaskView, Logger):
2020-09-05 17:55:12 +00:00
#default_methods = ['POST']
def get(self):
return "We're sorry; we are unable to complete your call as dialed. Please check the number and dial again, or call your operator to help you."
def post(self):
2020-09-05 17:31:49 +00:00
data = request.data
self.log('incoming_data! <--',data)
2020-09-05 16:26:37 +00:00
2020-09-05 17:31:49 +00:00
# step 1: decode
data = b64decode(data)
self.log('decoded data:',data)
2020-09-04 15:50:08 +00:00
2020-09-05 17:31:49 +00:00
# step 2: decrypt from phone
data = SMessage(OPERATOR.privkey_, TELEPHONE_PUBKEY).unwrap(data)
self.log('decrypted data:',data)
2020-09-05 14:09:31 +00:00
2020-09-05 17:55:12 +00:00
# step 3: give to The Operator
res = OPERATOR.route(data)
# return response to caller
return res
2020-09-05 16:26:37 +00:00
2020-09-05 19:09:30 +00:00
def run_forever(port='8080'):
2020-09-05 15:01:53 +00:00
global OPERATOR
OPERATOR = TheOperator()
app = Flask(__name__)
2020-09-05 16:26:37 +00:00
TheSwitchboard.register(app, route_base='/op/', route_prefix=None)
2020-09-05 19:09:30 +00:00
app.run(debug=True, port=port, host='0.0.0.0')
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()