pep.py/pep.py

208 lines
7.5 KiB
Python
Raw Normal View History

2016-04-19 17:40:59 +00:00
"""Hello, pep.py here, ex-owner of ripple and prime minister of Ripwot."""
import sys
import os
import threading
2016-04-19 17:40:59 +00:00
# Tornado
import tornado.ioloop
import tornado.web
import tornado.httpserver
import tornado.gen
from gevent import monkey as brit_monkey
brit_monkey.patch_all()
2016-06-15 17:01:00 +00:00
# Raven
from raven.contrib.tornado import AsyncSentryClient
2016-04-19 17:40:59 +00:00
# pep.py files
from constants import bcolors
2016-05-18 17:12:46 +00:00
from helpers import configHelper
from objects import glob
from objects import fokabot
from objects import banchoConfig
2016-08-10 10:00:33 +00:00
from objects import chatFilters
from helpers import consoleHelper
from helpers import databaseHelperNew
2016-05-18 17:12:46 +00:00
from helpers import generalFunctions
from helpers import logHelper as log
2016-06-15 17:01:00 +00:00
2016-06-02 20:33:39 +00:00
from handlers import mainHandler
from handlers import apiIsOnlineHandler
from handlers import apiOnlineUsersHandler
from handlers import apiServerStatusHandler
from handlers import ciTriggerHandler
from handlers import apiVerifiedStatusHandler
from handlers import apiFokabotMessageHandler
2016-06-02 20:33:39 +00:00
from irc import ircserver
2016-06-15 17:01:00 +00:00
def make_app():
return tornado.web.Application([
2016-06-02 20:33:39 +00:00
(r"/", mainHandler.handler),
(r"/api/v1/isOnline", apiIsOnlineHandler.handler),
(r"/api/v1/onlineUsers", apiOnlineUsersHandler.handler),
(r"/api/v1/serverStatus", apiServerStatusHandler.handler),
(r"/api/v1/ciTrigger", ciTriggerHandler.handler),
(r"/api/v1/verifiedStatus", apiVerifiedStatusHandler.handler),
(r"/api/v1/fokabotMessage", apiFokabotMessageHandler.handler)
])
2016-04-19 17:40:59 +00:00
if __name__ == "__main__":
# Server start
consoleHelper.printServerStartHeader(True)
# Read config.ini
consoleHelper.printNoNl("> Loading config file... ")
2016-05-18 17:12:46 +00:00
glob.conf = configHelper.config("config.ini")
2016-04-19 17:40:59 +00:00
2016-09-02 15:45:10 +00:00
if glob.conf.default:
2016-04-19 17:40:59 +00:00
# We have generated a default config.ini, quit server
consoleHelper.printWarning()
consoleHelper.printColored("[!] config.ini not found. A default one has been generated.", bcolors.YELLOW)
consoleHelper.printColored("[!] Please edit your config.ini and run the server again.", bcolors.YELLOW)
sys.exit()
# If we haven't generated a default config.ini, check if it's valid
2016-09-02 15:45:10 +00:00
if not glob.conf.checkConfig():
2016-04-19 17:40:59 +00:00
consoleHelper.printError()
consoleHelper.printColored("[!] Invalid config.ini. Please configure it properly", bcolors.RED)
consoleHelper.printColored("[!] Delete your config.ini to generate a default one", bcolors.RED)
sys.exit()
else:
consoleHelper.printDone()
# Create data folder if needed
consoleHelper.printNoNl("> Checking folders... ")
paths = [".data"]
for i in paths:
if not os.path.exists(i):
os.makedirs(i, 0o770)
consoleHelper.printDone()
2016-04-19 17:40:59 +00:00
# Connect to db
try:
consoleHelper.printNoNl("> Connecting to MySQL db")
glob.db = databaseHelperNew.db(glob.conf.config["db"]["host"], glob.conf.config["db"]["username"], glob.conf.config["db"]["password"], glob.conf.config["db"]["database"], int(glob.conf.config["db"]["workers"]))
consoleHelper.printNoNl(" ")
2016-04-19 17:40:59 +00:00
consoleHelper.printDone()
except:
# Exception while connecting to db
consoleHelper.printError()
consoleHelper.printColored("[!] Error while connection to database. Please check your config.ini and run the server again", bcolors.RED)
raise
# Load bancho_settings
try:
consoleHelper.printNoNl("> Loading bancho settings from DB... ")
glob.banchoConf = banchoConfig.banchoConfig()
consoleHelper.printDone()
except:
consoleHelper.printError()
consoleHelper.printColored("[!] Error while loading bancho_settings. Please make sure the table in DB has all the required rows", bcolors.RED)
raise
2016-06-10 11:15:42 +00:00
# Delete old bancho sessions
consoleHelper.printNoNl("> Deleting cached bancho sessions from DB... ")
glob.tokens.deleteBanchoSessions()
consoleHelper.printDone()
2016-08-08 03:19:52 +00:00
try:
consoleHelper.printNoNl("> Loading chat filters... ")
2016-08-10 10:00:33 +00:00
glob.chatFilters = chatFilters.chatFilters()
2016-08-08 03:19:52 +00:00
consoleHelper.printDone()
except:
consoleHelper.printError()
consoleHelper.printColored("[!] Error while loading chat filters. Make sure there is a filters.txt file present", bcolors.RED)
2016-08-10 10:00:33 +00:00
raise
2016-08-08 03:19:52 +00:00
2016-04-19 17:40:59 +00:00
# Initialize chat channels
print("> Initializing chat channels... ")
2016-04-19 17:40:59 +00:00
glob.channels.loadChannels()
consoleHelper.printDone()
# Start fokabot
consoleHelper.printNoNl("> Connecting FokaBot... ")
fokabot.connect()
consoleHelper.printDone()
# Initialize user timeout check loop
consoleHelper.printNoNl("> Initializing user timeout check loop... ")
glob.tokens.usersTimeoutCheckLoop()
consoleHelper.printDone()
2016-04-19 17:40:59 +00:00
2016-06-10 14:22:14 +00:00
# Initialize spam protection reset loop
consoleHelper.printNoNl("> Initializing spam protection reset loop... ")
glob.tokens.spamProtectionResetLoop()
consoleHelper.printDone()
2016-09-02 15:16:22 +00:00
# Cache user ids
2016-09-02 15:45:10 +00:00
#consoleHelper.printNoNl("> Caching user IDs... ")
#userHelper.cacheUserIDs()
#consoleHelper.printDone()
2016-09-02 15:16:22 +00:00
2016-04-19 17:40:59 +00:00
# Localize warning
glob.localize = generalFunctions.stringToBool(glob.conf.config["localize"]["enable"])
2016-09-02 15:45:10 +00:00
if not glob.localize:
consoleHelper.printColored("[!] Warning! Users localization is disabled!", bcolors.YELLOW)
2016-04-19 17:40:59 +00:00
2016-06-02 17:22:02 +00:00
# Discord
glob.discord = generalFunctions.stringToBool(glob.conf.config["discord"]["enable"])
2016-09-02 15:45:10 +00:00
if not glob.discord:
consoleHelper.printColored("[!] Warning! Discord logging is disabled!", bcolors.YELLOW)
# Gzip
glob.gzip = generalFunctions.stringToBool(glob.conf.config["server"]["gzip"])
glob.gziplevel = int(glob.conf.config["server"]["gziplevel"])
2016-09-02 15:45:10 +00:00
if not glob.gzip:
consoleHelper.printColored("[!] Warning! Gzip compression is disabled!", bcolors.YELLOW)
# Debug mode
glob.debug = generalFunctions.stringToBool(glob.conf.config["debug"]["enable"])
glob.outputPackets = generalFunctions.stringToBool(glob.conf.config["debug"]["packets"])
glob.outputRequestTime = generalFunctions.stringToBool(glob.conf.config["debug"]["time"])
2016-09-02 15:45:10 +00:00
if glob.debug:
consoleHelper.printColored("[!] Warning! Server running in debug mode!", bcolors.YELLOW)
2016-07-14 13:38:28 +00:00
# Make app
application = make_app()
# Set up sentry
try:
glob.sentry = generalFunctions.stringToBool(glob.conf.config["sentry"]["enable"])
2016-09-02 15:45:10 +00:00
if glob.sentry:
2016-07-14 13:38:28 +00:00
application.sentry_client = AsyncSentryClient(glob.conf.config["sentry"]["banchodns"], release=glob.VERSION)
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)
# Cloudflare memes
glob.cloudflare = generalFunctions.stringToBool(glob.conf.config["server"]["cloudflare"])
# IRC start message and console output
glob.irc = generalFunctions.stringToBool(glob.conf.config["irc"]["enable"])
2016-09-02 15:45:10 +00:00
if glob.irc:
# IRC port
try:
ircPort = int(glob.conf.config["irc"]["port"])
except:
consoleHelper.printColored("[!] Invalid IRC port! Please check your config.ini and run the server again", bcolors.RED)
log.logMessage("IRC server started!", discord=True, of="info.txt", stdout=False)
consoleHelper.printColored("> IRC server listening on 127.0.0.1:{}...".format(ircPort), bcolors.GREEN)
threading.Thread(target=lambda: ircserver.main(port=ircPort)).start()
else:
consoleHelper.printColored("[!] Warning! IRC server is disabled!", bcolors.YELLOW)
# Server port
try:
serverPort = int(glob.conf.config["server"]["port"])
except:
consoleHelper.printColored("[!] Invalid server port! Please check your config.ini and run the server again", bcolors.RED)
2016-04-19 17:40:59 +00:00
# Server start message and console output
log.logMessage("Server started!", discord=True, of="info.txt", stdout=False)
consoleHelper.printColored("> Tornado listening for HTTP(s) clients on 127.0.0.1:{}...".format(serverPort), bcolors.GREEN)
# Start tornado
2016-06-15 17:01:00 +00:00
application.listen(serverPort)
tornado.ioloop.IOLoop.instance().start()