Skip to content

Commit

Permalink
[core] remove server time cache (fix #114)
Browse files Browse the repository at this point in the history
  • Loading branch information
calendulish committed Sep 9, 2024
1 parent 5ccfe95 commit 36c9c2a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 41 deletions.
17 changes: 4 additions & 13 deletions src/steam_tools_ng/core/steamguard.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
#
import aiohttp
import asyncio
import binascii
import logging
from typing import AsyncGenerator

import aiohttp
from stlib import universe, webapi

from . import utils
from .. import i18n, config

Expand All @@ -34,23 +35,13 @@
_ = i18n.get_translation


@utils.time_offset_cache(ttl=3600)
def cached_server_time() -> int:
if not client:
raise ProcessLookupError

with client.SteamGameServer() as server:
real_time = server.get_server_real_time()
assert isinstance(real_time, int)
return real_time


async def main() -> AsyncGenerator[utils.ModuleData, None]:
shared_secret = config.parser.get("login", "shared_secret")
webapi_session = webapi.SteamWebAPI.get_session(0)

try:
server_time = cached_server_time()
with client.SteamGameServer() as server:
server_time = server.get_server_real_time()
except ProcessLookupError:
yield utils.ModuleData(error=_("Steam is not running."), info=_("Fallbacking server time to WebAPI"))

Expand Down
29 changes: 1 addition & 28 deletions src/steam_tools_ng/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
import codecs
import inspect
import logging
import time
from dataclasses import dataclass
from functools import cache, wraps
from typing import Tuple, Any, Callable, AsyncGenerator
from typing import Tuple, Any, AsyncGenerator


@dataclass
Expand Down Expand Up @@ -67,31 +65,6 @@ async def timed_module_data(wait_offset: int, module_data: ModuleData) -> AsyncG
await asyncio.sleep(1)


def time_offset_cache(ttl: int = 60) -> Callable[[Callable[[], int]], Callable[[], int]]:
def wrapper(function_: Any) -> Callable[[], int]:
function_ = cache(function_)
function_.time_base = time.time()

@wraps(function_)
def wrapped() -> int:
if time.time() >= function_.time_base + ttl:
function_.cache_clear()
function_.time_base = time.time()

time_raw = function_()

if function_.cache_info().currsize == 0:
assert isinstance(time_raw, int)
return time_raw

function_.time_offset = function_.time_base - time_raw
return round(time.time() + function_.time_offset)

return wrapped

return wrapper


def encode_password(__password: str) -> str:
password_key = codecs.encode(__password.encode(), 'base64')
encrypted_password = codecs.encode(password_key.decode(), 'rot13')
Expand Down

0 comments on commit 36c9c2a

Please sign in to comment.