2016-04-19 17:40:59 +00:00
|
|
|
"""Hello, pep.py here, ex-owner of ripple and prime minister of Ripwot."""
|
|
|
|
import sys
|
2016-05-19 20:53:09 +00:00
|
|
|
import os
|
2016-05-31 20:49:30 +00:00
|
|
|
from multiprocessing.pool import ThreadPool
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-05-31 20:49:30 +00:00
|
|
|
# Tornado
|
|
|
|
import tornado.ioloop
|
|
|
|
import tornado.web
|
|
|
|
import tornado.httpserver
|
|
|
|
import tornado.gen
|
2016-04-19 17:40:59 +00:00
|
|
|
|
|
|
|
# pep.py files
|
2016-05-17 21:40:34 +00:00
|
|
|
from constants import bcolors
|
2016-05-18 17:12:46 +00:00
|
|
|
from helpers import configHelper
|
2016-05-19 20:53:09 +00:00
|
|
|
from helpers import discordBotHelper
|
2016-05-18 17:12:46 +00:00
|
|
|
from objects import glob
|
|
|
|
from objects import fokabot
|
|
|
|
from objects import banchoConfig
|
2016-05-31 20:49:30 +00:00
|
|
|
from handlers import mainHandler
|
2016-05-17 21:40:34 +00:00
|
|
|
from helpers import consoleHelper
|
2016-05-31 20:49:30 +00:00
|
|
|
from helpers import databaseHelperNew
|
2016-05-18 17:12:46 +00:00
|
|
|
from helpers import generalFunctions
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-05-31 20:49:30 +00:00
|
|
|
def make_app():
|
|
|
|
return tornado.web.Application([
|
|
|
|
(r"/", mainHandler.handler)
|
|
|
|
])
|
2016-04-19 17:40:59 +00:00
|
|
|
|
|
|
|
# Ci trigger
|
2016-05-31 20:49:30 +00:00
|
|
|
'''@app.route("/ci-trigger")
|
2016-04-19 17:40:59 +00:00
|
|
|
@app.route("/api/ci-trigger")
|
|
|
|
def ciTrigger():
|
|
|
|
# Ci restart trigger
|
|
|
|
|
|
|
|
# Get ket from GET
|
|
|
|
key = flask.request.args.get('k')
|
|
|
|
|
|
|
|
# Get request ip
|
|
|
|
requestIP = flask.request.headers.get('X-Real-IP')
|
|
|
|
if requestIP == None:
|
|
|
|
requestIP = flask.request.remote_addr
|
|
|
|
|
|
|
|
# Check key
|
|
|
|
if key is None or key != glob.conf.config["ci"]["key"]:
|
|
|
|
consoleHelper.printColored("[!] Invalid ci trigger from {}".format(requestIP), bcolors.RED)
|
|
|
|
return flask.jsonify({"response" : "-1"})
|
|
|
|
|
|
|
|
# Ci event triggered, schedule server shutdown
|
|
|
|
consoleHelper.printColored("[!] Ci event triggered from {}".format(requestIP), bcolors.PINK)
|
|
|
|
systemHelper.scheduleShutdown(5, False, "A new Bancho update is available and the server will be restarted in 5 seconds. Thank you for your patience.")
|
|
|
|
|
|
|
|
return flask.jsonify({"response" : 1})
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/api/server-status")
|
|
|
|
def serverStatus():
|
|
|
|
# Server status api
|
|
|
|
# 1: Online
|
|
|
|
# -1: Restarting
|
|
|
|
return flask.jsonify({
|
|
|
|
"response" : 200,
|
|
|
|
"status" : -1 if glob.restarting == True else 1
|
|
|
|
})
|
|
|
|
|
2016-05-30 20:03:47 +00:00
|
|
|
@app.route("/api/v1/isOnline")
|
2016-05-31 20:33:24 +00:00
|
|
|
def apiIsOnline():
|
2016-05-30 20:03:47 +00:00
|
|
|
username = request.args.get("u")
|
|
|
|
if username == None:
|
|
|
|
result = False
|
|
|
|
else:
|
|
|
|
token = glob.tokens.getTokenFromUsername(username)
|
|
|
|
result = True if token != None else False
|
|
|
|
return flask.jsonify({
|
|
|
|
"result": result
|
|
|
|
})
|
|
|
|
|
2016-05-31 20:33:24 +00:00
|
|
|
@app.route("/api/v1/onlineUsers")
|
|
|
|
def apiOnlineUsers():
|
|
|
|
return flask.jsonify({
|
|
|
|
"result": len(glob.tokens.tokens)
|
|
|
|
})
|
2016-04-19 17:40:59 +00:00
|
|
|
|
|
|
|
# Main bancho server
|
|
|
|
@app.route("/", methods=['GET', 'POST'])
|
|
|
|
def banchoServer():
|
|
|
|
if flask.request.method == 'POST':
|
|
|
|
|
2016-05-31 20:49:30 +00:00
|
|
|
|
2016-04-19 17:40:59 +00:00
|
|
|
else:
|
|
|
|
# Not a POST request, send html page
|
2016-05-31 20:49:30 +00:00
|
|
|
return responseHelper.HTMLResponse()'''
|
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
|
|
|
|
|
|
|
if glob.conf.default == True:
|
|
|
|
# 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
|
|
|
|
if glob.conf.checkConfig() == False:
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
# Connect to db
|
|
|
|
try:
|
2016-05-31 20:49:30 +00:00
|
|
|
print("> 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"]))
|
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
|
|
|
|
|
2016-05-31 20:49:30 +00:00
|
|
|
# Create threads pool
|
|
|
|
try:
|
|
|
|
consoleHelper.printNoNl("> Creating threads pool... ")
|
|
|
|
glob.pool = ThreadPool(int(glob.conf.config["server"]["threads"]))
|
|
|
|
consoleHelper.printDone()
|
|
|
|
except:
|
|
|
|
consoleHelper.printError()
|
|
|
|
consoleHelper.printColored("[!] Error while creating threads pool. Please check your config.ini and run the server again", bcolors.RED)
|
|
|
|
|
2016-04-19 17:40:59 +00:00
|
|
|
# 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-05-19 20:53:09 +00:00
|
|
|
# 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
|
|
|
# Initialize chat channels
|
|
|
|
consoleHelper.printNoNl("> Initializing chat channels... ")
|
|
|
|
glob.channels.loadChannels()
|
|
|
|
consoleHelper.printDone()
|
|
|
|
|
|
|
|
# Start fokabot
|
|
|
|
consoleHelper.printNoNl("> Connecting FokaBot... ")
|
|
|
|
fokabot.connect()
|
|
|
|
consoleHelper.printDone()
|
|
|
|
|
|
|
|
# Initialize user timeout check loop
|
|
|
|
try:
|
|
|
|
consoleHelper.printNoNl("> Initializing user timeout check loop... ")
|
|
|
|
glob.tokens.usersTimeoutCheckLoop(int(glob.conf.config["server"]["timeouttime"]), int(glob.conf.config["server"]["timeoutlooptime"]))
|
|
|
|
consoleHelper.printDone()
|
|
|
|
except:
|
|
|
|
consoleHelper.printError()
|
|
|
|
consoleHelper.printColored("[!] Error while initializing user timeout check loop", bcolors.RED)
|
|
|
|
consoleHelper.printColored("[!] Make sure that 'timeouttime' and 'timeoutlooptime' in config.ini are numbers", bcolors.RED)
|
|
|
|
raise
|
|
|
|
|
|
|
|
# Localize warning
|
2016-06-02 17:22:02 +00:00
|
|
|
if generalFunctions.stringToBool(glob.conf.config["server"]["localizeusers"]) == False:
|
2016-04-19 17:40:59 +00:00
|
|
|
consoleHelper.printColored("[!] Warning! users localization is disabled!", bcolors.YELLOW)
|
|
|
|
|
2016-06-02 17:22:02 +00:00
|
|
|
# Discord
|
|
|
|
glob.discord = generalFunctions.stringToBool(glob.conf.config["discord"]["enable"])
|
|
|
|
if glob.discord == False:
|
|
|
|
consoleHelper.printColored("[!] Discord logging is disabled!", bcolors.YELLOW)
|
|
|
|
|
2016-04-19 17:40:59 +00:00
|
|
|
# Get server parameters from config.ini
|
|
|
|
serverPort = int(glob.conf.config["server"]["port"])
|
2016-05-31 20:49:30 +00:00
|
|
|
glob.requestTime = generalFunctions.stringToBool(glob.conf.config["server"]["outputrequesttime"])
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-05-31 20:49:30 +00:00
|
|
|
# Server start message and console output
|
2016-06-02 17:22:02 +00:00
|
|
|
discordBotHelper.sendConfidential("Server started!")
|
2016-05-31 20:49:30 +00:00
|
|
|
consoleHelper.printColored("> Tornado listening for clients on 127.0.0.1:{}...".format(serverPort), bcolors.GREEN)
|
2016-05-19 20:53:09 +00:00
|
|
|
|
2016-05-31 20:49:30 +00:00
|
|
|
# Start tornado
|
|
|
|
app = tornado.httpserver.HTTPServer(make_app())
|
|
|
|
app.listen(serverPort)
|
2016-06-01 18:58:42 +00:00
|
|
|
tornado.ioloop.IOLoop.instance().start()
|