You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Comrad/komrade/backend/switchboard.py

148 lines
4.9 KiB
Python

4 years ago
# 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.keymaker import *
from komrade.backend.mazes import *
from komrade.backend.operators import *
# external imports
from flask import Flask, request, jsonify
from flask_classful import FlaskView
### ACTUAL PHONE CONNECTIONS
class TheTelephone(Logger):
"""
API client class for Caller to interact with The Operator.
"""
def __init__(self, caller):
self.caller = caller
4 years ago
@property
def op_pubkey(self):
return b64decode(OPERATOR_PUBKEY)
4 years ago
def dial_operator(self,msg):
URL = OPERATOR_API_URL + msg
4 years ago
r=tor_request_in_python(OPERATOR_API_URL)
4 years ago
print(r)
print(r.text)
return r
4 years ago
@property
def sess(self):
"""
Get connection to Tor
"""
if not hasattr(self,'_sess'):
self._sess = get_tor_proxy_session()
return self._sess
def req(self,json_coming_from_phone={},json_coming_from_caller={}):
# Two parts of every request:
# 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()
4 years ago
op_pubkey
json_coming_from_caller_b_encr = SMessage(self.caller.privkey_,self.op_pubkey).wrap(json_coming_from_caller_b)
4 years ago
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
4 years ago
req_data_encr = SMessage(
b64decode(TELEPHONE_PRIVKEY),
b64decode(OPERATOR_PUBKEY)
).wrap(req_data)
4 years ago
req_data_encr_b64 = b64encode(req_data_encr)
self.log('req_data_encr_b64 <--',req_data_encr_b64)
# send!
req_data_encr_b64_str = req_data_encr_b64.decode('utf-8')
4 years ago
res = self.dial_operator(req_data_encr_b64_str)
4 years ago
self.log('result from operator?',res)
return res
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)
OPERATOR = None
4 years ago
from flask_classful import FlaskView, route
4 years ago
class TheSwitchboard(FlaskView, Logger):
#default_methods = ['POST']
4 years ago
def get(self,msg):
4 years ago
if not msg:
self.log('empty request!')
return OPERATOR_INTERCEPT_MESSAGE
4 years ago
4 years ago
if not isBase64(msg):
self.log('not valid input!')
return OPERATOR_INTERCEPT_MESSAGE
encr_b64_str msg = msg
4 years ago
4 years ago
# first try to get from string to bytes
self.log('incoming <--',encr_b64_str)
try:
4 years ago
encr_b64_b = encr_b64_str.encode('utf-8')
4 years ago
self.log('encr_b64_b',encr_b64_b)
4 years ago
except UnicodeEncodeError:
self.log('not valid unicode?')
4 years ago
return OPERATOR_INTERCEPT_MESSAGE
4 years ago
# then try to get from b64 bytes to raw bytes
4 years ago
try:
data = b64decode(encr_b64_b)
self.log('data',data)
self.log(f'successfully understood input')
except binascii.Error as e:
self.log('not valid b64?')
return OPERATOR_INTERCEPT_MESSAGE
4 years ago
4 years ago
# then try to unwrap top level encryption
4 years ago
try:
tele_pubkey = b64decode(TELEPHONE_PUBKEY)
4 years ago
data = SMessage(OPERATOR.privkey_, tele_pubkey).unwrap(data)
self.log('decrypted data:',data)
4 years ago
except ThemisError:
self.log('not really from the telephone?')
return OPERATOR_INTERCEPT_MESSAGE
4 years ago
# step 3: give to The Operator
4 years ago
try:
4 years ago
return 'Success! your message was: '+str(data)
4 years ago
res = OPERATOR.route(data)
return res
except Exception as e:
self.log('got exception!!',e)
return OPERATOR_INTERCEPT_MESSAGE
4 years ago
# return response to caller
4 years ago
return OPERATOR_INTERCEPT_MESSAGE
4 years ago
def run_forever(port='8080'):
global OPERATOR
OPERATOR = TheOperator()
app = Flask(__name__)
4 years ago
TheSwitchboard.register(app, route_base='/op/', route_prefix=None)
4 years ago
app.run(debug=True, port=port, host='0.0.0.0')