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

157 lines
5.1 KiB
Python
Raw Normal View History

2020-09-05 21:11:42 +00:00
# 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
2020-09-05 22:05:02 +00:00
@property
def op_pubkey(self):
return b64decode(OPERATOR_PUBKEY)
2020-09-05 22:15:15 +00:00
def dial_operator(self,msg):
2020-09-05 22:51:34 +00:00
URL = OPERATOR_API_URL + msg + '/'
self.log("DIALING THE OPERATOR:",URL)
2020-09-05 22:44:23 +00:00
r=tor_request_in_python(URL)
2020-09-05 22:15:15 +00:00
print(r)
print(r.text)
return r
2020-09-05 21:11:42 +00:00
@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()
2020-09-05 22:05:02 +00:00
op_pubkey
json_coming_from_caller_b_encr = SMessage(self.caller.privkey_,self.op_pubkey).wrap(json_coming_from_caller_b)
2020-09-05 21:11:42 +00:00
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
2020-09-05 22:05:02 +00:00
req_data_encr = SMessage(
b64decode(TELEPHONE_PRIVKEY),
b64decode(OPERATOR_PUBKEY)
).wrap(req_data)
2020-09-05 21:11:42 +00:00
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')
2020-09-05 22:51:34 +00:00
# escape slashes
req_data_encr_b64_str_esc = req_data_encr_b64_str.replace('/','_')
2020-09-05 22:15:15 +00:00
res = self.dial_operator(req_data_encr_b64_str)
2020-09-05 21:11:42 +00:00
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
2020-09-05 21:36:24 +00:00
from flask_classful import FlaskView, route
2020-09-05 21:11:42 +00:00
class TheSwitchboard(FlaskView, Logger):
#default_methods = ['POST']
2020-09-05 22:39:28 +00:00
def get(self,msg):
2020-09-05 22:44:23 +00:00
self.log('Incoming call!:',msg)
2020-09-05 22:41:43 +00:00
if not msg:
self.log('empty request!')
return OPERATOR_INTERCEPT_MESSAGE
2020-09-05 21:22:09 +00:00
2020-09-05 22:51:34 +00:00
# unenescape
msg = msg.replace('_','/')
2020-09-05 22:41:43 +00:00
if not isBase64(msg):
self.log('not valid input!')
return OPERATOR_INTERCEPT_MESSAGE
2020-09-05 22:42:02 +00:00
encr_b64_str = msg
2020-09-05 21:53:40 +00:00
2020-09-05 21:11:42 +00:00
# first try to get from string to bytes
self.log('incoming <--',encr_b64_str)
try:
2020-09-05 21:27:21 +00:00
encr_b64_b = encr_b64_str.encode('utf-8')
2020-09-05 21:11:42 +00:00
self.log('encr_b64_b',encr_b64_b)
2020-09-05 21:58:04 +00:00
except UnicodeEncodeError:
self.log('not valid unicode?')
2020-09-05 21:11:42 +00:00
return OPERATOR_INTERCEPT_MESSAGE
2020-09-05 22:10:10 +00:00
# then try to get from b64 bytes to raw bytes
2020-09-05 21:58:04 +00:00
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
2020-09-05 21:11:42 +00:00
2020-09-05 22:10:10 +00:00
# then try to unwrap top level encryption
2020-09-05 22:08:43 +00:00
try:
tele_pubkey = b64decode(TELEPHONE_PUBKEY)
2020-09-05 22:10:10 +00:00
data = SMessage(OPERATOR.privkey_, tele_pubkey).unwrap(data)
self.log('decrypted data:',data)
2020-09-05 22:08:43 +00:00
except ThemisError:
self.log('not really from the telephone?')
return OPERATOR_INTERCEPT_MESSAGE
2020-09-05 21:11:42 +00:00
# step 3: give to The Operator
2020-09-05 22:10:10 +00:00
try:
2020-09-05 22:17:57 +00:00
return 'Success! your message was: '+str(data)
2020-09-05 22:10:10 +00:00
res = OPERATOR.route(data)
return res
except Exception as e:
self.log('got exception!!',e)
return OPERATOR_INTERCEPT_MESSAGE
2020-09-05 21:11:42 +00:00
# return response to caller
2020-09-05 22:10:10 +00:00
return OPERATOR_INTERCEPT_MESSAGE
2020-09-05 21:11:42 +00:00
def run_forever(port='8080'):
global OPERATOR
OPERATOR = TheOperator()
app = Flask(__name__)
2020-09-05 21:47:29 +00:00
TheSwitchboard.register(app, route_base='/op/', route_prefix=None)
2020-09-05 21:11:42 +00:00
app.run(debug=True, port=port, host='0.0.0.0')