feat(commands|Network>getheaders): feat getheaders command

This commit is contained in:
Romain J 2021-01-26 15:24:10 +01:00
parent fbb61c247d
commit d66bec65ae
14 changed files with 172 additions and 59 deletions

View file

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: Tuxbot-bot\n" "Project-Id-Version: Tuxbot-bot\n"
"Report-Msgid-Bugs-To: rick@gnous.eu\n" "Report-Msgid-Bugs-To: rick@gnous.eu\n"
"POT-Creation-Date: 2021-01-25 16:09+0100\n" "POT-Creation-Date: 2021-01-26 15:18+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"

View file

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: Tuxbot-bot\n" "Project-Id-Version: Tuxbot-bot\n"
"Report-Msgid-Bugs-To: rick@gnous.eu\n" "Report-Msgid-Bugs-To: rick@gnous.eu\n"
"POT-Creation-Date: 2021-01-25 16:09+0100\n" "POT-Creation-Date: 2021-01-26 15:18+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"

View file

@ -7,7 +7,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: Tuxbot-bot\n" "Project-Id-Version: Tuxbot-bot\n"
"Report-Msgid-Bugs-To: rick@gnous.eu\n" "Report-Msgid-Bugs-To: rick@gnous.eu\n"
"POT-Creation-Date: 2020-10-21 01:15+0200\n" "POT-Creation-Date: 2021-01-26 15:18+0100\n"
"PO-Revision-Date: 2020-10-21 01:15+0200\n" "PO-Revision-Date: 2020-10-21 01:15+0200\n"
"Last-Translator: Automatically generated\n" "Last-Translator: Automatically generated\n"
"Language-Team: none\n" "Language-Team: none\n"
@ -16,3 +16,11 @@ msgstr ""
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n"
#: tuxbot/cogs/Logs/logs.py:295
msgid "Sockets stats"
msgstr ""
#: tuxbot/cogs/Logs/logs.py:297
msgid "{} socket events observed ({:.2f}/minute):"
msgstr ""

View file

@ -17,3 +17,11 @@ msgstr ""
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n" "Plural-Forms: nplurals=2; plural=(n > 1);\n"
#: tuxbot/cogs/Logs/logs.py:295
msgid "Sockets stats"
msgstr "Statistiques des évenements"
#: tuxbot/cogs/Logs/logs.py:297
msgid "{} socket events observed ({:.2f}/minute):"
msgstr "{} evenements ont été observés ({:.2f}/minute)"

View file

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: Tuxbot-bot\n" "Project-Id-Version: Tuxbot-bot\n"
"Report-Msgid-Bugs-To: rick@gnous.eu\n" "Report-Msgid-Bugs-To: rick@gnous.eu\n"
"POT-Creation-Date: 2020-10-21 01:15+0200\n" "POT-Creation-Date: 2021-01-26 15:18+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@ -16,3 +16,11 @@ msgstr ""
"MIME-Version: 1.0\n" "MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n" "Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
#: tuxbot/cogs/Logs/logs.py:295
msgid "Sockets stats"
msgstr ""
#: tuxbot/cogs/Logs/logs.py:297
msgid "{} socket events observed ({:.2f}/minute):"
msgstr ""

View file

@ -28,6 +28,26 @@ class IPConverter(commands.Converter):
raise InvalidIp(_("Invalid ip or domain")) raise InvalidIp(_("Invalid ip or domain"))
class IPCheckerConverter(commands.Converter):
async def convert(self, ctx, argument): # skipcq: PYL-W0613
argument_back = argument
argument = argument.replace("http://", "").replace("https://", "")
check_domain = re.match(DOMAIN_PATTERN, argument)
check_ipv4 = re.match(IPV4_PATTERN, argument)
check_ipv6 = re.match(IPV6_PATTERN, argument)
if check_domain or check_ipv4 or check_ipv6:
if argument_back.startswith("https://"):
return "https://" + argument
return "http://" + (
argument if not check_ipv6 else f"[{argument}]"
)
raise InvalidIp(_("Invalid ip or domain"))
class IPVersionConverter(commands.Converter): class IPVersionConverter(commands.Converter):
async def convert(self, ctx, argument): # skipcq: PYL-W0613 async def convert(self, ctx, argument): # skipcq: PYL-W0613
if not argument: if not argument:

View file

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: Tuxbot-bot\n" "Project-Id-Version: Tuxbot-bot\n"
"Report-Msgid-Bugs-To: rick@gnous.eu\n" "Report-Msgid-Bugs-To: rick@gnous.eu\n"
"POT-Creation-Date: 2021-01-19 14:39+0100\n" "POT-Creation-Date: 2021-01-26 15:18+0100\n"
"PO-Revision-Date: 2021-01-19 14:39+0100\n" "PO-Revision-Date: 2021-01-19 14:39+0100\n"
"Last-Translator: Automatically generated\n" "Last-Translator: Automatically generated\n"
"Language-Team: none\n" "Language-Team: none\n"
@ -18,47 +18,50 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n" "Plural-Forms: nplurals=2; plural=(n > 1);\n"
#: tuxbot/cogs/Network/functions/converters.py:22 #: tuxbot/cogs/Network/functions/converters.py:28
#: tuxbot/cogs/Network/functions/converters.py:48
msgid "Invalid ip or domain" msgid "Invalid ip or domain"
msgstr "" msgstr ""
#: tuxbot/cogs/Network/functions/converters.py:35 #: tuxbot/cogs/Network/functions/converters.py:59
msgid "Invalid ip version" msgid "Invalid ip version"
msgstr "" msgstr ""
#: tuxbot/cogs/Network/network.py:49 tuxbot/cogs/Network/network.py:52 #: tuxbot/cogs/Network/functions/utils.py:31
#, python-brace-format
msgid "in v{v}"
msgstr ""
#: tuxbot/cogs/Network/network.py:61
#, python-brace-format
msgid "Impossible to collect information on this in the given version" msgid "Impossible to collect information on this in the given version"
msgstr "" msgstr ""
#: tuxbot/cogs/Network/network.py:89 #: tuxbot/cogs/Network/functions/utils.py:54
#, python-brace-format #, python-brace-format
msgid "IP address {ip_address} is already defined as Private-Use Networks via RFC 1918." msgid "IP address {ip_address} is already defined as Private-Use Networks via RFC 1918."
msgstr "" msgstr ""
#: tuxbot/cogs/Network/network.py:109 #: tuxbot/cogs/Network/network.py:74
msgid "*Retrieving information...*" msgid "*Retrieving information...*"
msgstr "" msgstr ""
#: tuxbot/cogs/Network/network.py:123 #: tuxbot/cogs/Network/network.py:90
#, python-brace-format #, python-brace-format
msgid "Information for ``{ip} ({ip_address})``" msgid "Information for ``{ip} ({ip_address})``"
msgstr "" msgstr ""
#: tuxbot/cogs/Network/network.py:135 tuxbot/cogs/Network/network.py:156 #: tuxbot/cogs/Network/network.py:100 tuxbot/cogs/Network/network.py:130
msgid "Belongs to:" msgid "Belongs to:"
msgstr "" msgstr ""
#: tuxbot/cogs/Network/network.py:140 tuxbot/cogs/Network/network.py:161 #: tuxbot/cogs/Network/network.py:113 tuxbot/cogs/Network/network.py:142
msgid "Region:" msgid "Region:"
msgstr "" msgstr ""
#: tuxbot/cogs/Network/network.py:174 #: tuxbot/cogs/Network/network.py:152
#, python-brace-format #, python-brace-format
msgid "Hostname: {hostname}" msgid "Hostname: {hostname}"
msgstr "" msgstr ""
#: tuxbot/cogs/Network/network.py:182
msgid "[show all]({})"
msgstr ""
#: tuxbot/cogs/Network/network.py:192
msgid "Cannot connect to host {}"
msgstr ""

View file

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: Tuxbot-bot\n" "Project-Id-Version: Tuxbot-bot\n"
"Report-Msgid-Bugs-To: rick@gnous.eu\n" "Report-Msgid-Bugs-To: rick@gnous.eu\n"
"POT-Creation-Date: 2021-01-19 14:39+0100\n" "POT-Creation-Date: 2021-01-26 15:18+0100\n"
"PO-Revision-Date: 2021-01-19 14:39+0100\n" "PO-Revision-Date: 2021-01-19 14:39+0100\n"
"Last-Translator: Automatically generated\n" "Last-Translator: Automatically generated\n"
"Language-Team: none\n" "Language-Team: none\n"
@ -18,47 +18,50 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n" "Plural-Forms: nplurals=2; plural=(n > 1);\n"
#: tuxbot/cogs/Network/functions/converters.py:22 #: tuxbot/cogs/Network/functions/converters.py:28
#: tuxbot/cogs/Network/functions/converters.py:48
msgid "Invalid ip or domain" msgid "Invalid ip or domain"
msgstr "Nome de domaine ou adresse IP invalide" msgstr "Nome de domaine ou adresse IP invalide"
#: tuxbot/cogs/Network/functions/converters.py:35 #: tuxbot/cogs/Network/functions/converters.py:59
msgid "Invalid ip version" msgid "Invalid ip version"
msgstr "Version d'adresse IP invalide" msgstr "Version d'adresse IP invalide"
#: tuxbot/cogs/Network/network.py:49 tuxbot/cogs/Network/network.py:52 #: tuxbot/cogs/Network/functions/utils.py:31
#, python-brace-format
msgid "in v{v}"
msgstr "en v{v}"
#: tuxbot/cogs/Network/network.py:61
#, python-brace-format
msgid "Impossible to collect information on this in the given version" msgid "Impossible to collect information on this in the given version"
msgstr "Impossible de collecter des informations pour cette IP avec la version donnée" msgstr "Impossible de collecter des informations pour cette IP avec la version donnée"
#: tuxbot/cogs/Network/network.py:89 #: tuxbot/cogs/Network/functions/utils.py:54
#, python-brace-format #, python-brace-format
msgid "IP address {ip_address} is already defined as Private-Use Networks via RFC 1918." msgid "IP address {ip_address} is already defined as Private-Use Networks via RFC 1918."
msgstr "L'adresse ip {ip_address} est est reservée à un usage local selon la RFC 1918" msgstr "L'adresse ip {ip_address} est est reservée à un usage local selon la RFC 1918"
#: tuxbot/cogs/Network/network.py:109 #: tuxbot/cogs/Network/network.py:74
msgid "*Retrieving information...*" msgid "*Retrieving information...*"
msgstr "*Récupération des informations...*" msgstr "*Récupération des informations...*"
#: tuxbot/cogs/Network/network.py:123 #: tuxbot/cogs/Network/network.py:90
#, python-brace-format #, python-brace-format
msgid "Information for ``{ip} ({ip_address})``" msgid "Information for ``{ip} ({ip_address})``"
msgstr "Informations pour ``{ip} ({ip_address})``" msgstr "Informations pour ``{ip} ({ip_address})``"
#: tuxbot/cogs/Network/network.py:135 tuxbot/cogs/Network/network.py:156 #: tuxbot/cogs/Network/network.py:100 tuxbot/cogs/Network/network.py:130
msgid "Belongs to:" msgid "Belongs to:"
msgstr "Appartient à :" msgstr "Appartient à :"
#: tuxbot/cogs/Network/network.py:140 tuxbot/cogs/Network/network.py:161 #: tuxbot/cogs/Network/network.py:113 tuxbot/cogs/Network/network.py:142
msgid "Region:" msgid "Region:"
msgstr "Région :" msgstr "Région :"
#: tuxbot/cogs/Network/network.py:174 #: tuxbot/cogs/Network/network.py:152
#, python-brace-format #, python-brace-format
msgid "Hostname: {hostname}" msgid "Hostname: {hostname}"
msgstr "Nom d'hôte : {hostname}" msgstr "Nom d'hôte : {hostname}"
#: tuxbot/cogs/Network/network.py:182
msgid "[show all]({})"
msgstr "[tout afficher]({})"
#: tuxbot/cogs/Network/network.py:192
msgid "Cannot connect to host {}"
msgstr "Impossible de se connecter à l'hôte {}"

View file

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: Tuxbot-bot\n" "Project-Id-Version: Tuxbot-bot\n"
"Report-Msgid-Bugs-To: rick@gnous.eu\n" "Report-Msgid-Bugs-To: rick@gnous.eu\n"
"POT-Creation-Date: 2021-01-25 16:09+0100\n" "POT-Creation-Date: 2021-01-26 15:18+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@ -17,47 +17,50 @@ msgstr ""
"Content-Type: text/plain; charset=CHARSET\n" "Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
#: tuxbot/cogs/Network/functions/converters.py:22 #: tuxbot/cogs/Network/functions/converters.py:28
#: tuxbot/cogs/Network/functions/converters.py:48
msgid "Invalid ip or domain" msgid "Invalid ip or domain"
msgstr "" msgstr ""
#: tuxbot/cogs/Network/functions/converters.py:35 #: tuxbot/cogs/Network/functions/converters.py:59
msgid "Invalid ip version" msgid "Invalid ip version"
msgstr "" msgstr ""
#: tuxbot/cogs/Network/network.py:49 tuxbot/cogs/Network/network.py:52 #: tuxbot/cogs/Network/functions/utils.py:31
#, python-brace-format
msgid "in v{v}"
msgstr ""
#: tuxbot/cogs/Network/network.py:61
#, python-brace-format
msgid "Impossible to collect information on this in the given version" msgid "Impossible to collect information on this in the given version"
msgstr "" msgstr ""
#: tuxbot/cogs/Network/network.py:89 #: tuxbot/cogs/Network/functions/utils.py:54
#, python-brace-format #, python-brace-format
msgid "IP address {ip_address} is already defined as Private-Use Networks via RFC 1918." msgid "IP address {ip_address} is already defined as Private-Use Networks via RFC 1918."
msgstr "" msgstr ""
#: tuxbot/cogs/Network/network.py:109 #: tuxbot/cogs/Network/network.py:74
msgid "*Retrieving information...*" msgid "*Retrieving information...*"
msgstr "" msgstr ""
#: tuxbot/cogs/Network/network.py:123 #: tuxbot/cogs/Network/network.py:90
#, python-brace-format #, python-brace-format
msgid "Information for ``{ip} ({ip_address})``" msgid "Information for ``{ip} ({ip_address})``"
msgstr "" msgstr ""
#: tuxbot/cogs/Network/network.py:135 tuxbot/cogs/Network/network.py:156 #: tuxbot/cogs/Network/network.py:100 tuxbot/cogs/Network/network.py:130
msgid "Belongs to:" msgid "Belongs to:"
msgstr "" msgstr ""
#: tuxbot/cogs/Network/network.py:140 tuxbot/cogs/Network/network.py:161 #: tuxbot/cogs/Network/network.py:113 tuxbot/cogs/Network/network.py:142
msgid "Region:" msgid "Region:"
msgstr "" msgstr ""
#: tuxbot/cogs/Network/network.py:174 #: tuxbot/cogs/Network/network.py:152
#, python-brace-format #, python-brace-format
msgid "Hostname: {hostname}" msgid "Hostname: {hostname}"
msgstr "" msgstr ""
#: tuxbot/cogs/Network/network.py:182
msgid "[show all]({})"
msgstr ""
#: tuxbot/cogs/Network/network.py:192
msgid "Cannot connect to host {}"
msgstr ""

View file

@ -1,17 +1,14 @@
import functools import functools
import logging import logging
import discord import discord
from aiohttp import ClientConnectorError
from discord.ext import commands from discord.ext import commands
from ipinfo.exceptions import RequestQuotaExceededError from ipinfo.exceptions import RequestQuotaExceededError
from structured_config import ConfigFile from structured_config import ConfigFile
from tuxbot.cogs.Network.functions.converters import ( from tuxbot.cogs.Network.functions.converters import (
IPConverter, IPConverter,
IPVersionConverter, IPVersionConverter,
IPCheckerConverter,
) )
from tuxbot.cogs.Network.functions.exceptions import ( from tuxbot.cogs.Network.functions.exceptions import (
RFC18, RFC18,
@ -27,6 +24,7 @@ from tuxbot.core.utils.functions.extra import (
ContextPlus, ContextPlus,
command_extra, command_extra,
) )
from tuxbot.core.utils.functions.utils import shorten
from .config import NetworkConfig from .config import NetworkConfig
from .functions.utils import ( from .functions.utils import (
get_ip, get_ip,
@ -158,3 +156,38 @@ class Network(commands.Cog, name="Network"):
await self._tmp.delete() await self._tmp.delete()
await ctx.send(embed=e) await ctx.send(embed=e)
@command_extra(name="getheaders", aliases=["headers"], deletable=True)
async def _getheaders(
self, ctx: ContextPlus, ip: IPCheckerConverter, *, user_agent: str = ""
):
try:
headers = {"User-Agent": user_agent}
async with ctx.session.get(str(ip), headers=headers) as s:
e = discord.Embed(title=f"Headers : {ip}", color=0xD75858)
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)
for key, value in headers.items():
output = await shorten(ctx, value, 50)
if output["link"] is not None:
value = _(
"[show all]({})", ctx, self.bot.config
).format(output["link"])
else:
value = f"```{output['text']}```"
e.add_field(name=key, value=value, inline=True)
await ctx.send(embed=e, deletable=False)
except ClientConnectorError:
await ctx.send(
_("Cannot connect to host {}", ctx, self.bot.config).format(ip)
)

View file

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: Tuxbot-bot\n" "Project-Id-Version: Tuxbot-bot\n"
"Report-Msgid-Bugs-To: rick@gnous.eu\n" "Report-Msgid-Bugs-To: rick@gnous.eu\n"
"POT-Creation-Date: 2021-01-25 16:09+0100\n" "POT-Creation-Date: 2021-01-26 15:18+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"

View file

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: Tuxbot-bot\n" "Project-Id-Version: Tuxbot-bot\n"
"Report-Msgid-Bugs-To: rick@gnous.eu\n" "Report-Msgid-Bugs-To: rick@gnous.eu\n"
"POT-Creation-Date: 2021-01-25 16:09+0100\n" "POT-Creation-Date: 2021-01-26 15:18+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"

View file

@ -1,6 +1,7 @@
import asyncio import asyncio
import random import random
import aiohttp
import discord import discord
from discord import Embed from discord import Embed
from discord.ext import commands from discord.ext import commands
@ -97,6 +98,10 @@ class ContextPlus(commands.Context):
allowed_mentions=allowed_mentions, allowed_mentions=allowed_mentions,
) )
@property
def session(self) -> aiohttp.ClientSession:
return self.bot.session
class CommandPLus(commands.Command): class CommandPLus(commands.Command):
def __init__(self, function, **kwargs): def __init__(self, function, **kwargs):

View file

@ -1,5 +1,7 @@
import asyncio
import functools import functools
import aiohttp
from discord.ext import commands from discord.ext import commands
from tuxbot.core.utils.functions.extra import ContextPlus from tuxbot.core.utils.functions.extra import ContextPlus
@ -22,3 +24,23 @@ def typing(func):
await func(*args, **kwargs) await func(*args, **kwargs)
return wrapped return wrapped
async def shorten(ctx: ContextPlus, text: str, length: int) -> dict:
output = {"text": text[:length], "link": None}
if len(text) > length:
output["text"] += "[...]"
try:
async with ctx.session.post(
"https://paste.ramle.be/documents",
data=text.encode(),
timeout=aiohttp.ClientTimeout(total=2),
) as r:
output[
"link"
] = f"https://paste.ramle.be/{(await r.json())['key']}"
except (aiohttp.ClientError, asyncio.exceptions.TimeoutError):
pass
return output