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,19 +134,18 @@ 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(
"http://www.crimeflare.org:82/cgi-bin/cfsearch.cgi",
data=f"cfS={ip}",
timeout=aiohttp.ClientTimeout(total=15),
) as s:
result = re.search(r"(\d*\.\d*\.\d*\.\d*)", await s.text())
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=21),
) as s:
result = re.search(r"(\d*\.\d*\.\d*\.\d*)", await s.text())
if result:
return result.group()
if result:
return result.group()
except (aiohttp.ClientError, asyncio.exceptions.TimeoutError):
pass
@ -223,15 +222,14 @@ 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(
f"https://peeringdb.com/api/net?asn={asn}",
timeout=aiohttp.ClientTimeout(total=8),
) as s:
return await s.json()
async with aiohttp.ClientSession() as cs:
async with cs.get(
f"https://peeringdb.com/api/net?asn={asn}",
timeout=aiohttp.ClientTimeout(total=21),
) as s:
return await s.json()
except (asyncio.exceptions.TimeoutError,):
pass

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,38 +173,39 @@ class Network(commands.Cog):
"5": 0x343A40,
}
async with self.bot.session.get(
str(ip),
headers=headers,
timeout=aiohttp.ClientTimeout(total=8),
) as s:
e = discord.Embed(
title=f"Headers : {ip}",
color=colors.get(str(s.status)[0], 0x6C757D),
)
e.add_field(
name="Status", value=f"```{s.status}```", inline=True
)
e.set_thumbnail(url=f"https://http.cat/{s.status}")
async with aiohttp.ClientSession() as cs:
async with cs.get(
str(ip),
headers=headers,
timeout=aiohttp.ClientTimeout(total=8),
) as s:
e = discord.Embed(
title=f"Headers : {ip}",
color=colors.get(str(s.status)[0], 0x6C757D),
)
e.add_field(
name="Status", value=f"```{s.status}```", inline=True
)
e.set_thumbnail(url=f"https://http.cat/{s.status}")
headers = dict(s.headers.items())
headers.pop("Set-Cookie", headers)
headers = dict(s.headers.items())
headers.pop("Set-Cookie", headers)
fail = False
fail = False
for key, value in headers.items():
fail, output = await shorten(ctx.session, value, 50, fail)
for key, value in headers.items():
fail, output = await shorten(value, 50, fail)
if output["link"]:
value = _(
"[show all]({})", ctx, self.bot.config
).format(output["link"])
else:
value = f"```\n{output['text']}```"
if output["link"]:
value = _(
"[show all]({})", ctx, self.bot.config
).format(output["link"])
else:
value = f"```\n{output['text']}```"
e.add_field(name=key, value=value, inline=True)
e.add_field(name=key, value=value, inline=True)
await ctx.send(embed=e)
await ctx.send(embed=e)
except (
ClientConnectorError,
InvalidURL,
@ -260,25 +259,26 @@ 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(
f"https://isitdown.site/api/v3/{domain}",
timeout=aiohttp.ClientTimeout(total=8),
) as s:
json = await s.json()
async with aiohttp.ClientSession() as cs:
async with cs.get(
f"https://isitdown.site/api/v3/{domain}",
timeout=aiohttp.ClientTimeout(total=8),
) as s:
json = await s.json()
if json["isitdown"]:
title = _("Down...", ctx, self.bot.config)
color = 0xDC3545
else:
title = _("Up!", ctx, self.bot.config)
color = 0x28A745
if json["isitdown"]:
title = _("Down...", ctx, self.bot.config)
color = 0xDC3545
else:
title = _("Up!", ctx, self.bot.config)
color = 0x28A745
e = discord.Embed(title=title, color=color)
e.set_thumbnail(
url=f"https://http.cat/{json['response_code']}"
)
e = discord.Embed(title=title, color=color)
e.set_thumbnail(
url=f"https://http.cat/{json['response_code']}"
)
await ctx.send(embed=e)
await ctx.send(embed=e)
except (
ClientConnectorError,
@ -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,14 +37,15 @@ async def shorten(
if not fail:
try:
async with session.post(
"https://paste.ramle.be/documents",
data=text.encode(),
timeout=aiohttp.ClientTimeout(total=0.300),
) as r:
output[
"link"
] = f"https://paste.ramle.be/{(await r.json())['key']}"
async with aiohttp.ClientSession() as cs:
async with cs.post(
"https://paste.ramle.be/documents",
data=text.encode(),
timeout=aiohttp.ClientTimeout(total=0.300),
) as r:
output[
"link"
] = f"https://paste.ramle.be/{(await r.json())['key']}"
except (aiohttp.ClientError, asyncio.exceptions.TimeoutError):
fail = True