2018-05-31 23:35:33 +08:00
|
|
|
import logging
|
2017-08-25 22:47:08 +08:00
|
|
|
import json
|
|
|
|
|
|
|
|
import os
|
|
|
|
from flask import request, send_from_directory
|
|
|
|
import flask_restful
|
|
|
|
|
|
|
|
__author__ = 'Barak'
|
|
|
|
|
2018-05-31 23:35:33 +08:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2017-08-25 22:47:08 +08:00
|
|
|
|
|
|
|
MONKEY_DOWNLOADS = [
|
|
|
|
{
|
|
|
|
'type': 'linux',
|
|
|
|
'machine': 'x86_64',
|
|
|
|
'filename': 'monkey-linux-64',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'type': 'linux',
|
|
|
|
'machine': 'i686',
|
|
|
|
'filename': 'monkey-linux-32',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'type': 'linux',
|
2017-09-06 21:54:27 +08:00
|
|
|
'filename': 'monkey-linux-64',
|
2017-08-25 22:47:08 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
'type': 'windows',
|
|
|
|
'machine': 'x86',
|
|
|
|
'filename': 'monkey-windows-32.exe',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'type': 'windows',
|
|
|
|
'machine': 'amd64',
|
|
|
|
'filename': 'monkey-windows-64.exe',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'type': 'windows',
|
|
|
|
'filename': 'monkey-windows-32.exe',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def get_monkey_executable(host_os, machine):
|
|
|
|
for download in MONKEY_DOWNLOADS:
|
|
|
|
if host_os == download.get('type') and machine == download.get('machine'):
|
2018-05-31 23:35:33 +08:00
|
|
|
logger.info('Monkey exec found for os: {0} and machine: {1}'.format(host_os, machine))
|
2017-08-25 22:47:08 +08:00
|
|
|
return download
|
2018-05-31 23:35:33 +08:00
|
|
|
logger.warning('No monkey executables could be found for the host os or machine or both: host_os: {0}, machine: {1}'
|
|
|
|
.format(host_os, machine))
|
2017-08-25 22:47:08 +08:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
class MonkeyDownload(flask_restful.Resource):
|
2018-02-22 22:21:03 +08:00
|
|
|
|
|
|
|
# Used by monkey. can't secure.
|
2017-08-25 22:47:08 +08:00
|
|
|
def get(self, path):
|
|
|
|
return send_from_directory('binaries', path)
|
|
|
|
|
2018-02-22 22:21:03 +08:00
|
|
|
# Used by monkey. can't secure.
|
2017-08-25 22:47:08 +08:00
|
|
|
def post(self):
|
|
|
|
host_json = json.loads(request.data)
|
|
|
|
host_os = host_json.get('os')
|
|
|
|
if host_os:
|
|
|
|
result = get_monkey_executable(host_os.get('type'), host_os.get('machine'))
|
|
|
|
|
|
|
|
if result:
|
|
|
|
real_path = os.path.join('binaries', result['filename'])
|
|
|
|
if os.path.isfile(real_path):
|
|
|
|
result['size'] = os.path.getsize(real_path)
|
|
|
|
return result
|
|
|
|
|
|
|
|
return {}
|