Skip to content

Commit

Permalink
attempt 47217421
Browse files Browse the repository at this point in the history
  • Loading branch information
cop-discord committed Jun 15, 2024
1 parent 9cd8b9c commit b265e7f
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions discord/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,13 @@ def get_ratelimit(self, key: str) -> Ratelimit:
self._try_clear_expired_ratelimits()
return value

async def change_connector(self, local_addr: tuple):
self.__session._connector = aiohttp.TCPConnector(local_addr = local_addr, family = socket.AF_INET)

async def change_back(self):
self.__session._connector = self.connector


async def request(
self,
route: Route,
Expand Down Expand Up @@ -646,7 +653,7 @@ async def request(
'User-Agent': self.user_agent,
}
if local_addr is not None:
self.connector.local_addr=local_addr
self.__session._connector = aiohttp.TCPConnector(local_addr = local_addr, family = socket.AF_INET)
if token is not None:
headers['Authorization'] = token
else:
Expand Down Expand Up @@ -702,8 +709,6 @@ async def request(

# even errors have text involved in them so this is safe to call
data = await json_or_text(response)
if local_addr != None:
if self.local_addr != None: self.connector.local_addr=self.local_addr
# Update and use rate limit information if the bucket header is present
discord_hash = response.headers.get('X-Ratelimit-Bucket')
# I am unsure if X-Ratelimit-Bucket is always available
Expand Down Expand Up @@ -747,12 +752,16 @@ async def request(
# the request was successful so just return the text/json
if 300 > response.status >= 200:
_log.debug(f'{method} {url} has received {data}')
if local_addr is not None:
await self.change_back()
return data

# we are being rate limited
if response.status == 429:
if not response.headers.get('Via') or isinstance(data, str):
# Banned by Cloudflare more than likely.
if local_addr is not None:
await self.change_back()
raise HTTPException(response, data)

if ratelimit.remaining > 0:
Expand All @@ -770,6 +779,8 @@ async def request(
_log.warning(
f'We are being rate limited. {method} {url} responded with 429. Timeout of {retry_after:.2f} was too long, erroring instead.',
)
if local_addr is not None:
await self.change_back()
raise RateLimited(retry_after)

fmt = f'We are being rate limited. {method} {url} responded with 429. Retrying in {retry_after:.2f} seconds.'
Expand Down Expand Up @@ -806,12 +817,17 @@ async def request(
if self.anti_cloudflare_ban == True:
if self.redis != None:
d=await self.redis.ratelimited('invalidss',9950,6000,1)
if d == True: raise InvalidRatelimit(int(await self.redis.ttl(self.redis.rl_keys['invalidss'])))
if d == True:
if local_addr is not None:
await self.change_back()
raise InvalidRatelimit(int(await self.redis.ttl(self.redis.rl_keys['invalidss'])))
else:
d=await self.invalid_ratelimiter.ratelimit("invalids",self.invalid_limit,600)
if d == True:
v=self.invalid_ratelimiter
time_remaining=(v.delete['invalids']['last']+v.delete['invalids']['bucket'])-int(datetime.datetime.now().timestamp())
if local_addr is not None:
await self.change_back()
raise InvalidRatelimit(time_remaining)
if response.status == 403:
raise Forbidden(response, data)
Expand All @@ -832,11 +848,13 @@ async def request(

finally:
if local_addr is not None:
self.connector.local_addr=self.local_addr
await self.change_back()

if response is not None:
# We've run out of retries, raise.
if response.status >= 500:
if local_addr is not None:
await self.change_back()
raise DiscordServerError(response, data)

raise HTTPException(response, data)
Expand Down

0 comments on commit b265e7f

Please sign in to comment.