.HIDE. General refactoring

This commit is contained in:
Nyo 2016-12-26 10:33:05 +01:00
parent 501130721d
commit 62b67da9fb
26 changed files with 215 additions and 219 deletions

View File

@ -127,7 +127,7 @@ def kick(fro, chan, message):
return "Nope." return "Nope."
# Get target token and make sure is connected # Get target token and make sure is connected
tokens = glob.tokens.getTokenFromUsername(target, all=True) tokens = glob.tokens.getTokenFromUsername(target, _all=True)
if len(tokens) == 0: if len(tokens) == 0:
return "{} is not online".format(target) return "{} is not online".format(target)

View File

@ -1,6 +1,4 @@
from common.constants import actions
from common.log import logUtils as log from common.log import logUtils as log
from common.ripple import userUtils
from constants import clientPackets from constants import clientPackets
from constants import serverPackets from constants import serverPackets
from objects import glob from objects import glob

View File

@ -1,7 +1,6 @@
from common.log import logUtils as log from common.log import logUtils as log
from constants import clientPackets from constants import clientPackets
from constants import exceptions from constants import exceptions
from constants import serverPackets
from objects import glob from objects import glob

View File

@ -1,4 +1,3 @@
from common import generalUtils
from common.log import logUtils as log from common.log import logUtils as log
from constants import clientPackets from constants import clientPackets
from constants import exceptions from constants import exceptions

View File

@ -15,6 +15,7 @@ from objects import glob
def handle(tornadoRequest): def handle(tornadoRequest):
# Data to return # Data to return
responseToken = None
responseTokenString = "ayy" responseTokenString = "ayy"
responseData = bytes() responseData = bytes()
@ -29,9 +30,6 @@ def handle(tornadoRequest):
# 2:-3 thing is because requestData has some escape stuff that we don't need # 2:-3 thing is because requestData has some escape stuff that we don't need
loginData = str(tornadoRequest.request.body)[2:-3].split("\\n") loginData = str(tornadoRequest.request.body)[2:-3].split("\\n")
try: try:
# If true, print error to console
err = False
# Make sure loginData is valid # Make sure loginData is valid
if len(loginData) < 3: if len(loginData) < 3:
raise exceptions.invalidArgumentsException() raise exceptions.invalidArgumentsException()
@ -220,25 +218,23 @@ def handle(tornadoRequest):
except exceptions.loginFailedException: except exceptions.loginFailedException:
# Login failed error packet # Login failed error packet
# (we don't use enqueue because we don't have a token since login has failed) # (we don't use enqueue because we don't have a token since login has failed)
err = True
responseData += serverPackets.loginFailed() responseData += serverPackets.loginFailed()
except exceptions.invalidArgumentsException: except exceptions.invalidArgumentsException:
# Invalid POST data # Invalid POST data
# (we don't use enqueue because we don't have a token since login has failed) # (we don't use enqueue because we don't have a token since login has failed)
err = True
responseData += serverPackets.loginFailed() responseData += serverPackets.loginFailed()
responseData += serverPackets.notification("I see what you're doing...") responseData += serverPackets.notification("I see what you're doing...")
except exceptions.loginBannedException: except exceptions.loginBannedException:
# Login banned error packet # Login banned error packet
err = True
responseData += serverPackets.loginBanned() responseData += serverPackets.loginBanned()
except exceptions.loginLockedException: except exceptions.loginLockedException:
# Login banned error packet # Login banned error packet
err = True
responseData += serverPackets.loginLocked() responseData += serverPackets.loginLocked()
except exceptions.banchoMaintenanceException: except exceptions.banchoMaintenanceException:
# Bancho is in maintenance mode # Bancho is in maintenance mode
responseData = responseToken.queue responseData = bytes()
if responseToken is not None:
responseData = responseToken.queue
responseData += serverPackets.notification("Our bancho server is in maintenance mode. Please try to login again later.") responseData += serverPackets.notification("Our bancho server is in maintenance mode. Please try to login again later.")
responseData += serverPackets.loginFailed() responseData += serverPackets.loginFailed()
except exceptions.banchoRestartingException: except exceptions.banchoRestartingException:
@ -251,7 +247,6 @@ def handle(tornadoRequest):
except exceptions.haxException: except exceptions.haxException:
# Using oldoldold client, we don't have client data. Force update. # Using oldoldold client, we don't have client data. Force update.
# (we don't use enqueue because we don't have a token since login has failed) # (we don't use enqueue because we don't have a token since login has failed)
err = True
responseData += serverPackets.forceUpdate() responseData += serverPackets.forceUpdate()
responseData += serverPackets.notification("Hory shitto, your client is TOO old! Nice prehistory! Please turn update it from the settings!") responseData += serverPackets.notification("Hory shitto, your client is TOO old! Nice prehistory! Please turn update it from the settings!")
except: except:

View File

@ -1,6 +1,6 @@
from objects import glob from objects import glob
def handle(userToken, packetData, has): def handle(userToken, _, has):
# Get usertoken data # Get usertoken data
userID = userToken.userID userID = userToken.userID

View File

@ -1,6 +1,6 @@
from objects import glob from objects import glob
def handle(userToken, packetData): def handle(userToken, _):
# Get usertoken data # Get usertoken data
userID = userToken.userID userID = userToken.userID

View File

@ -1,5 +1,4 @@
from objects import glob from objects import glob
from constants import slotStatuses
from constants import serverPackets from constants import serverPackets
def handle(userToken, packetData): def handle(userToken, packetData):

View File

@ -1,6 +1,6 @@
from objects import glob from objects import glob
def handle(userToken, packetData): def handle(userToken, _):
# Get userToken data # Get userToken data
userID = userToken.userID userID = userToken.userID

View File

@ -1,6 +1,6 @@
from objects import glob from objects import glob
def handle(userToken, packetData): def handle(userToken, _):
# Get userToken data # Get userToken data
userID = userToken.userID userID = userToken.userID

View File

@ -1,6 +1,4 @@
from objects import glob from objects import glob
from constants import slotStatuses
from constants import serverPackets
def handle(userToken, _): def handle(userToken, _):

View File

@ -1,7 +1,5 @@
from common.log import logUtils as log from common.log import logUtils as log
from helpers import chatHelper as chat from helpers import chatHelper as chat
from objects import glob
def handle(userToken, _): def handle(userToken, _):
# Get usertoken data # Get usertoken data

View File

@ -1,6 +1,5 @@
from objects import glob from objects import glob
from constants import serverPackets from constants import serverPackets
from constants import exceptions
def handle(userToken, packetData): def handle(userToken, packetData):
# get token data # get token data

View File

@ -1,5 +1,4 @@
from constants import clientPackets from constants import clientPackets
from constants import serverPackets
from objects import glob from objects import glob
def handle(userToken, packetData): def handle(userToken, packetData):

View File

@ -58,190 +58,190 @@ from helpers import packetHelper
from objects import glob from objects import glob
class handler(SentryMixin, requestsManager.asyncRequestHandler): class handler(requestsManager.asyncRequestHandler):
@tornado.web.asynchronous @tornado.web.asynchronous
@tornado.gen.engine @tornado.gen.engine
def asyncPost(self): def asyncPost(self):
try: #try:
# Track time if needed # Track time if needed
if glob.outputRequestTime: if glob.outputRequestTime:
# Start time # Start time
st = datetime.datetime.now() st = datetime.datetime.now()
# Client's token string and request data # Client's token string and request data
requestTokenString = self.request.headers.get("osu-token") requestTokenString = self.request.headers.get("osu-token")
requestData = self.request.body requestData = self.request.body
# Server's token string and request data # Server's token string and request data
responseTokenString = "ayy" responseTokenString = "ayy"
responseData = bytes() responseData = bytes()
if requestTokenString is None: if requestTokenString is None:
# No token, first request. Handle login. # No token, first request. Handle login.
responseTokenString, responseData = loginEvent.handle(self) responseTokenString, responseData = loginEvent.handle(self)
else: else:
userToken = None # default value userToken = None # default value
try: try:
# This is not the first packet, send response based on client's request # This is not the first packet, send response based on client's request
# Packet start position, used to read stacked packets # Packet start position, used to read stacked packets
pos = 0 pos = 0
# Make sure the token exists # Make sure the token exists
if requestTokenString not in glob.tokens.tokens: if requestTokenString not in glob.tokens.tokens:
raise exceptions.tokenNotFoundException() raise exceptions.tokenNotFoundException()
# Token exists, get its object and lock it # Token exists, get its object and lock it
userToken = glob.tokens.tokens[requestTokenString] userToken = glob.tokens.tokens[requestTokenString]
userToken.lock.acquire() userToken.lock.acquire()
# Keep reading packets until everything has been read # Keep reading packets until everything has been read
while pos < len(requestData): while pos < len(requestData):
# Get packet from stack starting from new packet # Get packet from stack starting from new packet
leftData = requestData[pos:] leftData = requestData[pos:]
# Get packet ID, data length and data # Get packet ID, data length and data
packetID = packetHelper.readPacketID(leftData) packetID = packetHelper.readPacketID(leftData)
dataLength = packetHelper.readPacketLength(leftData) dataLength = packetHelper.readPacketLength(leftData)
packetData = requestData[pos:(pos+dataLength+7)] packetData = requestData[pos:(pos+dataLength+7)]
# Console output if needed # Console output if needed
if glob.outputPackets == True and packetID != 4: if glob.outputPackets == True and packetID != 4:
log.debug("Incoming packet ({})({}):\n\nPacket code: {}\nPacket length: {}\nSingle packet data: {}\n".format(requestTokenString, userToken.username, str(packetID), str(dataLength), str(packetData))) log.debug("Incoming packet ({})({}):\n\nPacket code: {}\nPacket length: {}\nSingle packet data: {}\n".format(requestTokenString, userToken.username, str(packetID), str(dataLength), str(packetData)))
# Event handler # Event handler
def handleEvent(ev): def handleEvent(ev):
def wrapper(): def wrapper():
ev.handle(userToken, packetData) ev.handle(userToken, packetData)
return wrapper return wrapper
eventHandler = { eventHandler = {
packetIDs.client_changeAction: handleEvent(changeActionEvent), packetIDs.client_changeAction: handleEvent(changeActionEvent),
packetIDs.client_logout: handleEvent(logoutEvent), packetIDs.client_logout: handleEvent(logoutEvent),
packetIDs.client_friendAdd: handleEvent(friendAddEvent), packetIDs.client_friendAdd: handleEvent(friendAddEvent),
packetIDs.client_friendRemove: handleEvent(friendRemoveEvent), packetIDs.client_friendRemove: handleEvent(friendRemoveEvent),
packetIDs.client_userStatsRequest: handleEvent(userStatsRequestEvent), packetIDs.client_userStatsRequest: handleEvent(userStatsRequestEvent),
packetIDs.client_requestStatusUpdate: handleEvent(requestStatusUpdateEvent), packetIDs.client_requestStatusUpdate: handleEvent(requestStatusUpdateEvent),
packetIDs.client_userPanelRequest: handleEvent(userPanelRequestEvent), packetIDs.client_userPanelRequest: handleEvent(userPanelRequestEvent),
packetIDs.client_channelJoin: handleEvent(channelJoinEvent), packetIDs.client_channelJoin: handleEvent(channelJoinEvent),
packetIDs.client_channelPart: handleEvent(channelPartEvent), packetIDs.client_channelPart: handleEvent(channelPartEvent),
packetIDs.client_sendPublicMessage: handleEvent(sendPublicMessageEvent), packetIDs.client_sendPublicMessage: handleEvent(sendPublicMessageEvent),
packetIDs.client_sendPrivateMessage: handleEvent(sendPrivateMessageEvent), packetIDs.client_sendPrivateMessage: handleEvent(sendPrivateMessageEvent),
packetIDs.client_setAwayMessage: handleEvent(setAwayMessageEvent), packetIDs.client_setAwayMessage: handleEvent(setAwayMessageEvent),
packetIDs.client_startSpectating: handleEvent(startSpectatingEvent), packetIDs.client_startSpectating: handleEvent(startSpectatingEvent),
packetIDs.client_stopSpectating: handleEvent(stopSpectatingEvent), packetIDs.client_stopSpectating: handleEvent(stopSpectatingEvent),
packetIDs.client_cantSpectate: handleEvent(cantSpectateEvent), packetIDs.client_cantSpectate: handleEvent(cantSpectateEvent),
packetIDs.client_spectateFrames: handleEvent(spectateFramesEvent), packetIDs.client_spectateFrames: handleEvent(spectateFramesEvent),
packetIDs.client_joinLobby: handleEvent(joinLobbyEvent), packetIDs.client_joinLobby: handleEvent(joinLobbyEvent),
packetIDs.client_partLobby: handleEvent(partLobbyEvent), packetIDs.client_partLobby: handleEvent(partLobbyEvent),
packetIDs.client_createMatch: handleEvent(createMatchEvent), packetIDs.client_createMatch: handleEvent(createMatchEvent),
packetIDs.client_joinMatch: handleEvent(joinMatchEvent), packetIDs.client_joinMatch: handleEvent(joinMatchEvent),
packetIDs.client_partMatch: handleEvent(partMatchEvent), packetIDs.client_partMatch: handleEvent(partMatchEvent),
packetIDs.client_matchChangeSlot: handleEvent(changeSlotEvent), packetIDs.client_matchChangeSlot: handleEvent(changeSlotEvent),
packetIDs.client_matchChangeSettings: handleEvent(changeMatchSettingsEvent), packetIDs.client_matchChangeSettings: handleEvent(changeMatchSettingsEvent),
packetIDs.client_matchChangePassword: handleEvent(changeMatchPasswordEvent), packetIDs.client_matchChangePassword: handleEvent(changeMatchPasswordEvent),
packetIDs.client_matchChangeMods: handleEvent(changeMatchModsEvent), packetIDs.client_matchChangeMods: handleEvent(changeMatchModsEvent),
packetIDs.client_matchReady: handleEvent(matchReadyEvent), packetIDs.client_matchReady: handleEvent(matchReadyEvent),
packetIDs.client_matchNotReady: handleEvent(matchReadyEvent), packetIDs.client_matchNotReady: handleEvent(matchReadyEvent),
packetIDs.client_matchLock: handleEvent(matchLockEvent), packetIDs.client_matchLock: handleEvent(matchLockEvent),
packetIDs.client_matchStart: handleEvent(matchStartEvent), packetIDs.client_matchStart: handleEvent(matchStartEvent),
packetIDs.client_matchLoadComplete: handleEvent(matchPlayerLoadEvent), packetIDs.client_matchLoadComplete: handleEvent(matchPlayerLoadEvent),
packetIDs.client_matchSkipRequest: handleEvent(matchSkipEvent), packetIDs.client_matchSkipRequest: handleEvent(matchSkipEvent),
packetIDs.client_matchScoreUpdate: handleEvent(matchFramesEvent), packetIDs.client_matchScoreUpdate: handleEvent(matchFramesEvent),
packetIDs.client_matchComplete: handleEvent(matchCompleteEvent), packetIDs.client_matchComplete: handleEvent(matchCompleteEvent),
packetIDs.client_matchNoBeatmap: handleEvent(matchNoBeatmapEvent), packetIDs.client_matchNoBeatmap: handleEvent(matchNoBeatmapEvent),
packetIDs.client_matchHasBeatmap: handleEvent(matchHasBeatmapEvent), packetIDs.client_matchHasBeatmap: handleEvent(matchHasBeatmapEvent),
packetIDs.client_matchTransferHost: handleEvent(matchTransferHostEvent), packetIDs.client_matchTransferHost: handleEvent(matchTransferHostEvent),
packetIDs.client_matchFailed: handleEvent(matchFailedEvent), packetIDs.client_matchFailed: handleEvent(matchFailedEvent),
packetIDs.client_matchChangeTeam: handleEvent(matchChangeTeamEvent), packetIDs.client_matchChangeTeam: handleEvent(matchChangeTeamEvent),
packetIDs.client_invite: handleEvent(matchInviteEvent), packetIDs.client_invite: handleEvent(matchInviteEvent),
packetIDs.client_tournamentMatchInfoRequest: handleEvent(tournamentMatchInfoRequestEvent), packetIDs.client_tournamentMatchInfoRequest: handleEvent(tournamentMatchInfoRequestEvent),
packetIDs.client_tournamentJoinMatchChannel: handleEvent(tournamentJoinMatchChannelEvent), packetIDs.client_tournamentJoinMatchChannel: handleEvent(tournamentJoinMatchChannelEvent),
packetIDs.client_tournamentLeaveMatchChannel: handleEvent(tournamentLeaveMatchChannelEvent), packetIDs.client_tournamentLeaveMatchChannel: handleEvent(tournamentLeaveMatchChannelEvent),
} }
# Packets processed if in restricted mode. # Packets processed if in restricted mode.
# All other packets will be ignored if the user is in restricted mode # All other packets will be ignored if the user is in restricted mode
packetsRestricted = [ packetsRestricted = [
packetIDs.client_logout, packetIDs.client_logout,
packetIDs.client_userStatsRequest, packetIDs.client_userStatsRequest,
packetIDs.client_requestStatusUpdate, packetIDs.client_requestStatusUpdate,
packetIDs.client_userPanelRequest, packetIDs.client_userPanelRequest,
packetIDs.client_changeAction, packetIDs.client_changeAction,
packetIDs.client_channelJoin, packetIDs.client_channelJoin,
packetIDs.client_channelPart, packetIDs.client_channelPart,
] ]
# Process/ignore packet # Process/ignore packet
if packetID != 4: if packetID != 4:
if packetID in eventHandler: if packetID in eventHandler:
if userToken.restricted == False or (userToken.restricted == True and packetID in packetsRestricted): if userToken.restricted == False or (userToken.restricted == True and packetID in packetsRestricted):
eventHandler[packetID]() eventHandler[packetID]()
else:
log.warning("Ignored packet id from {} ({}) (user is restricted)".format(requestTokenString, packetID))
else: else:
log.warning("Unknown packet id from {} ({})".format(requestTokenString, packetID)) log.warning("Ignored packet id from {} ({}) (user is restricted)".format(requestTokenString, packetID))
else:
log.warning("Unknown packet id from {} ({})".format(requestTokenString, packetID))
# Update pos so we can read the next stacked packet # Update pos so we can read the next stacked packet
# +7 because we add packet ID bytes, unused byte and data length bytes # +7 because we add packet ID bytes, unused byte and data length bytes
pos += dataLength+7 pos += dataLength+7
# Token queue built, send it # Token queue built, send it
responseTokenString = userToken.token responseTokenString = userToken.token
responseData = userToken.queue responseData = userToken.queue
userToken.resetQueue() userToken.resetQueue()
except exceptions.tokenNotFoundException: except exceptions.tokenNotFoundException:
# Token not found. Disconnect that user # Token not found. Disconnect that user
responseData = serverPackets.loginError() responseData = serverPackets.loginError()
responseData += serverPackets.notification("Whoops! Something went wrong, please login again.") responseData += serverPackets.notification("Whoops! Something went wrong, please login again.")
log.warning("Received packet from unknown token ({}).".format(requestTokenString)) log.warning("Received packet from unknown token ({}).".format(requestTokenString))
log.info("{} has been disconnected (invalid token)".format(requestTokenString)) log.info("{} has been disconnected (invalid token)".format(requestTokenString))
finally: finally:
# Unlock token # Unlock token
if userToken is not None: if userToken is not None:
# Update ping time for timeout # Update ping time for timeout
userToken.updatePingTime() userToken.updatePingTime()
# Release token lock # Release token lock
userToken.lock.release() userToken.lock.release()
# Delete token if kicked # Delete token if kicked
if userToken.kicked: if userToken.kicked:
glob.tokens.deleteToken(userToken) glob.tokens.deleteToken(userToken)
if glob.outputRequestTime: if glob.outputRequestTime:
# End time # End time
et = datetime.datetime.now() et = datetime.datetime.now()
# Total time: # Total time:
tt = float((et.microsecond-st.microsecond)/1000) tt = float((et.microsecond-st.microsecond)/1000)
log.debug("Request time: {}ms".format(tt)) log.debug("Request time: {}ms".format(tt))
# Send server's response to client # Send server's response to client
# We don't use token object because we might not have a token (failed login) # We don't use token object because we might not have a token (failed login)
if glob.gzip: if glob.gzip:
# First, write the gzipped response # First, write the gzipped response
self.write(gzip.compress(responseData, int(glob.conf.config["server"]["gziplevel"]))) self.write(gzip.compress(responseData, int(glob.conf.config["server"]["gziplevel"])))
# Then, add gzip headers # Then, add gzip headers
self.add_header("Vary", "Accept-Encoding") self.add_header("Vary", "Accept-Encoding")
self.add_header("Content-Encoding", "gzip") self.add_header("Content-Encoding", "gzip")
else: else:
# First, write the response # First, write the response
self.write(responseData) self.write(responseData)
# Add all the headers AFTER the response has been written # Add all the headers AFTER the response has been written
self.set_status(200) self.set_status(200)
self.add_header("cho-token", responseTokenString) self.add_header("cho-token", responseTokenString)
self.add_header("cho-protocol", "19") self.add_header("cho-protocol", "19")
self.add_header("Connection", "keep-alive") self.add_header("Connection", "keep-alive")
self.add_header("Keep-Alive", "timeout=5, max=100") self.add_header("Keep-Alive", "timeout=5, max=100")
self.add_header("Content-Type", "text/html; charset=UTF-8") self.add_header("Content-Type", "text/html; charset=UTF-8")
except: #except:
log.error("Unknown error!\n```\n{}\n{}```".format(sys.exc_info(), traceback.format_exc())) # log.error("Unknown error!\n```\n{}\n{}```".format(sys.exc_info(), traceback.format_exc()))
if glob.sentry: # if glob.sentry:
yield tornado.gen.Task(self.captureException, exc_info=True) # yield tornado.gen.Task(self.captureException, exc_info=True)
#finally: #finally:
# self.finish() # self.finish()

View File

@ -74,7 +74,7 @@ class config:
self.config.get("localize","enable") self.config.get("localize","enable")
self.config.get("localize","ipapiurl") self.config.get("localize","ipapiurl")
return True return True
except: except configparser.Error:
return False return False
def generateDefaultConfig(self): def generateDefaultConfig(self):

View File

@ -30,7 +30,7 @@ def getLocation(ip):
try: try:
# Try to get position from Pikolo Aul's Go-Sanic ip API # Try to get position from Pikolo Aul's Go-Sanic ip API
result = json.loads(urllib.request.urlopen("{}/{}".format(glob.conf.config["localize"]["ipapiurl"], ip), timeout=3).read().decode())["loc"].split(",") result = json.loads(urllib.request.urlopen("{}/{}".format(glob.conf.config["localize"]["ipapiurl"], ip), timeout=3).read().decode())["loc"].split(",")
return (float(result[0]), float(result[1])) return float(result[0]), float(result[1])
except: except:
log.error("Error in get position") log.error("Error in get position")
return (0, 0) return 0, 0

View File

@ -136,7 +136,7 @@ cpdef bytes packData(__data, int dataType):
return data return data
cpdef bytes buildPacket(int __packet, list __packetData = []): cpdef bytes buildPacket(int __packet, list __packetData = None):
""" """
Builds a packet Builds a packet
@ -144,6 +144,9 @@ cpdef bytes buildPacket(int __packet, list __packetData = []):
:param __packetData: packet structure [[data, dataType], [data, dataType], ...] :param __packetData: packet structure [[data, dataType], [data, dataType], ...]
:return: packet bytes :return: packet bytes
""" """
# Default argument
if __packetData is None:
__packetData = []
# Set some variables # Set some variables
cdef bytes packetData = bytes() cdef bytes packetData = bytes()
cdef int packetLength = 0 cdef int packetLength = 0
@ -183,7 +186,7 @@ cpdef int readPacketLength(bytes stream):
return unpackData(stream[3:7], dataTypes.UINT32) return unpackData(stream[3:7], dataTypes.UINT32)
cpdef readPacketData(bytes stream, list structure=[], bint hasFirstBytes = True): cpdef readPacketData(bytes stream, list structure=None, bint hasFirstBytes = True):
""" """
Read packet data from `stream` according to `structure` Read packet data from `stream` according to `structure`
:param stream: packet bytes :param stream: packet bytes
@ -192,6 +195,10 @@ cpdef readPacketData(bytes stream, list structure=[], bint hasFirstBytes = True)
if False, `stream` has only packet data. Default: True if False, `stream` has only packet data. Default: True
:return: {name: unpackedValue, ...} :return: {name: unpackedValue, ...}
""" """
# Default list argument
if structure is None:
structure = []
# Read packet ID (first 2 bytes) # Read packet ID (first 2 bytes)
cdef dict data = {} cdef dict data = {}

View File

@ -350,11 +350,11 @@ class Client:
self.sendMotd() self.sendMotd()
self.__handleCommand = self.mainHandler self.__handleCommand = self.mainHandler
def quitHandler(self, command, arguments): def quitHandler(self, _, arguments):
"""QUIT command handler""" """QUIT command handler"""
self.disconnect(self.IRCUsername if len(arguments) < 1 else arguments[0]) self.disconnect(self.IRCUsername if len(arguments) < 1 else arguments[0])
def joinHandler(self, command, arguments): def joinHandler(self, _, arguments):
"""JOIN command handler""" """JOIN command handler"""
if len(arguments) < 1: if len(arguments) < 1:
self.reply461("JOIN") self.reply461("JOIN")
@ -421,7 +421,7 @@ class Client:
self.reply403(channel) self.reply403(channel)
continue continue
def partHandler(self, command, arguments): def partHandler(self, _, arguments):
"""PART command handler""" """PART command handler"""
if len(arguments) < 1: if len(arguments) < 1:
self.reply461("PART") self.reply461("PART")
@ -505,7 +505,7 @@ class Client:
"""LUSERS command handler""" """LUSERS command handler"""
self.sendLusers() self.sendLusers()
def pingHandler(self, command, arguments): def pingHandler(self, _, arguments):
"""PING command handler""" """PING command handler"""
if len(arguments) < 1: if len(arguments) < 1:
self.replyCode(409, "No origin specified") self.replyCode(409, "No origin specified")
@ -516,7 +516,7 @@ class Client:
"""(fake) PONG command handler""" """(fake) PONG command handler"""
pass pass
def awayHandler(self, command, arguments): def awayHandler(self, _, arguments):
"""AWAY command handler""" """AWAY command handler"""
response = chat.IRCAway(self.banchoUsername, " ".join(arguments)) response = chat.IRCAway(self.banchoUsername, " ".join(arguments))
self.replyCode(response, "You are no longer marked as being away" if response == 305 else "You have been marked as being away") self.replyCode(response, "You are no longer marked as being away" if response == 305 else "You have been marked as being away")
@ -621,12 +621,11 @@ class Server:
value.message(":{} PRIVMSG {} :{}".format(fro, to, message)) value.message(":{} PRIVMSG {} :{}".format(fro, to, message))
def removeClient(self, client, quitmsg): def removeClient(self, client, _):
""" """
Remove a client from connected clients Remove a client from connected clients
:param client: client object :param client: client object
:param quitmsg: QUIT argument, useless atm
:return: :return:
""" """
if client.socket in self.clients: if client.socket in self.clients:
@ -639,6 +638,7 @@ class Server:
:return: :return:
""" """
# Sentry # Sentry
sentryClient = None
if glob.sentry: if glob.sentry:
sentryClient = raven.Client(glob.conf.config["sentry"]["ircdns"]) sentryClient = raven.Client(glob.conf.config["sentry"]["ircdns"])
@ -690,7 +690,7 @@ class Server:
lastAliveCheck = now lastAliveCheck = now
except: except:
log.error("[IRC] Unknown error!\n```\n{}\n{}```".format(sys.exc_info(), traceback.format_exc())) log.error("[IRC] Unknown error!\n```\n{}\n{}```".format(sys.exc_info(), traceback.format_exc()))
if glob.sentry: if glob.sentry and sentryClient is not None:
sentryClient.captureException() sentryClient.captureException()
def main(port=6667): def main(port=6667):

View File

@ -1,5 +1,4 @@
from common.log import logUtils as log from common.log import logUtils as log
from constants import serverPackets
from objects import channel from objects import channel
from objects import glob from objects import glob
from helpers import chatHelper as chat from helpers import chatHelper as chat

View File

@ -376,7 +376,7 @@ class match:
""" """
Add someone to users in match Add someone to users in match
:param userID: user id of the user :param user: user object of the user
:return: True if join success, False if fail (room is full) :return: True if join success, False if fail (room is full)
""" """
# Make sure we're not in this match # Make sure we're not in this match
@ -404,7 +404,7 @@ class match:
""" """
Remove someone from users in match Remove someone from users in match
:param userID: user if of the user :param user: user object of the user
:return: :return:
""" """
# Make sure the user is in room # Make sure the user is in room

View File

@ -1,7 +1,6 @@
from objects import match from objects import match
from objects import glob from objects import glob
from constants import serverPackets from constants import serverPackets
from common.log import logUtils as log
class matchList: class matchList:
def __init__(self): def __init__(self):
@ -40,11 +39,11 @@ class matchList:
return return
# Remove match object and stream # Remove match object and stream
match = self.matches.pop(matchID) _match = self.matches.pop(matchID)
glob.streams.dispose(match.streamName) glob.streams.dispose(_match.streamName)
glob.streams.dispose(match.playingStreamName) glob.streams.dispose(_match.playingStreamName)
glob.streams.remove(match.streamName) glob.streams.remove(_match.streamName)
glob.streams.remove(match.playingStreamName) glob.streams.remove(_match.playingStreamName)
# Send match dispose packet to everyone in lobby # Send match dispose packet to everyone in lobby
glob.streams.broadcast("lobby", serverPackets.disposeMatch(matchID)) glob.streams.broadcast("lobby", serverPackets.disposeMatch(matchID))

View File

@ -98,7 +98,7 @@ class token:
""" """
Add bytes (packets) to queue Add bytes (packets) to queue
:param bytes: (packet) bytes to enqueue :param bytes_: (packet) bytes to enqueue
""" """
# Never enqueue for IRC clients or Foka # Never enqueue for IRC clients or Foka
@ -144,7 +144,8 @@ class token:
""" """
Set client location Set client location
:param location: [latitude, longitude] :param latitude: latitude
:param longitude: longitude
""" """
self.location = (latitude, longitude) self.location = (latitude, longitude)

View File

@ -1,6 +1,8 @@
import threading import threading
import time import time
import redis
from common.ripple import userUtils from common.ripple import userUtils
from common.log import logUtils as log from common.log import logUtils as log
from constants import serverPackets from constants import serverPackets
@ -17,6 +19,7 @@ class tokenList:
Add a token object to tokens list Add a token object to tokens list
:param userID: user id associated to that token :param userID: user id associated to that token
:param ip: ip address of the client
:param irc: if True, set this token as IRC client :param irc: if True, set this token as IRC client
:param timeOffset: the time offset from UTC for this user. Default: 0. :param timeOffset: the time offset from UTC for this user. Default: 0.
:param tournament: if True, flag this client as a tournement client. Default: True. :param tournament: if True, flag this client as a tournement client. Default: True.
@ -54,13 +57,13 @@ class tokenList:
# Get userID associated to that token # Get userID associated to that token
return self.tokens[token].userID return self.tokens[token].userID
def getTokenFromUserID(self, userID, ignoreIRC=False, all=False): def getTokenFromUserID(self, userID, ignoreIRC=False, _all=False):
""" """
Get token from a user ID Get token from a user ID
:param userID: user ID to find :param userID: user ID to find
:param ignoreIRC: if True, consider bancho clients only and skip IRC clients :param ignoreIRC: if True, consider bancho clients only and skip IRC clients
:param all: if True, return a list with all clients that match given username, otherwise return :param _all: if True, return a list with all clients that match given username, otherwise return
only the first occurrence. only the first occurrence.
:return: False if not found, token object if found :return: False if not found, token object if found
""" """
@ -70,18 +73,18 @@ class tokenList:
if value.userID == userID: if value.userID == userID:
if ignoreIRC and value.irc: if ignoreIRC and value.irc:
continue continue
if all: if _all:
ret.append(value) ret.append(value)
else: else:
return value return value
# Return full list or None if not found # Return full list or None if not found
if all: if _all:
return ret return ret
else: else:
return None return None
def getTokenFromUsername(self, username, ignoreIRC=False, safe=False, all=False): def getTokenFromUsername(self, username, ignoreIRC=False, safe=False, _all=False):
""" """
Get an osuToken object from an username Get an osuToken object from an username
@ -89,7 +92,7 @@ class tokenList:
:param ignoreIRC: if True, consider bancho clients only and skip IRC clients :param ignoreIRC: if True, consider bancho clients only and skip IRC clients
:param safe: if True, username is a safe username, :param safe: if True, username is a safe username,
compare it with token's safe username rather than normal username compare it with token's safe username rather than normal username
:param all: if True, return a list with all clients that match given username, otherwise return :param _all: if True, return a list with all clients that match given username, otherwise return
only the first occurrence. only the first occurrence.
:return: osuToken object or None :return: osuToken object or None
""" """
@ -102,13 +105,13 @@ class tokenList:
if (not safe and value.username.lower() == who) or (safe and value.safeUsername == who): if (not safe and value.username.lower() == who) or (safe and value.safeUsername == who):
if ignoreIRC and value.irc: if ignoreIRC and value.irc:
continue continue
if all: if _all:
ret.append(value) ret.append(value)
else: else:
return value return value
# Return full list or None if not found # Return full list or None if not found
if all: if _all:
return ret return ret
else: else:
return None return None
@ -215,7 +218,7 @@ class tokenList:
try: try:
# TODO: Make function or some redis meme # TODO: Make function or some redis meme
glob.redis.eval("return redis.call('del', unpack(redis.call('keys', ARGV[1])))", 0, "peppy:sessions:*") glob.redis.eval("return redis.call('del', unpack(redis.call('keys', ARGV[1])))", 0, "peppy:sessions:*")
except: except redis.RedisError:
pass pass

8
pep.py
View File

@ -142,7 +142,7 @@ if __name__ == "__main__":
consoleHelper.printNoNl("> Creating threads pool... ") consoleHelper.printNoNl("> Creating threads pool... ")
glob.pool = ThreadPool(int(glob.conf.config["server"]["threads"])) glob.pool = ThreadPool(int(glob.conf.config["server"]["threads"]))
consoleHelper.printDone() consoleHelper.printDone()
except: except ValueError:
consoleHelper.printError() consoleHelper.printError()
consoleHelper.printColored("[!] Error while creating threads pool. Please check your config.ini and run the server again", bcolors.RED) consoleHelper.printColored("[!] Error while creating threads pool. Please check your config.ini and run the server again", bcolors.RED)
@ -248,9 +248,10 @@ if __name__ == "__main__":
glob.irc = generalUtils.stringToBool(glob.conf.config["irc"]["enable"]) glob.irc = generalUtils.stringToBool(glob.conf.config["irc"]["enable"])
if glob.irc: if glob.irc:
# IRC port # IRC port
ircPort = 0
try: try:
ircPort = int(glob.conf.config["irc"]["port"]) ircPort = int(glob.conf.config["irc"]["port"])
except: except ValueError:
consoleHelper.printColored("[!] Invalid IRC port! Please check your config.ini and run the server again", bcolors.RED) consoleHelper.printColored("[!] Invalid IRC port! Please check your config.ini and run the server again", bcolors.RED)
log.logMessage("**pep.py** IRC server started!", discord="bunker", of="info.txt", stdout=False) log.logMessage("**pep.py** IRC server started!", discord="bunker", of="info.txt", stdout=False)
consoleHelper.printColored("> IRC server listening on 127.0.0.1:{}...".format(ircPort), bcolors.GREEN) consoleHelper.printColored("> IRC server listening on 127.0.0.1:{}...".format(ircPort), bcolors.GREEN)
@ -259,9 +260,10 @@ if __name__ == "__main__":
consoleHelper.printColored("[!] Warning! IRC server is disabled!", bcolors.YELLOW) consoleHelper.printColored("[!] Warning! IRC server is disabled!", bcolors.YELLOW)
# Server port # Server port
serverPort = 0
try: try:
serverPort = int(glob.conf.config["server"]["port"]) serverPort = int(glob.conf.config["server"]["port"])
except: except ValueError:
consoleHelper.printColored("[!] Invalid server port! Please check your config.ini and run the server again", bcolors.RED) consoleHelper.printColored("[!] Invalid server port! Please check your config.ini and run the server again", bcolors.RED)
# Server start message and console output # Server start message and console output

View File

@ -6,4 +6,5 @@ raven
bcrypt>=3.1.1 bcrypt>=3.1.1
dill dill
redis redis
cython cython
datadog