2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-17 21:25:37 +00:00
Comrad/komrade/backend/switchboard.py

85 lines
2.5 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 *
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):
2020-09-06 20:26:55 +00:00
default_methods = ['GET']
excluded_methods = ['phone','op']
@property
def phone(self):
global TELEPHONE
2020-09-06 20:29:10 +00:00
from komrade.backend.the_telephone import TheTelephone
2020-09-06 20:26:55 +00:00
if not TELEPHONE: TELEPHONE=TheTelephone()
return TELEPHONE
@property
def op(self):
global OPERATOR
2020-09-06 20:29:10 +00:00
from komrade.backend.the_operator import TheOperator
2020-09-06 20:26:55 +00:00
if not OPERATOR: OPERATOR=TheOperator()
return OPERATOR
2020-09-05 21:11:42 +00:00
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
# # 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-06 20:26:55 +00:00
res = self.op.receive(data)
2020-09-05 22:10:10 +00:00
return res
2020-09-06 20:31:43 +00:00
except AssertionError as e:
2020-09-05 22:10:10 +00:00
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 22:09:36 +00:00
global OPERATOR,TELEPHONE
TELEPHONE = TheTelephone(allow_builtin=False)
2020-09-06 22:09:36 +00:00
OPERATOR = TheOperator(allow_builtin=False)
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')