2016-05-18 17:12:46 +00:00
from helpers import userHelper
from constants import serverPackets
from constants import exceptions
from objects import glob
2016-05-17 21:40:34 +00:00
from helpers import consoleHelper
from constants import bcolors
2016-05-18 17:12:46 +00:00
from helpers import locationHelper
from helpers import countryHelper
2016-04-19 17:40:59 +00:00
import time
2016-05-18 17:12:46 +00:00
from helpers import generalFunctions
from events import channelJoinEvent
2016-05-17 21:40:34 +00:00
import sys
import traceback
2016-05-31 20:49:30 +00:00
from helpers import requestHelper
2016-06-02 17:22:02 +00:00
from helpers import discordBotHelper
2016-06-04 10:44:54 +00:00
from helpers import logHelper as log
2016-04-19 17:40:59 +00:00
2016-05-31 20:49:30 +00:00
def handle ( tornadoRequest ) :
2016-04-19 17:40:59 +00:00
# Data to return
responseTokenString = " ayy "
responseData = bytes ( )
2016-06-04 10:44:54 +00:00
# Get IP from tornado request
2016-05-31 20:49:30 +00:00
requestIP = tornadoRequest . getRequestIP ( )
2016-04-19 17:40:59 +00:00
# Split POST body so we can get username/password/hardware data
# 2:-3 thing is because requestData has some escape stuff that we don't need
2016-05-31 20:49:30 +00:00
loginData = str ( tornadoRequest . request . body ) [ 2 : - 3 ] . split ( " \\ n " )
2016-04-19 17:40:59 +00:00
try :
# If true, print error to console
err = False
# Try to get the ID from username
userID = userHelper . getID ( str ( loginData [ 0 ] ) )
if userID == False :
# Invalid username
raise exceptions . loginFailedException ( )
if userHelper . checkLogin ( userID , loginData [ 1 ] ) == False :
# Invalid password
raise exceptions . loginFailedException ( )
# Make sure we are not banned
userAllowed = userHelper . getAllowed ( userID )
if userAllowed == 0 :
# Banned
raise exceptions . loginBannedException ( )
2016-06-11 16:43:27 +00:00
# 2FA check
if userHelper . check2FA ( userID , requestIP ) == True :
log . warning ( " Need 2FA check for user {} " . format ( loginData [ 0 ] ) )
raise exceptions . need2FAException ( )
2016-06-10 11:15:42 +00:00
# No login errors!
2016-05-29 11:32:11 +00:00
# Log user IP
userHelper . IPLog ( userID , requestIP )
2016-04-19 17:40:59 +00:00
# Delete old tokens for that user and generate a new one
glob . tokens . deleteOldTokens ( userID )
2016-06-10 11:15:42 +00:00
responseToken = glob . tokens . addToken ( userID , requestIP )
2016-04-19 17:40:59 +00:00
responseTokenString = responseToken . token
2016-06-09 08:43:28 +00:00
# Set silence end UNIX time in token
responseToken . silenceEndTime = userHelper . getSilenceEnd ( userID )
# Get only silence remaining seconds
silenceSeconds = responseToken . getSilenceSecondsLeft ( )
2016-04-19 17:40:59 +00:00
# Get supporter/GMT
userRank = userHelper . getRankPrivileges ( userID )
userGMT = False
userSupporter = True
if userRank > = 3 :
userGMT = True
# Server restarting check
if glob . restarting == True :
raise exceptions . banchoRestartingException ( )
2016-06-09 08:52:39 +00:00
# Send login notification before maintenance message
if glob . banchoConf . config [ " loginNotification " ] != " " :
responseToken . enqueue ( serverPackets . notification ( glob . banchoConf . config [ " loginNotification " ] ) )
2016-04-19 17:40:59 +00:00
# Maintenance check
if glob . banchoConf . config [ " banchoMaintenance " ] == True :
if userGMT == False :
# We are not mod/admin, delete token, send notification and logout
glob . tokens . deleteToken ( responseTokenString )
raise exceptions . banchoMaintenanceException ( )
else :
# We are mod/admin, send warning notification and continue
responseToken . enqueue ( serverPackets . notification ( " Bancho is in maintenance mode. Only mods/admins have full access to the server. \n Type !system maintenance off in chat to turn off maintenance mode. " ) )
# Send all needed login packets
2016-06-09 08:43:28 +00:00
responseToken . enqueue ( serverPackets . silenceEndTime ( silenceSeconds ) )
2016-04-19 17:40:59 +00:00
responseToken . enqueue ( serverPackets . userID ( userID ) )
responseToken . enqueue ( serverPackets . protocolVersion ( ) )
responseToken . enqueue ( serverPackets . userSupporterGMT ( userSupporter , userGMT ) )
responseToken . enqueue ( serverPackets . userPanel ( userID ) )
responseToken . enqueue ( serverPackets . userStats ( userID ) )
# Channel info end (before starting!?! wtf bancho?)
responseToken . enqueue ( serverPackets . channelInfoEnd ( ) )
# Default opened channels
# TODO: Configurable default channels
channelJoinEvent . joinChannel ( responseToken , " #osu " )
channelJoinEvent . joinChannel ( responseToken , " #announce " )
if userRank > = 3 :
# Join admin chanenl if we are mod/admin
# TODO: Separate channels for mods and admins
channelJoinEvent . joinChannel ( responseToken , " #admin " )
# Output channels info
for key , value in glob . channels . channels . items ( ) :
if value . publicRead == True :
responseToken . enqueue ( serverPackets . channelInfo ( key ) )
2016-06-09 08:43:28 +00:00
# Send friends list
2016-04-19 17:40:59 +00:00
responseToken . enqueue ( serverPackets . friendList ( userID ) )
2016-06-09 08:52:39 +00:00
# Send main menu icon
2016-04-19 17:40:59 +00:00
if glob . banchoConf . config [ " menuIcon " ] != " " :
responseToken . enqueue ( serverPackets . mainMenuIcon ( glob . banchoConf . config [ " menuIcon " ] ) )
# Get everyone else userpanel
# TODO: Better online users handling
2016-06-16 11:38:17 +00:00
#for key, value in glob.tokens.tokens.items():
# responseToken.enqueue(serverPackets.userPanel(value.userID))
# responseToken.enqueue(serverPackets.userStats(value.userID))
2016-04-19 17:40:59 +00:00
# Send online users IDs array
responseToken . enqueue ( serverPackets . onlineUsers ( ) )
# Get location and country from ip.zxq.co or database
2016-06-04 10:44:54 +00:00
if glob . localize == True :
2016-05-12 17:22:13 +00:00
# Get location and country from IP
location = locationHelper . getLocation ( requestIP )
countryLetters = locationHelper . getCountry ( requestIP )
country = countryHelper . getCountryID ( countryLetters )
2016-04-19 17:40:59 +00:00
else :
# Set location to 0,0 and get country from db
2016-06-04 10:44:54 +00:00
log . warning ( " Location skipped " )
2016-04-19 17:40:59 +00:00
location = [ 0 , 0 ]
2016-05-01 18:39:01 +00:00
countryLetters = " XX "
2016-04-19 17:40:59 +00:00
country = countryHelper . getCountryID ( userHelper . getCountry ( userID ) )
# Set location and country
responseToken . setLocation ( location )
responseToken . setCountry ( country )
2016-05-01 16:09:35 +00:00
# Set country in db if user has no country (first bancho login)
if userHelper . getCountry ( userID ) == " XX " :
userHelper . setCountry ( userID , countryLetters )
2016-04-19 17:40:59 +00:00
# Send to everyone our userpanel and userStats (so they now we have logged in)
glob . tokens . enqueueAll ( serverPackets . userPanel ( userID ) )
glob . tokens . enqueueAll ( serverPackets . userStats ( userID ) )
# Set reponse data to right value and reset our queue
responseData = responseToken . queue
responseToken . resetQueue ( )
except exceptions . loginFailedException :
# Login failed error packet
# (we don't use enqueue because we don't have a token since login has failed)
err = True
responseData + = serverPackets . loginFailed ( )
except exceptions . loginBannedException :
# Login banned error packet
err = True
responseData + = serverPackets . loginBanned ( )
except exceptions . banchoMaintenanceException :
# Bancho is in maintenance mode
responseData + = serverPackets . notification ( " Our bancho server is in maintenance mode. Please try to login again later. " )
2016-06-11 16:43:27 +00:00
responseData + = serverPackets . loginFailed ( )
2016-04-19 17:40:59 +00:00
except exceptions . banchoRestartingException :
# Bancho is restarting
responseData + = serverPackets . notification ( " Bancho is restarting. Try again in a few minutes. " )
2016-06-11 16:43:27 +00:00
responseData + = serverPackets . loginFailed ( )
except exceptions . need2FAException :
# User tried to log in from unknown IP
responseData + = serverPackets . needVerification ( )
2016-04-19 17:40:59 +00:00
finally :
2016-06-04 10:44:54 +00:00
# Console and discord log
msg = " Bancho login request from {} for user {} ( {} ) " . format ( requestIP , loginData [ 0 ] , " failed " if err == True else " success " )
log . info ( msg , True )
2016-06-02 17:22:02 +00:00
# Return token string and data
2016-04-19 17:40:59 +00:00
return ( responseTokenString , responseData )