2021-01-25 17:28:59 +01:00
|
|
|
import functools
|
|
|
|
import logging
|
2021-01-26 10:21:39 +01:00
|
|
|
|
2021-01-25 17:28:59 +01:00
|
|
|
|
|
|
|
import discord
|
2021-01-26 10:21:39 +01:00
|
|
|
|
2021-01-25 17:28:59 +01:00
|
|
|
from discord.ext import commands
|
|
|
|
from ipinfo.exceptions import RequestQuotaExceededError
|
2021-01-26 10:21:39 +01:00
|
|
|
|
2021-01-25 17:28:59 +01:00
|
|
|
from structured_config import ConfigFile
|
|
|
|
|
|
|
|
from tuxbot.cogs.Network.functions.converters import (
|
|
|
|
IPConverter,
|
|
|
|
IPVersionConverter,
|
|
|
|
)
|
2021-01-26 10:21:39 +01:00
|
|
|
from tuxbot.cogs.Network.functions.exceptions import (
|
|
|
|
RFC18,
|
|
|
|
InvalidIp,
|
|
|
|
VersionNotFound,
|
|
|
|
)
|
2021-01-25 17:28:59 +01:00
|
|
|
from tuxbot.core.bot import Tux
|
|
|
|
from tuxbot.core.i18n import (
|
|
|
|
Translator,
|
|
|
|
)
|
|
|
|
from tuxbot.core.utils.data_manager import cogs_data_path
|
|
|
|
from tuxbot.core.utils.functions.extra import (
|
|
|
|
ContextPlus,
|
|
|
|
command_extra,
|
|
|
|
)
|
|
|
|
from .config import NetworkConfig
|
2021-01-26 10:21:39 +01:00
|
|
|
from .functions.utils import (
|
|
|
|
get_ip,
|
|
|
|
get_hostname,
|
|
|
|
get_ipinfo_result,
|
|
|
|
get_ipwhois_result,
|
|
|
|
)
|
2021-01-25 17:28:59 +01:00
|
|
|
|
|
|
|
log = logging.getLogger("tuxbot.cogs.Network")
|
|
|
|
_ = Translator("Network", __file__)
|
|
|
|
|
|
|
|
|
|
|
|
class Network(commands.Cog, name="Network"):
|
2021-01-26 10:21:39 +01:00
|
|
|
_tmp: discord.Message
|
|
|
|
|
2021-01-25 17:28:59 +01:00
|
|
|
def __init__(self, bot: Tux):
|
|
|
|
self.bot = bot
|
2021-01-26 10:21:39 +01:00
|
|
|
self.__config: NetworkConfig = ConfigFile(
|
2021-01-25 17:28:59 +01:00
|
|
|
str(
|
|
|
|
cogs_data_path(self.bot.instance_name, "Network")
|
|
|
|
/ "config.yaml"
|
|
|
|
),
|
|
|
|
NetworkConfig,
|
|
|
|
).config
|
|
|
|
|
|
|
|
async def cog_command_error(self, ctx, error):
|
2021-01-26 10:21:39 +01:00
|
|
|
if isinstance(
|
|
|
|
error,
|
|
|
|
(RequestQuotaExceededError, RFC18, InvalidIp, VersionNotFound),
|
|
|
|
):
|
|
|
|
if self._tmp:
|
|
|
|
await self._tmp.delete()
|
2021-01-25 17:28:59 +01:00
|
|
|
|
2021-01-26 10:21:39 +01:00
|
|
|
await ctx.send(_(str(error), ctx, self.bot.config))
|
2021-01-25 17:28:59 +01:00
|
|
|
|
|
|
|
# =========================================================================
|
|
|
|
# =========================================================================
|
|
|
|
|
|
|
|
@command_extra(name="iplocalise", aliases=["localiseip"], deletable=True)
|
|
|
|
async def _iplocalise(
|
|
|
|
self,
|
|
|
|
ctx: ContextPlus,
|
|
|
|
ip: IPConverter,
|
|
|
|
version: IPVersionConverter = "",
|
|
|
|
):
|
2021-01-26 10:21:39 +01:00
|
|
|
self._tmp = await ctx.send(
|
2021-01-25 17:28:59 +01:00
|
|
|
_("*Retrieving information...*", ctx, self.bot.config),
|
|
|
|
deletable=False,
|
|
|
|
)
|
|
|
|
|
2021-01-26 10:21:39 +01:00
|
|
|
ip_address = await get_ip(str(ip), str(version))
|
|
|
|
ip_hostname = get_hostname(ip_address)
|
2021-01-25 17:28:59 +01:00
|
|
|
|
2021-01-26 10:21:39 +01:00
|
|
|
ipinfo_result = await get_ipinfo_result(
|
|
|
|
self.__config.ipinfoKey, ip_address
|
|
|
|
)
|
2021-01-25 17:28:59 +01:00
|
|
|
ipwhois_result = await self.bot.loop.run_in_executor(
|
2021-01-26 10:21:39 +01:00
|
|
|
None, functools.partial(get_ipwhois_result, ip_address)
|
2021-01-25 17:28:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
e = discord.Embed(
|
|
|
|
title=_(
|
|
|
|
"Information for ``{ip} ({ip_address})``", ctx, self.bot.config
|
|
|
|
).format(ip=ip, ip_address=ip_address),
|
|
|
|
color=0x5858D7,
|
|
|
|
)
|
|
|
|
|
|
|
|
if ipinfo_result:
|
|
|
|
org = ipinfo_result.get("org", "")
|
|
|
|
asn = org.split()[0]
|
|
|
|
|
|
|
|
e.add_field(
|
|
|
|
name=_("Belongs to:", ctx, self.bot.config),
|
|
|
|
value=f"[{org}](https://bgp.he.net/{asn})",
|
|
|
|
inline=True,
|
|
|
|
)
|
|
|
|
|
2021-01-26 10:21:39 +01:00
|
|
|
if ipwhois_result:
|
|
|
|
e.add_field(
|
|
|
|
name="RIR :",
|
|
|
|
value=f"```{ipwhois_result['asn_registry']}```",
|
|
|
|
inline=True,
|
|
|
|
)
|
|
|
|
|
2021-01-25 17:28:59 +01:00
|
|
|
e.add_field(
|
|
|
|
name=_("Region:", ctx, self.bot.config),
|
2021-01-26 10:21:39 +01:00
|
|
|
value=f"```{ipinfo_result.get('city', 'N/A')} - "
|
2021-01-25 17:28:59 +01:00
|
|
|
f"{ipinfo_result.get('region', 'N/A')} "
|
2021-01-26 10:21:39 +01:00
|
|
|
f"({ipinfo_result.get('country', 'N/A')})```",
|
2021-01-25 17:28:59 +01:00
|
|
|
inline=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
e.set_thumbnail(
|
|
|
|
url=f"https://www.countryflags.io/{ipinfo_result['country']}"
|
|
|
|
f"/shiny/64.png"
|
|
|
|
)
|
|
|
|
elif ipwhois_result:
|
|
|
|
org = ipwhois_result.get("asn_description", "N/A")
|
|
|
|
asn = ipwhois_result.get("asn", "N/A")
|
|
|
|
asn_country = ipwhois_result.get("asn_country_code", "N/A")
|
|
|
|
|
|
|
|
e.add_field(
|
|
|
|
name=_("Belongs to:", ctx, self.bot.config),
|
|
|
|
value=f"{org} ([AS{asn}](https://bgp.he.net/{asn}))",
|
|
|
|
inline=True,
|
|
|
|
)
|
|
|
|
|
2021-01-26 10:21:39 +01:00
|
|
|
e.add_field(
|
|
|
|
name="RIR :",
|
|
|
|
value=f"```{ipwhois_result['asn_registry']}```",
|
|
|
|
inline=True,
|
|
|
|
)
|
|
|
|
|
2021-01-25 17:28:59 +01:00
|
|
|
e.add_field(
|
|
|
|
name=_("Region:", ctx, self.bot.config),
|
2021-01-26 10:21:39 +01:00
|
|
|
value=f"```{asn_country}```",
|
2021-01-25 17:28:59 +01:00
|
|
|
inline=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
e.set_thumbnail(
|
|
|
|
url=f"https://www.countryflags.io/{asn_country}/shiny/64.png"
|
|
|
|
)
|
|
|
|
|
|
|
|
e.set_footer(
|
|
|
|
text=_("Hostname: {hostname}", ctx, self.bot.config).format(
|
|
|
|
hostname=ip_hostname
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2021-01-26 10:21:39 +01:00
|
|
|
await self._tmp.delete()
|
2021-01-25 17:28:59 +01:00
|
|
|
await ctx.send(embed=e)
|