def start(self): # get properties flaskHost = Configuration.getFlaskHost() if self.argument_port is not None: if type(self.argument_port) == int: flaskPort = self.argument_port else: flaskPort = Configuration.getFlaskPort() else: flaskPort = Configuration.getFlaskPort() flaskDebug = Configuration.getFlaskDebug() # logging if Configuration.getLogging(): logfile = Configuration.getLogfile() pathToLog = logfile.rsplit('/', 1)[0] if not os.path.exists(pathToLog): os.makedirs(pathToLog) maxLogSize = Configuration.getMaxLogSize() backlog = Configuration.getBacklog() file_handler = RotatingFileHandler(logfile, maxBytes=maxLogSize, backupCount=backlog) file_handler.setLevel(logging.ERROR) formatter = logging.Formatter( "%(asctime)s - %(name)s - %(levelname)s - %(message)s") file_handler.setFormatter(formatter) self.app.logger.addHandler(file_handler) if flaskDebug: # start debug flask server self.app.run(host=flaskHost, port=flaskPort, debug=flaskDebug) else: # start asynchronous server using tornado wrapper for flask # ssl connection print("Server starting...") if Configuration.useSSL(): ssl_options = { "certfile": os.path.join(_runPath, "../", Configuration.getSSLCert()), "keyfile": os.path.join(_runPath, "../", Configuration.getSSLKey()) } else: ssl_options = None signal.signal(signal.SIGTERM, self.sig_handler) signal.signal(signal.SIGINT, self.sig_handler) self.http_server = HTTPServer(WSGIContainer(self.app), ssl_options=ssl_options) self.http_server.bind(flaskPort, address=flaskHost) # self.http_server.start(0) # Forks multiple sub-processes self.http_server.start( ) # Don't fork, since MongoDB connection is made earlier and not Fork-safe. IOLoop.instance().start()
def main(): def sig_handler(sig, frame): print("Terminating...") tornado.ioloop.IOLoop.current().stop() tornado.options.parse_command_line() signal.signal(signal.SIGINT, sig_handler) signal.signal(signal.SIGTERM, sig_handler) db = Configuration.getMongoConnection() redisdb = Configuration.getRedisVendorConnection() port = Configuration.getFlaskPort() app = tornado.web.Application([ (r"/", MainHandler), (r"/api/stats", StatsHandler), (r"/api/search/([^/]+)", SearchHandler) ], db=db, redisdb=redisdb) app.listen(port) print("Listening on :%s" % port) tornado.ioloop.IOLoop.current().start()
def start(self): # get properties flaskHost = Configuration.getFlaskHost() flaskPort = Configuration.getFlaskPort() flaskDebug = Configuration.getFlaskDebug() # logging # if Configuration.getLogging(): # logfile = Configuration.getLogfile() # pathToLog = logfile.rsplit("/", 1)[0] # if not os.path.exists(pathToLog): # os.makedirs(pathToLog) # maxLogSize = Configuration.getMaxLogSize() # backlog = Configuration.getBacklog() # file_handler = RotatingFileHandler( # logfile, maxBytes=maxLogSize, backupCount=backlog # ) # file_handler.setLevel(logging.ERROR) # formatter = logging.Formatter( # "%(asctime)s - %(name)s - %(levelname)s - %(message)s" # ) # file_handler.setFormatter(formatter) # self.app.logger.addHandler(file_handler) if flaskDebug: # start debug flask server self.app.run(host=flaskHost, port=flaskPort, debug=flaskDebug) else: # start asynchronous server using tornado wrapper for flask # ssl connection print("Server starting...") if Configuration.useSSL(): ssl_options = { "certfile": os.path.join(_runPath, "../../../", Configuration.getSSLCert()), "keyfile": os.path.join(_runPath, "../../../", Configuration.getSSLKey()), } else: ssl_options = None signal.signal(signal.SIGTERM, self.sig_handler) signal.signal(signal.SIGINT, self.sig_handler) self.http_server = HTTPServer(WSGIContainer(self.app), ssl_options=ssl_options) self.http_server.bind(flaskPort, address=flaskHost) self.http_server.start(0) # Forks multiple sub-processes IOLoop.instance().start()
def start(self): # get properties flaskHost = Configuration.getFlaskHost() flaskPort = Configuration.getFlaskPort() flaskDebug = Configuration.getFlaskDebug() # logging if Configuration.getLogging(): logfile = Configuration.getLogfile() pathToLog = logfile.rsplit('/', 1)[0] if not os.path.exists(pathToLog): os.makedirs(pathToLog) maxLogSize = Configuration.getMaxLogSize() backlog = Configuration.getBacklog() file_handler = RotatingFileHandler(logfile, maxBytes=maxLogSize, backupCount=backlog) file_handler.setLevel(logging.ERROR) formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") file_handler.setFormatter(formatter) self.app.logger.addHandler(file_handler) # Placing routes - Doing this later allows us to define multiple API instances, without flask problems for route in web.Routes.get_routes(self): self.addRoute(route) if flaskDebug: # start debug flask server self.app.run(host=flaskHost, port=flaskPort, debug=flaskDebug) else: # start asynchronous server using tornado wrapper for flask # ssl connection print("Server starting...") if Configuration.useSSL(): ssl_options = {"certfile": os.path.join(_runPath, "../", Configuration.getSSLCert()), "keyfile": os.path.join(_runPath, "../", Configuration.getSSLKey())} else: ssl_options = None signal.signal(signal.SIGTERM, self.sig_handler) signal.signal(signal.SIGINT, self.sig_handler) self.http_server = HTTPServer(WSGIContainer(self.app), ssl_options=ssl_options) self.http_server.bind(flaskPort, address=flaskHost) self.http_server.start(0) # Forks multiple sub-processes IOLoop.instance().start()
def start(self): # get properties flaskHost = Configuration.getFlaskHost() flaskPort = Configuration.getFlaskPort() flaskDebug = Configuration.getFlaskDebug() # logging if Configuration.getLogging(): logfile = Configuration.getLogfile() pathToLog = logfile.rsplit('/', 1)[0] if not os.path.exists(pathToLog): os.makedirs(pathToLog) maxLogSize = Configuration.getMaxLogSize() backlog = Configuration.getBacklog() file_handler = RotatingFileHandler(logfile, maxBytes=maxLogSize, backupCount=backlog) file_handler.setLevel(logging.ERROR) formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") file_handler.setFormatter(formatter) self.app.logger.addHandler(file_handler) if flaskDebug: # start debug flask server self.app.run(host=flaskHost, port=flaskPort, debug=flaskDebug) else: # start asynchronous server using tornado wrapper for flask # ssl connection print("Server starting...") if Configuration.useSSL(): ssl_options = {"certfile": os.path.join(_runPath, "../", Configuration.getSSLCert()), "keyfile": os.path.join(_runPath, "../", Configuration.getSSLKey())} else: ssl_options = None signal.signal(signal.SIGTERM, self.sig_handler) signal.signal(signal.SIGINT, self.sig_handler) self.http_server = HTTPServer(WSGIContainer(self.app), ssl_options=ssl_options) self.http_server.bind(flaskPort, address=flaskHost) self.http_server.start(0) # Forks multiple sub-processes IOLoop.instance().start()
io_loop = IOLoop.instance() deadline = time.time() + MAX_WAIT_SECONDS_BEFORE_SHUTDOWN def stop_loop(): now = time.time() if now < deadline and (io_loop._callbacks or io_loop._timeouts): io_loop.add_timeout(now + 1, stop_loop) else: io_loop.stop() print('Shutdown') stop_loop() if __name__ == '__main__': # get properties flaskHost = Configuration.getFlaskHost() flaskPort = Configuration.getFlaskPort() flaskDebug = Configuration.getFlaskDebug() # logging if Configuration.getLogging(): logfile = Configuration.getLogfile() pathToLog = os.path.join(_runPath, logfile.rsplit('/', 1)[0]) #pathToLog = os.path.join(_runPath, pathToLog) logfile = os.path.join(_runPath, logfile) if not os.path.exists(pathToLog): os.makedirs(pathToLog) maxLogSize = Configuration.getMaxLogSize() backlog = Configuration.getBacklog() file_handler = RotatingFileHandler(logfile, maxBytes=maxLogSize, backupCount=backlog) file_handler.setLevel(logging.ERROR) formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") file_handler.setFormatter(formatter)
def stop_loop(): now = time.time() if now < deadline and (io_loop._callbacks or io_loop._timeouts): io_loop.add_timeout(now + 1, stop_loop) else: io_loop.stop() print('Shutdown') stop_loop() if __name__ == '__main__': # get properties flaskHost = Configuration.getFlaskHost() flaskPort = Configuration.getFlaskPort() flaskDebug = Configuration.getFlaskDebug() # logging if Configuration.getLogging(): logfile = Configuration.getLogfile() pathToLog = logfile.rsplit('/', 1)[0] if not os.path.exists(pathToLog): os.makedirs(pathToLog) maxLogSize = Configuration.getMaxLogSize() backlog = Configuration.getBacklog() file_handler = RotatingFileHandler(logfile, maxBytes=maxLogSize, backupCount=backlog) file_handler.setLevel(logging.ERROR) formatter = logging.Formatter( "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
deadline = time.time() + MAX_WAIT_SECONDS_BEFORE_SHUTDOWN def stop_loop(): now = time.time() if now < deadline and (io_loop._callbacks or io_loop._timeouts): io_loop.add_timeout(now + 1, stop_loop) else: io_loop.stop() print('Shutdown') stop_loop() if __name__ == '__main__': flaskHost = conf.getFlaskHost() flaskPort = conf.getFlaskPort() flaskDebug = conf.getFlaskDebug() if flaskDebug: #start debug flask server app.run(host=flaskHost, port=flaskPort, debug=flaskDebug) else: # start asynchronous server using tornado wrapper for flask # ssl connection print("Server starting...") if conf.useSSL(): ssl_options = { "certfile": conf.getSSLCert(), "keyfile": conf.getSSLKey() } else:
def start(self, port=None, systems=None): app = Flask(__name__, static_folder='static', static_url_path='/static') # functions # routes @app.route('/') def index(): return render_template('index.html', systems=systems) @app.route('/cve/<cveid>') def cve(cveid): host, port = Configuration.getCVESearch() data = (urlopen('http://%s:%s/api/cve/%s' % (host, port, cveid)).read()).decode('utf8') cvejson = json.loads(str(data)) if cvejson is {}: return page_not_found(404) return render_template('cve.html', cve=cvejson) # error handeling @app.errorhandler(404) def page_not_found(e): return render_template('404.html'), 404 # filters @app.template_filter('product') def product(banner): if banner: r = make_dict(banner) return r['product'] else: return "unknown" @app.template_filter('toHuman') def toHuman(cpe): cpe = cpe[7:] result = cpe.split(':')[0] + " - " for c in cpe.split(':')[1:]: c = c.replace(':', ' ') c = c.replace('_', ' ') result += (" %s" % (c)) result = result.title() return result @app.template_filter('currentTime') def currentTime(utc): output = toLocalTime(utc) return output @app.template_filter('impact') def impact(string): if string.lower() == "none": return "good" elif string.lower() == "partial": return "medium" elif string.lower() == "complete": return "bad" @app.template_filter('vFeedName') def vFeedName(string): string = string.replace('map_', '') string = string.replace('cve_', '') return string.title() @app.template_filter('htmlEncode') def htmlEncode(string): return urllib.parse.quote_plus(string).lower() @app.template_filter('isURL') def isURL(string): urlTypes = [re.escape(x) for x in ['http://', 'https://', 'www.']] return re.match("^(" + "|".join(urlTypes) + ")", string) # debug filter @app.template_filter('type') def isType(var): return type(var) #start webserver host = Configuration.getFlaskHost() port = Configuration.getFlaskPort() debug = Configuration.getFlaskDebug() app.run(host=host, port=port, debug=debug)
deadline = time.time() + MAX_WAIT_SECONDS_BEFORE_SHUTDOWN def stop_loop(): now = time.time() if now < deadline and (io_loop._callbacks or io_loop._timeouts): io_loop.add_timeout(now + 1, stop_loop) else: io_loop.stop() print("Shutdown") stop_loop() if __name__ == "__main__": flaskHost = conf.getFlaskHost() flaskPort = conf.getFlaskPort() flaskDebug = conf.getFlaskDebug() if flaskDebug: # start debug flask server app.run(host=flaskHost, port=flaskPort, debug=flaskDebug) else: # start asynchronous server using tornado wrapper for flask # ssl connection print("Server starting...") if conf.useSSL(): ssl_options = {"certfile": conf.getSSLCert(), "keyfile": conf.getSSLKey()} else: ssl_options = None signal.signal(signal.SIGTERM, sig_handler) signal.signal(signal.SIGINT, sig_handler)
def start(self,port=None,scan=None): app = Flask(__name__, static_folder='static', static_url_path='/static') # functions # routes @app.route('/') def index(): return render_template('index.html', scan=scan) @app.route('/cve/<cveid>') def cve(cveid): host,port=Configuration.getCVESearch() data = (urlopen('http://%s:%s/api/cve/%s'%(host,port,cveid)).read()).decode('utf8') cvejson=json.loads(str(data)) if cvejson is {}: return page_not_found(404) return render_template('cve.html', cve=cvejson) # error handeling @app.errorhandler(404) def page_not_found(e): return render_template('404.html'), 404 # filters @app.template_filter('product') def product(banner): if banner: r=make_dict(banner) return r['product'] if 'product' in r else 'unknown' else: return "unknown" @app.template_filter('toHuman') def humanify(cpe): return toHuman(cpe) @app.template_filter('currentTime') def currentTime(utc): return toLocalTime(utc) @app.template_filter('impact') def impact(string): if string.lower() == "none": return "good" elif string.lower() == "partial": return "medium" elif string.lower() == "complete": return "bad" @app.template_filter('vFeedName') def vFeedName(string): string=string.replace('map_','') string=string.replace('cve_','') return string.title() @app.template_filter('htmlEncode') def htmlEncode(string): return urllib.parse.quote_plus(string).lower() @app.template_filter('isURL') def isURL(string): urlTypes= [re.escape(x) for x in ['http://','https://', 'www.']] return re.match("^(" + "|".join(urlTypes) + ")", string) @app.template_filter('fromEpoch') def fromEpoch_filter(epoch): return fromEpoch(epoch) # debug filter @app.template_filter('type') def isType(var): return type(var) #start webserver host = Configuration.getFlaskHost() port = Configuration.getFlaskPort() debug = Configuration.getFlaskDebug() app.run(host=host, port=port, debug=debug)