1 Commits

Author SHA1 Message Date
Nyo
7699dd288c .BANCHO. Add basic api code 2016-10-31 09:44:36 +01:00
12 changed files with 40 additions and 73 deletions

1
.gitignore vendored
View File

@@ -6,4 +6,3 @@ filters.txt
common_funzia
common_refractor
common_memato
redistest.py

2
common

Submodule common updated: cccd620817...4b713cbb28

View File

@@ -11,17 +11,14 @@ def handle(userToken, packetData):
userID = userToken.userID
username = userToken.username
# Update privileges
userToken.updatePrivileges()
# Make sure we are not banned
if userUtils.isBanned(priv=userToken.privileges):
if userUtils.isBanned(userID):
userToken.enqueue(serverPackets.loginBanned())
return
# Send restricted message if needed
if not userToken.restricted:
if userUtils.isRestricted(priv=userToken.privileges):
if userUtils.isRestricted(userID):
userToken.setRestricted()
# Change action packet

View File

@@ -64,9 +64,9 @@ def handle(tornadoRequest):
# Make sure we are not banned or locked
priv = userUtils.getPrivileges(userID)
if userUtils.isBanned(priv=priv) == True and not userUtils.isPending(priv=priv):
if userUtils.isBanned(userID) == True and priv & privileges.USER_PENDING_VERIFICATION == 0:
raise exceptions.loginBannedException()
if userUtils.isLocked(priv=priv) == True and not userUtils.isPending(priv=priv):
if userUtils.isLocked(userID) == True and priv & privileges.USER_PENDING_VERIFICATION == 0:
raise exceptions.loginLockedException()
# 2FA check
@@ -195,10 +195,6 @@ def handle(tornadoRequest):
location = locationHelper.getLocation(requestIP)
countryLetters = locationHelper.getCountry(requestIP)
country = countryHelper.getCountryID(countryLetters)
# Set country in db if user has no country (first bancho login)
if userUtils.getCountry(userID) == "XX":
userUtils.setCountry(userID, countryLetters)
else:
# Set location to 0,0 and get country from db
log.warning("Location skipped")
@@ -210,6 +206,10 @@ def handle(tornadoRequest):
responseToken.setLocation(location)
responseToken.setCountry(country)
# Set country in db if user has no country (first bancho login)
if userUtils.getCountry(userID) == "XX":
userUtils.setCountry(userID, countryLetters)
# Send to everyone our userpanel if we are not restricted or tournament
if not responseToken.restricted:
glob.streams.broadcast("main", serverPackets.userPanel(userID))

0
handlers/api/__init__.py Normal file
View File

7
handlers/api/ping.py Normal file
View File

@@ -0,0 +1,7 @@
from common.web.api import api
class handler(api.asyncAPIHandler):
@api.api
@api.args("ses")
def asyncGet(self):
self.data["message"] = "狂乱 Hey Kids!!"

View File

@@ -256,10 +256,6 @@ def sendMessage(fro = "", to = "", message = "", token = None, toIRC = True):
# TODO: Make sure the recipient has not disabled PMs for non-friends or he's our friend
# Away check
if recipientToken.awayCheck(userID):
sendMessage(to, fro, "\x01ACTION is away: {message}\x01".format(code=chr(int(1)), message=recipientToken.awayMessage))
# Check message templates (mods/admins only)
if message in messageTemplates.templates and token.admin == True:
sendMessage(fro, to, messageTemplates.templates[message])
@@ -282,7 +278,7 @@ def sendMessage(fro = "", to = "", message = "", token = None, toIRC = True):
sendMessage("FokaBot", to if isChannel else fro, fokaMessage)
# File and discord logs (public chat only)
if to.startswith("#") and not (message.startswith("\x01ACTION is playing") and to.startswith("#spect_")):
if to.startswith("#"):
log.chat("{fro} @ {to}: {message}".format(fro=username, to=to, message=str(message.encode("utf-8"))))
glob.schiavo.sendChatlog("**{fro} @ {to}:** {message}".format(fro=username, to=to, message=str(message.encode("utf-8"))[2:-1]))
return 0
@@ -361,12 +357,4 @@ def IRCPartChannel(username, channel):
if not userID:
log.warning("{} doesn't exist".format(username))
return
return partChannel(userID, channel)
def IRCAway(username, message):
userID = userUtils.getID(username)
if not userID:
log.warning("{} doesn't exist".format(username))
return
glob.tokens.getTokenFromUserID(userID).setAwayMessage(message)
return 305 if message == "" else 306
return partChannel(userID, channel)

View File

@@ -296,6 +296,7 @@ class Client:
# Disconnect other IRC clients from the same user
for _, value in self.server.clients.items():
if value.IRCUsername.lower() == self.IRCUsername.lower() and value != self:
print("DISCONNECTERINOOOOOOOOOOOOOOOOOOOOO")
value.disconnect(quitmsg="Connected from another client")
return
elif command == "USER":
@@ -439,6 +440,7 @@ class Client:
# Send the message to bancho and reply
if not recipientIRC.startswith("#"):
print("PMPMPM!!!!!!!!!!")
recipientBancho = chat.fixUsernameForBancho(recipientIRC)
else:
recipientBancho = recipientIRC
@@ -487,14 +489,10 @@ class Client:
"""(fake) PONG command handler"""
pass
def awayHandler(self, command, 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")
def mainHandler(self, command, arguments):
"""Handler for post-login commands"""
handlers = {
"AWAY": self.awayHandler,
#"AWAY": away_handler,
#"ISON": ison_handler,
"JOIN": self.joinHandler,
#"LIST": list_handler,
@@ -520,6 +518,11 @@ class Client:
self.replyCode(421, "Unknown command ({})".format(command))
class Server:
def __init__(self, port):
#self.host = socket.getfqdn("127.0.0.1")[:63]
@@ -619,7 +622,7 @@ class Server:
[x.socket for x in self.clients.values()
if x.writeBufferSize() > 0],
[],
1)
2)
# Handle incoming connections
for x in iwtd:

View File

@@ -1,8 +1,10 @@
"""Global objects and variables"""
import time
from common.ddog import datadogClient
from common.files import fileBuffer, fileLocks
from common.web.api import rateLimit
from objects import channelList
from objects import matchList
from objects import streamList
@@ -25,6 +27,7 @@ banchoConf = None
tokens = tokenList.tokenList()
channels = channelList.channelList()
matches = matchList.matchList()
restarting = False
fLocks = fileLocks.fileLocks()
fileBuffers = fileBuffer.buffersList()
schiavo = schiavo.schiavo()
@@ -43,8 +46,9 @@ gzip = False
localize = False
sentry = False
irc = False
restarting = False
startTime = int(time.time())
streams = streamList.streamList()
rateLimits = rateLimit.rateLimiters(60, 60)

View File

@@ -30,7 +30,7 @@ class token:
self.privileges = userUtils.getPrivileges(self.userID)
self.admin = userUtils.isInPrivilegeGroup(self.userID, "developer") or userUtils.isInPrivilegeGroup(self.userID, "community manager")
self.irc = irc
self.restricted = userUtils.isRestricted(priv=self.privileges)
self.restricted = userUtils.isRestricted(self.userID)
self.loginTime = int(time.time())
self.pingTime = self.loginTime
self.timeOffset = timeOffset
@@ -51,7 +51,6 @@ class token:
self.country = 0
self.location = [0,0]
self.awayMessage = ""
self.sentAway = []
self.matchID = -1
self.tillerino = [0,0,-1.0] # beatmap, mods, acc
self.silenceEndTime = 0
@@ -427,24 +426,4 @@ class token:
def leaveAllStreams(self):
for i in self.streams:
self.leaveStream(i)
def awayCheck(self, userID):
"""
Returns True if userID doesn't know that we are away
Returns False if we are not away or if userID already knows we are away
:param userID: original sender userID
:return:
"""
if self.awayMessage == "" or userID in self.sentAway:
return False
self.sentAway.append(userID)
return True
def updatePrivileges(self):
"""
Force updating self.privileges from db
:return:
"""
self.privileges = userUtils.getPrivileges(self.userID)
self.leaveStream(i)

19
pep.py
View File

@@ -25,6 +25,7 @@ from handlers import apiVerifiedStatusHandler
from handlers import ciTriggerHandler
from handlers import mainHandler
from handlers import heavyHandler
from handlers.api import ping
from helpers import configHelper
from helpers import consoleHelper
from helpers import systemHelper as system
@@ -35,6 +36,7 @@ from objects import fokabot
from objects import glob
def make_app():
return tornado.web.Application([
(r"/", mainHandler.handler),
@@ -44,7 +46,9 @@ def make_app():
(r"/api/v1/ciTrigger", ciTriggerHandler.handler),
(r"/api/v1/verifiedStatus", apiVerifiedStatusHandler.handler),
(r"/api/v1/fokabotMessage", apiFokabotMessageHandler.handler),
(r"/stress", heavyHandler.handler)
(r"/stress", heavyHandler.handler),
(r"/api/v2/ping", ping.handler),
])
if __name__ == "__main__":
@@ -202,19 +206,6 @@ if __name__ == "__main__":
[
datadogClient.periodicCheck("online_users", lambda: len(glob.tokens.tokens)),
datadogClient.periodicCheck("multiplayer_matches", lambda: len(glob.matches.matches)),
datadogClient.periodicCheck("ram_clients", lambda: generalUtils.getTotalSize(glob.tokens)),
datadogClient.periodicCheck("ram_matches", lambda: generalUtils.getTotalSize(glob.matches)),
datadogClient.periodicCheck("ram_channels", lambda: generalUtils.getTotalSize(glob.channels)),
datadogClient.periodicCheck("ram_file_buffers", lambda: generalUtils.getTotalSize(glob.fileBuffers)),
datadogClient.periodicCheck("ram_file_locks", lambda: generalUtils.getTotalSize(glob.fLocks)),
datadogClient.periodicCheck("ram_datadog", lambda: generalUtils.getTotalSize(glob.datadogClient)),
datadogClient.periodicCheck("ram_verified_cache", lambda: generalUtils.getTotalSize(glob.verifiedCache)),
datadogClient.periodicCheck("ram_userid_cache", lambda: generalUtils.getTotalSize(glob.userIDCache)),
#datadogClient.periodicCheck("ram_pool", lambda: generalUtils.getTotalSize(glob.pool)),
datadogClient.periodicCheck("ram_irc", lambda: generalUtils.getTotalSize(glob.ircServer)),
datadogClient.periodicCheck("ram_tornado", lambda: generalUtils.getTotalSize(glob.application)),
datadogClient.periodicCheck("ram_db", lambda: generalUtils.getTotalSize(glob.db)),
])
else:
consoleHelper.printColored("[!] Warning! Datadog stats tracking is disabled!", bcolors.YELLOW)

View File

@@ -3,5 +3,4 @@ tornado
mysqlclient
psutil
raven
bcrypt>=3.1.1
dill
bcrypt