2016-05-18 17:12:46 +00:00
|
|
|
from objects import osuToken
|
2016-06-10 11:15:42 +00:00
|
|
|
from objects import glob
|
2016-04-19 17:40:59 +00:00
|
|
|
import time
|
|
|
|
import threading
|
2016-05-18 17:12:46 +00:00
|
|
|
from events import logoutEvent
|
2016-06-07 20:46:31 +00:00
|
|
|
from helpers import logHelper as log
|
2016-06-10 11:15:42 +00:00
|
|
|
from helpers import userHelper
|
2016-04-19 17:40:59 +00:00
|
|
|
|
|
|
|
class tokenList:
|
|
|
|
"""
|
|
|
|
List of connected osu tokens
|
|
|
|
|
|
|
|
tokens -- dictionary. key: token string, value: token object
|
|
|
|
"""
|
|
|
|
|
2016-06-10 14:22:14 +00:00
|
|
|
def __init__(self):
|
|
|
|
"""
|
|
|
|
Initialize a tokens list
|
|
|
|
"""
|
|
|
|
self.tokens = {}
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-07-14 10:37:07 +00:00
|
|
|
def addToken(self, userID, ip = "", irc = False):
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
|
|
|
Add a token object to tokens list
|
|
|
|
|
2016-06-10 11:15:42 +00:00
|
|
|
userID -- user id associated to that token
|
2016-07-14 10:37:07 +00:00
|
|
|
irc -- if True, set this token as IRC client
|
2016-04-19 17:40:59 +00:00
|
|
|
return -- token object
|
|
|
|
"""
|
|
|
|
|
2016-07-14 10:37:07 +00:00
|
|
|
newToken = osuToken.token(userID, ip=ip, irc=irc)
|
2016-04-19 17:40:59 +00:00
|
|
|
self.tokens[newToken.token] = newToken
|
|
|
|
return newToken
|
|
|
|
|
2016-06-10 11:15:42 +00:00
|
|
|
def deleteToken(self, token):
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
|
|
|
Delete a token from token list if it exists
|
|
|
|
|
2016-06-10 11:15:42 +00:00
|
|
|
token -- token string
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
|
|
|
|
2016-06-10 11:15:42 +00:00
|
|
|
if token in self.tokens:
|
|
|
|
# Delete session from DB
|
2016-07-14 10:37:07 +00:00
|
|
|
if self.tokens[token].ip != "":
|
|
|
|
userHelper.deleteBanchoSessions(self.tokens[token].userID, self.tokens[token].ip)
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-06-10 11:15:42 +00:00
|
|
|
# Pop token from list
|
|
|
|
self.tokens.pop(token)
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-06-10 11:15:42 +00:00
|
|
|
|
|
|
|
def getUserIDFromToken(self, token):
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
|
|
|
Get user ID from a token
|
|
|
|
|
2016-06-10 11:15:42 +00:00
|
|
|
token -- token to find
|
2016-04-19 17:40:59 +00:00
|
|
|
|
|
|
|
return: false if not found, userID if found
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Make sure the token exists
|
2016-06-10 11:15:42 +00:00
|
|
|
if token not in self.tokens:
|
2016-04-19 17:40:59 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
# Get userID associated to that token
|
2016-06-10 11:15:42 +00:00
|
|
|
return self.tokens[token].userID
|
2016-04-19 17:40:59 +00:00
|
|
|
|
|
|
|
|
2016-06-10 11:15:42 +00:00
|
|
|
def getTokenFromUserID(self, userID):
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
|
|
|
Get token from a user ID
|
|
|
|
|
2016-06-10 11:15:42 +00:00
|
|
|
userID -- user ID to find
|
2016-04-19 17:40:59 +00:00
|
|
|
return -- False if not found, token object if found
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Make sure the token exists
|
|
|
|
for _, value in self.tokens.items():
|
2016-06-10 11:15:42 +00:00
|
|
|
if value.userID == userID:
|
2016-04-19 17:40:59 +00:00
|
|
|
return value
|
|
|
|
|
|
|
|
# Return none if not found
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2016-06-10 11:15:42 +00:00
|
|
|
def getTokenFromUsername(self, username):
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
|
|
|
Get token from a username
|
|
|
|
|
2016-06-10 11:15:42 +00:00
|
|
|
username -- username to find
|
2016-04-19 17:40:59 +00:00
|
|
|
return -- False if not found, token object if found
|
|
|
|
"""
|
|
|
|
|
|
|
|
# lowercase
|
2016-06-10 11:15:42 +00:00
|
|
|
who = username.lower()
|
2016-04-19 17:40:59 +00:00
|
|
|
|
|
|
|
# Make sure the token exists
|
|
|
|
for _, value in self.tokens.items():
|
|
|
|
if value.username.lower() == who:
|
|
|
|
return value
|
|
|
|
|
|
|
|
# Return none if not found
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2016-06-10 11:15:42 +00:00
|
|
|
def deleteOldTokens(self, userID):
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
|
|
|
Delete old userID's tokens if found
|
|
|
|
|
2016-06-10 11:15:42 +00:00
|
|
|
userID -- tokens associated to this user will be deleted
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Delete older tokens
|
2016-07-14 10:37:07 +00:00
|
|
|
for key, value in list(self.tokens.items()):
|
2016-06-10 11:15:42 +00:00
|
|
|
if value.userID == userID:
|
2016-04-19 17:40:59 +00:00
|
|
|
# Delete this token from the dictionary
|
2016-07-14 10:37:07 +00:00
|
|
|
self.tokens[key].kick("You have logged in from somewhere else. You can't connect to Bancho/IRC from more than one device at the same time.")
|
|
|
|
#self.tokens.pop(key)
|
2016-04-19 17:40:59 +00:00
|
|
|
|
|
|
|
# break or items() function throws errors
|
2016-07-14 10:37:07 +00:00
|
|
|
#break
|
2016-04-19 17:40:59 +00:00
|
|
|
|
|
|
|
|
2016-06-10 11:15:42 +00:00
|
|
|
def multipleEnqueue(self, packet, who, but = False):
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
|
|
|
Enqueue a packet to multiple users
|
|
|
|
|
2016-06-10 11:15:42 +00:00
|
|
|
packet -- packet bytes to enqueue
|
|
|
|
who -- userIDs array
|
|
|
|
but -- if True, enqueue to everyone but users in who array
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
for _, value in self.tokens.items():
|
|
|
|
shouldEnqueue = False
|
2016-06-10 11:15:42 +00:00
|
|
|
if value.userID in who and not but:
|
2016-04-19 17:40:59 +00:00
|
|
|
shouldEnqueue = True
|
2016-06-10 11:15:42 +00:00
|
|
|
elif value.userID not in who and but:
|
2016-04-19 17:40:59 +00:00
|
|
|
shouldEnqueue = True
|
|
|
|
|
|
|
|
if shouldEnqueue:
|
2016-06-10 11:15:42 +00:00
|
|
|
value.enqueue(packet)
|
2016-04-19 17:40:59 +00:00
|
|
|
|
2016-06-10 11:15:42 +00:00
|
|
|
def enqueueAll(self, packet):
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
|
|
|
Enqueue packet(s) to every connected user
|
|
|
|
|
2016-06-10 11:15:42 +00:00
|
|
|
packet -- packet bytes to enqueue
|
2016-04-19 17:40:59 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
for _, value in self.tokens.items():
|
2016-06-10 11:15:42 +00:00
|
|
|
value.enqueue(packet)
|
2016-04-19 17:40:59 +00:00
|
|
|
|
|
|
|
def usersTimeoutCheckLoop(self, __timeoutTime = 100, __checkTime = 100):
|
|
|
|
"""
|
|
|
|
Deletes all timed out users.
|
|
|
|
If called once, will recall after __checkTime seconds and so on, forever
|
|
|
|
CALL THIS FUNCTION ONLY ONCE!
|
|
|
|
|
|
|
|
__timeoutTime - seconds of inactivity required to disconnect someone (Default: 100)
|
|
|
|
__checkTime - seconds between loops (Default: 100)
|
|
|
|
"""
|
|
|
|
|
|
|
|
timedOutTokens = [] # timed out users
|
|
|
|
timeoutLimit = time.time()-__timeoutTime
|
|
|
|
for key, value in self.tokens.items():
|
|
|
|
# Check timeout (fokabot is ignored)
|
2016-07-14 10:37:07 +00:00
|
|
|
if value.pingTime < timeoutLimit and value.userID != 999 and value.irc == False:
|
2016-04-19 17:40:59 +00:00
|
|
|
# That user has timed out, add to disconnected tokens
|
|
|
|
# We can't delete it while iterating or items() throws an error
|
|
|
|
timedOutTokens.append(key)
|
|
|
|
|
|
|
|
# Delete timed out users from self.tokens
|
|
|
|
# i is token string (dictionary key)
|
|
|
|
for i in timedOutTokens:
|
|
|
|
logoutEvent.handle(self.tokens[i], None)
|
|
|
|
|
|
|
|
# Schedule a new check (endless loop)
|
|
|
|
threading.Timer(__checkTime, self.usersTimeoutCheckLoop, [__timeoutTime, __checkTime]).start()
|
2016-06-10 14:22:14 +00:00
|
|
|
|
|
|
|
def spamProtectionResetLoop(self):
|
|
|
|
"""
|
|
|
|
Reset spam rate every 10 seconds.
|
|
|
|
CALL THIS FUNCTION ONLY ONCE!
|
|
|
|
"""
|
2016-06-16 11:38:17 +00:00
|
|
|
#log.debug("Resetting spam protection...")
|
2016-06-10 14:22:14 +00:00
|
|
|
|
|
|
|
# Reset spamRate for every token
|
|
|
|
for _, value in self.tokens.items():
|
|
|
|
value.spamRate = 0
|
|
|
|
|
|
|
|
# Schedule a new check (endless loop)
|
|
|
|
threading.Timer(10, self.spamProtectionResetLoop).start()
|
2016-06-10 11:15:42 +00:00
|
|
|
|
|
|
|
def deleteBanchoSessions(self):
|
|
|
|
"""
|
|
|
|
Truncate bancho_sessions table.
|
|
|
|
Call at bancho startup to delete old cached sessions
|
|
|
|
"""
|
|
|
|
glob.db.execute("TRUNCATE TABLE bancho_sessions")
|
2016-07-14 10:37:07 +00:00
|
|
|
|
|
|
|
def tokenExists(self, username = "", userID = -1):
|
|
|
|
"""
|
|
|
|
Check if a token exists (aka check if someone is connected)
|
|
|
|
|
|
|
|
username -- Optional.
|
|
|
|
userID -- Optional.
|
|
|
|
return -- True if it exists, otherwise False
|
|
|
|
|
|
|
|
Use username or userid, not both at the same time.
|
|
|
|
"""
|
|
|
|
if userID > -1:
|
|
|
|
return True if self.getTokenFromUserID(userID) is not None else False
|
|
|
|
else:
|
|
|
|
return True if self.getTokenFromUsername(username) is not None else False
|