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 *
|
2020-09-06 06:50:23 +00:00
|
|
|
from komrade.backend import *
|
2020-09-05 21:11:42 +00:00
|
|
|
|
|
|
|
# external imports
|
|
|
|
from flask import Flask, request, jsonify
|
|
|
|
from flask_classful import FlaskView
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OPERATOR = None
|
2020-09-06 19:39:44 +00:00
|
|
|
TELEPHONE = 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-06 19:54:00 +00:00
|
|
|
# then try to split
|
|
|
|
# try:
|
|
|
|
# unencr_data,
|
|
|
|
|
|
|
|
# # then try to unwrap top level encryption
|
|
|
|
# try:
|
|
|
|
# data = SMessage(OPERATOR.privkey_, TELEPHONE.pubkey_).unwrap(data)
|
|
|
|
# self.log('decrypted data:',data)
|
|
|
|
# except ThemisError:
|
|
|
|
# self.log('not really from the telephone?')
|
|
|
|
# return OPERATOR_INTERCEPT_MESSAGE
|
2020-09-05 21:11:42 +00:00
|
|
|
|
2020-09-06 19:54:00 +00:00
|
|
|
# # step 3: give to The Operator
|
2020-09-05 22:10:10 +00:00
|
|
|
try:
|
2020-09-06 08:39:31 +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'):
|
2020-09-06 19:39:44 +00:00
|
|
|
global OPERATOR,TELEPHONE
|
2020-09-05 21:11:42 +00:00
|
|
|
OPERATOR = TheOperator()
|
2020-09-06 19:39:44 +00:00
|
|
|
TELEPHONE = TheTelephone()
|
2020-09-05 21:11:42 +00:00
|
|
|
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')
|