monkey/monkey_island/cc/resources/root.py

57 lines
1.8 KiB
Python
Raw Normal View History

2017-08-25 22:47:08 +08:00
from datetime import datetime
import flask_restful
2018-01-16 23:23:17 +08:00
from flask import request, make_response, jsonify
2017-08-25 22:47:08 +08:00
2018-02-22 22:21:03 +08:00
from cc.auth import jwt_required
2017-08-25 22:47:08 +08:00
from cc.database import mongo
from cc.services.config import ConfigService
from cc.services.node import NodeService
2017-12-24 17:23:57 +08:00
from cc.services.report import ReportService
from cc.utils import local_ip_addresses
2017-08-25 22:47:08 +08:00
__author__ = 'Barak'
class Root(flask_restful.Resource):
2018-02-22 22:21:03 +08:00
@jwt_required()
2017-08-25 22:47:08 +08:00
def get(self, action=None):
if not action:
action = request.args.get('action')
2017-08-29 19:12:07 +08:00
2017-08-25 22:47:08 +08:00
if not action:
2018-01-16 23:23:17 +08:00
return Root.get_server_info()
2017-08-25 22:47:08 +08:00
elif action == "reset":
2018-01-16 23:23:17 +08:00
return Root.reset_db()
2017-08-25 22:47:08 +08:00
elif action == "killall":
2018-01-16 23:23:17 +08:00
return Root.kill_all()
2017-08-25 22:47:08 +08:00
else:
2017-08-29 19:12:07 +08:00
return make_response(400, {'error': 'unknown action'})
2017-09-18 19:18:57 +08:00
2018-01-16 23:23:17 +08:00
@staticmethod
def get_server_info():
return jsonify(ip_addresses=local_ip_addresses(), mongo=str(mongo.db),
completed_steps=Root.get_completed_steps())
@staticmethod
def reset_db():
# We can't drop system collections.
[mongo.db[x].drop() for x in mongo.db.collection_names() if not x.startswith('system.')]
2018-01-16 23:23:17 +08:00
ConfigService.init_config()
return jsonify(status='OK')
@staticmethod
def kill_all():
mongo.db.monkey.update({'dead': False}, {'$set': {'config.alive': False, 'modifytime': datetime.now()}},
upsert=False,
multi=True)
return jsonify(status='OK')
@staticmethod
def get_completed_steps():
is_any_exists = NodeService.is_any_monkey_exists()
2017-12-24 17:23:57 +08:00
infection_done = NodeService.is_monkey_finished_running()
report_done = ReportService.is_report_generated()
return dict(run_server=True, run_monkey=is_any_exists, infection_done=infection_done, report_done=report_done)