2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-05 21:20:51 +00:00
Comrad/komrade/backend/switchboard.py

77 lines
2.4 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
# PATH_OPERATOR_WEB_KEYS_URI = hashish(b'keys')
2020-09-07 17:11:52 +00:00
# PATH_OPERATOR_WEB_KEYS_FILE = f'/home/ryan/www/website-komrade/.builtin.keys'
# PATH_OPERATOR_WEB_KEYS_URL = f'http://{KOMRADE_ONION}/.builtin.keys'
# print(PATH_OPERATOR_WEB_KEYS_URL)
2020-09-05 21:11:42 +00:00
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','send']
2020-09-06 20:26:55 +00:00
@route('/.builtin.keys/')
def keys(self):
if not os.path.exists(PATH_OPERATOR_WEB_KEYS_FILE):
self.log('no keys file exists!',PATH_OPERATOR_WEB_KEYS_FILE)
return OPERATOR_INTERCEPT_MESSAGE
with open(PATH_OPERATOR_WEB_KEYS_FILE,'rb') as f:
return f.read()
2020-09-06 20:26:55 +00:00
@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
def send(self,res):
return res
def route(self,msg):
# give to The Operator
2020-09-07 20:03:37 +00:00
# try:
self.log('Success! your message was: '+str(msg))
res = self.op.recv(msg)
self.log('Your return result should be:',res)
return self.send(res)
# except AssertionError as e:
# self.log('got exception!!',e)
return OPERATOR_INTERCEPT_MESSAGE
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 22:51:34 +00:00
# unenescape
msg = msg.replace('_','/')
return self.route(msg)
2020-09-05 21:11:42 +00:00
def run_forever(port='8080'):
2020-09-07 17:13:39 +00:00
global OPERATOR,TELEPHONE,TELEPHONE_KEYCHAIN,OPERATOR_KEYCHAIN
2020-09-07 17:53:16 +00:00
OPERATOR_KEYCHAIN,TELEPHONE_KEYCHAIN=connect_phonelines()
TELEPHONE = TheTelephone()
OPERATOR = TheOperator()
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-07 18:26:19 +00:00
app.run(debug=False, port=port, host='0.0.0.0')