pep.py/objects/tokenList.py

198 lines
4.5 KiB
Python
Raw Normal View History

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
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-06-10 11:15:42 +00:00
def addToken(self, userID, ip = ""):
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-04-19 17:40:59 +00:00
return -- token object
"""
2016-06-10 11:15:42 +00:00
newToken = osuToken.token(userID, ip=ip)
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
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
for key, 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
# Delete this token from the dictionary
self.tokens.pop(key)
# break or items() function throws errors
break
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)
if value.pingTime < timeoutLimit and value.userID != 999:
# 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!
"""
#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")