Add multiprocessing on queries

This commit is contained in:
Raphaël Vinot 2016-10-02 22:25:52 +02:00
parent a4dfadfe04
commit 1078d2f159

View file

@ -17,6 +17,9 @@ import os
import M2Crypto import M2Crypto
from tornado.concurrent import run_on_executor
from concurrent.futures import ThreadPoolExecutor
import tornado.httpserver import tornado.httpserver
import tornado.ioloop import tornado.ioloop
import tornado.options import tornado.options
@ -28,16 +31,18 @@ certrepo = '/data/certs{}'
ipmaxsize = 512 # /23 ipmaxsize = 512 # /23
servername = 'SSL Certificate API - https://github.com/adulau/crl-monitor' servername = 'SSL Certificate API - https://github.com/adulau/crl-monitor'
def checksha1(value=False): def checksha1(value=False):
if value is False or len(value) != 40: if value is False or len(value) != 40:
return False return False
try: try:
sha1int = int(value, 16) int(value, 16)
except ValueError: except ValueError:
return False return False
return True return True
def bpath(ha=None, level=6): def bpath(ha=None, level=6):
if ha is None: if ha is None:
return False return False
@ -49,14 +54,20 @@ def bpath(ha=None, level=6):
class SSLQueryHandler(tornado.web.RequestHandler): class SSLQueryHandler(tornado.web.RequestHandler):
def get(self, input): # Default value in Python 3.5
# https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor
nb_threads = tornado.process.cpu_count() * 5
executor = ThreadPoolExecutor(nb_threads)
@run_on_executor
def run_request(self, q):
try: try:
# Redis structure Set of (FP) per IP # Redis structure Set of (FP) per IP
r = redis.StrictRedis(host='127.0.0.1', port=8323) r = redis.StrictRedis(host='127.0.0.1', port=8323)
except: except:
print "Unable to connect to the Redis server" print("Unable to connect to the Redis server")
sys.exit(255) sys.exit(255)
subnets = [input] subnets = [q]
out = {} out = {}
for subnet in subnets: for subnet in subnets:
if re.findall(r":", subnet): if re.findall(r":", subnet):
@ -97,16 +108,32 @@ class SSLQueryHandler(tornado.web.RequestHandler):
if not self._finished: if not self._finished:
self.set_header('Content-Type', 'application/json') self.set_header('Content-Type', 'application/json')
self.set_header('Server', servername) self.set_header('Server', servername)
self.write(json.dumps(out)) return json.dumps(out)
def get(self, q):
print("Query:", q)
try:
r = yield self.run_request(q)
self.write(r)
except Exception as e:
print('Something went wrong with {}:\n{}'.format(q, e))
class CertificateQueryHandler(tornado.web.RequestHandler): class CertificateQueryHandler(tornado.web.RequestHandler):
def get(self, input):
# Default value in Python 3.5
# https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor
nb_threads = tornado.process.cpu_count() * 5
executor = ThreadPoolExecutor(nb_threads)
@run_on_executor
def run_request(self, q):
try: try:
r = redis.StrictRedis(host='127.0.0.1', port=8323) r = redis.StrictRedis(host='127.0.0.1', port=8323)
except: except:
print "Unable to connect to the Redis server" print("Unable to connect to the Redis server")
sys.exit(255) sys.exit(255)
fp = input.lower() fp = q.lower()
if not checksha1(value=fp): if not checksha1(value=fp):
self.clear() self.clear()
self.set_status(400) self.set_status(400)
@ -123,23 +150,33 @@ class CertificateQueryHandler(tornado.web.RequestHandler):
if not self._finished: if not self._finished:
self.set_header('Content-Type', 'application/json') self.set_header('Content-Type', 'application/json')
self.set_header('Server', servername) self.set_header('Server', servername)
self.write(json.dumps(out)) return json.dumps(out)
def get(self, q):
print("Query:", q)
try:
r = yield self.run_request(q)
self.write(r)
except Exception as e:
print('Something went wrong with {}:\n{}'.format(q, e))
class FetchCertificateHandler(tornado.web.RequestHandler): class FetchCertificateHandler(tornado.web.RequestHandler):
def get(self, input):
try:
r = redis.StrictRedis(host='127.0.0.1', port=8323)
except:
print ("Unable to connect to the Redis server")
sys.exit(255)
# Default value in Python 3.5
# https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor
nb_threads = tornado.process.cpu_count() * 5
executor = ThreadPoolExecutor(nb_threads)
@run_on_executor
def run_request(self, q):
# ICSI data # ICSI data
try: try:
ricsi = redis.StrictRedis(host='localhost', port=6380, db=5) ricsi = redis.StrictRedis(host='localhost', port=6380, db=5)
except: except:
print("Unable to connect to the Redis ICSI notary server") print("Unable to connect to the Redis ICSI notary server")
fp = input.lower() fp = q.lower()
if not checksha1(value=fp): if not checksha1(value=fp):
self.clear() self.clear()
self.set_status(400) self.set_status(400)
@ -175,9 +212,18 @@ class FetchCertificateHandler(tornado.web.RequestHandler):
if not self._finished: if not self._finished:
self.set_header('Content-Type', 'application/json') self.set_header('Content-Type', 'application/json')
self.set_header('Server', servername) self.set_header('Server', servername)
self.write(json.dumps(out)) return json.dumps(out)
if __name__ == "__main__": def get(self, q):
print("Query:", q)
try:
r = yield self.run_request(q)
self.write(r)
except Exception as e:
print('Something went wrong with {}:\n{}'.format(q, e))
def main():
tornado.options.parse_command_line() tornado.options.parse_command_line()
app = tornado.web.Application(handlers=[ app = tornado.web.Application(handlers=[
(r"/query/(.*)", SSLQueryHandler), (r"/query/(.*)", SSLQueryHandler),
@ -188,3 +234,6 @@ if __name__ == "__main__":
http_server.listen(options.port) http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start() tornado.ioloop.IOLoop.instance().start()
if __name__ == '__main__':
sys.exit(main())