monkey/monkey_island/cc/app.py

109 lines
3.6 KiB
Python
Raw Normal View History

2018-02-22 16:33:37 +08:00
import os
2017-08-25 22:47:08 +08:00
from datetime import datetime
2018-02-22 16:33:37 +08:00
2017-08-30 23:14:24 +08:00
import bson
2017-08-25 22:47:08 +08:00
import flask_restful
2018-02-22 16:33:37 +08:00
from bson.json_util import dumps
from flask import Flask, send_from_directory, make_response
2017-09-21 20:55:02 +08:00
from werkzeug.exceptions import NotFound
2017-08-25 22:47:08 +08:00
2018-02-22 16:33:37 +08:00
from cc.auth import init_jwt
2017-08-25 22:47:08 +08:00
from cc.database import mongo
from cc.environment.environment import env
from cc.resources.client_run import ClientRun
2018-02-22 16:33:37 +08:00
from cc.resources.edge import Edge
2017-08-25 22:47:08 +08:00
from cc.resources.local_run import LocalRun
2018-02-22 16:33:37 +08:00
from cc.resources.monkey import Monkey
2017-08-29 19:12:07 +08:00
from cc.resources.monkey_configuration import MonkeyConfiguration
2017-08-25 22:47:08 +08:00
from cc.resources.monkey_download import MonkeyDownload
from cc.resources.netmap import NetMap
2018-03-04 21:22:34 +08:00
from cc.resources.pthmap import PthMap
2017-08-30 23:14:24 +08:00
from cc.resources.node import Node
2017-10-16 01:06:26 +08:00
from cc.resources.report import Report
2017-08-25 22:47:08 +08:00
from cc.resources.root import Root
2018-02-22 16:33:37 +08:00
from cc.resources.telemetry import Telemetry
from cc.resources.telemetry_feed import TelemetryFeed
from cc.services.config import ConfigService
2017-08-25 22:47:08 +08:00
__author__ = 'Barak'
2017-09-21 20:55:02 +08:00
def serve_static_file(static_path):
if static_path.startswith('api/'):
raise NotFound()
2017-09-25 01:12:43 +08:00
try:
return send_from_directory('ui/dist', static_path)
except NotFound:
# Because react uses various urls for same index page, this is probably the user's intention.
2017-09-26 16:33:08 +08:00
return serve_home()
2017-08-25 22:47:08 +08:00
2017-08-29 19:12:07 +08:00
def serve_home():
return serve_static_file('index.html')
2017-08-25 22:47:08 +08:00
def normalize_obj(obj):
2017-08-30 23:14:24 +08:00
if '_id' in obj and not 'id' in obj:
2017-08-25 22:47:08 +08:00
obj['id'] = obj['_id']
del obj['_id']
for key, value in obj.items():
if type(value) is bson.objectid.ObjectId:
obj[key] = str(value)
if type(value) is datetime:
obj[key] = str(value)
if type(value) is dict:
obj[key] = normalize_obj(value)
if type(value) is list:
for i in range(0, len(value)):
if type(value[i]) is dict:
value[i] = normalize_obj(value[i])
return obj
2017-08-29 23:41:39 +08:00
def output_json(obj, code, headers=None):
obj = normalize_obj(obj)
resp = make_response(dumps(obj), code)
resp.headers.extend(headers or {})
return resp
2017-08-25 22:47:08 +08:00
def init_app(mongo_url):
app = Flask(__name__)
api = flask_restful.Api(app)
2017-08-29 23:41:39 +08:00
api.representations = {'application/json': output_json}
2017-08-25 22:47:08 +08:00
app.config['MONGO_URI'] = mongo_url
2018-02-22 16:33:37 +08:00
app.config['SECRET_KEY'] = os.urandom(32)
app.config['JWT_AUTH_URL_RULE'] = '/api/auth'
app.config['JWT_EXPIRATION_DELTA'] = env.get_auth_expiration_time()
2018-02-22 16:33:37 +08:00
init_jwt(app)
2017-08-25 22:47:08 +08:00
mongo.init_app(app)
with app.app_context():
ConfigService.init_config()
2017-08-29 19:12:07 +08:00
app.add_url_rule('/', 'serve_home', serve_home)
2017-09-21 20:55:02 +08:00
app.add_url_rule('/<path:static_path>', 'serve_static_file', serve_static_file)
2017-08-25 22:47:08 +08:00
api.add_resource(Root, '/api')
api.add_resource(Monkey, '/api/monkey', '/api/monkey/', '/api/monkey/<string:guid>')
api.add_resource(LocalRun, '/api/local-monkey', '/api/local-monkey/')
api.add_resource(ClientRun, '/api/client-monkey', '/api/client-monkey/')
2017-08-25 22:47:08 +08:00
api.add_resource(Telemetry, '/api/telemetry', '/api/telemetry/', '/api/telemetry/<string:monkey_guid>')
2017-08-29 19:12:07 +08:00
api.add_resource(MonkeyConfiguration, '/api/configuration', '/api/configuration/')
2017-08-25 22:47:08 +08:00
api.add_resource(MonkeyDownload, '/api/monkey/download', '/api/monkey/download/',
'/api/monkey/download/<string:path>')
2017-08-25 22:47:08 +08:00
api.add_resource(NetMap, '/api/netmap', '/api/netmap/')
2017-08-29 23:41:39 +08:00
api.add_resource(Edge, '/api/netmap/edge', '/api/netmap/edge/')
api.add_resource(Node, '/api/netmap/node', '/api/netmap/node/')
2017-10-16 01:06:26 +08:00
api.add_resource(Report, '/api/report', '/api/report/')
api.add_resource(TelemetryFeed, '/api/telemetry-feed', '/api/telemetry-feed/')
2018-03-04 21:22:34 +08:00
api.add_resource(PthMap, '/api/pthmap', '/api/pthmap/')
2017-08-25 22:47:08 +08:00
return app