From 97262eda128d309721eb1a1f417ce12872259501 Mon Sep 17 00:00:00 2001 From: Nyo Date: Wed, 15 Jun 2016 19:01:00 +0200 Subject: [PATCH] .BANCHO. Add Sentry logging --- handlers/mainHandler.py | 71 +++++++++++++++++++++------------------- helpers/configHelper.py | 7 ++++ helpers/requestHelper.py | 12 +++++-- objects/glob.py | 3 +- pep.py | 22 +++++++++++-- 5 files changed, 77 insertions(+), 38 deletions(-) diff --git a/handlers/mainHandler.py b/handlers/mainHandler.py index 4b03d7e..e4e4110 100644 --- a/handlers/mainHandler.py +++ b/handlers/mainHandler.py @@ -44,11 +44,18 @@ from events import matchTransferHostEvent from events import matchFailedEvent from events import matchInviteEvent from events import matchChangeTeamEvent + +# Exception tracking +import tornado.web +import tornado.gen import sys import traceback +from raven.contrib.tornado import SentryMixin from helpers import logHelper as log -class handler(requestHelper.asyncRequestHandler): +class handler(SentryMixin, requestHelper.asyncRequestHandler): + @tornado.web.asynchronous + @tornado.gen.engine def asyncPost(self): try: # Track time if needed @@ -195,37 +202,35 @@ class handler(requestHelper.asyncRequestHandler): else: self.write(responseData) except: - msg = "Unhandled exception in mainHandler:\n```\n{}\n{}\n```".format(sys.exc_info(), traceback.format_exc()) - log.error("{}".format(msg), True) - finally: - try: - if not self._finished: - self.finish() - except: - pass - - def asyncGet(self): - try: - html = "MA MAURO ESISTE?
"
-			html += "           _                 __
" - html += " (_) / /
" - html += " ______ __ ____ ____ / /____
" - html += " / ___/ / _ \\/ _ \\/ / _ \\
" - html += " / / / / /_) / /_) / / ____/
" - html += "/__/ /__/ .___/ .___/__/ \\_____/
" - html += " / / / /
" - html += " /__/ /__/
" - html += "PYTHON > ALL VERSION

" - html += "
" - html += " .. o .
" - html += " o.o o . o
" - html += " oo...
" - html += " __[]__
" - html += " phwr--> _\\:D/_/o_o_o_|__ u wot m8
" - html += " \\\"\"\"\"\"\"\"\"\"\"\"\"\"\"/
" - html += " \\ . .. .. . /
" - html += "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
" - html += "

reverse engineering a protocol impossible to reverse engineer since always
we are actually reverse engineering bancho successfully. for the third time.
" - self.write(html) + log.error("Unknown error!\n```\n{}\n{}```".format(sys.exc_info(), traceback.format_exc())) + if glob.sentry: + yield tornado.gen.Task(self.captureException, exc_info=True) finally: self.finish() + + @tornado.web.asynchronous + @tornado.gen.engine + def asyncGet(self): + html = "MA MAURO ESISTE?
"
+		html += "           _                 __
" + html += " (_) / /
" + html += " ______ __ ____ ____ / /____
" + html += " / ___/ / _ \\/ _ \\/ / _ \\
" + html += " / / / / /_) / /_) / / ____/
" + html += "/__/ /__/ .___/ .___/__/ \\_____/
" + html += " / / / /
" + html += " /__/ /__/
" + html += "PYTHON > ALL VERSION

" + html += "
" + html += " .. o .
" + html += " o.o o . o
" + html += " oo...
" + html += " __[]__
" + html += " phwr--> _\\:D/_/o_o_o_|__ u wot m8
" + html += " \\\"\"\"\"\"\"\"\"\"\"\"\"\"\"/
" + html += " \\ . .. .. . /
" + html += "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
" + html += "

reverse engineering a protocol impossible to reverse engineer since always
we are actually reverse engineering bancho successfully. for the third time.
" + self.write(html) + #yield tornado.gen.Task(self.captureMessage, "test") + self.finish() diff --git a/helpers/configHelper.py b/helpers/configHelper.py index 64015a4..68cb198 100644 --- a/helpers/configHelper.py +++ b/helpers/configHelper.py @@ -59,6 +59,9 @@ class config: self.config.get("debug","packets") self.config.get("debug","time") + self.config.get("sentry","enable") + self.config.get("sentry","dns") + self.config.get("discord","enable") self.config.get("discord","boturl") self.config.get("discord","devgroup") @@ -95,6 +98,10 @@ class config: self.config.set("debug", "packets", "0") self.config.set("debug", "time", "0") + self.config.add_section("sentry") + self.config.set("sentry", "enable", "0") + self.config.set("sentry", "dns", "") + self.config.add_section("discord") self.config.set("discord", "enable", "0") self.config.set("discord", "boturl", "") diff --git a/helpers/requestHelper.py b/helpers/requestHelper.py index 436030b..24dfd3b 100644 --- a/helpers/requestHelper.py +++ b/helpers/requestHelper.py @@ -3,6 +3,8 @@ import tornado.web import tornado.gen from tornado.ioloop import IOLoop from objects import glob +from raven.contrib.tornado import SentryMixin +from raven.contrib.tornado import AsyncSentryClient class asyncRequestHandler(tornado.web.RequestHandler): """ @@ -14,12 +16,18 @@ class asyncRequestHandler(tornado.web.RequestHandler): @tornado.web.asynchronous @tornado.gen.engine def get(self, *args, **kwargs): - yield tornado.gen.Task(runBackground, (self.asyncGet, tuple(args), dict(kwargs))) + try: + yield tornado.gen.Task(runBackground, (self.asyncGet, tuple(args), dict(kwargs))) + except Exception as e: + yield tornado.gen.Task(self.captureException, exc_info=True) @tornado.web.asynchronous @tornado.gen.engine def post(self, *args, **kwargs): - yield tornado.gen.Task(runBackground, (self.asyncPost, tuple(args), dict(kwargs))) + try: + yield tornado.gen.Task(runBackground, (self.asyncPost, tuple(args), dict(kwargs))) + except Exception as e: + yield tornado.gen.Task(self.captureException, exc_info=True) def asyncGet(self, *args, **kwargs): self.send_error(405) diff --git a/objects/glob.py b/objects/glob.py index 523aa69..3eef30d 100644 --- a/objects/glob.py +++ b/objects/glob.py @@ -4,6 +4,7 @@ from objects import tokenList from objects import channelList from objects import matchList from objects import fileLocks +from raven import Client VERSION = "1.2" @@ -17,10 +18,10 @@ restarting = False pool = None fLocks = fileLocks.fileLocks() - debug = False outputRequestTime = False outputPackets = False discord = False gzip = False localize = False +sentry = False diff --git a/pep.py b/pep.py index ad8c07e..095d761 100644 --- a/pep.py +++ b/pep.py @@ -9,6 +9,9 @@ import tornado.web import tornado.httpserver import tornado.gen +# Raven +from raven.contrib.tornado import AsyncSentryClient + # pep.py files from constants import bcolors from helpers import configHelper @@ -20,12 +23,14 @@ from helpers import databaseHelperNew from helpers import generalFunctions from helpers import logHelper as log + from handlers import mainHandler from handlers import apiIsOnlineHandler from handlers import apiOnlineUsersHandler from handlers import apiServerStatusHandler from handlers import ciTriggerHandler + def make_app(): return tornado.web.Application([ (r"/", mainHandler.handler), @@ -152,11 +157,24 @@ if __name__ == "__main__": except: consoleHelper.printColored("[!] Invalid server port! Please check your config.ini and run the server again", bcolors.RED) + # Make app + #application = tornado.httpserver.HTTPServer(make_app()) + application = make_app() + + # Set up sentry + try: + glob.sentry = generalFunctions.stringToBool(glob.conf.config["sentry"]["enable"]) + if glob.sentry == True: + application.sentry_client = AsyncSentryClient(glob.conf.config["sentry"]["dns"]) + else: + consoleHelper.printColored("[!] Warning! Sentry logging is disabled!", bcolors.YELLOW) + except: + consoleHelper.printColored("[!] Error while starting sentry client! Please check your config.ini and run the server again", bcolors.RED) + # Server start message and console output log.logMessage("Server started!", discord=True, of="info.txt", stdout=False) consoleHelper.printColored("> Tornado listening for clients on 127.0.0.1:{}...".format(serverPort), bcolors.GREEN) # Start tornado - app = tornado.httpserver.HTTPServer(make_app()) - app.listen(serverPort) + application.listen(serverPort) tornado.ioloop.IOLoop.instance().start()