Commit 79eee266 authored by Geovanny's avatar Geovanny

Added timeout for sessions

parent 7446f00d
...@@ -3,6 +3,7 @@ server/node_modules/ ...@@ -3,6 +3,7 @@ server/node_modules/
server/.env server/.env
discord/.env discord/.env
discord/__pycache__ discord/__pycache__
discord/testy.py
web/node_modules web/node_modules
*.pyc *.pyc
__pycache__ __pycache__
......
...@@ -12,15 +12,18 @@ from house.war.manager import WarManager ...@@ -12,15 +12,18 @@ from house.war.manager import WarManager
from util.command_error_handler import CommandErrorHandler from util.command_error_handler import CommandErrorHandler
from util.help import EditedMinimalHelpCommand, PaginatedHelpCommand from util.help import EditedMinimalHelpCommand, PaginatedHelpCommand
from util.api_requests import Api from util.api_requests import Api
from util.timed_dict import TimedDict
from settings import DISCORD_TOKEN from settings import DISCORD_TOKEN
COOKIE_LIFETIME = 18000;
class ConqBot(commands.Bot): class ConqBot(commands.Bot):
def __init__(self, command_prefix='>'): def __init__(self, command_prefix='>'):
super().__init__( super().__init__(
command_prefix=command_prefix, command_prefix=command_prefix,
help_command=EditedMinimalHelpCommand()) help_command=EditedMinimalHelpCommand())
self.id_to_session = {} self.id_to_session = TimedDict()
self.static_help_command = self.help_command self.static_help_command = self.help_command
command_impl = self.help_command._command_impl command_impl = self.help_command._command_impl
...@@ -38,6 +41,7 @@ class ConqBot(commands.Bot): ...@@ -38,6 +41,7 @@ class ConqBot(commands.Bot):
return self.id_to_session[user.id] return self.id_to_session[user.id]
new_session = Session() new_session = Session()
await Api.postSession('/user/d-login', {"discordId": user.id}, new_session); await Api.postSession('/user/d-login', {"discordId": user.id}, new_session);
self.id_to_session.set_key(user.id, new_session, COOKIE_LIFETIME)
self.id_to_session[user.id] = new_session self.id_to_session[user.id] = new_session
return new_session return new_session
......
import time
class TimedDict(dict):
def __init__(self):
self.__table = {}
def set_key(self, key, value, timeout=1):
# add timer to table
self.__table[key] = time.time() + timeout;
super(TimedDict, self).__setitem__(key, value);
def __contains__(self, key):
return (key in self.__table) and (time.time() < self.__table.get(key))
def __iter__(self):
for item in dict.__iter__(self):
if time.time() < self.__table.get(item):
yield item
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment