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
|
|
|
|
|
2020-09-07 13:34:29 +00:00
|
|
|
# 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'
|
2020-09-07 13:34:29 +00:00
|
|
|
|
|
|
|
# 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']
|
2020-09-07 08:44:40 +00:00
|
|
|
excluded_methods = ['phone','op','send']
|
2020-09-06 20:26:55 +00:00
|
|
|
|
2020-09-07 13:31:56 +00:00
|
|
|
@route('/.builtin.keys/')
|
2020-09-07 13:04:23 +00:00
|
|
|
def keys(self):
|
|
|
|
if not os.path.exists(PATH_OPERATOR_WEB_KEYS_FILE):
|
2020-09-07 13:34:29 +00:00
|
|
|
self.log('no keys file exists!',PATH_OPERATOR_WEB_KEYS_FILE)
|
2020-09-07 13:04:23 +00:00
|
|
|
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
|
|
|
|
2020-09-07 08:44:40 +00:00
|
|
|
def send(self,res):
|
|
|
|
return res
|
|
|
|
|
2020-09-07 11:23:10 +00:00
|
|
|
def route(self,msg):
|
|
|
|
# give to The Operator
|
|
|
|
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('_','/')
|
2020-09-07 11:23:10 +00:00
|
|
|
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-05 21:11:42 +00:00
|
|
|
app.run(debug=True, port=port, host='0.0.0.0')
|