fix(commands:peeringdb|Network): fix timeout

This commit is contained in:
Romain J 2021-04-23 00:52:23 +02:00
parent 2e7934148e
commit 5b7c905ac8
4 changed files with 75 additions and 78 deletions

View file

@ -130,7 +130,7 @@ class Logs(commands.Cog):
dt = datetime.datetime.utcfromtimestamp(record.created)
msg = (
f"{emoji} `[{dt:%Y-%m-%d %H:%M:%S}] "
f"{await shorten(self.bot.session, record.msg, 1500)}`"
f"{await shorten(record.msg, 1500)}`"
)
await self.webhook("gateway").send(msg)

View file

@ -134,14 +134,13 @@ async def get_ipinfo_result(apikey: str, ip: str) -> dict:
cache=Cache.MEMORY,
namespace="network",
)
async def get_crimeflare_result(
session: aiohttp.ClientSession, ip: str
) -> Optional[str]:
async def get_crimeflare_result(ip: str) -> Optional[str]:
try:
async with session.post(
async with aiohttp.ClientSession() as cs:
async with cs.post(
"http://www.crimeflare.org:82/cgi-bin/cfsearch.cgi",
data=f"cfS={ip}",
timeout=aiohttp.ClientTimeout(total=15),
timeout=aiohttp.ClientTimeout(total=21),
) as s:
result = re.search(r"(\d*\.\d*\.\d*\.\d*)", await s.text())
@ -223,13 +222,12 @@ async def get_pydig_result(
cache=Cache.MEMORY,
namespace="network",
)
async def get_peeringdb_net_result(
session: aiohttp.ClientSession, asn: str
) -> dict:
async def get_peeringdb_net_result(asn: str) -> dict:
try:
async with session.get(
async with aiohttp.ClientSession() as cs:
async with cs.get(
f"https://peeringdb.com/api/net?asn={asn}",
timeout=aiohttp.ClientTimeout(total=8),
timeout=aiohttp.ClientTimeout(total=21),
) as s:
return await s.json()
except (asyncio.exceptions.TimeoutError,):

View file

@ -143,9 +143,7 @@ class Network(commands.Cog):
ctx: ContextPlus,
ip: DomainConverter,
):
crimeflare_result = await get_crimeflare_result(
self.bot.session, str(ip)
)
crimeflare_result = await get_crimeflare_result(str(ip))
if crimeflare_result:
alt_ctx = await copy_context_with(
@ -175,7 +173,8 @@ class Network(commands.Cog):
"5": 0x343A40,
}
async with self.bot.session.get(
async with aiohttp.ClientSession() as cs:
async with cs.get(
str(ip),
headers=headers,
timeout=aiohttp.ClientTimeout(total=8),
@ -195,7 +194,7 @@ class Network(commands.Cog):
fail = False
for key, value in headers.items():
fail, output = await shorten(ctx.session, value, 50, fail)
fail, output = await shorten(value, 50, fail)
if output["link"]:
value = _(
@ -260,7 +259,8 @@ class Network(commands.Cog):
@command_extra(name="isdown", aliases=["is_down", "down?"], deletable=True)
async def _isdown(self, ctx: ContextPlus, domain: IPConverter):
try:
async with self.bot.session.get(
async with aiohttp.ClientSession() as cs:
async with cs.get(
f"https://isitdown.site/api/v3/{domain}",
timeout=aiohttp.ClientTimeout(total=8),
) as s:
@ -297,9 +297,7 @@ class Network(commands.Cog):
async def _peeringdb(self, ctx: ContextPlus, asn: ASConverter):
check_asn_or_raise(str(asn))
data: dict = (
await get_peeringdb_net_result(self.bot.session, str(asn))
)["data"]
data: dict = (await get_peeringdb_net_result(str(asn)))["data"]
if not data:
return await ctx.send(
@ -344,7 +342,7 @@ class Network(commands.Cog):
)
if data["notes"]:
output = (await shorten(self.bot.session, data["notes"], 550))[1]
output = (await shorten(data["notes"], 550))[1]
e.description = output["text"]
if data["created"]:
e.timestamp = datetime.strptime(

View file

@ -28,7 +28,7 @@ def typing(func):
async def shorten(
session, text: str, length: int, fail: bool = False
text: str, length: int, fail: bool = False
) -> tuple[bool, dict]:
output: Dict[str, str] = {"text": text[:length], "link": ""}
@ -37,7 +37,8 @@ async def shorten(
if not fail:
try:
async with session.post(
async with aiohttp.ClientSession() as cs:
async with cs.post(
"https://paste.ramle.be/documents",
data=text.encode(),
timeout=aiohttp.ClientTimeout(total=0.300),