From b69e38840878dd898a92d812daa804409149cf6e Mon Sep 17 00:00:00 2001 From: quadrismegistus Date: Sun, 6 Sep 2020 21:17:47 +0100 Subject: [PATCH] updates --- komrade/backend/__init__.py | 9 ++++++++- komrade/backend/callers.py | 11 ++++------- komrade/backend/switchboard.py | 2 +- komrade/backend/the_operator.py | 32 ++++++++++++++++++++++---------- komrade/backend/the_telephone.py | 6 +++--- 5 files changed, 38 insertions(+), 22 deletions(-) diff --git a/komrade/backend/__init__.py b/komrade/backend/__init__.py index 6cbb7c5..31c148d 100644 --- a/komrade/backend/__init__.py +++ b/komrade/backend/__init__.py @@ -11,4 +11,11 @@ from .the_operator import * from .switchboard import * from .the_telephone import * -from .callers import * \ No newline at end of file +from .callers import * + + + + +## define default entities +OPERATOR = TheOperator() +TELEPHONE = TheTelephone() diff --git a/komrade/backend/callers.py b/komrade/backend/callers.py index 4206fd5..4d37e89 100644 --- a/komrade/backend/callers.py +++ b/komrade/backend/callers.py @@ -13,12 +13,9 @@ class Caller(Operator): @property def phone(self): - """ - Operator on the line. - """ - if not hasattr(self,'_phone'): - self._phone = TheTelephone(caller = self) - return self._phone + return TELEPHONE + @property + def op(self): return OPERATOR def get_new_keys(self,pubkey_pass = None, privkey_pass = None, adminkey_pass = None): """ @@ -44,6 +41,6 @@ class Caller(Operator): 'adminkey_decr_encr', 'adminkey_decr_decr'] try: - return await self.phone.req(json_coming_from_phone = req_json) + return await self.phone.req(json_coming_from_phone = req_json, caller=self) except TypeError: return None \ No newline at end of file diff --git a/komrade/backend/switchboard.py b/komrade/backend/switchboard.py index 4a89137..499b537 100644 --- a/komrade/backend/switchboard.py +++ b/komrade/backend/switchboard.py @@ -75,8 +75,8 @@ class TheSwitchboard(FlaskView, Logger): def run_forever(port='8080'): global OPERATOR,TELEPHONE - OPERATOR = TheOperator() TELEPHONE = TheTelephone() + OPERATOR = TheOperator(phone=TELEPHONE) app = Flask(__name__) TheSwitchboard.register(app, route_base='/op/', route_prefix=None) app.run(debug=True, port=port, host='0.0.0.0') \ No newline at end of file diff --git a/komrade/backend/the_operator.py b/komrade/backend/the_operator.py index c0fdb62..59d540f 100644 --- a/komrade/backend/the_operator.py +++ b/komrade/backend/the_operator.py @@ -5,9 +5,7 @@ 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 * -from komrade.backend.crypt import * -from komrade.backend.operators import * -from komrade.backend.mazes import * +from komrade.backend import * @@ -15,6 +13,9 @@ class TheOperator(Operator): """ The remote operator """ + @property + def phone(self): + return TELEPHONE def __init__(self, name = OPERATOR_NAME, passphrase='acc'): @@ -27,20 +28,31 @@ class TheOperator(Operator): passphrase=getpass.getpass('Hello, this is the Operator speaking. What is the passphrase?\n> ') super().__init__(name,passphrase,path_crypt_keys=PATH_CRYPT_OP_KEYS,path_crypt_data=PATH_CRYPT_OP_DATA) - def decrypt_incoming(self,data): # step 1 split: - data_unencr,data_encr = data.split(BSEP) - self.log('data_unencr =',data_unencr) - self.log('data_encr =',data_encr) + data_encr_by_phone,data_encr_by_caller = data.split(BSEP) + + self.log('data_encr_by_phone =',data_encr_by_phone) + self.log('data_encr_by_caller =',data_encr_by_caller) + + if data_encr_by_phone: + # then try to unwrap telephone encryption + try: + data_unencr_by_phone = SMessage(self.privkey_, self.phone.pubkey_).unwrap(data) + self.log('decrypted data !!!:',data_unencr_by_phone) + except ThemisError: + self.log('not really from the telephone?') + return OPERATOR_INTERCEPT_MESSAGE + data_by_phone = TELEPHONE. + + 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) + caller = Caller(name) + data_unencr2 = SMessage(self.privkey_, caller.pubkey_).unwrap(data_encr) if type(data_unencr)==dict and type(data_unencr2)==dict: data = data_unencr diff --git a/komrade/backend/the_telephone.py b/komrade/backend/the_telephone.py index a522fee..3070ec9 100644 --- a/komrade/backend/the_telephone.py +++ b/komrade/backend/the_telephone.py @@ -9,15 +9,15 @@ class TheTelephone(Operator): API client class for Caller to interact with The Operator. """ def __init__(self, caller=None): - self.op = TheOperator() - self.caller = None - super().__init__( name=TELEPHONE_NAME, path_crypt_keys=PATH_CRYPT_CA_KEYS, path_crypt_data=PATH_CRYPT_CA_KEYS ) + @property + def op(self): return OPERATOR + async def dial_operator(self,msg): msg=msg.replace('/','_') URL = OPERATOR_API_URL + msg + '/'