From 7467498d04e69b67619fa46b3d5b359fd6c1d614 Mon Sep 17 00:00:00 2001 From: detiam Date: Sun, 13 Oct 2024 23:47:57 +0800 Subject: [PATCH] fix(cm): Fetches CM server list from WebAPI https://github.com/ValvePython/steam/issues/474#issuecomment-2408851031 --- steam/core/cm.py | 50 ++++++++++++++---------------------------------- 1 file changed, 14 insertions(+), 36 deletions(-) diff --git a/steam/core/cm.py b/steam/core/cm.py index 0b6c51a5..57887dc8 100644 --- a/steam/core/cm.py +++ b/steam/core/cm.py @@ -136,7 +136,7 @@ def connect(self, retry=0, delay=0): return False if isinstance(self.connection, WebsocketConnection): - self.cm_servers.bootstrap_from_webapi_websocket() + self.cm_servers.bootstrap_from_webapi(cmtype='websockets') elif isinstance(self.connection, TCPConnection): if not self.cm_servers.bootstrap_from_webapi(): self.cm_servers.bootstrap_from_dns() @@ -484,26 +484,33 @@ def bootstrap_from_dns(self): self._LOG.error("DNS boostrap: cm0.steampowered.com resolved no A records") return False - def bootstrap_from_webapi(self, cell_id=0): + def bootstrap_from_webapi(self, cell_id=0, cmtype='netfilter'): """ Fetches CM server list from WebAPI and replaces the current one :param cellid: cell id (0 = global) :type cellid: :class:`int` + :param cmtype: CM type filter + :type cellid: :class:`str` :return: booststrap success :rtype: :class:`bool` """ - self._LOG.debug("Attempting bootstrap via WebAPI") + self._LOG.debug("Attempting bootstrap via WebAPI for %s" % cmtype) from steam import webapi try: - resp = webapi.get('ISteamDirectory', 'GetCMList', 1, params={'cellid': cell_id, - 'http_timeout': 3}) + resp = webapi.get('ISteamDirectory', 'GetCMListForConnect', 1, + params={ + 'cellid': cell_id, + 'cmtype': cmtype, + 'http_timeout': 3 + } + ) except Exception as exp: self._LOG.error("WebAPI boostrap failed: %s" % str(exp)) return False - result = EResult(resp['response']['result']) + result = EResult(resp['response']['success']) if result != EResult.OK: self._LOG.error("GetCMList failed with %s" % repr(result)) @@ -512,41 +519,12 @@ def bootstrap_from_webapi(self, cell_id=0): serverlist = resp['response']['serverlist'] self._LOG.debug("Received %d servers from WebAPI" % len(serverlist)) - def str_to_tuple(serveraddr): - ip, port = serveraddr.split(':') - return str(ip), int(port) - - self.clear() - self.cell_id = cell_id - self.merge_list(map(str_to_tuple, serverlist)) - - return True - - def bootstrap_from_webapi_websocket(self): - """ - Fetches CM server list from WebAPI and replaces the current one - - :return: booststrap success - :rtype: :class:`bool` - """ - self._LOG.debug("Attempting bootstrap via WebAPI for websocket") - - from steam import webapi - try: - resp = webapi.get('ISteamDirectory', 'GetCMListForConnect', 1, params={'cmtype': 'websockets', - 'http_timeout': 3}) - except Exception as exp: - self._LOG.error("WebAPI boostrap failed: %s" % str(exp)) - return False - - serverlist = resp['response']['serverlist'] - self._LOG.debug("Received %d servers from WebAPI" % len(serverlist)) - def str_to_tuple(serverinfo): ip, port = serverinfo['endpoint'].split(':') return str(ip), int(port) self.clear() + self.cell_id = cell_id self.merge_list(map(str_to_tuple, serverlist)) return True