Make loader more flexible

pull/7/head
Raphaël Vinot 2016-03-25 15:39:55 +01:00
parent 69801a4fc3
commit 98f72489c5
3 changed files with 37 additions and 29 deletions

View File

@ -25,36 +25,40 @@ import tornado.web
import importlib import importlib
import json import json
import logging import logging
import re import fnmatch
runPath = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(runPath, '..'))
port = 6666 port = 6666
log = logging.getLogger('misp-modules')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler = logging.StreamHandler(stream=sys.stdout)
handler.setFormatter(formatter)
handler.setLevel(logging.INFO)
log.addHandler(handler) def init_logger():
log.setLevel(logging.INFO) log = logging.getLogger('misp-modules')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler = logging.StreamHandler(stream=sys.stdout)
handler.setFormatter(formatter)
handler.setLevel(logging.INFO)
modulesdir = '../modules/expansion' log.addHandler(handler)
log.setLevel(logging.INFO)
return log
mhandlers = {}
modules = [] def load_modules(mod_dir):
for module in os.listdir(modulesdir): sys.path.append(mod_dir)
if ".py" not in module or ".pyc" in module or ".py~" in module: mhandlers = {}
continue modules = []
if re.match("^\.", module): for root, dirnames, filenames in os.walk(mod_dir):
continue if os.path.basename(root) == '__pycache__':
modulename = module.split(".")[0] continue
moduletype = os.path.split(modulesdir)[1] for filename in fnmatch.filter(filenames, '*.py'):
modules.append(modulename) if filename == '__init__.py':
log.info('MISP modules {0} imported'.format(modulename)) continue
mhandlers[modulename] = importlib.import_module('modules.expansion.' + modulename) modulename = filename.split(".")[0]
mhandlers['type:' + modulename] = moduletype moduletype = os.path.split(modulesdir)[1]
modules.append(modulename)
log.info('MISP modules {0} imported'.format(modulename))
mhandlers[modulename] = importlib.import_module(os.path.basename(root) + '.' + modulename)
mhandlers['type:' + modulename] = moduletype
return mhandlers, modules
class ListModules(tornado.web.RequestHandler): class ListModules(tornado.web.RequestHandler):
@ -80,9 +84,13 @@ class QueryModule(tornado.web.RequestHandler):
self.write(json.dumps(ret)) self.write(json.dumps(ret))
service = [(r'/modules', ListModules), (r'/query', QueryModule)] if __name__ == '__main__':
modulesdir = '../modules'
log = init_logger()
mhandlers, modules = load_modules(modulesdir)
service = [(r'/modules', ListModules), (r'/query', QueryModule)]
application = tornado.web.Application(service) application = tornado.web.Application(service)
log.info('MISP modules server started on TCP port {0}'.format(port)) log.info('MISP modules server started on TCP port {0}'.format(port))
application.listen(port) application.listen(port)
tornado.ioloop.IOLoop.instance().start() tornado.ioloop.IOLoop.instance().start()

0
modules/__init__.py Normal file
View File

View File