Browse Source

Utilize provided proxies in build info fetching (#759)

pull/10109/head
Rocco A 5 months ago
committed by GitHub
parent
commit
f7bec82d65
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 7
      discord/http.py
  2. 20
      discord/utils.py

7
discord/http.py

@ -635,7 +635,12 @@ class HTTPClient:
connector=self.connector, trace_configs=None if self.http_trace is None else [self.http_trace] connector=self.connector, trace_configs=None if self.http_trace is None else [self.http_trace]
) )
) )
self.super_properties, self.encoded_super_properties = sp, _ = await utils._get_info(session)
proxy = self.proxy
proxy_auth = self.proxy_auth
self.super_properties, self.encoded_super_properties = sp, _ = await utils._get_info(session, proxy, proxy_auth)
_log.info('Found user agent %s, build number %s.', sp.get('browser_user_agent'), sp.get('client_build_number')) _log.info('Found user agent %s, build number %s.', sp.get('browser_user_agent'), sp.get('client_build_number'))
self._started = True self._started = True

20
discord/utils.py

@ -73,6 +73,7 @@ import sys
from threading import Timer from threading import Timer
import types import types
import warnings import warnings
import aiohttp
import yarl import yarl
@ -1452,22 +1453,22 @@ _CLIENT_ASSET_REGEX = re.compile(r'assets/([a-z0-9.]+)\.js')
_BUILD_NUMBER_REGEX = re.compile(r'build_number:"(\d+)"') _BUILD_NUMBER_REGEX = re.compile(r'build_number:"(\d+)"')
async def _get_info(session: ClientSession) -> Tuple[Dict[str, Any], str]: async def _get_info(session: ClientSession, proxy: Optional[str] = None, proxy_auth: Optional[aiohttp.BasicAuth] = None) -> Tuple[Dict[str, Any], str]:
try: try:
async with session.post('https://cordapi.dolfi.es/api/v2/properties/web', timeout=5) as resp: async with session.post('https://cordapi.dolfi.es/api/v2/properties/web', timeout=5, proxy=proxy, proxy_auth=proxy_auth) as resp:
json = await resp.json() json = await resp.json()
return json['properties'], json['encoded'] return json['properties'], json['encoded']
except Exception: except Exception:
_log.info('Info API temporarily down. Falling back to manual retrieval...') _log.info('Info API temporarily down. Falling back to manual retrieval...')
try: try:
bn = await _get_build_number(session) bn = await _get_build_number(session, proxy, proxy_auth)
except Exception: except Exception:
_log.critical('Could not retrieve client build number. Falling back to hardcoded value...') _log.critical('Could not retrieve client build number. Falling back to hardcoded value...')
bn = FALLBACK_BUILD_NUMBER bn = FALLBACK_BUILD_NUMBER
try: try:
bv = await _get_browser_version(session) bv = await _get_browser_version(session, proxy, proxy_auth)
except Exception: except Exception:
_log.critical('Could not retrieve browser version. Falling back to hardcoded value...') _log.critical('Could not retrieve browser version. Falling back to hardcoded value...')
bv = FALLBACK_BROWSER_VERSION bv = FALLBACK_BROWSER_VERSION
@ -1492,16 +1493,16 @@ async def _get_info(session: ClientSession) -> Tuple[Dict[str, Any], str]:
return properties, b64encode(_to_json(properties).encode()).decode('utf-8') return properties, b64encode(_to_json(properties).encode()).decode('utf-8')
async def _get_build_number(session: ClientSession) -> int: async def _get_build_number(session: ClientSession, proxy: Optional[str] = None, proxy_auth: Optional[aiohttp.BasicAuth] = None) -> int:
"""Fetches client build number""" """Fetches client build number"""
async with session.get('https://discord.com/login') as resp: async with session.get('https://discord.com/login', proxy=proxy, proxy_auth=proxy_auth) as resp:
app = await resp.text() app = await resp.text()
assets = _CLIENT_ASSET_REGEX.findall(app) assets = _CLIENT_ASSET_REGEX.findall(app)
if not assets: if not assets:
raise RuntimeError('Could not find client asset files') raise RuntimeError('Could not find client asset files')
for asset in assets[::-1]: for asset in assets[::-1]:
async with session.get(f'https://discord.com/assets/{asset}.js') as resp: async with session.get(f'https://discord.com/assets/{asset}.js', proxy=proxy, proxy_auth=proxy_auth) as resp:
build = await resp.text() build = await resp.text()
match = _BUILD_NUMBER_REGEX.search(build) match = _BUILD_NUMBER_REGEX.search(build)
if match is None: if match is None:
@ -1511,10 +1512,11 @@ async def _get_build_number(session: ClientSession) -> int:
raise RuntimeError('Could not find client build number') raise RuntimeError('Could not find client build number')
async def _get_browser_version(session: ClientSession) -> str: async def _get_browser_version(session: ClientSession, proxy: Optional[str] = None, proxy_auth: Optional[aiohttp.BasicAuth] = None) -> str:
"""Fetches the latest Windows 10/Chrome major browser version.""" """Fetches the latest Windows 10/Chrome major browser version."""
async with session.get( async with session.get(
'https://versionhistory.googleapis.com/v1/chrome/platforms/win/channels/stable/versions' 'https://versionhistory.googleapis.com/v1/chrome/platforms/win/channels/stable/versions',
proxy=proxy, proxy_auth=proxy_auth
) as response: ) as response:
data = await response.json() data = await response.json()
major = data['versions'][0]['version'].split('.')[0] major = data['versions'][0]['version'].split('.')[0]

Loading…
Cancel
Save