diff --git a/komrade/constants.py b/komrade/constants.py index 3e94fda..9005bc0 100644 --- a/komrade/constants.py +++ b/komrade/constants.py @@ -1,6 +1,6 @@ # addresses URL_KOMRADE = '128.232.229.63' #'komrade.app' -OPERATOR_API_URL = f'http://{URL_KOMRADE}:6999/op/req/' +OPERATOR_API_URL = f'http://{URL_KOMRADE}:6999/op/' # paths diff --git a/komrade/operators/operators.py b/komrade/operators/operators.py index 31dc505..a13c3ab 100644 --- a/komrade/operators/operators.py +++ b/komrade/operators/operators.py @@ -78,6 +78,27 @@ class TheOperator(Operator): passphrase=getpass.getpass('Hello, this is the Operator speaking. What is the passphrase?\n> ') super().__init__(name,passphrase) + 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) ### ACTUAL PHONE CONNECTIONS class TheTelephone(Logger): @@ -139,9 +160,12 @@ class TheTelephone(Logger): OPERATOR = None class TheSwitchboard(FlaskView, Logger): - default_methods = ['POST'] - - def req(self): + #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): data = request.data self.log('incoming_data! <--',data) @@ -153,7 +177,11 @@ class TheSwitchboard(FlaskView, Logger): data = SMessage(OPERATOR.privkey_, TELEPHONE_PUBKEY).unwrap(data) self.log('decrypted data:',data) - return data + # step 3: give to The Operator + res = OPERATOR.route(data) + + # return response to caller + return res def run_forever(): global OPERATOR diff --git a/komrade/utils.py b/komrade/utils.py index defe0c3..ce025c1 100644 --- a/komrade/utils.py +++ b/komrade/utils.py @@ -12,3 +12,50 @@ class Logger(object): mytype = type(self).__name__ caller = calframe[1][3] print(f'\n[{mytype}.{caller}()]',*x) + + + + + + + + + + + + + +# Recursive dictionary merge +# https://gist.github.com/angstwad/bf22d1822c38a92ec0a9 +# +# Copyright (C) 2016 Paul Durivage +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +import collections + +def dict_merge(dct, merge_dct): + """ Recursive dict merge. Inspired by :meth:``dict.update()``, instead of + updating only top-level keys, dict_merge recurses down into dicts nested + to an arbitrary depth, updating keys. The ``merge_dct`` is merged into + ``dct``. + :param dct: dict onto which the merge is executed + :param merge_dct: dct merged into dct + :return: None + """ + for k, v in merge_dct.iteritems(): + if (k in dct and isinstance(dct[k], dict) + and isinstance(merge_dct[k], collections.Mapping)): + dict_merge(dct[k], merge_dct[k]) + else: + dct[k] = merge_dct[k] \ No newline at end of file