From 6a2b5e317c9fac01fcd82cb3a2e8b8217575d9ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Mon, 29 Aug 2016 16:53:53 +0200 Subject: [PATCH] Big refactoring, make the server similar to misp-modules --- .gitignore | 4 +++ README.md | 10 +++++++- bin/qof-server.py => qos_server/__init__.py | 27 ++++++++++++++++++--- setup.py | 27 +++++++++++++++++++++ 4 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 .gitignore rename bin/qof-server.py => qos_server/__init__.py (95%) create mode 100644 setup.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..813c7e0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +__pycache__ +*.swp +*.pyc +qos_server.egg-info/ diff --git a/README.md b/README.md index bc15fec..9fead71 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,14 @@ Requirements - [Tornado](http://www.tornadoweb.org) - Python [redis](https://pypi.python.org/pypi/redis/) client +Installation +------------ + +``` +pip3 install . +``` + + Running the qof-server ---------------------- @@ -17,7 +25,7 @@ The server is using the default Redis configuration for the pdns-toolkit. Don't configuration for your Passive dns data store. ```bash -python3 ./bin/qos-server.py +qos-server ``` Usage diff --git a/bin/qof-server.py b/qos_server/__init__.py similarity index 95% rename from bin/qof-server.py rename to qos_server/__init__.py index f707754..3d04f6e 100644 --- a/bin/qof-server.py +++ b/qos_server/__init__.py @@ -15,13 +15,22 @@ # Copyright (c) 2013 Alexandre Dulaunoy - a@foo.be import tornado.escape -import tornado.ioloop +from tornado.ioloop import IOLoop import tornado.web +import tornado.process +from tornado.concurrent import run_on_executor +from concurrent.futures import ThreadPoolExecutor +import argparse from ipaddress import ip_address import redis import json import sys +import signal + + +def handle_signal(sig, frame): + IOLoop.instance().add_callback(IOLoop.instance().stop) def getFirstSeen(t1=None, t2=None): @@ -170,6 +179,7 @@ class FullQueryHandler(tornado.web.RequestHandler): to_return.append(JsonQOF(getRecord(t=x.strip()))) return to_return + @tornado.gen.coroutine def get(self, q): print("fquery: " + q) try: @@ -187,6 +197,15 @@ def main(): global r global rrset_supported global origin + signal.signal(signal.SIGINT, handle_signal) + signal.signal(signal.SIGTERM, handle_signal) + argParser = argparse.ArgumentParser(description='qof-server server') + argParser.add_argument('-p', default=8888, help='qof-server TCP port (default 8888)') + argParser.add_argument('-l', default='localhost', help='misp-modules listen address (default localhost)') + args = argParser.parse_args() + port = args.p + listen = args.l + rrset = [ {"Reference": "[RFC1035]", "Type": "A", "Value": "1", "Meaning": "a host address", "Template": "", "Registration Date": ""}, {"Reference": "[RFC1035]", "Type": "NS", "Value": "2", "Meaning": "an authoritative name server", "Template": "", "Registration Date": ""}, @@ -281,8 +300,10 @@ def main(): (r"/info", InfoHandler) ]) - application.listen(8888) - tornado.ioloop.IOLoop.instance().start() + application.listen(port, address=listen) + IOLoop.instance().start() + IOLoop.instance().stop() + return 0 if __name__ == '__main__': sys.exit(main()) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..d9c626b --- /dev/null +++ b/setup.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +from setuptools import setup, find_packages + +setup( + name='qos-server', + version='1.0', + author='Alexandre Dulaunoy', + author_email='alexandre.dulaunoy@circl.lu', + maintainer='Alexandre Dulaunoy', + url='https://github.com/adulau/pdns-qof-server', + description='pdns-qof server is a "Common Output Format" compliant passive DNS query interface', + packages=find_packages(), + entry_points={'console_scripts': ['qos-server = qos_server:main']}, + classifiers=[ + 'License :: OSI Approved :: GNU Affero General Public License v3', + 'Development Status :: 5 - Production/Stable', + 'Environment :: Console', + 'Intended Audience :: Science/Research', + 'Programming Language :: Python :: 3', + 'Topic :: Security', + ], + install_requires=[ + 'tornado', + 'redis', + ] +)