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) dt = datetime.datetime.utcfromtimestamp(record.created)
msg = ( msg = (
f"{emoji} `[{dt:%Y-%m-%d %H:%M:%S}] " 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) 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, cache=Cache.MEMORY,
namespace="network", namespace="network",
) )
async def get_crimeflare_result( async def get_crimeflare_result(ip: str) -> Optional[str]:
session: aiohttp.ClientSession, ip: str
) -> Optional[str]:
try: try:
async with session.post( async with aiohttp.ClientSession() as cs:
async with cs.post(
"http://www.crimeflare.org:82/cgi-bin/cfsearch.cgi", "http://www.crimeflare.org:82/cgi-bin/cfsearch.cgi",
data=f"cfS={ip}", data=f"cfS={ip}",
timeout=aiohttp.ClientTimeout(total=15), timeout=aiohttp.ClientTimeout(total=21),
) as s: ) as s:
result = re.search(r"(\d*\.\d*\.\d*\.\d*)", await s.text()) result = re.search(r"(\d*\.\d*\.\d*\.\d*)", await s.text())
@ -223,13 +222,12 @@ async def get_pydig_result(
cache=Cache.MEMORY, cache=Cache.MEMORY,
namespace="network", namespace="network",
) )
async def get_peeringdb_net_result( async def get_peeringdb_net_result(asn: str) -> dict:
session: aiohttp.ClientSession, asn: str
) -> dict:
try: try:
async with session.get( async with aiohttp.ClientSession() as cs:
async with cs.get(
f"https://peeringdb.com/api/net?asn={asn}", f"https://peeringdb.com/api/net?asn={asn}",
timeout=aiohttp.ClientTimeout(total=8), timeout=aiohttp.ClientTimeout(total=21),
) as s: ) as s:
return await s.json() return await s.json()
except (asyncio.exceptions.TimeoutError,): except (asyncio.exceptions.TimeoutError,):

View file

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

View file

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