2016-05-31 20:49:30 +00:00
|
|
|
import datetime
|
|
|
|
import gzip
|
2016-10-02 20:48:14 +00:00
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
import tornado.gen
|
|
|
|
import tornado.web
|
|
|
|
from raven.contrib.tornado import SentryMixin
|
|
|
|
|
|
|
|
from common.log import logUtils as log
|
|
|
|
from common.web import requestsManager
|
2016-05-31 20:49:30 +00:00
|
|
|
from constants import exceptions
|
|
|
|
from constants import packetIDs
|
|
|
|
from constants import serverPackets
|
2016-10-02 20:48:14 +00:00
|
|
|
from events import cantSpectateEvent
|
|
|
|
from events import changeActionEvent
|
|
|
|
from events import changeMatchModsEvent
|
|
|
|
from events import changeMatchPasswordEvent
|
|
|
|
from events import changeMatchSettingsEvent
|
|
|
|
from events import changeSlotEvent
|
2016-05-31 20:49:30 +00:00
|
|
|
from events import channelJoinEvent
|
|
|
|
from events import channelPartEvent
|
2016-10-02 20:48:14 +00:00
|
|
|
from events import createMatchEvent
|
2016-05-31 20:49:30 +00:00
|
|
|
from events import friendAddEvent
|
|
|
|
from events import friendRemoveEvent
|
|
|
|
from events import joinLobbyEvent
|
|
|
|
from events import joinMatchEvent
|
2016-10-02 20:48:14 +00:00
|
|
|
from events import loginEvent
|
|
|
|
from events import logoutEvent
|
|
|
|
from events import matchChangeTeamEvent
|
|
|
|
from events import matchCompleteEvent
|
|
|
|
from events import matchFailedEvent
|
|
|
|
from events import matchFramesEvent
|
|
|
|
from events import matchHasBeatmapEvent
|
|
|
|
from events import matchInviteEvent
|
2016-05-31 20:49:30 +00:00
|
|
|
from events import matchLockEvent
|
2016-10-02 20:48:14 +00:00
|
|
|
from events import matchNoBeatmapEvent
|
2016-05-31 20:49:30 +00:00
|
|
|
from events import matchPlayerLoadEvent
|
2016-10-02 20:48:14 +00:00
|
|
|
from events import matchReadyEvent
|
2016-05-31 20:49:30 +00:00
|
|
|
from events import matchSkipEvent
|
2016-10-02 20:48:14 +00:00
|
|
|
from events import matchStartEvent
|
2016-05-31 20:49:30 +00:00
|
|
|
from events import matchTransferHostEvent
|
2016-10-02 20:48:14 +00:00
|
|
|
from events import partLobbyEvent
|
|
|
|
from events import partMatchEvent
|
2016-06-16 11:38:17 +00:00
|
|
|
from events import requestStatusUpdateEvent
|
2016-10-02 20:48:14 +00:00
|
|
|
from events import sendPrivateMessageEvent
|
|
|
|
from events import sendPublicMessageEvent
|
|
|
|
from events import setAwayMessageEvent
|
|
|
|
from events import spectateFramesEvent
|
|
|
|
from events import startSpectatingEvent
|
|
|
|
from events import stopSpectatingEvent
|
2016-06-17 15:43:49 +00:00
|
|
|
from events import userPanelRequestEvent
|
2016-10-02 20:48:14 +00:00
|
|
|
from events import userStatsRequestEvent
|
2016-10-05 21:28:26 +00:00
|
|
|
from events import tournamentMatchInfoRequestEvent
|
|
|
|
from events import tournamentJoinMatchChannelEvent
|
|
|
|
from events import tournamentLeaveMatchChannelEvent
|
2016-10-02 20:48:14 +00:00
|
|
|
from helpers import packetHelper
|
|
|
|
from objects import glob
|
2016-06-15 17:01:00 +00:00
|
|
|
|
2016-05-31 20:49:30 +00:00
|
|
|
|
2016-10-02 20:48:14 +00:00
|
|
|
class handler(SentryMixin, requestsManager.asyncRequestHandler):
|
2016-06-15 17:01:00 +00:00
|
|
|
@tornado.web.asynchronous
|
|
|
|
@tornado.gen.engine
|
2016-05-31 20:49:30 +00:00
|
|
|
def asyncPost(self):
|
|
|
|
try:
|
|
|
|
# Track time if needed
|
2016-09-02 15:45:10 +00:00
|
|
|
if glob.outputRequestTime:
|
2016-05-31 20:49:30 +00:00
|
|
|
# Start time
|
|
|
|
st = datetime.datetime.now()
|
|
|
|
|
|
|
|
# Client's token string and request data
|
2016-06-02 16:41:12 +00:00
|
|
|
requestTokenString = self.request.headers.get("osu-token")
|
2016-05-31 20:49:30 +00:00
|
|
|
requestData = self.request.body
|
|
|
|
|
|
|
|
# Server's token string and request data
|
|
|
|
responseTokenString = "ayy"
|
2016-09-04 08:56:10 +00:00
|
|
|
responseData = bytes()
|
2016-05-31 20:49:30 +00:00
|
|
|
|
2016-09-02 15:45:10 +00:00
|
|
|
if requestTokenString is None:
|
2016-05-31 20:49:30 +00:00
|
|
|
# No token, first request. Handle login.
|
|
|
|
responseTokenString, responseData = loginEvent.handle(self)
|
|
|
|
else:
|
2016-06-02 17:07:33 +00:00
|
|
|
userToken = None # default value
|
2016-05-31 20:49:30 +00:00
|
|
|
try:
|
|
|
|
# This is not the first packet, send response based on client's request
|
|
|
|
# Packet start position, used to read stacked packets
|
|
|
|
pos = 0
|
|
|
|
|
|
|
|
# Make sure the token exists
|
|
|
|
if requestTokenString not in glob.tokens.tokens:
|
|
|
|
raise exceptions.tokenNotFoundException()
|
|
|
|
|
2016-06-02 17:07:33 +00:00
|
|
|
# Token exists, get its object and lock it
|
2016-05-31 20:49:30 +00:00
|
|
|
userToken = glob.tokens.tokens[requestTokenString]
|
2016-06-02 17:07:33 +00:00
|
|
|
userToken.lock.acquire()
|
2016-05-31 20:49:30 +00:00
|
|
|
|
|
|
|
# Keep reading packets until everything has been read
|
|
|
|
while pos < len(requestData):
|
|
|
|
# Get packet from stack starting from new packet
|
|
|
|
leftData = requestData[pos:]
|
|
|
|
|
|
|
|
# Get packet ID, data length and data
|
|
|
|
packetID = packetHelper.readPacketID(leftData)
|
|
|
|
dataLength = packetHelper.readPacketLength(leftData)
|
|
|
|
packetData = requestData[pos:(pos+dataLength+7)]
|
|
|
|
|
|
|
|
# Console output if needed
|
2016-06-04 10:44:54 +00:00
|
|
|
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)))
|
2016-05-31 20:49:30 +00:00
|
|
|
|
|
|
|
# Event handler
|
|
|
|
def handleEvent(ev):
|
|
|
|
def wrapper():
|
|
|
|
ev.handle(userToken, packetData)
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
eventHandler = {
|
2016-08-10 10:24:41 +00:00
|
|
|
packetIDs.client_changeAction: handleEvent(changeActionEvent),
|
|
|
|
packetIDs.client_logout: handleEvent(logoutEvent),
|
|
|
|
packetIDs.client_friendAdd: handleEvent(friendAddEvent),
|
|
|
|
packetIDs.client_friendRemove: handleEvent(friendRemoveEvent),
|
|
|
|
packetIDs.client_userStatsRequest: handleEvent(userStatsRequestEvent),
|
|
|
|
packetIDs.client_requestStatusUpdate: handleEvent(requestStatusUpdateEvent),
|
|
|
|
packetIDs.client_userPanelRequest: handleEvent(userPanelRequestEvent),
|
2016-11-20 10:31:51 +00:00
|
|
|
|
2016-08-10 10:24:41 +00:00
|
|
|
packetIDs.client_channelJoin: handleEvent(channelJoinEvent),
|
|
|
|
packetIDs.client_channelPart: handleEvent(channelPartEvent),
|
2016-05-31 20:49:30 +00:00
|
|
|
packetIDs.client_sendPublicMessage: handleEvent(sendPublicMessageEvent),
|
|
|
|
packetIDs.client_sendPrivateMessage: handleEvent(sendPrivateMessageEvent),
|
|
|
|
packetIDs.client_setAwayMessage: handleEvent(setAwayMessageEvent),
|
2016-08-10 10:24:41 +00:00
|
|
|
|
2016-05-31 20:49:30 +00:00
|
|
|
packetIDs.client_startSpectating: handleEvent(startSpectatingEvent),
|
|
|
|
packetIDs.client_stopSpectating: handleEvent(stopSpectatingEvent),
|
|
|
|
packetIDs.client_cantSpectate: handleEvent(cantSpectateEvent),
|
|
|
|
packetIDs.client_spectateFrames: handleEvent(spectateFramesEvent),
|
2016-08-10 10:24:41 +00:00
|
|
|
|
2016-05-31 20:49:30 +00:00
|
|
|
packetIDs.client_joinLobby: handleEvent(joinLobbyEvent),
|
|
|
|
packetIDs.client_partLobby: handleEvent(partLobbyEvent),
|
|
|
|
packetIDs.client_createMatch: handleEvent(createMatchEvent),
|
|
|
|
packetIDs.client_joinMatch: handleEvent(joinMatchEvent),
|
|
|
|
packetIDs.client_partMatch: handleEvent(partMatchEvent),
|
|
|
|
packetIDs.client_matchChangeSlot: handleEvent(changeSlotEvent),
|
|
|
|
packetIDs.client_matchChangeSettings: handleEvent(changeMatchSettingsEvent),
|
|
|
|
packetIDs.client_matchChangePassword: handleEvent(changeMatchPasswordEvent),
|
|
|
|
packetIDs.client_matchChangeMods: handleEvent(changeMatchModsEvent),
|
|
|
|
packetIDs.client_matchReady: handleEvent(matchReadyEvent),
|
|
|
|
packetIDs.client_matchNotReady: handleEvent(matchReadyEvent),
|
|
|
|
packetIDs.client_matchLock: handleEvent(matchLockEvent),
|
|
|
|
packetIDs.client_matchStart: handleEvent(matchStartEvent),
|
|
|
|
packetIDs.client_matchLoadComplete: handleEvent(matchPlayerLoadEvent),
|
|
|
|
packetIDs.client_matchSkipRequest: handleEvent(matchSkipEvent),
|
|
|
|
packetIDs.client_matchScoreUpdate: handleEvent(matchFramesEvent),
|
|
|
|
packetIDs.client_matchComplete: handleEvent(matchCompleteEvent),
|
|
|
|
packetIDs.client_matchNoBeatmap: handleEvent(matchNoBeatmapEvent),
|
|
|
|
packetIDs.client_matchHasBeatmap: handleEvent(matchHasBeatmapEvent),
|
|
|
|
packetIDs.client_matchTransferHost: handleEvent(matchTransferHostEvent),
|
|
|
|
packetIDs.client_matchFailed: handleEvent(matchFailedEvent),
|
2016-06-16 11:38:17 +00:00
|
|
|
packetIDs.client_matchChangeTeam: handleEvent(matchChangeTeamEvent),
|
2016-08-10 10:24:41 +00:00
|
|
|
packetIDs.client_invite: handleEvent(matchInviteEvent),
|
2016-10-05 21:28:26 +00:00
|
|
|
|
|
|
|
packetIDs.client_tournamentMatchInfoRequest: handleEvent(tournamentMatchInfoRequestEvent),
|
|
|
|
packetIDs.client_tournamentJoinMatchChannel: handleEvent(tournamentJoinMatchChannelEvent),
|
|
|
|
packetIDs.client_tournamentLeaveMatchChannel: handleEvent(tournamentLeaveMatchChannelEvent),
|
2016-05-31 20:49:30 +00:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:51:19 +00:00
|
|
|
# Packets processed if in restricted mode.
|
|
|
|
# All other packets will be ignored if the user is in restricted mode
|
|
|
|
packetsRestricted = [
|
|
|
|
packetIDs.client_logout,
|
|
|
|
packetIDs.client_userStatsRequest,
|
|
|
|
packetIDs.client_requestStatusUpdate,
|
|
|
|
packetIDs.client_userPanelRequest,
|
|
|
|
packetIDs.client_changeAction,
|
|
|
|
packetIDs.client_channelJoin,
|
|
|
|
packetIDs.client_channelPart,
|
|
|
|
]
|
|
|
|
|
|
|
|
# Process/ignore packet
|
2016-05-31 20:49:30 +00:00
|
|
|
if packetID != 4:
|
|
|
|
if packetID in eventHandler:
|
2016-07-03 18:51:19 +00:00
|
|
|
if userToken.restricted == False or (userToken.restricted == True and packetID in packetsRestricted):
|
|
|
|
eventHandler[packetID]()
|
|
|
|
else:
|
|
|
|
log.warning("Ignored packet id from {} ({}) (user is restricted)".format(requestTokenString, packetID))
|
2016-05-31 20:49:30 +00:00
|
|
|
else:
|
2016-06-04 10:44:54 +00:00
|
|
|
log.warning("Unknown packet id from {} ({})".format(requestTokenString, packetID))
|
2016-05-31 20:49:30 +00:00
|
|
|
|
|
|
|
# Update pos so we can read the next stacked packet
|
|
|
|
# +7 because we add packet ID bytes, unused byte and data length bytes
|
|
|
|
pos += dataLength+7
|
|
|
|
|
|
|
|
# Token queue built, send it
|
|
|
|
responseTokenString = userToken.token
|
|
|
|
responseData = userToken.queue
|
|
|
|
userToken.resetQueue()
|
|
|
|
except exceptions.tokenNotFoundException:
|
|
|
|
# Token not found. Disconnect that user
|
|
|
|
responseData = serverPackets.loginError()
|
|
|
|
responseData += serverPackets.notification("Whoops! Something went wrong, please login again.")
|
2016-06-04 10:44:54 +00:00
|
|
|
log.warning("Received packet from unknown token ({}).".format(requestTokenString))
|
2016-06-06 17:10:32 +00:00
|
|
|
log.info("{} has been disconnected (invalid token)".format(requestTokenString))
|
2016-06-02 17:07:33 +00:00
|
|
|
finally:
|
|
|
|
# Unlock token
|
2016-09-02 15:45:10 +00:00
|
|
|
if userToken is not None:
|
2016-10-09 17:12:18 +00:00
|
|
|
# Update ping time for timeout
|
|
|
|
userToken.updatePingTime()
|
|
|
|
# Release token lock
|
2016-06-02 17:07:33 +00:00
|
|
|
userToken.lock.release()
|
2016-11-20 10:31:51 +00:00
|
|
|
# Delete token if kicked
|
|
|
|
if userToken.kicked:
|
|
|
|
glob.tokens.deleteToken(userToken)
|
2016-05-31 20:49:30 +00:00
|
|
|
|
2016-09-02 15:45:10 +00:00
|
|
|
if glob.outputRequestTime:
|
2016-05-31 20:49:30 +00:00
|
|
|
# End time
|
|
|
|
et = datetime.datetime.now()
|
|
|
|
|
|
|
|
# Total time:
|
|
|
|
tt = float((et.microsecond-st.microsecond)/1000)
|
2016-06-04 10:44:54 +00:00
|
|
|
log.debug("Request time: {}ms".format(tt))
|
2016-05-31 20:49:30 +00:00
|
|
|
|
|
|
|
# Send server's response to client
|
|
|
|
# We don't use token object because we might not have a token (failed login)
|
2016-09-02 15:45:10 +00:00
|
|
|
if glob.gzip:
|
2016-06-16 10:28:02 +00:00
|
|
|
# First, write the gzipped response
|
|
|
|
self.write(gzip.compress(responseData, int(glob.conf.config["server"]["gziplevel"])))
|
|
|
|
|
|
|
|
# Then, add gzip headers
|
|
|
|
self.add_header("Vary", "Accept-Encoding")
|
|
|
|
self.add_header("Content-Encoding", "gzip")
|
|
|
|
else:
|
|
|
|
# First, write the response
|
|
|
|
self.write(responseData)
|
|
|
|
|
|
|
|
# Add all the headers AFTER the response has been written
|
2016-06-01 17:17:31 +00:00
|
|
|
self.set_status(200)
|
2016-05-31 20:49:30 +00:00
|
|
|
self.add_header("cho-token", responseTokenString)
|
|
|
|
self.add_header("cho-protocol", "19")
|
2016-09-02 10:41:19 +00:00
|
|
|
self.add_header("Connection", "keep-alive")
|
|
|
|
self.add_header("Keep-Alive", "timeout=5, max=100")
|
2016-05-31 20:49:30 +00:00
|
|
|
self.add_header("Content-Type", "text/html; charset=UTF-8")
|
|
|
|
except:
|
2016-06-15 17:01:00 +00:00
|
|
|
log.error("Unknown error!\n```\n{}\n{}```".format(sys.exc_info(), traceback.format_exc()))
|
|
|
|
if glob.sentry:
|
|
|
|
yield tornado.gen.Task(self.captureException, exc_info=True)
|
2016-06-16 11:38:17 +00:00
|
|
|
#finally:
|
|
|
|
# self.finish()
|
|
|
|
|
2016-06-15 17:01:00 +00:00
|
|
|
@tornado.web.asynchronous
|
|
|
|
@tornado.gen.engine
|
2016-05-31 20:49:30 +00:00
|
|
|
def asyncGet(self):
|
2016-06-15 17:01:00 +00:00
|
|
|
html = "<html><head><title>MA MAURO ESISTE?</title><style type='text/css'>body{width:30%}</style></head><body><pre>"
|
|
|
|
html += " _ __<br>"
|
|
|
|
html += " (_) / /<br>"
|
|
|
|
html += " ______ __ ____ ____ / /____<br>"
|
|
|
|
html += " / ___/ / _ \\/ _ \\/ / _ \\<br>"
|
|
|
|
html += " / / / / /_) / /_) / / ____/<br>"
|
|
|
|
html += "/__/ /__/ .___/ .___/__/ \\_____/<br>"
|
|
|
|
html += " / / / /<br>"
|
|
|
|
html += " /__/ /__/<br>"
|
|
|
|
html += "<b>PYTHON > ALL VERSION</b><br><br>"
|
|
|
|
html += "<marquee style='white-space:pre;'><br>"
|
|
|
|
html += " .. o .<br>"
|
|
|
|
html += " o.o o . o<br>"
|
|
|
|
html += " oo...<br>"
|
|
|
|
html += " __[]__<br>"
|
|
|
|
html += " phwr--> _\\:D/_/o_o_o_|__ <span style=\"font-family: 'Comic Sans MS'; font-size: 8pt;\">u wot m8</span><br>"
|
|
|
|
html += " \\\"\"\"\"\"\"\"\"\"\"\"\"\"\"/<br>"
|
|
|
|
html += " \\ . .. .. . /<br>"
|
|
|
|
html += "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br>"
|
2016-07-14 10:37:07 +00:00
|
|
|
html += "</marquee><br><strike>reverse engineering a protocol impossible to reverse engineer since always</strike><br>we are actually reverse engineering bancho successfully. for the third time.<br><br><i>© Ripple team, 2016</i></pre></body></html>"
|
2016-09-02 10:41:19 +00:00
|
|
|
self.write(html)
|